t8*: adjust the references to the default branch name "main"
[git] / t / helper / test-parse-options.c
1 #include "test-tool.h"
2 #include "cache.h"
3 #include "parse-options.h"
4 #include "string-list.h"
5 #include "trace2.h"
6
7 static int boolean = 0;
8 static int integer = 0;
9 static unsigned long magnitude = 0;
10 static timestamp_t timestamp;
11 static int abbrev = 7;
12 static int verbose = -1; /* unspecified */
13 static int dry_run = 0, quiet = 0;
14 static char *string = NULL;
15 static char *file = NULL;
16 static int ambiguous;
17 static struct string_list list = STRING_LIST_INIT_NODUP;
18
19 static struct {
20         int called;
21         const char *arg;
22         int unset;
23 } length_cb;
24
25 static int length_callback(const struct option *opt, const char *arg, int unset)
26 {
27         length_cb.called = 1;
28         length_cb.arg = arg;
29         length_cb.unset = unset;
30
31         if (unset)
32                 return 1; /* do not support unset */
33
34         *(int *)opt->value = strlen(arg);
35         return 0;
36 }
37
38 static int number_callback(const struct option *opt, const char *arg, int unset)
39 {
40         BUG_ON_OPT_NEG(unset);
41         *(int *)opt->value = strtol(arg, NULL, 10);
42         return 0;
43 }
44
45 static int collect_expect(const struct option *opt, const char *arg, int unset)
46 {
47         struct string_list *expect;
48         struct string_list_item *item;
49         struct strbuf label = STRBUF_INIT;
50         const char *colon;
51
52         if (!arg || unset)
53                 die("malformed --expect option");
54
55         expect = (struct string_list *)opt->value;
56         colon = strchr(arg, ':');
57         if (!colon)
58                 die("malformed --expect option, lacking a colon");
59         strbuf_add(&label, arg, colon - arg);
60         item = string_list_insert(expect, strbuf_detach(&label, NULL));
61         if (item->util)
62                 die("malformed --expect option, duplicate %s", label.buf);
63         item->util = (void *)arg;
64         return 0;
65 }
66
67 __attribute__((format (printf,3,4)))
68 static void show(struct string_list *expect, int *status, const char *fmt, ...)
69 {
70         struct string_list_item *item;
71         struct strbuf buf = STRBUF_INIT;
72         va_list args;
73
74         va_start(args, fmt);
75         strbuf_vaddf(&buf, fmt, args);
76         va_end(args);
77
78         if (!expect->nr)
79                 printf("%s\n", buf.buf);
80         else {
81                 char *colon = strchr(buf.buf, ':');
82                 if (!colon)
83                         die("malformed output format, output lacking colon: %s", fmt);
84                 *colon = '\0';
85                 item = string_list_lookup(expect, buf.buf);
86                 *colon = ':';
87                 if (!item)
88                         ; /* not among entries being checked */
89                 else {
90                         if (strcmp((const char *)item->util, buf.buf)) {
91                                 printf("-%s\n", (char *)item->util);
92                                 printf("+%s\n", buf.buf);
93                                 *status = 1;
94                         }
95                 }
96         }
97         strbuf_release(&buf);
98 }
99
100 int cmd__parse_options(int argc, const char **argv)
101 {
102         const char *prefix = "prefix/";
103         const char *usage[] = {
104                 "test-tool parse-options <options>",
105                 "",
106                 "A helper function for the parse-options API.",
107                 NULL
108         };
109         struct string_list expect = STRING_LIST_INIT_NODUP;
110         struct option options[] = {
111                 OPT_BOOL(0, "yes", &boolean, "get a boolean"),
112                 OPT_BOOL('D', "no-doubt", &boolean, "begins with 'no-'"),
113                 { OPTION_SET_INT, 'B', "no-fear", &boolean, NULL,
114                   "be brave", PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1 },
115                 OPT_COUNTUP('b', "boolean", &boolean, "increment by one"),
116                 OPT_BIT('4', "or4", &boolean,
117                         "bitwise-or boolean with ...0100", 4),
118                 OPT_NEGBIT(0, "neg-or4", &boolean, "same as --no-or4", 4),
119                 OPT_GROUP(""),
120                 OPT_INTEGER('i', "integer", &integer, "get a integer"),
121                 OPT_INTEGER('j', NULL, &integer, "get a integer, too"),
122                 OPT_MAGNITUDE('m', "magnitude", &magnitude, "get a magnitude"),
123                 OPT_SET_INT(0, "set23", &integer, "set integer to 23", 23),
124                 OPT_CMDMODE(0, "mode1", &integer, "set integer to 1 (cmdmode option)", 1),
125                 OPT_CMDMODE(0, "mode2", &integer, "set integer to 2 (cmdmode option)", 2),
126                 OPT_CALLBACK('L', "length", &integer, "str",
127                         "get length of <str>", length_callback),
128                 OPT_FILENAME('F', "file", &file, "set file to <file>"),
129                 OPT_GROUP("String options"),
130                 OPT_STRING('s', "string", &string, "string", "get a string"),
131                 OPT_STRING(0, "string2", &string, "str", "get another string"),
132                 OPT_STRING(0, "st", &string, "st", "get another string (pervert ordering)"),
133                 OPT_STRING('o', NULL, &string, "str", "get another string"),
134                 OPT_NOOP_NOARG(0, "obsolete"),
135                 OPT_STRING_LIST(0, "list", &list, "str", "add str to list"),
136                 OPT_GROUP("Magic arguments"),
137                 OPT_ARGUMENT("quux", NULL, "means --quux"),
138                 OPT_NUMBER_CALLBACK(&integer, "set integer to NUM",
139                         number_callback),
140                 { OPTION_COUNTUP, '+', NULL, &boolean, NULL, "same as -b",
141                   PARSE_OPT_NOARG | PARSE_OPT_NONEG | PARSE_OPT_NODASH },
142                 { OPTION_COUNTUP, 0, "ambiguous", &ambiguous, NULL,
143                   "positive ambiguity", PARSE_OPT_NOARG | PARSE_OPT_NONEG },
144                 { OPTION_COUNTUP, 0, "no-ambiguous", &ambiguous, NULL,
145                   "negative ambiguity", PARSE_OPT_NOARG | PARSE_OPT_NONEG },
146                 OPT_GROUP("Standard options"),
147                 OPT__ABBREV(&abbrev),
148                 OPT__VERBOSE(&verbose, "be verbose"),
149                 OPT__DRY_RUN(&dry_run, "dry run"),
150                 OPT__QUIET(&quiet, "be quiet"),
151                 OPT_CALLBACK(0, "expect", &expect, "string",
152                              "expected output in the variable dump",
153                              collect_expect),
154                 OPT_GROUP("Alias"),
155                 OPT_STRING('A', "alias-source", &string, "string", "get a string"),
156                 OPT_ALIAS('Z', "alias-target", "alias-source"),
157                 OPT_END(),
158         };
159         int i;
160         int ret = 0;
161
162         trace2_cmd_name("_parse_");
163
164         argc = parse_options(argc, (const char **)argv, prefix, options, usage, 0);
165
166         if (length_cb.called) {
167                 const char *arg = length_cb.arg;
168                 int unset = length_cb.unset;
169                 show(&expect, &ret, "Callback: \"%s\", %d",
170                      (arg ? arg : "not set"), unset);
171         }
172         show(&expect, &ret, "boolean: %d", boolean);
173         show(&expect, &ret, "integer: %d", integer);
174         show(&expect, &ret, "magnitude: %lu", magnitude);
175         show(&expect, &ret, "timestamp: %"PRItime, timestamp);
176         show(&expect, &ret, "string: %s", string ? string : "(not set)");
177         show(&expect, &ret, "abbrev: %d", abbrev);
178         show(&expect, &ret, "verbose: %d", verbose);
179         show(&expect, &ret, "quiet: %d", quiet);
180         show(&expect, &ret, "dry run: %s", dry_run ? "yes" : "no");
181         show(&expect, &ret, "file: %s", file ? file : "(not set)");
182
183         for (i = 0; i < list.nr; i++)
184                 show(&expect, &ret, "list: %s", list.items[i].string);
185
186         for (i = 0; i < argc; i++)
187                 show(&expect, &ret, "arg %02d: %s", i, argv[i]);
188
189         return ret;
190 }