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