blob: 61d2c39814529bd0264e4c9e40241131d51d819c [file] [log] [blame]
Johannes Schindelinbeb47432007-10-13 17:34:45 +01001#include "cache.h"
2#include "parse-options.h"
3
4static int boolean = 0;
Junio C Hamanoc4aca9c2008-07-30 12:53:45 -07005static int integer = 0;
6static unsigned long timestamp;
Stephan Beyer010a2da2008-06-22 17:04:26 +02007static int abbrev = 7;
8static int verbose = 0, dry_run = 0, quiet = 0;
Johannes Schindelinbeb47432007-10-13 17:34:45 +01009static char *string = NULL;
10
Stephan Beyer010a2da2008-06-22 17:04:26 +020011int length_callback(const struct option *opt, const char *arg, int unset)
12{
13 printf("Callback: \"%s\", %d\n",
14 (arg ? arg : "not set"), unset);
15 if (unset)
16 return 1; /* do not support unset */
17
Brandon Casey8caa3ac2008-08-13 19:48:57 -050018 *(int *)opt->value = strlen(arg);
Stephan Beyer010a2da2008-06-22 17:04:26 +020019 return 0;
20}
21
Johannes Schindelinbeb47432007-10-13 17:34:45 +010022int main(int argc, const char **argv)
23{
24 const char *usage[] = {
25 "test-parse-options <options>",
26 NULL
27 };
28 struct option options[] = {
29 OPT_BOOLEAN('b', "boolean", &boolean, "get a boolean"),
Stephan Beyer010a2da2008-06-22 17:04:26 +020030 OPT_BIT('4', "or4", &boolean,
31 "bitwise-or boolean with ...0100", 4),
32 OPT_GROUP(""),
Johannes Schindelinbeb47432007-10-13 17:34:45 +010033 OPT_INTEGER('i', "integer", &integer, "get a integer"),
34 OPT_INTEGER('j', NULL, &integer, "get a integer, too"),
Stephan Beyer010a2da2008-06-22 17:04:26 +020035 OPT_SET_INT(0, "set23", &integer, "set integer to 23", 23),
Junio C Hamanoc4aca9c2008-07-30 12:53:45 -070036 OPT_DATE('t', NULL, &timestamp, "get timestamp of <time>"),
Stephan Beyer010a2da2008-06-22 17:04:26 +020037 OPT_CALLBACK('L', "length", &integer, "str",
38 "get length of <str>", length_callback),
39 OPT_GROUP("String options"),
Johannes Schindelinbeb47432007-10-13 17:34:45 +010040 OPT_STRING('s', "string", &string, "string", "get a string"),
41 OPT_STRING(0, "string2", &string, "str", "get another string"),
Johannes Schindelin243e0612007-11-05 13:15:21 +000042 OPT_STRING(0, "st", &string, "st", "get another string (pervert ordering)"),
Pierre Habouzit3a9f0f42008-01-26 12:26:57 +010043 OPT_STRING('o', NULL, &string, "str", "get another string"),
Stephan Beyer010a2da2008-06-22 17:04:26 +020044 OPT_SET_PTR(0, "default-string", &string,
45 "set string to default", (unsigned long)"default"),
46 OPT_GROUP("Magic arguments"),
Pierre Habouzit580d5bf2008-03-02 11:35:56 +010047 OPT_ARGUMENT("quux", "means --quux"),
Stephan Beyer010a2da2008-06-22 17:04:26 +020048 OPT_GROUP("Standard options"),
49 OPT__ABBREV(&abbrev),
50 OPT__VERBOSE(&verbose),
51 OPT__DRY_RUN(&dry_run),
52 OPT__QUIET(&quiet),
Johannes Schindelinbeb47432007-10-13 17:34:45 +010053 OPT_END(),
54 };
55 int i;
56
57 argc = parse_options(argc, argv, options, usage, 0);
58
59 printf("boolean: %d\n", boolean);
Junio C Hamanoc4aca9c2008-07-30 12:53:45 -070060 printf("integer: %u\n", integer);
61 printf("timestamp: %lu\n", timestamp);
Johannes Schindelinbeb47432007-10-13 17:34:45 +010062 printf("string: %s\n", string ? string : "(not set)");
Stephan Beyer010a2da2008-06-22 17:04:26 +020063 printf("abbrev: %d\n", abbrev);
64 printf("verbose: %d\n", verbose);
65 printf("quiet: %s\n", quiet ? "yes" : "no");
66 printf("dry run: %s\n", dry_run ? "yes" : "no");
Johannes Schindelinbeb47432007-10-13 17:34:45 +010067
68 for (i = 0; i < argc; i++)
69 printf("arg %02d: %s\n", i, argv[i]);
70
71 return 0;
72}