parse-opt: do not print errors on unknown options, return -2 intead.
[git/git.git] / parse-options.c
CommitLineData
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
7e7bbcb4 7static inline const char *get_arg(struct parse_opt_ctx_t *p)
4a59fd13
PH
8{
9 if (p->opt) {
10 const char *res = p->opt;
11 p->opt = NULL;
12 return res;
13 }
14 p->argc--;
15 return *++p->argv;
16}
17
18static inline const char *skip_prefix(const char *str, const char *prefix)
19{
20 size_t len = strlen(prefix);
21 return strncmp(str, prefix, len) ? NULL : str + len;
22}
23
24static int opterror(const struct option *opt, const char *reason, int flags)
25{
26 if (flags & OPT_SHORT)
27 return error("switch `%c' %s", opt->short_name, reason);
28 if (flags & OPT_UNSET)
29 return error("option `no-%s' %s", opt->long_name, reason);
30 return error("option `%s' %s", opt->long_name, reason);
31}
32
7e7bbcb4 33static int get_value(struct parse_opt_ctx_t *p,
4a59fd13
PH
34 const struct option *opt, int flags)
35{
ffe659f9 36 const char *s, *arg;
db7244bd 37 const int unset = flags & OPT_UNSET;
4a59fd13 38
db7244bd 39 if (unset && p->opt)
4a59fd13 40 return opterror(opt, "takes no value", flags);
db7244bd
PH
41 if (unset && (opt->flags & PARSE_OPT_NONEG))
42 return opterror(opt, "isn't available", flags);
4a59fd13 43
db7244bd
PH
44 if (!(flags & OPT_SHORT) && p->opt) {
45 switch (opt->type) {
46 case OPTION_CALLBACK:
47 if (!(opt->flags & PARSE_OPT_NOARG))
48 break;
49 /* FALLTHROUGH */
50 case OPTION_BOOLEAN:
51 case OPTION_BIT:
52 case OPTION_SET_INT:
53 case OPTION_SET_PTR:
4a59fd13 54 return opterror(opt, "takes no value", flags);
db7244bd
PH
55 default:
56 break;
57 }
58 }
59
60 arg = p->opt ? p->opt : (p->argc > 1 ? p->argv[1] : NULL);
61 switch (opt->type) {
62 case OPTION_BIT:
63 if (unset)
64 *(int *)opt->value &= ~opt->defval;
4a59fd13 65 else
db7244bd
PH
66 *(int *)opt->value |= opt->defval;
67 return 0;
68
69 case OPTION_BOOLEAN:
70 *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
71 return 0;
72
73 case OPTION_SET_INT:
74 *(int *)opt->value = unset ? 0 : opt->defval;
75 return 0;
76
77 case OPTION_SET_PTR:
78 *(void **)opt->value = unset ? NULL : (void *)opt->defval;
4a59fd13
PH
79 return 0;
80
81 case OPTION_STRING:
db7244bd
PH
82 if (unset) {
83 *(const char **)opt->value = NULL;
4a59fd13
PH
84 return 0;
85 }
c43a2483 86 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
ffe659f9
PH
87 *(const char **)opt->value = (const char *)opt->defval;
88 return 0;
89 }
90 if (!arg)
4a59fd13
PH
91 return opterror(opt, "requires a value", flags);
92 *(const char **)opt->value = get_arg(p);
93 return 0;
94
ffe659f9 95 case OPTION_CALLBACK:
db7244bd 96 if (unset)
07fe54db 97 return (*opt->callback)(opt, NULL, 1) ? (-1) : 0;
db7244bd 98 if (opt->flags & PARSE_OPT_NOARG)
07fe54db 99 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
c43a2483 100 if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
07fe54db 101 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
ffe659f9
PH
102 if (!arg)
103 return opterror(opt, "requires a value", flags);
07fe54db 104 return (*opt->callback)(opt, get_arg(p), 0) ? (-1) : 0;
ffe659f9 105
4a59fd13 106 case OPTION_INTEGER:
db7244bd 107 if (unset) {
4a59fd13
PH
108 *(int *)opt->value = 0;
109 return 0;
110 }
c43a2483 111 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
ffe659f9
PH
112 *(int *)opt->value = opt->defval;
113 return 0;
114 }
115 if (!arg)
4a59fd13
PH
116 return opterror(opt, "requires a value", flags);
117 *(int *)opt->value = strtol(get_arg(p), (char **)&s, 10);
118 if (*s)
119 return opterror(opt, "expects a numerical value", flags);
120 return 0;
121
122 default:
123 die("should not happen, someone must be hit on the forehead");
124 }
125}
126
7e7bbcb4 127static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)
4a59fd13
PH
128{
129 for (; options->type != OPTION_END; options++) {
130 if (options->short_name == *p->opt) {
131 p->opt = p->opt[1] ? p->opt + 1 : NULL;
132 return get_value(p, options, OPT_SHORT);
133 }
134 }
07fe54db 135 return -2;
4a59fd13
PH
136}
137
7e7bbcb4 138static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
4a59fd13
PH
139 const struct option *options)
140{
7f275b91 141 const char *arg_end = strchr(arg, '=');
243e0614
JS
142 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
143 int abbrev_flags = 0, ambiguous_flags = 0;
7f275b91
JS
144
145 if (!arg_end)
146 arg_end = arg + strlen(arg);
147
4a59fd13
PH
148 for (; options->type != OPTION_END; options++) {
149 const char *rest;
150 int flags = 0;
151
152 if (!options->long_name)
153 continue;
154
155 rest = skip_prefix(arg, options->long_name);
580d5bff
PH
156 if (options->type == OPTION_ARGUMENT) {
157 if (!rest)
158 continue;
159 if (*rest == '=')
160 return opterror(options, "takes no value", flags);
161 if (*rest)
162 continue;
163 p->out[p->cpidx++] = arg - 2;
164 return 0;
165 }
4a59fd13 166 if (!rest) {
7f275b91
JS
167 /* abbreviated? */
168 if (!strncmp(options->long_name, arg, arg_end - arg)) {
169is_abbreviated:
243e0614
JS
170 if (abbrev_option) {
171 /*
172 * If this is abbreviated, it is
173 * ambiguous. So when there is no
174 * exact match later, we need to
175 * error out.
176 */
177 ambiguous_option = abbrev_option;
178 ambiguous_flags = abbrev_flags;
179 }
7f275b91
JS
180 if (!(flags & OPT_UNSET) && *arg_end)
181 p->opt = arg_end + 1;
182 abbrev_option = options;
183 abbrev_flags = flags;
184 continue;
185 }
186 /* negated and abbreviated very much? */
187 if (!prefixcmp("no-", arg)) {
188 flags |= OPT_UNSET;
189 goto is_abbreviated;
190 }
191 /* negated? */
4a59fd13
PH
192 if (strncmp(arg, "no-", 3))
193 continue;
194 flags |= OPT_UNSET;
195 rest = skip_prefix(arg + 3, options->long_name);
7f275b91
JS
196 /* abbreviated and negated? */
197 if (!rest && !prefixcmp(options->long_name, arg + 3))
198 goto is_abbreviated;
4a59fd13
PH
199 if (!rest)
200 continue;
201 }
202 if (*rest) {
203 if (*rest != '=')
204 continue;
205 p->opt = rest + 1;
206 }
207 return get_value(p, options, flags);
208 }
243e0614
JS
209
210 if (ambiguous_option)
211 return error("Ambiguous option: %s "
212 "(could be --%s%s or --%s%s)",
213 arg,
214 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
215 ambiguous_option->long_name,
216 (abbrev_flags & OPT_UNSET) ? "no-" : "",
217 abbrev_option->long_name);
7f275b91
JS
218 if (abbrev_option)
219 return get_value(p, abbrev_option, abbrev_flags);
07fe54db 220 return -2;
4a59fd13
PH
221}
222
3a9f0f41
PH
223void check_typos(const char *arg, const struct option *options)
224{
225 if (strlen(arg) < 3)
226 return;
227
228 if (!prefixcmp(arg, "no-")) {
229 error ("did you mean `--%s` (with two dashes ?)", arg);
230 exit(129);
231 }
232
233 for (; options->type != OPTION_END; options++) {
234 if (!options->long_name)
235 continue;
236 if (!prefixcmp(options->long_name, arg)) {
237 error ("did you mean `--%s` (with two dashes ?)", arg);
238 exit(129);
239 }
240 }
241}
242
7e7bbcb4
PH
243void parse_options_start(struct parse_opt_ctx_t *ctx,
244 int argc, const char **argv, int flags)
245{
246 memset(ctx, 0, sizeof(*ctx));
247 ctx->argc = argc - 1;
248 ctx->argv = argv + 1;
249 ctx->out = argv;
250 ctx->flags = flags;
251}
252
ee68b87a 253static int usage_with_options_internal(const char * const *,
ff43ec3e 254 const struct option *, int);
dd3bf0f4 255
ff43ec3e
PH
256int parse_options_step(struct parse_opt_ctx_t *ctx,
257 const struct option *options,
258 const char * const usagestr[])
4a59fd13 259{
ff43ec3e
PH
260 for (; ctx->argc; ctx->argc--, ctx->argv++) {
261 const char *arg = ctx->argv[0];
4a59fd13
PH
262
263 if (*arg != '-' || !arg[1]) {
ff43ec3e 264 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
a0ec9d25 265 break;
ff43ec3e 266 ctx->out[ctx->cpidx++] = ctx->argv[0];
4a59fd13
PH
267 continue;
268 }
269
270 if (arg[1] != '-') {
ff43ec3e
PH
271 ctx->opt = arg + 1;
272 if (*ctx->opt == 'h')
273 return parse_options_usage(usagestr, options);
07fe54db
PH
274 switch (parse_short_opt(ctx, options)) {
275 case -1:
276 return parse_options_usage(usagestr, options);
277 case -2:
278 return PARSE_OPT_UNKNOWN;
279 }
ff43ec3e 280 if (ctx->opt)
3a9f0f41 281 check_typos(arg + 1, options);
ff43ec3e
PH
282 while (ctx->opt) {
283 if (*ctx->opt == 'h')
284 return parse_options_usage(usagestr, options);
07fe54db
PH
285 switch (parse_short_opt(ctx, options)) {
286 case -1:
287 return parse_options_usage(usagestr, options);
288 case -2:
289 return PARSE_OPT_UNKNOWN;
290 }
3a9f0f41 291 }
4a59fd13
PH
292 continue;
293 }
294
295 if (!arg[2]) { /* "--" */
ff43ec3e
PH
296 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
297 ctx->argc--;
298 ctx->argv++;
4a59fd13
PH
299 }
300 break;
301 }
302
dd3bf0f4 303 if (!strcmp(arg + 2, "help-all"))
ff43ec3e 304 return usage_with_options_internal(usagestr, options, 1);
4a59fd13 305 if (!strcmp(arg + 2, "help"))
ff43ec3e 306 return parse_options_usage(usagestr, options);
07fe54db
PH
307 switch (parse_long_opt(ctx, arg + 2, options)) {
308 case -1:
309 return parse_options_usage(usagestr, options);
310 case -2:
311 return PARSE_OPT_UNKNOWN;
312 }
ff43ec3e
PH
313 }
314 return PARSE_OPT_DONE;
315}
316
317int parse_options_end(struct parse_opt_ctx_t *ctx)
318{
319 memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out));
320 ctx->out[ctx->cpidx + ctx->argc] = NULL;
321 return ctx->cpidx + ctx->argc;
322}
323
324int parse_options(int argc, const char **argv, const struct option *options,
325 const char * const usagestr[], int flags)
326{
327 struct parse_opt_ctx_t ctx;
328
329 parse_options_start(&ctx, argc, argv, flags);
330 switch (parse_options_step(&ctx, options, usagestr)) {
331 case PARSE_OPT_HELP:
332 exit(129);
333 case PARSE_OPT_DONE:
334 break;
335 default: /* PARSE_OPT_UNKNOWN */
07fe54db
PH
336 if (ctx.argv[0][1] == '-') {
337 error("unknown option `%s'", ctx.argv[0] + 2);
338 } else {
339 error("unknown switch `%c'", *ctx.opt);
340 }
341 usage_with_options(usagestr, options);
4a59fd13
PH
342 }
343
7e7bbcb4 344 return parse_options_end(&ctx);
4a59fd13 345}
d7a38c54
PH
346
347#define USAGE_OPTS_WIDTH 24
348#define USAGE_GAP 2
349
ee68b87a 350int usage_with_options_internal(const char * const *usagestr,
ff43ec3e 351 const struct option *opts, int full)
d7a38c54 352{
f389c808
AR
353 fprintf(stderr, "usage: %s\n", *usagestr++);
354 while (*usagestr && **usagestr)
355 fprintf(stderr, " or: %s\n", *usagestr++);
44d86e91
JK
356 while (*usagestr) {
357 fprintf(stderr, "%s%s\n",
358 **usagestr ? " " : "",
359 *usagestr);
360 usagestr++;
361 }
d7a38c54
PH
362
363 if (opts->type != OPTION_GROUP)
f389c808 364 fputc('\n', stderr);
d7a38c54
PH
365
366 for (; opts->type != OPTION_END; opts++) {
367 size_t pos;
368 int pad;
369
370 if (opts->type == OPTION_GROUP) {
f389c808 371 fputc('\n', stderr);
d7a38c54 372 if (*opts->help)
f389c808 373 fprintf(stderr, "%s\n", opts->help);
d7a38c54
PH
374 continue;
375 }
dd3bf0f4
PH
376 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
377 continue;
d7a38c54 378
f389c808 379 pos = fprintf(stderr, " ");
d7a38c54 380 if (opts->short_name)
f389c808 381 pos += fprintf(stderr, "-%c", opts->short_name);
d7a38c54 382 if (opts->long_name && opts->short_name)
f389c808 383 pos += fprintf(stderr, ", ");
d7a38c54 384 if (opts->long_name)
f389c808 385 pos += fprintf(stderr, "--%s", opts->long_name);
d7a38c54
PH
386
387 switch (opts->type) {
580d5bff
PH
388 case OPTION_ARGUMENT:
389 break;
d7a38c54 390 case OPTION_INTEGER:
ffe659f9 391 if (opts->flags & PARSE_OPT_OPTARG)
6422f633
MB
392 if (opts->long_name)
393 pos += fprintf(stderr, "[=<n>]");
394 else
395 pos += fprintf(stderr, "[<n>]");
ffe659f9
PH
396 else
397 pos += fprintf(stderr, " <n>");
d7a38c54 398 break;
ffe659f9 399 case OPTION_CALLBACK:
f481e22a
PH
400 if (opts->flags & PARSE_OPT_NOARG)
401 break;
402 /* FALLTHROUGH */
403 case OPTION_STRING:
ffe659f9
PH
404 if (opts->argh) {
405 if (opts->flags & PARSE_OPT_OPTARG)
6422f633
MB
406 if (opts->long_name)
407 pos += fprintf(stderr, "[=<%s>]", opts->argh);
408 else
409 pos += fprintf(stderr, "[<%s>]", opts->argh);
ffe659f9
PH
410 else
411 pos += fprintf(stderr, " <%s>", opts->argh);
412 } else {
413 if (opts->flags & PARSE_OPT_OPTARG)
6422f633
MB
414 if (opts->long_name)
415 pos += fprintf(stderr, "[=...]");
416 else
417 pos += fprintf(stderr, "[...]");
ffe659f9
PH
418 else
419 pos += fprintf(stderr, " ...");
420 }
d7a38c54 421 break;
db7244bd 422 default: /* OPTION_{BIT,BOOLEAN,SET_INT,SET_PTR} */
d7a38c54
PH
423 break;
424 }
425
f389c808
AR
426 if (pos <= USAGE_OPTS_WIDTH)
427 pad = USAGE_OPTS_WIDTH - pos;
d7a38c54 428 else {
f389c808 429 fputc('\n', stderr);
d7a38c54
PH
430 pad = USAGE_OPTS_WIDTH;
431 }
f389c808 432 fprintf(stderr, "%*s%s\n", pad + USAGE_GAP, "", opts->help);
d7a38c54 433 }
f389c808
AR
434 fputc('\n', stderr);
435
ee68b87a 436 return PARSE_OPT_HELP;
d7a38c54 437}
0ce865b1 438
dd3bf0f4 439void usage_with_options(const char * const *usagestr,
ff43ec3e 440 const struct option *opts)
dd3bf0f4 441{
ff43ec3e
PH
442 usage_with_options_internal(usagestr, opts, 0);
443 exit(129);
dd3bf0f4
PH
444}
445
ee68b87a
PH
446int parse_options_usage(const char * const *usagestr,
447 const struct option *opts)
448{
ff43ec3e 449 return usage_with_options_internal(usagestr, opts, 0);
ee68b87a
PH
450}
451
452
0ce865b1
PH
453/*----- some often used options -----*/
454#include "cache.h"
db7244bd 455
0ce865b1
PH
456int parse_opt_abbrev_cb(const struct option *opt, const char *arg, int unset)
457{
458 int v;
459
460 if (!arg) {
461 v = unset ? 0 : DEFAULT_ABBREV;
462 } else {
463 v = strtol(arg, (char **)&arg, 10);
464 if (*arg)
465 return opterror(opt, "expects a numerical value", 0);
466 if (v && v < MINIMUM_ABBREV)
467 v = MINIMUM_ABBREV;
468 else if (v > 40)
469 v = 40;
470 }
471 *(int *)(opt->value) = v;
472 return 0;
473}
1f4a711a
MB
474
475int parse_opt_approxidate_cb(const struct option *opt, const char *arg,
476 int unset)
477{
478 *(unsigned long *)(opt->value) = approxidate(arg);
479 return 0;
480}