parse-options: detect attempt to add a duplicate short option name
[git/mingw.git] / parse-options.c
blob34a15aa73be1f1b68fcde19b5390ea6d2113283e
1 #include "git-compat-util.h"
2 #include "parse-options.h"
3 #include "cache.h"
4 #include "commit.h"
5 #include "color.h"
6 #include "utf8.h"
8 static int parse_options_usage(struct parse_opt_ctx_t *ctx,
9 const char * const *usagestr,
10 const struct option *opts, int err);
12 #define OPT_SHORT 1
13 #define OPT_UNSET 2
15 int optbug(const struct option *opt, const char *reason)
17 if (opt->long_name) {
18 if (opt->short_name)
19 return error("BUG: switch '%c' (--%s) %s",
20 opt->short_name, opt->long_name, reason);
21 return error("BUG: option '%s' %s", opt->long_name, reason);
23 return error("BUG: switch '%c' %s", opt->short_name, reason);
26 static int get_arg(struct parse_opt_ctx_t *p, const struct option *opt,
27 int flags, const char **arg)
29 if (p->opt) {
30 *arg = p->opt;
31 p->opt = NULL;
32 } else if (p->argc == 1 && (opt->flags & PARSE_OPT_LASTARG_DEFAULT)) {
33 *arg = (const char *)opt->defval;
34 } else if (p->argc > 1) {
35 p->argc--;
36 *arg = *++p->argv;
37 } else
38 return opterror(opt, "requires a value", flags);
39 return 0;
42 static void fix_filename(const char *prefix, const char **file)
44 if (!file || !*file || !prefix || is_absolute_path(*file)
45 || !strcmp("-", *file))
46 return;
47 *file = xstrdup(prefix_filename(prefix, strlen(prefix), *file));
50 static int opt_command_mode_error(const struct option *opt,
51 const struct option *all_opts,
52 int flags)
54 const struct option *that;
55 struct strbuf message = STRBUF_INIT;
56 struct strbuf that_name = STRBUF_INIT;
59 * Find the other option that was used to set the variable
60 * already, and report that this is not compatible with it.
62 for (that = all_opts; that->type != OPTION_END; that++) {
63 if (that == opt ||
64 that->type != OPTION_CMDMODE ||
65 that->value != opt->value ||
66 that->defval != *(int *)opt->value)
67 continue;
69 if (that->long_name)
70 strbuf_addf(&that_name, "--%s", that->long_name);
71 else
72 strbuf_addf(&that_name, "-%c", that->short_name);
73 strbuf_addf(&message, ": incompatible with %s", that_name.buf);
74 strbuf_release(&that_name);
75 opterror(opt, message.buf, flags);
76 strbuf_release(&message);
77 return -1;
79 return opterror(opt, ": incompatible with something else", flags);
82 static int get_value(struct parse_opt_ctx_t *p,
83 const struct option *opt,
84 const struct option *all_opts,
85 int flags)
87 const char *s, *arg;
88 const int unset = flags & OPT_UNSET;
89 int err;
91 if (unset && p->opt)
92 return opterror(opt, "takes no value", flags);
93 if (unset && (opt->flags & PARSE_OPT_NONEG))
94 return opterror(opt, "isn't available", flags);
95 if (!(flags & OPT_SHORT) && p->opt && (opt->flags & PARSE_OPT_NOARG))
96 return opterror(opt, "takes no value", flags);
98 switch (opt->type) {
99 case OPTION_LOWLEVEL_CALLBACK:
100 return (*(parse_opt_ll_cb *)opt->callback)(p, opt, unset);
102 case OPTION_BIT:
103 if (unset)
104 *(int *)opt->value &= ~opt->defval;
105 else
106 *(int *)opt->value |= opt->defval;
107 return 0;
109 case OPTION_NEGBIT:
110 if (unset)
111 *(int *)opt->value |= opt->defval;
112 else
113 *(int *)opt->value &= ~opt->defval;
114 return 0;
116 case OPTION_COUNTUP:
117 *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
118 return 0;
120 case OPTION_SET_INT:
121 *(int *)opt->value = unset ? 0 : opt->defval;
122 return 0;
124 case OPTION_CMDMODE:
126 * Giving the same mode option twice, although is unnecessary,
127 * is not a grave error, so let it pass.
129 if (*(int *)opt->value && *(int *)opt->value != opt->defval)
130 return opt_command_mode_error(opt, all_opts, flags);
131 *(int *)opt->value = opt->defval;
132 return 0;
134 case OPTION_STRING:
135 if (unset)
136 *(const char **)opt->value = NULL;
137 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
138 *(const char **)opt->value = (const char *)opt->defval;
139 else
140 return get_arg(p, opt, flags, (const char **)opt->value);
141 return 0;
143 case OPTION_FILENAME:
144 err = 0;
145 if (unset)
146 *(const char **)opt->value = NULL;
147 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
148 *(const char **)opt->value = (const char *)opt->defval;
149 else
150 err = get_arg(p, opt, flags, (const char **)opt->value);
152 if (!err)
153 fix_filename(p->prefix, (const char **)opt->value);
154 return err;
156 case OPTION_CALLBACK:
157 if (unset)
158 return (*opt->callback)(opt, NULL, 1) ? (-1) : 0;
159 if (opt->flags & PARSE_OPT_NOARG)
160 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
161 if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
162 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
163 if (get_arg(p, opt, flags, &arg))
164 return -1;
165 return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
167 case OPTION_INTEGER:
168 if (unset) {
169 *(int *)opt->value = 0;
170 return 0;
172 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
173 *(int *)opt->value = opt->defval;
174 return 0;
176 if (get_arg(p, opt, flags, &arg))
177 return -1;
178 *(int *)opt->value = strtol(arg, (char **)&s, 10);
179 if (*s)
180 return opterror(opt, "expects a numerical value", flags);
181 return 0;
183 default:
184 die("should not happen, someone must be hit on the forehead");
188 static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)
190 const struct option *all_opts = options;
191 const struct option *numopt = NULL;
193 for (; options->type != OPTION_END; options++) {
194 if (options->short_name == *p->opt) {
195 p->opt = p->opt[1] ? p->opt + 1 : NULL;
196 return get_value(p, options, all_opts, OPT_SHORT);
200 * Handle the numerical option later, explicit one-digit
201 * options take precedence over it.
203 if (options->type == OPTION_NUMBER)
204 numopt = options;
206 if (numopt && isdigit(*p->opt)) {
207 size_t len = 1;
208 char *arg;
209 int rc;
211 while (isdigit(p->opt[len]))
212 len++;
213 arg = xmemdupz(p->opt, len);
214 p->opt = p->opt[len] ? p->opt + len : NULL;
215 rc = (*numopt->callback)(numopt, arg, 0) ? (-1) : 0;
216 free(arg);
217 return rc;
219 return -2;
222 static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
223 const struct option *options)
225 const struct option *all_opts = options;
226 const char *arg_end = strchrnul(arg, '=');
227 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
228 int abbrev_flags = 0, ambiguous_flags = 0;
230 for (; options->type != OPTION_END; options++) {
231 const char *rest, *long_name = options->long_name;
232 int flags = 0, opt_flags = 0;
234 if (!long_name)
235 continue;
237 again:
238 rest = skip_prefix(arg, long_name);
239 if (options->type == OPTION_ARGUMENT) {
240 if (!rest)
241 continue;
242 if (*rest == '=')
243 return opterror(options, "takes no value", flags);
244 if (*rest)
245 continue;
246 p->out[p->cpidx++] = arg - 2;
247 return 0;
249 if (!rest) {
250 /* abbreviated? */
251 if (!strncmp(long_name, arg, arg_end - arg)) {
252 is_abbreviated:
253 if (abbrev_option) {
255 * If this is abbreviated, it is
256 * ambiguous. So when there is no
257 * exact match later, we need to
258 * error out.
260 ambiguous_option = abbrev_option;
261 ambiguous_flags = abbrev_flags;
263 if (!(flags & OPT_UNSET) && *arg_end)
264 p->opt = arg_end + 1;
265 abbrev_option = options;
266 abbrev_flags = flags ^ opt_flags;
267 continue;
269 /* negation allowed? */
270 if (options->flags & PARSE_OPT_NONEG)
271 continue;
272 /* negated and abbreviated very much? */
273 if (starts_with("no-", arg)) {
274 flags |= OPT_UNSET;
275 goto is_abbreviated;
277 /* negated? */
278 if (!starts_with(arg, "no-")) {
279 if (starts_with(long_name, "no-")) {
280 long_name += 3;
281 opt_flags |= OPT_UNSET;
282 goto again;
284 continue;
286 flags |= OPT_UNSET;
287 rest = skip_prefix(arg + 3, long_name);
288 /* abbreviated and negated? */
289 if (!rest && starts_with(long_name, arg + 3))
290 goto is_abbreviated;
291 if (!rest)
292 continue;
294 if (*rest) {
295 if (*rest != '=')
296 continue;
297 p->opt = rest + 1;
299 return get_value(p, options, all_opts, flags ^ opt_flags);
302 if (ambiguous_option)
303 return error("Ambiguous option: %s "
304 "(could be --%s%s or --%s%s)",
305 arg,
306 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
307 ambiguous_option->long_name,
308 (abbrev_flags & OPT_UNSET) ? "no-" : "",
309 abbrev_option->long_name);
310 if (abbrev_option)
311 return get_value(p, abbrev_option, all_opts, abbrev_flags);
312 return -2;
315 static int parse_nodash_opt(struct parse_opt_ctx_t *p, const char *arg,
316 const struct option *options)
318 const struct option *all_opts = options;
320 for (; options->type != OPTION_END; options++) {
321 if (!(options->flags & PARSE_OPT_NODASH))
322 continue;
323 if (options->short_name == arg[0] && arg[1] == '\0')
324 return get_value(p, options, all_opts, OPT_SHORT);
326 return -2;
329 static void check_typos(const char *arg, const struct option *options)
331 if (strlen(arg) < 3)
332 return;
334 if (starts_with(arg, "no-")) {
335 error ("did you mean `--%s` (with two dashes ?)", arg);
336 exit(129);
339 for (; options->type != OPTION_END; options++) {
340 if (!options->long_name)
341 continue;
342 if (starts_with(options->long_name, arg)) {
343 error ("did you mean `--%s` (with two dashes ?)", arg);
344 exit(129);
349 static void parse_options_check(const struct option *opts)
351 int err = 0;
352 char short_opts[128];
354 memset(short_opts, '\0', sizeof(short_opts));
355 for (; opts->type != OPTION_END; opts++) {
356 if ((opts->flags & PARSE_OPT_LASTARG_DEFAULT) &&
357 (opts->flags & PARSE_OPT_OPTARG))
358 err |= optbug(opts, "uses incompatible flags "
359 "LASTARG_DEFAULT and OPTARG");
360 if (opts->short_name) {
361 if (0x7F <= opts->short_name)
362 err |= optbug(opts, "invalid short name");
363 else if (short_opts[opts->short_name]++)
364 err |= optbug(opts, "short name already used");
366 if (opts->flags & PARSE_OPT_NODASH &&
367 ((opts->flags & PARSE_OPT_OPTARG) ||
368 !(opts->flags & PARSE_OPT_NOARG) ||
369 !(opts->flags & PARSE_OPT_NONEG) ||
370 opts->long_name))
371 err |= optbug(opts, "uses feature "
372 "not supported for dashless options");
373 switch (opts->type) {
374 case OPTION_COUNTUP:
375 case OPTION_BIT:
376 case OPTION_NEGBIT:
377 case OPTION_SET_INT:
378 case OPTION_NUMBER:
379 if ((opts->flags & PARSE_OPT_OPTARG) ||
380 !(opts->flags & PARSE_OPT_NOARG))
381 err |= optbug(opts, "should not accept an argument");
382 default:
383 ; /* ok. (usually accepts an argument) */
385 if (opts->argh &&
386 strcspn(opts->argh, " _") != strlen(opts->argh))
387 err |= optbug(opts, "multi-word argh should use dash to separate words");
389 if (err)
390 exit(128);
393 void parse_options_start(struct parse_opt_ctx_t *ctx,
394 int argc, const char **argv, const char *prefix,
395 const struct option *options, int flags)
397 memset(ctx, 0, sizeof(*ctx));
398 ctx->argc = argc - 1;
399 ctx->argv = argv + 1;
400 ctx->out = argv;
401 ctx->prefix = prefix;
402 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
403 ctx->flags = flags;
404 if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
405 (flags & PARSE_OPT_STOP_AT_NON_OPTION))
406 die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
407 parse_options_check(options);
410 static int usage_with_options_internal(struct parse_opt_ctx_t *,
411 const char * const *,
412 const struct option *, int, int);
414 int parse_options_step(struct parse_opt_ctx_t *ctx,
415 const struct option *options,
416 const char * const usagestr[])
418 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
420 /* we must reset ->opt, unknown short option leave it dangling */
421 ctx->opt = NULL;
423 for (; ctx->argc; ctx->argc--, ctx->argv++) {
424 const char *arg = ctx->argv[0];
426 if (*arg != '-' || !arg[1]) {
427 if (parse_nodash_opt(ctx, arg, options) == 0)
428 continue;
429 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
430 return PARSE_OPT_NON_OPTION;
431 ctx->out[ctx->cpidx++] = ctx->argv[0];
432 continue;
435 if (arg[1] != '-') {
436 ctx->opt = arg + 1;
437 if (internal_help && *ctx->opt == 'h')
438 return parse_options_usage(ctx, usagestr, options, 0);
439 switch (parse_short_opt(ctx, options)) {
440 case -1:
441 return parse_options_usage(ctx, usagestr, options, 1);
442 case -2:
443 if (ctx->opt)
444 check_typos(arg + 1, options);
445 goto unknown;
447 if (ctx->opt)
448 check_typos(arg + 1, options);
449 while (ctx->opt) {
450 if (internal_help && *ctx->opt == 'h')
451 return parse_options_usage(ctx, usagestr, options, 0);
452 switch (parse_short_opt(ctx, options)) {
453 case -1:
454 return parse_options_usage(ctx, usagestr, options, 1);
455 case -2:
456 /* fake a short option thing to hide the fact that we may have
457 * started to parse aggregated stuff
459 * This is leaky, too bad.
461 ctx->argv[0] = xstrdup(ctx->opt - 1);
462 *(char *)ctx->argv[0] = '-';
463 goto unknown;
466 continue;
469 if (!arg[2]) { /* "--" */
470 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
471 ctx->argc--;
472 ctx->argv++;
474 break;
477 if (internal_help && !strcmp(arg + 2, "help-all"))
478 return usage_with_options_internal(ctx, usagestr, options, 1, 0);
479 if (internal_help && !strcmp(arg + 2, "help"))
480 return parse_options_usage(ctx, usagestr, options, 0);
481 switch (parse_long_opt(ctx, arg + 2, options)) {
482 case -1:
483 return parse_options_usage(ctx, usagestr, options, 1);
484 case -2:
485 goto unknown;
487 continue;
488 unknown:
489 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
490 return PARSE_OPT_UNKNOWN;
491 ctx->out[ctx->cpidx++] = ctx->argv[0];
492 ctx->opt = NULL;
494 return PARSE_OPT_DONE;
497 int parse_options_end(struct parse_opt_ctx_t *ctx)
499 memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out));
500 ctx->out[ctx->cpidx + ctx->argc] = NULL;
501 return ctx->cpidx + ctx->argc;
504 int parse_options(int argc, const char **argv, const char *prefix,
505 const struct option *options, const char * const usagestr[],
506 int flags)
508 struct parse_opt_ctx_t ctx;
510 parse_options_start(&ctx, argc, argv, prefix, options, flags);
511 switch (parse_options_step(&ctx, options, usagestr)) {
512 case PARSE_OPT_HELP:
513 exit(129);
514 case PARSE_OPT_NON_OPTION:
515 case PARSE_OPT_DONE:
516 break;
517 default: /* PARSE_OPT_UNKNOWN */
518 if (ctx.argv[0][1] == '-') {
519 error("unknown option `%s'", ctx.argv[0] + 2);
520 } else if (isascii(*ctx.opt)) {
521 error("unknown switch `%c'", *ctx.opt);
522 } else {
523 error("unknown non-ascii option in string: `%s'",
524 ctx.argv[0]);
526 usage_with_options(usagestr, options);
529 precompose_argv(argc, argv);
530 return parse_options_end(&ctx);
533 static int usage_argh(const struct option *opts, FILE *outfile)
535 const char *s;
536 int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) || !opts->argh;
537 if (opts->flags & PARSE_OPT_OPTARG)
538 if (opts->long_name)
539 s = literal ? "[=%s]" : "[=<%s>]";
540 else
541 s = literal ? "[%s]" : "[<%s>]";
542 else
543 s = literal ? " %s" : " <%s>";
544 return utf8_fprintf(outfile, s, opts->argh ? _(opts->argh) : _("..."));
547 #define USAGE_OPTS_WIDTH 24
548 #define USAGE_GAP 2
550 static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
551 const char * const *usagestr,
552 const struct option *opts, int full, int err)
554 FILE *outfile = err ? stderr : stdout;
556 if (!usagestr)
557 return PARSE_OPT_HELP;
559 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
560 fprintf(outfile, "cat <<\\EOF\n");
562 fprintf_ln(outfile, _("usage: %s"), _(*usagestr++));
563 while (*usagestr && **usagestr)
564 /* TRANSLATORS: the colon here should align with the
565 one in "usage: %s" translation */
566 fprintf_ln(outfile, _(" or: %s"), _(*usagestr++));
567 while (*usagestr) {
568 if (**usagestr)
569 fprintf_ln(outfile, _(" %s"), _(*usagestr));
570 else
571 putchar('\n');
572 usagestr++;
575 if (opts->type != OPTION_GROUP)
576 fputc('\n', outfile);
578 for (; opts->type != OPTION_END; opts++) {
579 size_t pos;
580 int pad;
582 if (opts->type == OPTION_GROUP) {
583 fputc('\n', outfile);
584 if (*opts->help)
585 fprintf(outfile, "%s\n", _(opts->help));
586 continue;
588 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
589 continue;
591 pos = fprintf(outfile, " ");
592 if (opts->short_name) {
593 if (opts->flags & PARSE_OPT_NODASH)
594 pos += fprintf(outfile, "%c", opts->short_name);
595 else
596 pos += fprintf(outfile, "-%c", opts->short_name);
598 if (opts->long_name && opts->short_name)
599 pos += fprintf(outfile, ", ");
600 if (opts->long_name)
601 pos += fprintf(outfile, "--%s", opts->long_name);
602 if (opts->type == OPTION_NUMBER)
603 pos += utf8_fprintf(outfile, _("-NUM"));
605 if ((opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
606 !(opts->flags & PARSE_OPT_NOARG))
607 pos += usage_argh(opts, outfile);
609 if (pos <= USAGE_OPTS_WIDTH)
610 pad = USAGE_OPTS_WIDTH - pos;
611 else {
612 fputc('\n', outfile);
613 pad = USAGE_OPTS_WIDTH;
615 fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", _(opts->help));
617 fputc('\n', outfile);
619 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
620 fputs("EOF\n", outfile);
622 return PARSE_OPT_HELP;
625 void NORETURN usage_with_options(const char * const *usagestr,
626 const struct option *opts)
628 usage_with_options_internal(NULL, usagestr, opts, 0, 1);
629 exit(129);
632 void NORETURN usage_msg_opt(const char *msg,
633 const char * const *usagestr,
634 const struct option *options)
636 fprintf(stderr, "%s\n\n", msg);
637 usage_with_options(usagestr, options);
640 static int parse_options_usage(struct parse_opt_ctx_t *ctx,
641 const char * const *usagestr,
642 const struct option *opts, int err)
644 return usage_with_options_internal(ctx, usagestr, opts, 0, err);
647 #undef opterror
648 int opterror(const struct option *opt, const char *reason, int flags)
650 if (flags & OPT_SHORT)
651 return error("switch `%c' %s", opt->short_name, reason);
652 if (flags & OPT_UNSET)
653 return error("option `no-%s' %s", opt->long_name, reason);
654 return error("option `%s' %s", opt->long_name, reason);