config: add core.mode = progress pseudo-config
[git] / test-parse-options.c
1 #include "cache.h"
2 #include "parse-options.h"
3 #include "string-list.h"
4
5 static int boolean = 0;
6 static int integer = 0;
7 static unsigned long magnitude = 0;
8 static unsigned long timestamp;
9 static int abbrev = 7;
10 static int verbose = 0, dry_run = 0, quiet = 0;
11 static char *string = NULL;
12 static char *file = NULL;
13 static int ambiguous;
14 static struct string_list list;
15
16 static int length_callback(const struct option *opt, const char *arg, int unset)
17 {
18         printf("Callback: \"%s\", %d\n",
19                 (arg ? arg : "not set"), unset);
20         if (unset)
21                 return 1; /* do not support unset */
22
23         *(int *)opt->value = strlen(arg);
24         return 0;
25 }
26
27 static int number_callback(const struct option *opt, const char *arg, int unset)
28 {
29         *(int *)opt->value = strtol(arg, NULL, 10);
30         return 0;
31 }
32
33 int main(int argc, char **argv)
34 {
35         const char *prefix = "prefix/";
36         const char *usage[] = {
37                 "test-parse-options <options>",
38                 NULL
39         };
40         struct option options[] = {
41                 OPT_BOOL(0, "yes", &boolean, "get a boolean"),
42                 OPT_BOOL('D', "no-doubt", &boolean, "begins with 'no-'"),
43                 { OPTION_SET_INT, 'B', "no-fear", &boolean, NULL,
44                   "be brave", PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1 },
45                 OPT_COUNTUP('b', "boolean", &boolean, "increment by one"),
46                 OPT_BIT('4', "or4", &boolean,
47                         "bitwise-or boolean with ...0100", 4),
48                 OPT_NEGBIT(0, "neg-or4", &boolean, "same as --no-or4", 4),
49                 OPT_GROUP(""),
50                 OPT_INTEGER('i', "integer", &integer, "get a integer"),
51                 OPT_INTEGER('j', NULL, &integer, "get a integer, too"),
52                 OPT_MAGNITUDE('m', "magnitude", &magnitude, "get a magnitude"),
53                 OPT_SET_INT(0, "set23", &integer, "set integer to 23", 23),
54                 OPT_DATE('t', NULL, &timestamp, "get timestamp of <time>"),
55                 OPT_CALLBACK('L', "length", &integer, "str",
56                         "get length of <str>", length_callback),
57                 OPT_FILENAME('F', "file", &file, "set file to <file>"),
58                 OPT_GROUP("String options"),
59                 OPT_STRING('s', "string", &string, "string", "get a string"),
60                 OPT_STRING(0, "string2", &string, "str", "get another string"),
61                 OPT_STRING(0, "st", &string, "st", "get another string (pervert ordering)"),
62                 OPT_STRING('o', NULL, &string, "str", "get another string"),
63                 OPT_NOOP_NOARG(0, "obsolete"),
64                 OPT_STRING_LIST(0, "list", &list, "str", "add str to list"),
65                 OPT_GROUP("Magic arguments"),
66                 OPT_ARGUMENT("quux", "means --quux"),
67                 OPT_NUMBER_CALLBACK(&integer, "set integer to NUM",
68                         number_callback),
69                 { OPTION_COUNTUP, '+', NULL, &boolean, NULL, "same as -b",
70                   PARSE_OPT_NOARG | PARSE_OPT_NONEG | PARSE_OPT_NODASH },
71                 { OPTION_COUNTUP, 0, "ambiguous", &ambiguous, NULL,
72                   "positive ambiguity", PARSE_OPT_NOARG | PARSE_OPT_NONEG },
73                 { OPTION_COUNTUP, 0, "no-ambiguous", &ambiguous, NULL,
74                   "negative ambiguity", PARSE_OPT_NOARG | PARSE_OPT_NONEG },
75                 OPT_GROUP("Standard options"),
76                 OPT__ABBREV(&abbrev),
77                 OPT__VERBOSE(&verbose, "be verbose"),
78                 OPT__DRY_RUN(&dry_run, "dry run"),
79                 OPT__QUIET(&quiet, "be quiet"),
80                 OPT_END(),
81         };
82         int i;
83
84         argc = parse_options(argc, (const char **)argv, prefix, options, usage, 0);
85
86         printf("boolean: %d\n", boolean);
87         printf("integer: %d\n", integer);
88         printf("magnitude: %lu\n", magnitude);
89         printf("timestamp: %lu\n", timestamp);
90         printf("string: %s\n", string ? string : "(not set)");
91         printf("abbrev: %d\n", abbrev);
92         printf("verbose: %d\n", verbose);
93         printf("quiet: %s\n", quiet ? "yes" : "no");
94         printf("dry run: %s\n", dry_run ? "yes" : "no");
95         printf("file: %s\n", file ? file : "(not set)");
96
97         for (i = 0; i < list.nr; i++)
98                 printf("list: %s\n", list.items[i].string);
99
100         for (i = 0; i < argc; i++)
101                 printf("arg %02d: %s\n", i, argv[i]);
102
103         return 0;
104 }