]> git.ipfire.org Git - thirdparty/git.git/blame - t/helper/test-parse-options.c
hashmap_add takes "struct hashmap_entry *"
[thirdparty/git.git] / t / helper / test-parse-options.c
CommitLineData
2f17c78c 1#include "test-tool.h"
beb47437
JS
2#include "cache.h"
3#include "parse-options.h"
c8ba1639 4#include "string-list.h"
ee4512ed 5#include "trace2.h"
beb47437
JS
6
7static int boolean = 0;
c4aca9cc 8static int integer = 0;
2a514ed8 9static unsigned long magnitude = 0;
dddbad72 10static timestamp_t timestamp;
010a2dac 11static int abbrev = 7;
e0070e8b
PB
12static int verbose = -1; /* unspecified */
13static int dry_run = 0, quiet = 0;
beb47437 14static char *string = NULL;
df217ed6 15static char *file = NULL;
6bbfd1fa 16static int ambiguous;
2721ce21 17static struct string_list list = STRING_LIST_INIT_NODUP;
beb47437 18
accac419
JH
19static struct {
20 int called;
21 const char *arg;
22 int unset;
23} length_cb;
24
2af202be 25static int length_callback(const struct option *opt, const char *arg, int unset)
010a2dac 26{
accac419
JH
27 length_cb.called = 1;
28 length_cb.arg = arg;
29 length_cb.unset = unset;
30
010a2dac
SB
31 if (unset)
32 return 1; /* do not support unset */
33
8caa3acf 34 *(int *)opt->value = strlen(arg);
010a2dac
SB
35 return 0;
36}
37
2af202be 38static int number_callback(const struct option *opt, const char *arg, int unset)
e0319ff5 39{
517fe807 40 BUG_ON_OPT_NEG(unset);
e0319ff5
RS
41 *(int *)opt->value = strtol(arg, NULL, 10);
42 return 0;
43}
44
ab6b28b0
JH
45static 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)))
68static 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
2f17c78c 100int cmd__parse_options(int argc, const char **argv)
beb47437 101{
df217ed6 102 const char *prefix = "prefix/";
beb47437 103 const char *usage[] = {
2f17c78c 104 "test-tool parse-options <options>",
c97ee171
BC
105 "",
106 "A helper function for the parse-options API.",
beb47437
JS
107 NULL
108 };
ab6b28b0 109 struct string_list expect = STRING_LIST_INIT_NODUP;
beb47437 110 struct option options[] = {
b9e63ddd
RS
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"),
010a2dac
SB
116 OPT_BIT('4', "or4", &boolean,
117 "bitwise-or boolean with ...0100", 4),
2f4b97f9 118 OPT_NEGBIT(0, "neg-or4", &boolean, "same as --no-or4", 4),
010a2dac 119 OPT_GROUP(""),
beb47437
JS
120 OPT_INTEGER('i', "integer", &integer, "get a integer"),
121 OPT_INTEGER('j', NULL, &integer, "get a integer, too"),
2a514ed8 122 OPT_MAGNITUDE('m', "magnitude", &magnitude, "get a magnitude"),
010a2dac 123 OPT_SET_INT(0, "set23", &integer, "set integer to 23", 23),
010a2dac
SB
124 OPT_CALLBACK('L', "length", &integer, "str",
125 "get length of <str>", length_callback),
41dbcd45 126 OPT_FILENAME('F', "file", &file, "set file to <file>"),
010a2dac 127 OPT_GROUP("String options"),
beb47437
JS
128 OPT_STRING('s', "string", &string, "string", "get a string"),
129 OPT_STRING(0, "string2", &string, "str", "get another string"),
243e0614 130 OPT_STRING(0, "st", &string, "st", "get another string (pervert ordering)"),
3a9f0f41 131 OPT_STRING('o', NULL, &string, "str", "get another string"),
6acec038 132 OPT_NOOP_NOARG(0, "obsolete"),
c8ba1639 133 OPT_STRING_LIST(0, "list", &list, "str", "add str to list"),
010a2dac 134 OPT_GROUP("Magic arguments"),
1a85b49b 135 OPT_ARGUMENT("quux", NULL, "means --quux"),
e0319ff5
RS
136 OPT_NUMBER_CALLBACK(&integer, "set integer to NUM",
137 number_callback),
b9e63ddd 138 { OPTION_COUNTUP, '+', NULL, &boolean, NULL, "same as -b",
51a9949e 139 PARSE_OPT_NOARG | PARSE_OPT_NONEG | PARSE_OPT_NODASH },
b9e63ddd 140 { OPTION_COUNTUP, 0, "ambiguous", &ambiguous, NULL,
6bbfd1fa 141 "positive ambiguity", PARSE_OPT_NOARG | PARSE_OPT_NONEG },
b9e63ddd 142 { OPTION_COUNTUP, 0, "no-ambiguous", &ambiguous, NULL,
6bbfd1fa 143 "negative ambiguity", PARSE_OPT_NOARG | PARSE_OPT_NONEG },
010a2dac
SB
144 OPT_GROUP("Standard options"),
145 OPT__ABBREV(&abbrev),
fd03881a 146 OPT__VERBOSE(&verbose, "be verbose"),
e21adb8c 147 OPT__DRY_RUN(&dry_run, "dry run"),
d52ee6e6 148 OPT__QUIET(&quiet, "be quiet"),
ab6b28b0
JH
149 OPT_CALLBACK(0, "expect", &expect, "string",
150 "expected output in the variable dump",
151 collect_expect),
5c387428
NTND
152 OPT_GROUP("Alias"),
153 OPT_STRING('A', "alias-source", &string, "string", "get a string"),
154 OPT_ALIAS('Z', "alias-target", "alias-source"),
beb47437
JS
155 OPT_END(),
156 };
157 int i;
ab6b28b0 158 int ret = 0;
beb47437 159
ee4512ed
JH
160 trace2_cmd_name("_parse_");
161
84d32bf7 162 argc = parse_options(argc, (const char **)argv, prefix, options, usage, 0);
beb47437 163
accac419
JH
164 if (length_cb.called) {
165 const char *arg = length_cb.arg;
166 int unset = length_cb.unset;
ab6b28b0
JH
167 show(&expect, &ret, "Callback: \"%s\", %d",
168 (arg ? arg : "not set"), unset);
accac419 169 }
ab6b28b0
JH
170 show(&expect, &ret, "boolean: %d", boolean);
171 show(&expect, &ret, "integer: %d", integer);
172 show(&expect, &ret, "magnitude: %lu", magnitude);
cb71f8bd 173 show(&expect, &ret, "timestamp: %"PRItime, timestamp);
ab6b28b0
JH
174 show(&expect, &ret, "string: %s", string ? string : "(not set)");
175 show(&expect, &ret, "abbrev: %d", abbrev);
176 show(&expect, &ret, "verbose: %d", verbose);
177 show(&expect, &ret, "quiet: %d", quiet);
178 show(&expect, &ret, "dry run: %s", dry_run ? "yes" : "no");
179 show(&expect, &ret, "file: %s", file ? file : "(not set)");
beb47437 180
c8ba1639 181 for (i = 0; i < list.nr; i++)
ab6b28b0 182 show(&expect, &ret, "list: %s", list.items[i].string);
c8ba1639 183
beb47437 184 for (i = 0; i < argc; i++)
ab6b28b0 185 show(&expect, &ret, "arg %02d: %s", i, argv[i]);
beb47437 186
ab6b28b0 187 return ret;
beb47437 188}