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