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
18 static struct {
19 int called;
20 const char *arg;
21 int unset;
22 } length_cb;
23
length_callback(const struct option * opt,const char * arg,int unset)24 static int length_callback(const struct option *opt, const char *arg, int unset)
25 {
26 length_cb.called = 1;
27 length_cb.arg = arg;
28 length_cb.unset = unset;
29
30 if (unset)
31 return 1; /* do not support unset */
32
33 *(int *)opt->value = strlen(arg);
34 return 0;
35 }
36
number_callback(const struct option * opt,const char * arg,int unset)37 static int number_callback(const struct option *opt, const char *arg, int unset)
38 {
39 BUG_ON_OPT_NEG(unset);
40 *(int *)opt->value = strtol(arg, NULL, 10);
41 return 0;
42 }
43
collect_expect(const struct option * opt,const char * arg,int unset)44 static int collect_expect(const struct option *opt, const char *arg, int unset)
45 {
46 struct string_list *expect;
47 struct string_list_item *item;
48 struct strbuf label = STRBUF_INIT;
49 const char *colon;
50
51 if (!arg || unset)
52 die("malformed --expect option");
53
54 expect = (struct string_list *)opt->value;
55 colon = strchr(arg, ':');
56 if (!colon)
57 die("malformed --expect option, lacking a colon");
58 strbuf_add(&label, arg, colon - arg);
59 item = string_list_insert(expect, strbuf_detach(&label, NULL));
60 if (item->util)
61 die("malformed --expect option, duplicate %s", label.buf);
62 item->util = (void *)arg;
63 return 0;
64 }
65
66 __attribute__((format (printf,3,4)))
show(struct string_list * expect,int * status,const char * fmt,...)67 static void show(struct string_list *expect, int *status, const char *fmt, ...)
68 {
69 struct string_list_item *item;
70 struct strbuf buf = STRBUF_INIT;
71 va_list args;
72
73 va_start(args, fmt);
74 strbuf_vaddf(&buf, fmt, args);
75 va_end(args);
76
77 if (!expect->nr)
78 printf("%s\n", buf.buf);
79 else {
80 char *colon = strchr(buf.buf, ':');
81 if (!colon)
82 die("malformed output format, output lacking colon: %s", fmt);
83 *colon = '\0';
84 item = string_list_lookup(expect, buf.buf);
85 *colon = ':';
86 if (!item)
87 ; /* not among entries being checked */
88 else {
89 if (strcmp((const char *)item->util, buf.buf)) {
90 printf("-%s\n", (char *)item->util);
91 printf("+%s\n", buf.buf);
92 *status = 1;
93 }
94 }
95 }
96 strbuf_release(&buf);
97 }
98
cmd__parse_options(int argc,const char ** argv)99 int cmd__parse_options(int argc, const char **argv)
100 {
101 const char *prefix = "prefix/";
102 const char *usage[] = {
103 "test-tool parse-options <options>",
104 "",
105 "A helper function for the parse-options API.",
106 NULL
107 };
108 struct string_list expect = STRING_LIST_INIT_NODUP;
109 struct string_list list = STRING_LIST_INIT_NODUP;
110
111 struct option options[] = {
112 OPT_BOOL(0, "yes", &boolean, "get a boolean"),
113 OPT_BOOL('D', "no-doubt", &boolean, "begins with 'no-'"),
114 { OPTION_SET_INT, 'B', "no-fear", &boolean, NULL,
115 "be brave", PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1 },
116 OPT_COUNTUP('b', "boolean", &boolean, "increment by one"),
117 OPT_BIT('4', "or4", &boolean,
118 "bitwise-or boolean with ...0100", 4),
119 OPT_NEGBIT(0, "neg-or4", &boolean, "same as --no-or4", 4),
120 OPT_GROUP(""),
121 OPT_INTEGER('i', "integer", &integer, "get a integer"),
122 OPT_INTEGER('j', NULL, &integer, "get a integer, too"),
123 OPT_MAGNITUDE('m', "magnitude", &magnitude, "get a magnitude"),
124 OPT_SET_INT(0, "set23", &integer, "set integer to 23", 23),
125 OPT_CMDMODE(0, "mode1", &integer, "set integer to 1 (cmdmode option)", 1),
126 OPT_CMDMODE(0, "mode2", &integer, "set integer to 2 (cmdmode option)", 2),
127 OPT_CALLBACK('L', "length", &integer, "str",
128 "get length of <str>", length_callback),
129 OPT_FILENAME('F', "file", &file, "set file to <file>"),
130 OPT_GROUP("String options"),
131 OPT_STRING('s', "string", &string, "string", "get a string"),
132 OPT_STRING(0, "string2", &string, "str", "get another string"),
133 OPT_STRING(0, "st", &string, "st", "get another string (pervert ordering)"),
134 OPT_STRING('o', NULL, &string, "str", "get another string"),
135 OPT_NOOP_NOARG(0, "obsolete"),
136 OPT_STRING_LIST(0, "list", &list, "str", "add str to list"),
137 OPT_GROUP("Magic arguments"),
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 expect.strdup_strings = 1;
190 string_list_clear(&expect, 0);
191 string_list_clear(&list, 0);
192
193 return ret;
194 }
195