Commit | Line | Data |
---|---|---|
4a59fd13 PH |
1 | #ifndef PARSE_OPTIONS_H |
2 | #define PARSE_OPTIONS_H | |
3 | ||
4 | enum parse_opt_type { | |
5 | OPTION_END, | |
d7a38c54 | 6 | OPTION_GROUP, |
4a59fd13 PH |
7 | OPTION_BOOLEAN, |
8 | OPTION_STRING, | |
9 | OPTION_INTEGER, | |
ffe659f9 | 10 | OPTION_CALLBACK, |
4a59fd13 PH |
11 | }; |
12 | ||
13 | enum parse_opt_flags { | |
14 | PARSE_OPT_KEEP_DASHDASH = 1, | |
15 | }; | |
16 | ||
ffe659f9 PH |
17 | enum parse_opt_option_flags { |
18 | PARSE_OPT_OPTARG = 1, | |
f481e22a | 19 | PARSE_OPT_NOARG = 2, |
ffe659f9 PH |
20 | }; |
21 | ||
22 | struct option; | |
23 | typedef int parse_opt_cb(const struct option *, const char *arg, int unset); | |
24 | ||
4a59fd13 PH |
25 | struct option { |
26 | enum parse_opt_type type; | |
27 | int short_name; | |
28 | const char *long_name; | |
29 | void *value; | |
d7a38c54 PH |
30 | const char *argh; |
31 | const char *help; | |
ffe659f9 PH |
32 | |
33 | int flags; | |
34 | parse_opt_cb *callback; | |
35 | /* holds default value for PARSE_OPT_OPTARG, | |
36 | though callbacks can use it like they want */ | |
37 | intptr_t defval; | |
4a59fd13 PH |
38 | }; |
39 | ||
40 | #define OPT_END() { OPTION_END } | |
d7a38c54 PH |
41 | #define OPT_GROUP(h) { OPTION_GROUP, 0, NULL, NULL, NULL, (h) } |
42 | #define OPT_BOOLEAN(s, l, v, h) { OPTION_BOOLEAN, (s), (l), (v), NULL, (h) } | |
43 | #define OPT_INTEGER(s, l, v, h) { OPTION_INTEGER, (s), (l), (v), NULL, (h) } | |
44 | #define OPT_STRING(s, l, v, a, h) { OPTION_STRING, (s), (l), (v), (a), (h) } | |
ffe659f9 PH |
45 | #define OPT_CALLBACK(s, l, v, a, h, f) \ |
46 | { OPTION_CALLBACK, (s), (l), (v), (a), (h), 0, (f) } | |
4a59fd13 PH |
47 | |
48 | /* parse_options() will filter out the processed options and leave the | |
49 | * non-option argments in argv[]. | |
50 | * Returns the number of arguments left in argv[]. | |
51 | */ | |
52 | extern int parse_options(int argc, const char **argv, | |
53 | const struct option *options, | |
d7a38c54 PH |
54 | const char * const usagestr[], int flags); |
55 | ||
56 | extern NORETURN void usage_with_options(const char * const *usagestr, | |
57 | const struct option *options); | |
4a59fd13 | 58 | |
0ce865b1 PH |
59 | /*----- some often used options -----*/ |
60 | extern int parse_opt_abbrev_cb(const struct option *, const char *, int); | |
61 | ||
62 | #define OPT__VERBOSE(var) OPT_BOOLEAN('v', "verbose", (var), "be verbose") | |
63 | #define OPT__QUIET(var) OPT_BOOLEAN('q', "quiet", (var), "be quiet") | |
64 | #define OPT__DRY_RUN(var) OPT_BOOLEAN('n', "dry-run", (var), "dry run") | |
65 | #define OPT__ABBREV(var) \ | |
66 | { OPTION_CALLBACK, 0, "abbrev", (var), "n", \ | |
67 | "use <n> digits to display SHA-1s", \ | |
68 | PARSE_OPT_OPTARG, &parse_opt_abbrev_cb, 0 } | |
69 | ||
4a59fd13 | 70 | #endif |