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