2 #include "parse-options.h"
3 #include "string-list.h"
5 static int boolean = 0;
6 static int integer = 0;
7 static unsigned long magnitude = 0;
8 static unsigned long timestamp;
10 static int verbose = -1; /* unspecified */
11 static int dry_run = 0, quiet = 0;
12 static char *string = NULL;
13 static char *file = NULL;
15 static struct string_list list;
23 static int length_callback(const struct option *opt, const char *arg, int unset)
27 length_cb.unset = unset;
30 return 1; /* do not support unset */
32 *(int *)opt->value = strlen(arg);
36 static int number_callback(const struct option *opt, const char *arg, int unset)
38 *(int *)opt->value = strtol(arg, NULL, 10);
42 static int collect_expect(const struct option *opt, const char *arg, int unset)
44 struct string_list *expect;
45 struct string_list_item *item;
46 struct strbuf label = STRBUF_INIT;
50 die("malformed --expect option");
52 expect = (struct string_list *)opt->value;
53 colon = strchr(arg, ':');
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));
59 die("malformed --expect option, duplicate %s", label.buf);
60 item->util = (void *)arg;
64 __attribute__((format (printf,3,4)))
65 static void show(struct string_list *expect, int *status, const char *fmt, ...)
67 struct string_list_item *item;
68 struct strbuf buf = STRBUF_INIT;
72 strbuf_vaddf(&buf, fmt, args);
76 printf("%s\n", buf.buf);
78 char *colon = strchr(buf.buf, ':');
80 die("malformed output format, output lacking colon: %s", fmt);
82 item = string_list_lookup(expect, buf.buf);
85 ; /* not among entries being checked */
87 if (strcmp((const char *)item->util, buf.buf)) {
88 printf("-%s\n", (char *)item->util);
89 printf("+%s\n", buf.buf);
97 int main(int argc, char **argv)
99 const char *prefix = "prefix/";
100 const char *usage[] = {
101 "test-parse-options <options>",
104 struct string_list expect = STRING_LIST_INIT_NODUP;
105 struct option options[] = {
106 OPT_BOOL(0, "yes", &boolean, "get a boolean"),
107 OPT_BOOL('D', "no-doubt", &boolean, "begins with 'no-'"),
108 { OPTION_SET_INT, 'B', "no-fear", &boolean, NULL,
109 "be brave", PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1 },
110 OPT_COUNTUP('b', "boolean", &boolean, "increment by one"),
111 OPT_BIT('4', "or4", &boolean,
112 "bitwise-or boolean with ...0100", 4),
113 OPT_NEGBIT(0, "neg-or4", &boolean, "same as --no-or4", 4),
115 OPT_INTEGER('i', "integer", &integer, "get a integer"),
116 OPT_INTEGER('j', NULL, &integer, "get a integer, too"),
117 OPT_MAGNITUDE('m', "magnitude", &magnitude, "get a magnitude"),
118 OPT_SET_INT(0, "set23", &integer, "set integer to 23", 23),
119 OPT_DATE('t', NULL, ×tamp, "get timestamp of <time>"),
120 OPT_CALLBACK('L', "length", &integer, "str",
121 "get length of <str>", length_callback),
122 OPT_FILENAME('F', "file", &file, "set file to <file>"),
123 OPT_GROUP("String options"),
124 OPT_STRING('s', "string", &string, "string", "get a string"),
125 OPT_STRING(0, "string2", &string, "str", "get another string"),
126 OPT_STRING(0, "st", &string, "st", "get another string (pervert ordering)"),
127 OPT_STRING('o', NULL, &string, "str", "get another string"),
128 OPT_NOOP_NOARG(0, "obsolete"),
129 OPT_STRING_LIST(0, "list", &list, "str", "add str to list"),
130 OPT_GROUP("Magic arguments"),
131 OPT_ARGUMENT("quux", "means --quux"),
132 OPT_NUMBER_CALLBACK(&integer, "set integer to NUM",
134 { OPTION_COUNTUP, '+', NULL, &boolean, NULL, "same as -b",
135 PARSE_OPT_NOARG | PARSE_OPT_NONEG | PARSE_OPT_NODASH },
136 { OPTION_COUNTUP, 0, "ambiguous", &ambiguous, NULL,
137 "positive ambiguity", PARSE_OPT_NOARG | PARSE_OPT_NONEG },
138 { OPTION_COUNTUP, 0, "no-ambiguous", &ambiguous, NULL,
139 "negative ambiguity", PARSE_OPT_NOARG | PARSE_OPT_NONEG },
140 OPT_GROUP("Standard options"),
141 OPT__ABBREV(&abbrev),
142 OPT__VERBOSE(&verbose, "be verbose"),
143 OPT__DRY_RUN(&dry_run, "dry run"),
144 OPT__QUIET(&quiet, "be quiet"),
145 OPT_CALLBACK(0, "expect", &expect, "string",
146 "expected output in the variable dump",
153 argc = parse_options(argc, (const char **)argv, prefix, options, usage, 0);
155 if (length_cb.called) {
156 const char *arg = length_cb.arg;
157 int unset = length_cb.unset;
158 show(&expect, &ret, "Callback: \"%s\", %d",
159 (arg ? arg : "not set"), unset);
161 show(&expect, &ret, "boolean: %d", boolean);
162 show(&expect, &ret, "integer: %d", integer);
163 show(&expect, &ret, "magnitude: %lu", magnitude);
164 show(&expect, &ret, "timestamp: %lu", timestamp);
165 show(&expect, &ret, "string: %s", string ? string : "(not set)");
166 show(&expect, &ret, "abbrev: %d", abbrev);
167 show(&expect, &ret, "verbose: %d", verbose);
168 show(&expect, &ret, "quiet: %d", quiet);
169 show(&expect, &ret, "dry run: %s", dry_run ? "yes" : "no");
170 show(&expect, &ret, "file: %s", file ? file : "(not set)");
172 for (i = 0; i < list.nr; i++)
173 show(&expect, &ret, "list: %s", list.items[i].string);
175 for (i = 0; i < argc; i++)
176 show(&expect, &ret, "arg %02d: %s", i, argv[i]);