test-parse-options: fix output when callback option fails
[git/debian.git] / test-parse-options.c
blobb5f4e900baf90717feb6da37f3ca66a179315654
1 #include "cache.h"
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;
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;
17 static struct {
18 int called;
19 const char *arg;
20 int unset;
21 } length_cb;
23 static int length_callback(const struct option *opt, const char *arg, int unset)
25 length_cb.called = 1;
26 length_cb.arg = arg;
27 length_cb.unset = unset;
29 if (unset)
30 return 1; /* do not support unset */
32 *(int *)opt->value = strlen(arg);
33 return 0;
36 static int number_callback(const struct option *opt, const char *arg, int unset)
38 *(int *)opt->value = strtol(arg, NULL, 10);
39 return 0;
42 int main(int argc, char **argv)
44 const char *prefix = "prefix/";
45 const char *usage[] = {
46 "test-parse-options <options>",
47 NULL
49 struct option options[] = {
50 OPT_BOOL(0, "yes", &boolean, "get a boolean"),
51 OPT_BOOL('D', "no-doubt", &boolean, "begins with 'no-'"),
52 { OPTION_SET_INT, 'B', "no-fear", &boolean, NULL,
53 "be brave", PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1 },
54 OPT_COUNTUP('b', "boolean", &boolean, "increment by one"),
55 OPT_BIT('4', "or4", &boolean,
56 "bitwise-or boolean with ...0100", 4),
57 OPT_NEGBIT(0, "neg-or4", &boolean, "same as --no-or4", 4),
58 OPT_GROUP(""),
59 OPT_INTEGER('i', "integer", &integer, "get a integer"),
60 OPT_INTEGER('j', NULL, &integer, "get a integer, too"),
61 OPT_MAGNITUDE('m', "magnitude", &magnitude, "get a magnitude"),
62 OPT_SET_INT(0, "set23", &integer, "set integer to 23", 23),
63 OPT_DATE('t', NULL, &timestamp, "get timestamp of <time>"),
64 OPT_CALLBACK('L', "length", &integer, "str",
65 "get length of <str>", length_callback),
66 OPT_FILENAME('F', "file", &file, "set file to <file>"),
67 OPT_GROUP("String options"),
68 OPT_STRING('s', "string", &string, "string", "get a string"),
69 OPT_STRING(0, "string2", &string, "str", "get another string"),
70 OPT_STRING(0, "st", &string, "st", "get another string (pervert ordering)"),
71 OPT_STRING('o', NULL, &string, "str", "get another string"),
72 OPT_NOOP_NOARG(0, "obsolete"),
73 OPT_STRING_LIST(0, "list", &list, "str", "add str to list"),
74 OPT_GROUP("Magic arguments"),
75 OPT_ARGUMENT("quux", "means --quux"),
76 OPT_NUMBER_CALLBACK(&integer, "set integer to NUM",
77 number_callback),
78 { OPTION_COUNTUP, '+', NULL, &boolean, NULL, "same as -b",
79 PARSE_OPT_NOARG | PARSE_OPT_NONEG | PARSE_OPT_NODASH },
80 { OPTION_COUNTUP, 0, "ambiguous", &ambiguous, NULL,
81 "positive ambiguity", PARSE_OPT_NOARG | PARSE_OPT_NONEG },
82 { OPTION_COUNTUP, 0, "no-ambiguous", &ambiguous, NULL,
83 "negative ambiguity", PARSE_OPT_NOARG | PARSE_OPT_NONEG },
84 OPT_GROUP("Standard options"),
85 OPT__ABBREV(&abbrev),
86 OPT__VERBOSE(&verbose, "be verbose"),
87 OPT__DRY_RUN(&dry_run, "dry run"),
88 OPT__QUIET(&quiet, "be quiet"),
89 OPT_END(),
91 int i;
93 argc = parse_options(argc, (const char **)argv, prefix, options, usage, 0);
95 if (length_cb.called) {
96 const char *arg = length_cb.arg;
97 int unset = length_cb.unset;
98 printf("Callback: \"%s\", %d\n",
99 (arg ? arg : "not set"), unset);
101 printf("boolean: %d\n", boolean);
102 printf("integer: %d\n", integer);
103 printf("magnitude: %lu\n", magnitude);
104 printf("timestamp: %lu\n", timestamp);
105 printf("string: %s\n", string ? string : "(not set)");
106 printf("abbrev: %d\n", abbrev);
107 printf("verbose: %d\n", verbose);
108 printf("quiet: %d\n", quiet);
109 printf("dry run: %s\n", dry_run ? "yes" : "no");
110 printf("file: %s\n", file ? file : "(not set)");
112 for (i = 0; i < list.nr; i++)
113 printf("list: %s\n", list.items[i].string);
115 for (i = 0; i < argc; i++)
116 printf("arg %02d: %s\n", i, argv[i]);
118 return 0;