1 #include "git-compat-util.h"
2 #include "parse-options.h"
9 static int disallow_abbreviated_options;
14 int optbug(const struct option *opt, const char *reason)
18 return error("BUG: switch '%c' (--%s) %s",
19 opt->short_name, opt->long_name, reason);
20 return error("BUG: option '%s' %s", opt->long_name, reason);
22 return error("BUG: switch '%c' %s", opt->short_name, reason);
25 static enum parse_opt_result get_arg(struct parse_opt_ctx_t *p,
26 const struct option *opt,
27 int flags, const char **arg)
32 } else if (p->argc == 1 && (opt->flags & PARSE_OPT_LASTARG_DEFAULT)) {
33 *arg = (const char *)opt->defval;
34 } else if (p->argc > 1) {
38 return error(_("%s requires a value"), optname(opt, flags));
42 static void fix_filename(const char *prefix, const char **file)
44 if (!file || !*file || !prefix || is_absolute_path(*file)
45 || !strcmp("-", *file))
47 *file = prefix_filename(prefix, *file);
50 static enum parse_opt_result opt_command_mode_error(
51 const struct option *opt,
52 const struct option *all_opts,
55 const struct option *that;
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++) {
64 that->type != OPTION_CMDMODE ||
65 that->value != opt->value ||
66 that->defval != *(int *)opt->value)
70 strbuf_addf(&that_name, "--%s", that->long_name);
72 strbuf_addf(&that_name, "-%c", that->short_name);
73 error(_("%s is incompatible with %s"),
74 optname(opt, flags), that_name.buf);
75 strbuf_release(&that_name);
76 return PARSE_OPT_ERROR;
78 return error(_("%s : incompatible with something else"),
82 static enum parse_opt_result get_value(struct parse_opt_ctx_t *p,
83 const struct option *opt,
84 const struct option *all_opts,
88 const int unset = flags & OPT_UNSET;
92 return error(_("%s takes no value"), optname(opt, flags));
93 if (unset && (opt->flags & PARSE_OPT_NONEG))
94 return error(_("%s isn't available"), optname(opt, flags));
95 if (!(flags & OPT_SHORT) && p->opt && (opt->flags & PARSE_OPT_NOARG))
96 return error(_("%s takes no value"), optname(opt, flags));
99 case OPTION_LOWLEVEL_CALLBACK:
100 return opt->ll_callback(p, opt, NULL, unset);
104 *(int *)opt->value &= ~opt->defval;
106 *(int *)opt->value |= opt->defval;
111 *(int *)opt->value |= opt->defval;
113 *(int *)opt->value &= ~opt->defval;
118 BUG("BITOP can't have unset form");
119 *(int *)opt->value &= ~opt->extra;
120 *(int *)opt->value |= opt->defval;
124 if (*(int *)opt->value < 0)
125 *(int *)opt->value = 0;
126 *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
130 *(int *)opt->value = unset ? 0 : opt->defval;
135 * Giving the same mode option twice, although is unnecessary,
136 * is not a grave error, so let it pass.
138 if (*(int *)opt->value && *(int *)opt->value != opt->defval)
139 return opt_command_mode_error(opt, all_opts, flags);
140 *(int *)opt->value = opt->defval;
145 *(const char **)opt->value = NULL;
146 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
147 *(const char **)opt->value = (const char *)opt->defval;
149 return get_arg(p, opt, flags, (const char **)opt->value);
152 case OPTION_FILENAME:
155 *(const char **)opt->value = NULL;
156 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
157 *(const char **)opt->value = (const char *)opt->defval;
159 err = get_arg(p, opt, flags, (const char **)opt->value);
162 fix_filename(p->prefix, (const char **)opt->value);
165 case OPTION_CALLBACK:
167 const char *p_arg = NULL;
172 else if (opt->flags & PARSE_OPT_NOARG)
174 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
176 else if (get_arg(p, opt, flags, &arg))
183 return (*opt->callback)(opt, p_arg, p_unset) ? (-1) : 0;
185 return (*opt->ll_callback)(p, opt, p_arg, p_unset);
189 *(int *)opt->value = 0;
192 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
193 *(int *)opt->value = opt->defval;
196 if (get_arg(p, opt, flags, &arg))
198 *(int *)opt->value = strtol(arg, (char **)&s, 10);
200 return error(_("%s expects a numerical value"),
201 optname(opt, flags));
204 case OPTION_MAGNITUDE:
206 *(unsigned long *)opt->value = 0;
209 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
210 *(unsigned long *)opt->value = opt->defval;
213 if (get_arg(p, opt, flags, &arg))
215 if (!git_parse_ulong(arg, opt->value))
216 return error(_("%s expects a non-negative integer value"
217 " with an optional k/m/g suffix"),
218 optname(opt, flags));
222 BUG("opt->type %d should not happen", opt->type);
226 static enum parse_opt_result parse_short_opt(struct parse_opt_ctx_t *p,
227 const struct option *options)
229 const struct option *all_opts = options;
230 const struct option *numopt = NULL;
232 for (; options->type != OPTION_END; options++) {
233 if (options->short_name == *p->opt) {
234 p->opt = p->opt[1] ? p->opt + 1 : NULL;
235 return get_value(p, options, all_opts, OPT_SHORT);
239 * Handle the numerical option later, explicit one-digit
240 * options take precedence over it.
242 if (options->type == OPTION_NUMBER)
245 if (numopt && isdigit(*p->opt)) {
250 while (isdigit(p->opt[len]))
252 arg = xmemdupz(p->opt, len);
253 p->opt = p->opt[len] ? p->opt + len : NULL;
254 if (numopt->callback)
255 rc = (*numopt->callback)(numopt, arg, 0) ? (-1) : 0;
257 rc = (*numopt->ll_callback)(p, numopt, arg, 0);
261 return PARSE_OPT_UNKNOWN;
264 static enum parse_opt_result parse_long_opt(
265 struct parse_opt_ctx_t *p, const char *arg,
266 const struct option *options)
268 const struct option *all_opts = options;
269 const char *arg_end = strchrnul(arg, '=');
270 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
271 int abbrev_flags = 0, ambiguous_flags = 0;
273 for (; options->type != OPTION_END; options++) {
274 const char *rest, *long_name = options->long_name;
275 int flags = 0, opt_flags = 0;
281 if (!skip_prefix(arg, long_name, &rest))
283 if (options->type == OPTION_ARGUMENT) {
287 return error(_("%s takes no value"),
288 optname(options, flags));
291 p->out[p->cpidx++] = arg - 2;
292 return PARSE_OPT_DONE;
296 if (!(p->flags & PARSE_OPT_KEEP_UNKNOWN) &&
297 !strncmp(long_name, arg, arg_end - arg)) {
301 * If this is abbreviated, it is
302 * ambiguous. So when there is no
303 * exact match later, we need to
306 ambiguous_option = abbrev_option;
307 ambiguous_flags = abbrev_flags;
309 if (!(flags & OPT_UNSET) && *arg_end)
310 p->opt = arg_end + 1;
311 abbrev_option = options;
312 abbrev_flags = flags ^ opt_flags;
315 /* negation allowed? */
316 if (options->flags & PARSE_OPT_NONEG)
318 /* negated and abbreviated very much? */
319 if (starts_with("no-", arg)) {
324 if (!starts_with(arg, "no-")) {
325 if (starts_with(long_name, "no-")) {
327 opt_flags |= OPT_UNSET;
333 if (!skip_prefix(arg + 3, long_name, &rest)) {
334 /* abbreviated and negated? */
335 if (starts_with(long_name, arg + 3))
346 return get_value(p, options, all_opts, flags ^ opt_flags);
349 if (disallow_abbreviated_options && (ambiguous_option || abbrev_option))
350 die("disallowed abbreviated or ambiguous option '%.*s'",
351 (int)(arg_end - arg), arg);
353 if (ambiguous_option) {
354 error(_("ambiguous option: %s "
355 "(could be --%s%s or --%s%s)"),
357 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
358 ambiguous_option->long_name,
359 (abbrev_flags & OPT_UNSET) ? "no-" : "",
360 abbrev_option->long_name);
361 return PARSE_OPT_HELP;
364 return get_value(p, abbrev_option, all_opts, abbrev_flags);
365 return PARSE_OPT_UNKNOWN;
368 static int parse_nodash_opt(struct parse_opt_ctx_t *p, const char *arg,
369 const struct option *options)
371 const struct option *all_opts = options;
373 for (; options->type != OPTION_END; options++) {
374 if (!(options->flags & PARSE_OPT_NODASH))
376 if (options->short_name == arg[0] && arg[1] == '\0')
377 return get_value(p, options, all_opts, OPT_SHORT);
382 static void check_typos(const char *arg, const struct option *options)
387 if (starts_with(arg, "no-")) {
388 error(_("did you mean `--%s` (with two dashes ?)"), arg);
392 for (; options->type != OPTION_END; options++) {
393 if (!options->long_name)
395 if (starts_with(options->long_name, arg)) {
396 error(_("did you mean `--%s` (with two dashes ?)"), arg);
402 static void parse_options_check(const struct option *opts)
405 char short_opts[128];
407 memset(short_opts, '\0', sizeof(short_opts));
408 for (; opts->type != OPTION_END; opts++) {
409 if ((opts->flags & PARSE_OPT_LASTARG_DEFAULT) &&
410 (opts->flags & PARSE_OPT_OPTARG))
411 err |= optbug(opts, "uses incompatible flags "
412 "LASTARG_DEFAULT and OPTARG");
413 if (opts->short_name) {
414 if (0x7F <= opts->short_name)
415 err |= optbug(opts, "invalid short name");
416 else if (short_opts[opts->short_name]++)
417 err |= optbug(opts, "short name already used");
419 if (opts->flags & PARSE_OPT_NODASH &&
420 ((opts->flags & PARSE_OPT_OPTARG) ||
421 !(opts->flags & PARSE_OPT_NOARG) ||
422 !(opts->flags & PARSE_OPT_NONEG) ||
424 err |= optbug(opts, "uses feature "
425 "not supported for dashless options");
426 switch (opts->type) {
432 if ((opts->flags & PARSE_OPT_OPTARG) ||
433 !(opts->flags & PARSE_OPT_NOARG))
434 err |= optbug(opts, "should not accept an argument");
436 case OPTION_CALLBACK:
437 if (!opts->callback && !opts->ll_callback)
438 BUG("OPTION_CALLBACK needs one callback");
439 if (opts->callback && opts->ll_callback)
440 BUG("OPTION_CALLBACK can't have two callbacks");
442 case OPTION_LOWLEVEL_CALLBACK:
443 if (!opts->ll_callback)
444 BUG("OPTION_LOWLEVEL_CALLBACK needs a callback");
446 BUG("OPTION_LOWLEVEL_CALLBACK needs no high level callback");
449 ; /* ok. (usually accepts an argument) */
452 strcspn(opts->argh, " _") != strlen(opts->argh))
453 err |= optbug(opts, "multi-word argh should use dash to separate words");
459 void parse_options_start(struct parse_opt_ctx_t *ctx,
460 int argc, const char **argv, const char *prefix,
461 const struct option *options, int flags)
463 memset(ctx, 0, sizeof(*ctx));
466 if (!(flags & PARSE_OPT_ONE_SHOT)) {
470 ctx->total = ctx->argc;
472 ctx->prefix = prefix;
473 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
475 if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
476 (flags & PARSE_OPT_STOP_AT_NON_OPTION) &&
477 !(flags & PARSE_OPT_ONE_SHOT))
478 BUG("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
479 if ((flags & PARSE_OPT_ONE_SHOT) &&
480 (flags & PARSE_OPT_KEEP_ARGV0))
481 BUG("Can't keep argv0 if you don't have it");
482 parse_options_check(options);
485 static void show_negated_gitcomp(const struct option *opts, int nr_noopts)
487 int printed_dashdash = 0;
489 for (; opts->type != OPTION_END; opts++) {
490 int has_unset_form = 0;
493 if (!opts->long_name)
495 if (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE))
497 if (opts->flags & PARSE_OPT_NONEG)
500 switch (opts->type) {
502 case OPTION_FILENAME:
504 case OPTION_MAGNITUDE:
505 case OPTION_CALLBACK:
518 if (skip_prefix(opts->long_name, "no-", &name)) {
520 printf(" --%s", name);
521 } else if (nr_noopts >= 0) {
522 if (nr_noopts && !printed_dashdash) {
524 printed_dashdash = 1;
526 printf(" --no-%s", opts->long_name);
532 static int show_gitcomp(struct parse_opt_ctx_t *ctx,
533 const struct option *opts)
535 const struct option *original_opts = opts;
538 for (; opts->type != OPTION_END; opts++) {
539 const char *suffix = "";
541 if (!opts->long_name)
543 if (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE))
546 switch (opts->type) {
550 case OPTION_FILENAME:
552 case OPTION_MAGNITUDE:
553 case OPTION_CALLBACK:
554 if (opts->flags & PARSE_OPT_NOARG)
556 if (opts->flags & PARSE_OPT_OPTARG)
558 if (opts->flags & PARSE_OPT_LASTARG_DEFAULT)
565 if (opts->flags & PARSE_OPT_COMP_ARG)
567 if (starts_with(opts->long_name, "no-"))
569 printf(" --%s%s", opts->long_name, suffix);
571 show_negated_gitcomp(original_opts, -1);
572 show_negated_gitcomp(original_opts, nr_noopts);
574 return PARSE_OPT_COMPLETE;
577 static int usage_with_options_internal(struct parse_opt_ctx_t *,
578 const char * const *,
579 const struct option *, int, int);
581 int parse_options_step(struct parse_opt_ctx_t *ctx,
582 const struct option *options,
583 const char * const usagestr[])
585 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
587 /* we must reset ->opt, unknown short option leave it dangling */
590 for (; ctx->argc; ctx->argc--, ctx->argv++) {
591 const char *arg = ctx->argv[0];
593 if (ctx->flags & PARSE_OPT_ONE_SHOT &&
594 ctx->argc != ctx->total)
597 if (*arg != '-' || !arg[1]) {
598 if (parse_nodash_opt(ctx, arg, options) == 0)
600 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
601 return PARSE_OPT_NON_OPTION;
602 ctx->out[ctx->cpidx++] = ctx->argv[0];
606 /* lone -h asks for help */
607 if (internal_help && ctx->total == 1 && !strcmp(arg + 1, "h"))
610 /* lone --git-completion-helper is asked by git-completion.bash */
611 if (ctx->total == 1 && !strcmp(arg + 1, "-git-completion-helper"))
612 return show_gitcomp(ctx, options);
616 switch (parse_short_opt(ctx, options)) {
617 case PARSE_OPT_ERROR:
618 return PARSE_OPT_ERROR;
619 case PARSE_OPT_UNKNOWN:
621 check_typos(arg + 1, options);
622 if (internal_help && *ctx->opt == 'h')
625 case PARSE_OPT_NON_OPTION:
627 case PARSE_OPT_COMPLETE:
628 BUG("parse_short_opt() cannot return these");
633 check_typos(arg + 1, options);
635 switch (parse_short_opt(ctx, options)) {
636 case PARSE_OPT_ERROR:
637 return PARSE_OPT_ERROR;
638 case PARSE_OPT_UNKNOWN:
639 if (internal_help && *ctx->opt == 'h')
642 /* fake a short option thing to hide the fact that we may have
643 * started to parse aggregated stuff
645 * This is leaky, too bad.
647 ctx->argv[0] = xstrdup(ctx->opt - 1);
648 *(char *)ctx->argv[0] = '-';
650 case PARSE_OPT_NON_OPTION:
651 case PARSE_OPT_COMPLETE:
653 BUG("parse_short_opt() cannot return these");
661 if (!arg[2]) { /* "--" */
662 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
669 if (internal_help && !strcmp(arg + 2, "help-all"))
670 return usage_with_options_internal(ctx, usagestr, options, 1, 0);
671 if (internal_help && !strcmp(arg + 2, "help"))
673 switch (parse_long_opt(ctx, arg + 2, options)) {
674 case PARSE_OPT_ERROR:
675 return PARSE_OPT_ERROR;
676 case PARSE_OPT_UNKNOWN:
680 case PARSE_OPT_NON_OPTION:
681 case PARSE_OPT_COMPLETE:
682 BUG("parse_long_opt() cannot return these");
688 if (ctx->flags & PARSE_OPT_ONE_SHOT)
690 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
691 return PARSE_OPT_UNKNOWN;
692 ctx->out[ctx->cpidx++] = ctx->argv[0];
695 return PARSE_OPT_DONE;
698 return usage_with_options_internal(ctx, usagestr, options, 0, 0);
701 int parse_options_end(struct parse_opt_ctx_t *ctx)
703 if (ctx->flags & PARSE_OPT_ONE_SHOT)
704 return ctx->total - ctx->argc;
706 MOVE_ARRAY(ctx->out + ctx->cpidx, ctx->argv, ctx->argc);
707 ctx->out[ctx->cpidx + ctx->argc] = NULL;
708 return ctx->cpidx + ctx->argc;
711 int parse_options(int argc, const char **argv, const char *prefix,
712 const struct option *options, const char * const usagestr[],
715 struct parse_opt_ctx_t ctx;
717 disallow_abbreviated_options =
718 git_env_bool("GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS", 0);
720 parse_options_start(&ctx, argc, argv, prefix, options, flags);
721 switch (parse_options_step(&ctx, options, usagestr)) {
723 case PARSE_OPT_ERROR:
725 case PARSE_OPT_COMPLETE:
727 case PARSE_OPT_NON_OPTION:
730 default: /* PARSE_OPT_UNKNOWN */
731 if (ctx.argv[0][1] == '-') {
732 error(_("unknown option `%s'"), ctx.argv[0] + 2);
733 } else if (isascii(*ctx.opt)) {
734 error(_("unknown switch `%c'"), *ctx.opt);
736 error(_("unknown non-ascii option in string: `%s'"),
739 usage_with_options(usagestr, options);
742 precompose_argv(argc, argv);
743 return parse_options_end(&ctx);
746 static int usage_argh(const struct option *opts, FILE *outfile)
749 int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
750 !opts->argh || !!strpbrk(opts->argh, "()<>[]|");
751 if (opts->flags & PARSE_OPT_OPTARG)
753 s = literal ? "[=%s]" : "[=<%s>]";
755 s = literal ? "[%s]" : "[<%s>]";
757 s = literal ? " %s" : " <%s>";
758 return utf8_fprintf(outfile, s, opts->argh ? _(opts->argh) : _("..."));
761 #define USAGE_OPTS_WIDTH 24
764 static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
765 const char * const *usagestr,
766 const struct option *opts, int full, int err)
768 FILE *outfile = err ? stderr : stdout;
772 return PARSE_OPT_HELP;
774 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
775 fprintf(outfile, "cat <<\\EOF\n");
777 fprintf_ln(outfile, _("usage: %s"), _(*usagestr++));
778 while (*usagestr && **usagestr)
780 * TRANSLATORS: the colon here should align with the
781 * one in "usage: %s" translation.
783 fprintf_ln(outfile, _(" or: %s"), _(*usagestr++));
786 fprintf_ln(outfile, _(" %s"), _(*usagestr));
788 fputc('\n', outfile);
794 for (; opts->type != OPTION_END; opts++) {
798 if (opts->type == OPTION_GROUP) {
799 fputc('\n', outfile);
802 fprintf(outfile, "%s\n", _(opts->help));
805 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
809 fputc('\n', outfile);
813 pos = fprintf(outfile, " ");
814 if (opts->short_name) {
815 if (opts->flags & PARSE_OPT_NODASH)
816 pos += fprintf(outfile, "%c", opts->short_name);
818 pos += fprintf(outfile, "-%c", opts->short_name);
820 if (opts->long_name && opts->short_name)
821 pos += fprintf(outfile, ", ");
823 pos += fprintf(outfile, "--%s", opts->long_name);
824 if (opts->type == OPTION_NUMBER)
825 pos += utf8_fprintf(outfile, _("-NUM"));
827 if ((opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
828 !(opts->flags & PARSE_OPT_NOARG))
829 pos += usage_argh(opts, outfile);
831 if (pos <= USAGE_OPTS_WIDTH)
832 pad = USAGE_OPTS_WIDTH - pos;
834 fputc('\n', outfile);
835 pad = USAGE_OPTS_WIDTH;
837 fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", _(opts->help));
839 fputc('\n', outfile);
841 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
842 fputs("EOF\n", outfile);
844 return PARSE_OPT_HELP;
847 void NORETURN usage_with_options(const char * const *usagestr,
848 const struct option *opts)
850 usage_with_options_internal(NULL, usagestr, opts, 0, 1);
854 void NORETURN usage_msg_opt(const char *msg,
855 const char * const *usagestr,
856 const struct option *options)
858 fprintf(stderr, "fatal: %s\n\n", msg);
859 usage_with_options(usagestr, options);
862 const char *optname(const struct option *opt, int flags)
864 static struct strbuf sb = STRBUF_INIT;
867 if (flags & OPT_SHORT)
868 strbuf_addf(&sb, "switch `%c'", opt->short_name);
869 else if (flags & OPT_UNSET)
870 strbuf_addf(&sb, "option `no-%s'", opt->long_name);
872 strbuf_addf(&sb, "option `%s'", opt->long_name);