]>
Commit | Line | Data |
---|---|---|
beb47437 JS |
1 | #include "cache.h" |
2 | #include "parse-options.h" | |
c8ba1639 | 3 | #include "string-list.h" |
beb47437 JS |
4 | |
5 | static int boolean = 0; | |
c4aca9cc JH |
6 | static int integer = 0; |
7 | static unsigned long timestamp; | |
010a2dac SB |
8 | static int abbrev = 7; |
9 | static int verbose = 0, dry_run = 0, quiet = 0; | |
beb47437 | 10 | static char *string = NULL; |
df217ed6 | 11 | static char *file = NULL; |
6bbfd1fa | 12 | static int ambiguous; |
c8ba1639 | 13 | static struct string_list list; |
beb47437 | 14 | |
2af202be | 15 | static int length_callback(const struct option *opt, const char *arg, int unset) |
010a2dac SB |
16 | { |
17 | printf("Callback: \"%s\", %d\n", | |
18 | (arg ? arg : "not set"), unset); | |
19 | if (unset) | |
20 | return 1; /* do not support unset */ | |
21 | ||
8caa3acf | 22 | *(int *)opt->value = strlen(arg); |
010a2dac SB |
23 | return 0; |
24 | } | |
25 | ||
2af202be | 26 | static int number_callback(const struct option *opt, const char *arg, int unset) |
e0319ff5 RS |
27 | { |
28 | *(int *)opt->value = strtol(arg, NULL, 10); | |
29 | return 0; | |
30 | } | |
31 | ||
beb47437 JS |
32 | int main(int argc, const char **argv) |
33 | { | |
df217ed6 | 34 | const char *prefix = "prefix/"; |
beb47437 JS |
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"), | |
010a2dac SB |
41 | OPT_BIT('4', "or4", &boolean, |
42 | "bitwise-or boolean with ...0100", 4), | |
2f4b97f9 | 43 | OPT_NEGBIT(0, "neg-or4", &boolean, "same as --no-or4", 4), |
010a2dac | 44 | OPT_GROUP(""), |
beb47437 JS |
45 | OPT_INTEGER('i', "integer", &integer, "get a integer"), |
46 | OPT_INTEGER('j', NULL, &integer, "get a integer, too"), | |
010a2dac | 47 | OPT_SET_INT(0, "set23", &integer, "set integer to 23", 23), |
c4aca9cc | 48 | OPT_DATE('t', NULL, ×tamp, "get timestamp of <time>"), |
010a2dac SB |
49 | OPT_CALLBACK('L', "length", &integer, "str", |
50 | "get length of <str>", length_callback), | |
41dbcd45 | 51 | OPT_FILENAME('F', "file", &file, "set file to <file>"), |
010a2dac | 52 | OPT_GROUP("String options"), |
beb47437 JS |
53 | OPT_STRING('s', "string", &string, "string", "get a string"), |
54 | OPT_STRING(0, "string2", &string, "str", "get another string"), | |
243e0614 | 55 | OPT_STRING(0, "st", &string, "st", "get another string (pervert ordering)"), |
3a9f0f41 | 56 | OPT_STRING('o', NULL, &string, "str", "get another string"), |
6acec038 | 57 | OPT_NOOP_NOARG(0, "obsolete"), |
010a2dac SB |
58 | OPT_SET_PTR(0, "default-string", &string, |
59 | "set string to default", (unsigned long)"default"), | |
c8ba1639 | 60 | OPT_STRING_LIST(0, "list", &list, "str", "add str to list"), |
010a2dac | 61 | OPT_GROUP("Magic arguments"), |
580d5bff | 62 | OPT_ARGUMENT("quux", "means --quux"), |
e0319ff5 RS |
63 | OPT_NUMBER_CALLBACK(&integer, "set integer to NUM", |
64 | number_callback), | |
51a9949e RS |
65 | { OPTION_BOOLEAN, '+', NULL, &boolean, NULL, "same as -b", |
66 | PARSE_OPT_NOARG | PARSE_OPT_NONEG | PARSE_OPT_NODASH }, | |
6bbfd1fa AS |
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 }, | |
010a2dac SB |
71 | OPT_GROUP("Standard options"), |
72 | OPT__ABBREV(&abbrev), | |
fd03881a | 73 | OPT__VERBOSE(&verbose, "be verbose"), |
e21adb8c | 74 | OPT__DRY_RUN(&dry_run, "dry run"), |
d52ee6e6 | 75 | OPT__QUIET(&quiet, "be quiet"), |
beb47437 JS |
76 | OPT_END(), |
77 | }; | |
78 | int i; | |
79 | ||
df217ed6 | 80 | argc = parse_options(argc, argv, prefix, options, usage, 0); |
beb47437 JS |
81 | |
82 | printf("boolean: %d\n", boolean); | |
c4aca9cc JH |
83 | printf("integer: %u\n", integer); |
84 | printf("timestamp: %lu\n", timestamp); | |
beb47437 | 85 | printf("string: %s\n", string ? string : "(not set)"); |
010a2dac SB |
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"); | |
df217ed6 | 90 | printf("file: %s\n", file ? file : "(not set)"); |
beb47437 | 91 | |
c8ba1639 JK |
92 | for (i = 0; i < list.nr; i++) |
93 | printf("list: %s\n", list.items[i].string); | |
94 | ||
beb47437 JS |
95 | for (i = 0; i < argc; i++) |
96 | printf("arg %02d: %s\n", i, argv[i]); | |
97 | ||
98 | return 0; | |
99 | } |