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 timestamp;
8 static int abbrev = 7;
9 static int verbose = 0, dry_run = 0, quiet = 0;
10 static char *string = NULL;
11 static char *file = NULL;
12 static int ambiguous;
13 static struct string_list list;
15 static int length_callback(const struct option *opt, const char *arg, int unset)
16 {
17 printf("Callback: \"%s\", %d\n",
18 (arg ? arg : "not set"), unset);
19 if (unset)
20 return 1; /* do not support unset */
22 *(int *)opt->value = strlen(arg);
23 return 0;
24 }
26 static int number_callback(const struct option *opt, const char *arg, int unset)
27 {
28 *(int *)opt->value = strtol(arg, NULL, 10);
29 return 0;
30 }
32 int main(int argc, const char **argv)
33 {
34 const char *prefix = "prefix/";
35 const char *usage[] = {
36 "test-parse-options <options>",
37 NULL
38 };
39 struct option options[] = {
40 OPT_BOOLEAN('b', "boolean", &boolean, "get a boolean"),
41 OPT_BIT('4', "or4", &boolean,
42 "bitwise-or boolean with ...0100", 4),
43 OPT_NEGBIT(0, "neg-or4", &boolean, "same as --no-or4", 4),
44 OPT_GROUP(""),
45 OPT_INTEGER('i', "integer", &integer, "get a integer"),
46 OPT_INTEGER('j', NULL, &integer, "get a integer, too"),
47 OPT_SET_INT(0, "set23", &integer, "set integer to 23", 23),
48 OPT_DATE('t', NULL, ×tamp, "get timestamp of <time>"),
49 OPT_CALLBACK('L', "length", &integer, "str",
50 "get length of <str>", length_callback),
51 OPT_FILENAME('F', "file", &file, "set file to <file>"),
52 OPT_GROUP("String options"),
53 OPT_STRING('s', "string", &string, "string", "get a string"),
54 OPT_STRING(0, "string2", &string, "str", "get another string"),
55 OPT_STRING(0, "st", &string, "st", "get another string (pervert ordering)"),
56 OPT_STRING('o', NULL, &string, "str", "get another string"),
57 OPT_NOOP_NOARG(0, "obsolete"),
58 OPT_SET_PTR(0, "default-string", &string,
59 "set string to default", (unsigned long)"default"),
60 OPT_STRING_LIST(0, "list", &list, "str", "add str to list"),
61 OPT_GROUP("Magic arguments"),
62 OPT_ARGUMENT("quux", "means --quux"),
63 OPT_NUMBER_CALLBACK(&integer, "set integer to NUM",
64 number_callback),
65 { OPTION_BOOLEAN, '+', NULL, &boolean, NULL, "same as -b",
66 PARSE_OPT_NOARG | PARSE_OPT_NONEG | PARSE_OPT_NODASH },
67 { OPTION_BOOLEAN, 0, "ambiguous", &ambiguous, NULL,
68 "positive ambiguity", PARSE_OPT_NOARG | PARSE_OPT_NONEG },
69 { OPTION_BOOLEAN, 0, "no-ambiguous", &ambiguous, NULL,
70 "negative ambiguity", PARSE_OPT_NOARG | PARSE_OPT_NONEG },
71 OPT_GROUP("Standard options"),
72 OPT__ABBREV(&abbrev),
73 OPT__VERBOSE(&verbose, "be verbose"),
74 OPT__DRY_RUN(&dry_run, "dry run"),
75 OPT__QUIET(&quiet, "be quiet"),
76 OPT_END(),
77 };
78 int i;
80 argc = parse_options(argc, argv, prefix, options, usage, 0);
82 printf("boolean: %d\n", boolean);
83 printf("integer: %u\n", integer);
84 printf("timestamp: %lu\n", timestamp);
85 printf("string: %s\n", string ? string : "(not set)");
86 printf("abbrev: %d\n", abbrev);
87 printf("verbose: %d\n", verbose);
88 printf("quiet: %s\n", quiet ? "yes" : "no");
89 printf("dry run: %s\n", dry_run ? "yes" : "no");
90 printf("file: %s\n", file ? file : "(not set)");
92 for (i = 0; i < list.nr; i++)
93 printf("list: %s\n", list.items[i].string);
95 for (i = 0; i < argc; i++)
96 printf("arg %02d: %s\n", i, argv[i]);
98 return 0;
99 }