1#ifndef PARSE_OPTIONS_H
2#define PARSE_OPTIONS_H
3
4enum parse_opt_type {
5
6 OPTION_END,
7 OPTION_ARGUMENT,
8 OPTION_GROUP,
9
10 OPTION_BIT,
11 OPTION_BOOLEAN,
12 OPTION_SET_INT,
13 OPTION_SET_PTR,
14
15 OPTION_STRING,
16 OPTION_INTEGER,
17 OPTION_LONG,
18 OPTION_CALLBACK,
19};
20
21enum parse_opt_flags {
22 PARSE_OPT_KEEP_DASHDASH = 1,
23 PARSE_OPT_STOP_AT_NON_OPTION = 2,
24 PARSE_OPT_KEEP_ARGV0 = 4,
25 PARSE_OPT_KEEP_UNKNOWN = 8,
26 PARSE_OPT_NO_INTERNAL_HELP = 16,
27};
28
29enum parse_opt_option_flags {
30 PARSE_OPT_OPTARG = 1,
31 PARSE_OPT_NOARG = 2,
32 PARSE_OPT_NONEG = 4,
33 PARSE_OPT_HIDDEN = 8,
34 PARSE_OPT_LASTARG_DEFAULT = 16,
35};
36
37struct option;
38typedef int parse_opt_cb(const struct option *, const char *arg, int unset);
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80struct option {
81 enum parse_opt_type type;
82 int short_name;
83 const char *long_name;
84 void *value;
85 const char *argh;
86 const char *help;
87
88 int flags;
89 parse_opt_cb *callback;
90 intptr_t defval;
91};
92
93#define OPT_END() { .type = OPTION_END }
94#define OPT_ARGUMENT(l, h) { .type = OPTION_ARGUMENT, .long_name = (l), .help = (h) }
95#define OPT_GROUP(h) { .type = OPTION_GROUP, .help = (h) }
96#define OPT_BIT(s, l, v, h, b) { .type = OPTION_BIT, .short_name = (s), .long_name = (l), .value = (v), .help = (h), .defval = (b) }
97#define OPT_BOOLEAN(s, l, v, h) { .type = OPTION_BOOLEAN, .short_name = (s), .long_name = (l), .value = (v), .help = (h) }
98#define OPT_SET_INT(s, l, v, h, i) { .type = OPTION_SET_INT, .short_name = (s), .long_name = (l), .value = (v), .help = (h), .defval = (i) }
99#define OPT_SET_PTR(s, l, v, h, p) { .type = OPTION_SET_PTR, .short_name = (s), .long_name = (l), .value = (v), .help = (h), .defval = (p) }
100#define OPT_INTEGER(s, l, v, h) { .type = OPTION_INTEGER, .short_name = (s), .long_name = (l), .value = (v), .help = (h) }
101#define OPT_LONG(s, l, v, h) { .type = OPTION_LONG, .short_name = (s), .long_name = (l), .value = (v), .help = (h) }
102#define OPT_STRING(s, l, v, a, h) { .type = OPTION_STRING, .short_name = (s), .long_name = (l), .value = (v), (a), .help = (h) }
103#define OPT_DATE(s, l, v, h) \
104 { .type = OPTION_CALLBACK, .short_name = (s), .long_name = (l), .value = (v), .argh = "time", .help = (h), .callback = parse_opt_approxidate_cb }
105#define OPT_CALLBACK(s, l, v, a, h, f) \
106 { .type = OPTION_CALLBACK, .short_name = (s), .long_name = (l), .value = (v), (a), .help = (h), .callback = (f) }
107#define OPT_CALLBACK_NOOPT(s, l, v, a, h, f) \
108 { .type = OPTION_CALLBACK, .short_name = (s), .long_name = (l), .value = (v), (a), .help = (h), .callback = (f), .flags = PARSE_OPT_NOARG }
109#define OPT_CALLBACK_DEFAULT(s, l, v, a, h, f, d) \
110 { .type = OPTION_CALLBACK, .short_name = (s), .long_name = (l), .value = (v), (a), .help = (h), .callback = (f), .defval = (intptr_t)d, .flags = PARSE_OPT_LASTARG_DEFAULT }
111
112
113
114
115
116extern int parse_options(int argc, const char **argv,
117 const struct option *options,
118 const char * const usagestr[], int flags);
119
120extern NORETURN void usage_with_options(const char * const *usagestr,
121 const struct option *options);
122
123
124
125enum {
126 PARSE_OPT_HELP = -1,
127 PARSE_OPT_DONE,
128 PARSE_OPT_UNKNOWN,
129};
130
131
132
133
134
135
136struct parse_opt_ctx_t {
137 const char **argv;
138 const char **out;
139 int argc, cpidx;
140 const char *opt;
141 int flags;
142};
143
144extern int parse_options_usage(const char * const *usagestr,
145 const struct option *opts);
146
147extern void parse_options_start(struct parse_opt_ctx_t *ctx,
148 int argc, const char **argv, int flags);
149
150extern int parse_options_step(struct parse_opt_ctx_t *ctx,
151 const struct option *options,
152 const char * const usagestr[]);
153
154extern int parse_options_end(struct parse_opt_ctx_t *ctx);
155
156
157
158extern int parse_opt_abbrev_cb(const struct option *, const char *, int);
159extern int parse_opt_approxidate_cb(const struct option *, const char *, int);
160extern int parse_opt_verbosity_cb(const struct option *, const char *, int);
161
162#define OPT__VERBOSE(var) OPT_BOOLEAN('v', "verbose", (var), "be verbose")
163#define OPT__QUIET(var) OPT_BOOLEAN('q', "quiet", (var), "be quiet")
164#define OPT__VERBOSITY(var) \
165 { OPTION_CALLBACK, 'v', "verbose", (var), NULL, "be more verbose", \
166 PARSE_OPT_NOARG, &parse_opt_verbosity_cb, 0 }, \
167 { OPTION_CALLBACK, 'q', "quiet", (var), NULL, "be more quiet", \
168 PARSE_OPT_NOARG, &parse_opt_verbosity_cb, 0 }
169#define OPT__DRY_RUN(var) OPT_BOOLEAN('n', "dry-run", (var), "dry run")
170#define OPT__ABBREV(var) \
171 { OPTION_CALLBACK, 0, "abbrev", (var), "n", \
172 "use <n> digits to display SHA-1s", \
173 PARSE_OPT_OPTARG, &parse_opt_abbrev_cb, 0 }
174
175extern const char *parse_options_fix_filename(const char *prefix, const char *file);
176
177#endif
178