]>
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, | |
19 | }; | |
20 | ||
21 | struct option; | |
22 | typedef int parse_opt_cb(const struct option *, const char *arg, int unset); | |
23 | ||
4a59fd13 PH |
24 | struct option { |
25 | enum parse_opt_type type; | |
26 | int short_name; | |
27 | const char *long_name; | |
28 | void *value; | |
d7a38c54 PH |
29 | const char *argh; |
30 | const char *help; | |
ffe659f9 PH |
31 | |
32 | int flags; | |
33 | parse_opt_cb *callback; | |
34 | /* holds default value for PARSE_OPT_OPTARG, | |
35 | though callbacks can use it like they want */ | |
36 | intptr_t defval; | |
4a59fd13 PH |
37 | }; |
38 | ||
39 | #define OPT_END() { OPTION_END } | |
d7a38c54 PH |
40 | #define OPT_GROUP(h) { OPTION_GROUP, 0, NULL, NULL, NULL, (h) } |
41 | #define OPT_BOOLEAN(s, l, v, h) { OPTION_BOOLEAN, (s), (l), (v), NULL, (h) } | |
42 | #define OPT_INTEGER(s, l, v, h) { OPTION_INTEGER, (s), (l), (v), NULL, (h) } | |
43 | #define OPT_STRING(s, l, v, a, h) { OPTION_STRING, (s), (l), (v), (a), (h) } | |
ffe659f9 PH |
44 | #define OPT_CALLBACK(s, l, v, a, h, f) \ |
45 | { OPTION_CALLBACK, (s), (l), (v), (a), (h), 0, (f) } | |
4a59fd13 PH |
46 | |
47 | /* parse_options() will filter out the processed options and leave the | |
48 | * non-option argments in argv[]. | |
49 | * Returns the number of arguments left in argv[]. | |
50 | */ | |
51 | extern int parse_options(int argc, const char **argv, | |
52 | const struct option *options, | |
d7a38c54 PH |
53 | const char * const usagestr[], int flags); |
54 | ||
55 | extern NORETURN void usage_with_options(const char * const *usagestr, | |
56 | const struct option *options); | |
4a59fd13 PH |
57 | |
58 | #endif |