]>
Commit | Line | Data |
---|---|---|
beb47437 JS |
1 | #include "cache.h" |
2 | #include "parse-options.h" | |
3 | ||
4 | static int boolean = 0; | |
c4aca9cc JH |
5 | static int integer = 0; |
6 | static unsigned long timestamp; | |
010a2dac SB |
7 | static int abbrev = 7; |
8 | static int verbose = 0, dry_run = 0, quiet = 0; | |
beb47437 JS |
9 | static char *string = NULL; |
10 | ||
010a2dac SB |
11 | int length_callback(const struct option *opt, const char *arg, int unset) |
12 | { | |
13 | printf("Callback: \"%s\", %d\n", | |
14 | (arg ? arg : "not set"), unset); | |
15 | if (unset) | |
16 | return 1; /* do not support unset */ | |
17 | ||
8caa3acf | 18 | *(int *)opt->value = strlen(arg); |
010a2dac SB |
19 | return 0; |
20 | } | |
21 | ||
e0319ff5 RS |
22 | int number_callback(const struct option *opt, const char *arg, int unset) |
23 | { | |
24 | *(int *)opt->value = strtol(arg, NULL, 10); | |
25 | return 0; | |
26 | } | |
27 | ||
beb47437 JS |
28 | int main(int argc, const char **argv) |
29 | { | |
30 | const char *usage[] = { | |
31 | "test-parse-options <options>", | |
32 | NULL | |
33 | }; | |
34 | struct option options[] = { | |
35 | OPT_BOOLEAN('b', "boolean", &boolean, "get a boolean"), | |
010a2dac SB |
36 | OPT_BIT('4', "or4", &boolean, |
37 | "bitwise-or boolean with ...0100", 4), | |
2f4b97f9 | 38 | OPT_NEGBIT(0, "neg-or4", &boolean, "same as --no-or4", 4), |
010a2dac | 39 | OPT_GROUP(""), |
beb47437 JS |
40 | OPT_INTEGER('i', "integer", &integer, "get a integer"), |
41 | OPT_INTEGER('j', NULL, &integer, "get a integer, too"), | |
010a2dac | 42 | OPT_SET_INT(0, "set23", &integer, "set integer to 23", 23), |
c4aca9cc | 43 | OPT_DATE('t', NULL, ×tamp, "get timestamp of <time>"), |
010a2dac SB |
44 | OPT_CALLBACK('L', "length", &integer, "str", |
45 | "get length of <str>", length_callback), | |
46 | OPT_GROUP("String options"), | |
beb47437 JS |
47 | OPT_STRING('s', "string", &string, "string", "get a string"), |
48 | OPT_STRING(0, "string2", &string, "str", "get another string"), | |
243e0614 | 49 | OPT_STRING(0, "st", &string, "st", "get another string (pervert ordering)"), |
3a9f0f41 | 50 | OPT_STRING('o', NULL, &string, "str", "get another string"), |
010a2dac SB |
51 | OPT_SET_PTR(0, "default-string", &string, |
52 | "set string to default", (unsigned long)"default"), | |
53 | OPT_GROUP("Magic arguments"), | |
580d5bff | 54 | OPT_ARGUMENT("quux", "means --quux"), |
e0319ff5 RS |
55 | OPT_NUMBER_CALLBACK(&integer, "set integer to NUM", |
56 | number_callback), | |
010a2dac SB |
57 | OPT_GROUP("Standard options"), |
58 | OPT__ABBREV(&abbrev), | |
59 | OPT__VERBOSE(&verbose), | |
60 | OPT__DRY_RUN(&dry_run), | |
61 | OPT__QUIET(&quiet), | |
beb47437 JS |
62 | OPT_END(), |
63 | }; | |
64 | int i; | |
65 | ||
66 | argc = parse_options(argc, argv, options, usage, 0); | |
67 | ||
68 | printf("boolean: %d\n", boolean); | |
c4aca9cc JH |
69 | printf("integer: %u\n", integer); |
70 | printf("timestamp: %lu\n", timestamp); | |
beb47437 | 71 | printf("string: %s\n", string ? string : "(not set)"); |
010a2dac SB |
72 | printf("abbrev: %d\n", abbrev); |
73 | printf("verbose: %d\n", verbose); | |
74 | printf("quiet: %s\n", quiet ? "yes" : "no"); | |
75 | printf("dry run: %s\n", dry_run ? "yes" : "no"); | |
beb47437 JS |
76 | |
77 | for (i = 0; i < argc; i++) | |
78 | printf("arg %02d: %s\n", i, argv[i]); | |
79 | ||
80 | return 0; | |
81 | } |