]>
Commit | Line | Data |
---|---|---|
4a59fd13 PH |
1 | #include "git-compat-util.h" |
2 | #include "parse-options.h" | |
4a59fd13 PH |
3 | |
4 | #define OPT_SHORT 1 | |
5 | #define OPT_UNSET 2 | |
6 | ||
7 | struct optparse_t { | |
8 | const char **argv; | |
9 | int argc; | |
10 | const char *opt; | |
11 | }; | |
12 | ||
13 | static inline const char *get_arg(struct optparse_t *p) | |
14 | { | |
15 | if (p->opt) { | |
16 | const char *res = p->opt; | |
17 | p->opt = NULL; | |
18 | return res; | |
19 | } | |
20 | p->argc--; | |
21 | return *++p->argv; | |
22 | } | |
23 | ||
24 | static inline const char *skip_prefix(const char *str, const char *prefix) | |
25 | { | |
26 | size_t len = strlen(prefix); | |
27 | return strncmp(str, prefix, len) ? NULL : str + len; | |
28 | } | |
29 | ||
30 | static int opterror(const struct option *opt, const char *reason, int flags) | |
31 | { | |
32 | if (flags & OPT_SHORT) | |
33 | return error("switch `%c' %s", opt->short_name, reason); | |
34 | if (flags & OPT_UNSET) | |
35 | return error("option `no-%s' %s", opt->long_name, reason); | |
36 | return error("option `%s' %s", opt->long_name, reason); | |
37 | } | |
38 | ||
39 | static int get_value(struct optparse_t *p, | |
40 | const struct option *opt, int flags) | |
41 | { | |
ffe659f9 | 42 | const char *s, *arg; |
db7244bd | 43 | const int unset = flags & OPT_UNSET; |
4a59fd13 | 44 | |
db7244bd | 45 | if (unset && p->opt) |
4a59fd13 | 46 | return opterror(opt, "takes no value", flags); |
db7244bd PH |
47 | if (unset && (opt->flags & PARSE_OPT_NONEG)) |
48 | return opterror(opt, "isn't available", flags); | |
4a59fd13 | 49 | |
db7244bd PH |
50 | if (!(flags & OPT_SHORT) && p->opt) { |
51 | switch (opt->type) { | |
52 | case OPTION_CALLBACK: | |
53 | if (!(opt->flags & PARSE_OPT_NOARG)) | |
54 | break; | |
55 | /* FALLTHROUGH */ | |
56 | case OPTION_BOOLEAN: | |
57 | case OPTION_BIT: | |
58 | case OPTION_SET_INT: | |
59 | case OPTION_SET_PTR: | |
4a59fd13 | 60 | return opterror(opt, "takes no value", flags); |
db7244bd PH |
61 | default: |
62 | break; | |
63 | } | |
64 | } | |
65 | ||
66 | arg = p->opt ? p->opt : (p->argc > 1 ? p->argv[1] : NULL); | |
67 | switch (opt->type) { | |
68 | case OPTION_BIT: | |
69 | if (unset) | |
70 | *(int *)opt->value &= ~opt->defval; | |
4a59fd13 | 71 | else |
db7244bd PH |
72 | *(int *)opt->value |= opt->defval; |
73 | return 0; | |
74 | ||
75 | case OPTION_BOOLEAN: | |
76 | *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1; | |
77 | return 0; | |
78 | ||
79 | case OPTION_SET_INT: | |
80 | *(int *)opt->value = unset ? 0 : opt->defval; | |
81 | return 0; | |
82 | ||
83 | case OPTION_SET_PTR: | |
84 | *(void **)opt->value = unset ? NULL : (void *)opt->defval; | |
4a59fd13 PH |
85 | return 0; |
86 | ||
87 | case OPTION_STRING: | |
db7244bd PH |
88 | if (unset) { |
89 | *(const char **)opt->value = NULL; | |
4a59fd13 PH |
90 | return 0; |
91 | } | |
ffe659f9 PH |
92 | if (opt->flags & PARSE_OPT_OPTARG && (!arg || *arg == '-')) { |
93 | *(const char **)opt->value = (const char *)opt->defval; | |
94 | return 0; | |
95 | } | |
96 | if (!arg) | |
4a59fd13 PH |
97 | return opterror(opt, "requires a value", flags); |
98 | *(const char **)opt->value = get_arg(p); | |
99 | return 0; | |
100 | ||
ffe659f9 | 101 | case OPTION_CALLBACK: |
db7244bd | 102 | if (unset) |
ffe659f9 | 103 | return (*opt->callback)(opt, NULL, 1); |
db7244bd | 104 | if (opt->flags & PARSE_OPT_NOARG) |
f481e22a | 105 | return (*opt->callback)(opt, NULL, 0); |
ffe659f9 PH |
106 | if (opt->flags & PARSE_OPT_OPTARG && (!arg || *arg == '-')) |
107 | return (*opt->callback)(opt, NULL, 0); | |
108 | if (!arg) | |
109 | return opterror(opt, "requires a value", flags); | |
110 | return (*opt->callback)(opt, get_arg(p), 0); | |
111 | ||
4a59fd13 | 112 | case OPTION_INTEGER: |
db7244bd | 113 | if (unset) { |
4a59fd13 PH |
114 | *(int *)opt->value = 0; |
115 | return 0; | |
116 | } | |
ffe659f9 PH |
117 | if (opt->flags & PARSE_OPT_OPTARG && (!arg || !isdigit(*arg))) { |
118 | *(int *)opt->value = opt->defval; | |
119 | return 0; | |
120 | } | |
121 | if (!arg) | |
4a59fd13 PH |
122 | return opterror(opt, "requires a value", flags); |
123 | *(int *)opt->value = strtol(get_arg(p), (char **)&s, 10); | |
124 | if (*s) | |
125 | return opterror(opt, "expects a numerical value", flags); | |
126 | return 0; | |
127 | ||
128 | default: | |
129 | die("should not happen, someone must be hit on the forehead"); | |
130 | } | |
131 | } | |
132 | ||
133 | static int parse_short_opt(struct optparse_t *p, const struct option *options) | |
134 | { | |
135 | for (; options->type != OPTION_END; options++) { | |
136 | if (options->short_name == *p->opt) { | |
137 | p->opt = p->opt[1] ? p->opt + 1 : NULL; | |
138 | return get_value(p, options, OPT_SHORT); | |
139 | } | |
140 | } | |
141 | return error("unknown switch `%c'", *p->opt); | |
142 | } | |
143 | ||
144 | static int parse_long_opt(struct optparse_t *p, const char *arg, | |
145 | const struct option *options) | |
146 | { | |
7f275b91 | 147 | const char *arg_end = strchr(arg, '='); |
243e0614 JS |
148 | const struct option *abbrev_option = NULL, *ambiguous_option = NULL; |
149 | int abbrev_flags = 0, ambiguous_flags = 0; | |
7f275b91 JS |
150 | |
151 | if (!arg_end) | |
152 | arg_end = arg + strlen(arg); | |
153 | ||
4a59fd13 PH |
154 | for (; options->type != OPTION_END; options++) { |
155 | const char *rest; | |
156 | int flags = 0; | |
157 | ||
158 | if (!options->long_name) | |
159 | continue; | |
160 | ||
161 | rest = skip_prefix(arg, options->long_name); | |
162 | if (!rest) { | |
7f275b91 JS |
163 | /* abbreviated? */ |
164 | if (!strncmp(options->long_name, arg, arg_end - arg)) { | |
165 | is_abbreviated: | |
243e0614 JS |
166 | if (abbrev_option) { |
167 | /* | |
168 | * If this is abbreviated, it is | |
169 | * ambiguous. So when there is no | |
170 | * exact match later, we need to | |
171 | * error out. | |
172 | */ | |
173 | ambiguous_option = abbrev_option; | |
174 | ambiguous_flags = abbrev_flags; | |
175 | } | |
7f275b91 JS |
176 | if (!(flags & OPT_UNSET) && *arg_end) |
177 | p->opt = arg_end + 1; | |
178 | abbrev_option = options; | |
179 | abbrev_flags = flags; | |
180 | continue; | |
181 | } | |
182 | /* negated and abbreviated very much? */ | |
183 | if (!prefixcmp("no-", arg)) { | |
184 | flags |= OPT_UNSET; | |
185 | goto is_abbreviated; | |
186 | } | |
187 | /* negated? */ | |
4a59fd13 PH |
188 | if (strncmp(arg, "no-", 3)) |
189 | continue; | |
190 | flags |= OPT_UNSET; | |
191 | rest = skip_prefix(arg + 3, options->long_name); | |
7f275b91 JS |
192 | /* abbreviated and negated? */ |
193 | if (!rest && !prefixcmp(options->long_name, arg + 3)) | |
194 | goto is_abbreviated; | |
4a59fd13 PH |
195 | if (!rest) |
196 | continue; | |
197 | } | |
198 | if (*rest) { | |
199 | if (*rest != '=') | |
200 | continue; | |
201 | p->opt = rest + 1; | |
202 | } | |
203 | return get_value(p, options, flags); | |
204 | } | |
243e0614 JS |
205 | |
206 | if (ambiguous_option) | |
207 | return error("Ambiguous option: %s " | |
208 | "(could be --%s%s or --%s%s)", | |
209 | arg, | |
210 | (ambiguous_flags & OPT_UNSET) ? "no-" : "", | |
211 | ambiguous_option->long_name, | |
212 | (abbrev_flags & OPT_UNSET) ? "no-" : "", | |
213 | abbrev_option->long_name); | |
7f275b91 JS |
214 | if (abbrev_option) |
215 | return get_value(p, abbrev_option, abbrev_flags); | |
4a59fd13 PH |
216 | return error("unknown option `%s'", arg); |
217 | } | |
218 | ||
dd3bf0f4 PH |
219 | static NORETURN void usage_with_options_internal(const char * const *, |
220 | const struct option *, int); | |
221 | ||
4a59fd13 | 222 | int parse_options(int argc, const char **argv, const struct option *options, |
d7a38c54 | 223 | const char * const usagestr[], int flags) |
4a59fd13 PH |
224 | { |
225 | struct optparse_t args = { argv + 1, argc - 1, NULL }; | |
226 | int j = 0; | |
227 | ||
228 | for (; args.argc; args.argc--, args.argv++) { | |
229 | const char *arg = args.argv[0]; | |
230 | ||
231 | if (*arg != '-' || !arg[1]) { | |
232 | argv[j++] = args.argv[0]; | |
233 | continue; | |
234 | } | |
235 | ||
236 | if (arg[1] != '-') { | |
237 | args.opt = arg + 1; | |
238 | do { | |
239 | if (*args.opt == 'h') | |
d7a38c54 | 240 | usage_with_options(usagestr, options); |
4a59fd13 | 241 | if (parse_short_opt(&args, options) < 0) |
d7a38c54 | 242 | usage_with_options(usagestr, options); |
4a59fd13 PH |
243 | } while (args.opt); |
244 | continue; | |
245 | } | |
246 | ||
247 | if (!arg[2]) { /* "--" */ | |
248 | if (!(flags & PARSE_OPT_KEEP_DASHDASH)) { | |
249 | args.argc--; | |
250 | args.argv++; | |
251 | } | |
252 | break; | |
253 | } | |
254 | ||
dd3bf0f4 PH |
255 | if (!strcmp(arg + 2, "help-all")) |
256 | usage_with_options_internal(usagestr, options, 1); | |
4a59fd13 | 257 | if (!strcmp(arg + 2, "help")) |
d7a38c54 | 258 | usage_with_options(usagestr, options); |
4a59fd13 | 259 | if (parse_long_opt(&args, arg + 2, options)) |
d7a38c54 | 260 | usage_with_options(usagestr, options); |
4a59fd13 PH |
261 | } |
262 | ||
263 | memmove(argv + j, args.argv, args.argc * sizeof(*argv)); | |
264 | argv[j + args.argc] = NULL; | |
265 | return j + args.argc; | |
266 | } | |
d7a38c54 PH |
267 | |
268 | #define USAGE_OPTS_WIDTH 24 | |
269 | #define USAGE_GAP 2 | |
270 | ||
dd3bf0f4 PH |
271 | void usage_with_options_internal(const char * const *usagestr, |
272 | const struct option *opts, int full) | |
d7a38c54 | 273 | { |
f389c808 AR |
274 | fprintf(stderr, "usage: %s\n", *usagestr++); |
275 | while (*usagestr && **usagestr) | |
276 | fprintf(stderr, " or: %s\n", *usagestr++); | |
277 | while (*usagestr) | |
278 | fprintf(stderr, " %s\n", *usagestr++); | |
d7a38c54 PH |
279 | |
280 | if (opts->type != OPTION_GROUP) | |
f389c808 | 281 | fputc('\n', stderr); |
d7a38c54 PH |
282 | |
283 | for (; opts->type != OPTION_END; opts++) { | |
284 | size_t pos; | |
285 | int pad; | |
286 | ||
287 | if (opts->type == OPTION_GROUP) { | |
f389c808 | 288 | fputc('\n', stderr); |
d7a38c54 | 289 | if (*opts->help) |
f389c808 | 290 | fprintf(stderr, "%s\n", opts->help); |
d7a38c54 PH |
291 | continue; |
292 | } | |
dd3bf0f4 PH |
293 | if (!full && (opts->flags & PARSE_OPT_HIDDEN)) |
294 | continue; | |
d7a38c54 | 295 | |
f389c808 | 296 | pos = fprintf(stderr, " "); |
d7a38c54 | 297 | if (opts->short_name) |
f389c808 | 298 | pos += fprintf(stderr, "-%c", opts->short_name); |
d7a38c54 | 299 | if (opts->long_name && opts->short_name) |
f389c808 | 300 | pos += fprintf(stderr, ", "); |
d7a38c54 | 301 | if (opts->long_name) |
f389c808 | 302 | pos += fprintf(stderr, "--%s", opts->long_name); |
d7a38c54 PH |
303 | |
304 | switch (opts->type) { | |
305 | case OPTION_INTEGER: | |
ffe659f9 PH |
306 | if (opts->flags & PARSE_OPT_OPTARG) |
307 | pos += fprintf(stderr, " [<n>]"); | |
308 | else | |
309 | pos += fprintf(stderr, " <n>"); | |
d7a38c54 | 310 | break; |
ffe659f9 | 311 | case OPTION_CALLBACK: |
f481e22a PH |
312 | if (opts->flags & PARSE_OPT_NOARG) |
313 | break; | |
314 | /* FALLTHROUGH */ | |
315 | case OPTION_STRING: | |
ffe659f9 PH |
316 | if (opts->argh) { |
317 | if (opts->flags & PARSE_OPT_OPTARG) | |
318 | pos += fprintf(stderr, " [<%s>]", opts->argh); | |
319 | else | |
320 | pos += fprintf(stderr, " <%s>", opts->argh); | |
321 | } else { | |
322 | if (opts->flags & PARSE_OPT_OPTARG) | |
323 | pos += fprintf(stderr, " [...]"); | |
324 | else | |
325 | pos += fprintf(stderr, " ..."); | |
326 | } | |
d7a38c54 | 327 | break; |
db7244bd | 328 | default: /* OPTION_{BIT,BOOLEAN,SET_INT,SET_PTR} */ |
d7a38c54 PH |
329 | break; |
330 | } | |
331 | ||
f389c808 AR |
332 | if (pos <= USAGE_OPTS_WIDTH) |
333 | pad = USAGE_OPTS_WIDTH - pos; | |
d7a38c54 | 334 | else { |
f389c808 | 335 | fputc('\n', stderr); |
d7a38c54 PH |
336 | pad = USAGE_OPTS_WIDTH; |
337 | } | |
f389c808 | 338 | fprintf(stderr, "%*s%s\n", pad + USAGE_GAP, "", opts->help); |
d7a38c54 | 339 | } |
f389c808 AR |
340 | fputc('\n', stderr); |
341 | ||
342 | exit(129); | |
d7a38c54 | 343 | } |
0ce865b1 | 344 | |
dd3bf0f4 PH |
345 | void usage_with_options(const char * const *usagestr, |
346 | const struct option *opts) | |
347 | { | |
348 | usage_with_options_internal(usagestr, opts, 0); | |
349 | } | |
350 | ||
0ce865b1 PH |
351 | /*----- some often used options -----*/ |
352 | #include "cache.h" | |
db7244bd | 353 | |
0ce865b1 PH |
354 | int parse_opt_abbrev_cb(const struct option *opt, const char *arg, int unset) |
355 | { | |
356 | int v; | |
357 | ||
358 | if (!arg) { | |
359 | v = unset ? 0 : DEFAULT_ABBREV; | |
360 | } else { | |
361 | v = strtol(arg, (char **)&arg, 10); | |
362 | if (*arg) | |
363 | return opterror(opt, "expects a numerical value", 0); | |
364 | if (v && v < MINIMUM_ABBREV) | |
365 | v = MINIMUM_ABBREV; | |
366 | else if (v > 40) | |
367 | v = 40; | |
368 | } | |
369 | *(int *)(opt->value) = v; | |
370 | return 0; | |
371 | } |