object-store: factor out odb_clear_loose_cache()
[git.git] / t / helper / test-parse-options.c
blob47fee660b86a6e6977735e2c940de79d4330d519
1 #include "test-tool.h"
2 #include "cache.h"
3 #include "parse-options.h"
4 #include "string-list.h"
6 static int boolean = 0;
7 static int integer = 0;
8 static unsigned long magnitude = 0;
9 static timestamp_t timestamp;
10 static int abbrev = 7;
11 static int verbose = -1; /* unspecified */
12 static int dry_run = 0, quiet = 0;
13 static char *string = NULL;
14 static char *file = NULL;
15 static int ambiguous;
16 static struct string_list list = STRING_LIST_INIT_NODUP;
18 static struct {
19 int called;
20 const char *arg;
21 int unset;
22 } length_cb;
24 static int length_callback(const struct option *opt, const char *arg, int unset)
26 length_cb.called = 1;
27 length_cb.arg = arg;
28 length_cb.unset = unset;
30 if (unset)
31 return 1; /* do not support unset */
33 *(int *)opt->value = strlen(arg);
34 return 0;
37 static int number_callback(const struct option *opt, const char *arg, int unset)
39 BUG_ON_OPT_NEG(unset);
40 *(int *)opt->value = strtol(arg, NULL, 10);
41 return 0;
44 static int collect_expect(const struct option *opt, const char *arg, int unset)
46 struct string_list *expect;
47 struct string_list_item *item;
48 struct strbuf label = STRBUF_INIT;
49 const char *colon;
51 if (!arg || unset)
52 die("malformed --expect option");
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;
66 __attribute__((format (printf,3,4)))
67 static void show(struct string_list *expect, int *status, const char *fmt, ...)
69 struct string_list_item *item;
70 struct strbuf buf = STRBUF_INIT;
71 va_list args;
73 va_start(args, fmt);
74 strbuf_vaddf(&buf, fmt, args);
75 va_end(args);
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;
96 strbuf_release(&buf);
99 int cmd__parse_options(int argc, const char **argv)
101 const char *prefix = "prefix/";
102 const char *usage[] = {
103 "test-tool parse-options <options>",
105 "A helper function for the parse-options API.",
106 NULL
108 struct string_list expect = STRING_LIST_INIT_NODUP;
109 struct option options[] = {
110 OPT_BOOL(0, "yes", &boolean, "get a boolean"),
111 OPT_BOOL('D', "no-doubt", &boolean, "begins with 'no-'"),
112 { OPTION_SET_INT, 'B', "no-fear", &boolean, NULL,
113 "be brave", PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1 },
114 OPT_COUNTUP('b', "boolean", &boolean, "increment by one"),
115 OPT_BIT('4', "or4", &boolean,
116 "bitwise-or boolean with ...0100", 4),
117 OPT_NEGBIT(0, "neg-or4", &boolean, "same as --no-or4", 4),
118 OPT_GROUP(""),
119 OPT_INTEGER('i', "integer", &integer, "get a integer"),
120 OPT_INTEGER('j', NULL, &integer, "get a integer, too"),
121 OPT_MAGNITUDE('m', "magnitude", &magnitude, "get a magnitude"),
122 OPT_SET_INT(0, "set23", &integer, "set integer to 23", 23),
123 OPT_CALLBACK('L', "length", &integer, "str",
124 "get length of <str>", length_callback),
125 OPT_FILENAME('F', "file", &file, "set file to <file>"),
126 OPT_GROUP("String options"),
127 OPT_STRING('s', "string", &string, "string", "get a string"),
128 OPT_STRING(0, "string2", &string, "str", "get another string"),
129 OPT_STRING(0, "st", &string, "st", "get another string (pervert ordering)"),
130 OPT_STRING('o', NULL, &string, "str", "get another string"),
131 OPT_NOOP_NOARG(0, "obsolete"),
132 OPT_STRING_LIST(0, "list", &list, "str", "add str to list"),
133 OPT_GROUP("Magic arguments"),
134 OPT_ARGUMENT("quux", "means --quux"),
135 OPT_NUMBER_CALLBACK(&integer, "set integer to NUM",
136 number_callback),
137 { OPTION_COUNTUP, '+', NULL, &boolean, NULL, "same as -b",
138 PARSE_OPT_NOARG | PARSE_OPT_NONEG | PARSE_OPT_NODASH },
139 { OPTION_COUNTUP, 0, "ambiguous", &ambiguous, NULL,
140 "positive ambiguity", PARSE_OPT_NOARG | PARSE_OPT_NONEG },
141 { OPTION_COUNTUP, 0, "no-ambiguous", &ambiguous, NULL,
142 "negative ambiguity", PARSE_OPT_NOARG | PARSE_OPT_NONEG },
143 OPT_GROUP("Standard options"),
144 OPT__ABBREV(&abbrev),
145 OPT__VERBOSE(&verbose, "be verbose"),
146 OPT__DRY_RUN(&dry_run, "dry run"),
147 OPT__QUIET(&quiet, "be quiet"),
148 OPT_CALLBACK(0, "expect", &expect, "string",
149 "expected output in the variable dump",
150 collect_expect),
151 OPT_END(),
153 int i;
154 int ret = 0;
156 argc = parse_options(argc, (const char **)argv, prefix, options, usage, 0);
158 if (length_cb.called) {
159 const char *arg = length_cb.arg;
160 int unset = length_cb.unset;
161 show(&expect, &ret, "Callback: \"%s\", %d",
162 (arg ? arg : "not set"), unset);
164 show(&expect, &ret, "boolean: %d", boolean);
165 show(&expect, &ret, "integer: %d", integer);
166 show(&expect, &ret, "magnitude: %lu", magnitude);
167 show(&expect, &ret, "timestamp: %"PRItime, timestamp);
168 show(&expect, &ret, "string: %s", string ? string : "(not set)");
169 show(&expect, &ret, "abbrev: %d", abbrev);
170 show(&expect, &ret, "verbose: %d", verbose);
171 show(&expect, &ret, "quiet: %d", quiet);
172 show(&expect, &ret, "dry run: %s", dry_run ? "yes" : "no");
173 show(&expect, &ret, "file: %s", file ? file : "(not set)");
175 for (i = 0; i < list.nr; i++)
176 show(&expect, &ret, "list: %s", list.items[i].string);
178 for (i = 0; i < argc; i++)
179 show(&expect, &ret, "arg %02d: %s", i, argv[i]);
181 return ret;