1 #include "git-compat-util.h"
2 #include "parse-options.h"
12 int optbug(const struct option *opt, const char *reason)
16 return error("BUG: switch '%c' (--%s) %s",
17 opt->short_name, opt->long_name, reason);
18 return error("BUG: option '%s' %s", opt->long_name, reason);
20 return error("BUG: switch '%c' %s", opt->short_name, reason);
23 static enum parse_opt_result get_arg(struct parse_opt_ctx_t *p,
24 const struct option *opt,
25 int flags, const char **arg)
30 } else if (p->argc == 1 && (opt->flags & PARSE_OPT_LASTARG_DEFAULT)) {
31 *arg = (const char *)opt->defval;
32 } else if (p->argc > 1) {
36 return error(_("%s requires a value"), optname(opt, flags));
40 static void fix_filename(const char *prefix, const char **file)
42 if (!file || !*file || !prefix || is_absolute_path(*file)
43 || !strcmp("-", *file))
45 *file = prefix_filename(prefix, *file);
48 static enum parse_opt_result opt_command_mode_error(
49 const struct option *opt,
50 const struct option *all_opts,
53 const struct option *that;
54 struct strbuf that_name = STRBUF_INIT;
57 * Find the other option that was used to set the variable
58 * already, and report that this is not compatible with it.
60 for (that = all_opts; that->type != OPTION_END; that++) {
62 that->type != OPTION_CMDMODE ||
63 that->value != opt->value ||
64 that->defval != *(int *)opt->value)
68 strbuf_addf(&that_name, "--%s", that->long_name);
70 strbuf_addf(&that_name, "-%c", that->short_name);
71 error(_("%s is incompatible with %s"),
72 optname(opt, flags), that_name.buf);
73 strbuf_release(&that_name);
74 return PARSE_OPT_ERROR;
76 return error(_("%s : incompatible with something else"),
80 static enum parse_opt_result get_value(struct parse_opt_ctx_t *p,
81 const struct option *opt,
82 const struct option *all_opts,
86 const int unset = flags & OPT_UNSET;
90 return error(_("%s takes no value"), optname(opt, flags));
91 if (unset && (opt->flags & PARSE_OPT_NONEG))
92 return error(_("%s isn't available"), optname(opt, flags));
93 if (!(flags & OPT_SHORT) && p->opt && (opt->flags & PARSE_OPT_NOARG))
94 return error(_("%s takes no value"), optname(opt, flags));
97 case OPTION_LOWLEVEL_CALLBACK:
98 return opt->ll_callback(p, opt, NULL, unset);
102 *(int *)opt->value &= ~opt->defval;
104 *(int *)opt->value |= opt->defval;
109 *(int *)opt->value |= opt->defval;
111 *(int *)opt->value &= ~opt->defval;
116 BUG("BITOP can't have unset form");
117 *(int *)opt->value &= ~opt->extra;
118 *(int *)opt->value |= opt->defval;
122 if (*(int *)opt->value < 0)
123 *(int *)opt->value = 0;
124 *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
128 *(int *)opt->value = unset ? 0 : opt->defval;
133 * Giving the same mode option twice, although is unnecessary,
134 * is not a grave error, so let it pass.
136 if (*(int *)opt->value && *(int *)opt->value != opt->defval)
137 return opt_command_mode_error(opt, all_opts, flags);
138 *(int *)opt->value = opt->defval;
143 *(const char **)opt->value = NULL;
144 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
145 *(const char **)opt->value = (const char *)opt->defval;
147 return get_arg(p, opt, flags, (const char **)opt->value);
150 case OPTION_FILENAME:
153 *(const char **)opt->value = NULL;
154 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
155 *(const char **)opt->value = (const char *)opt->defval;
157 err = get_arg(p, opt, flags, (const char **)opt->value);
160 fix_filename(p->prefix, (const char **)opt->value);
163 case OPTION_CALLBACK:
165 const char *p_arg = NULL;
170 else if (opt->flags & PARSE_OPT_NOARG)
172 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
174 else if (get_arg(p, opt, flags, &arg))
181 return (*opt->callback)(opt, p_arg, p_unset) ? (-1) : 0;
183 return (*opt->ll_callback)(p, opt, p_arg, p_unset);
187 *(int *)opt->value = 0;
190 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
191 *(int *)opt->value = opt->defval;
194 if (get_arg(p, opt, flags, &arg))
197 return error(_("%s expects a numerical value"),
198 optname(opt, flags));
199 *(int *)opt->value = strtol(arg, (char **)&s, 10);
201 return error(_("%s expects a numerical value"),
202 optname(opt, flags));
205 case OPTION_MAGNITUDE:
207 *(unsigned long *)opt->value = 0;
210 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
211 *(unsigned long *)opt->value = opt->defval;
214 if (get_arg(p, opt, flags, &arg))
216 if (!git_parse_ulong(arg, opt->value))
217 return error(_("%s expects a non-negative integer value"
218 " with an optional k/m/g suffix"),
219 optname(opt, flags));
223 BUG("opt->type %d should not happen", opt->type);
227 static enum parse_opt_result parse_short_opt(struct parse_opt_ctx_t *p,
228 const struct option *options)
230 const struct option *all_opts = options;
231 const struct option *numopt = NULL;
233 for (; options->type != OPTION_END; options++) {
234 if (options->short_name == *p->opt) {
235 p->opt = p->opt[1] ? p->opt + 1 : NULL;
236 return get_value(p, options, all_opts, OPT_SHORT);
240 * Handle the numerical option later, explicit one-digit
241 * options take precedence over it.
243 if (options->type == OPTION_NUMBER)
246 if (numopt && isdigit(*p->opt)) {
251 while (isdigit(p->opt[len]))
253 arg = xmemdupz(p->opt, len);
254 p->opt = p->opt[len] ? p->opt + len : NULL;
255 if (numopt->callback)
256 rc = (*numopt->callback)(numopt, arg, 0) ? (-1) : 0;
258 rc = (*numopt->ll_callback)(p, numopt, arg, 0);
262 return PARSE_OPT_UNKNOWN;
265 static enum parse_opt_result parse_long_opt(
266 struct parse_opt_ctx_t *p, const char *arg,
267 const struct option *options)
269 const struct option *all_opts = options;
270 const char *arg_end = strchrnul(arg, '=');
271 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
272 int abbrev_flags = 0, ambiguous_flags = 0;
274 for (; options->type != OPTION_END; options++) {
275 const char *rest, *long_name = options->long_name;
276 int flags = 0, opt_flags = 0;
282 if (!skip_prefix(arg, long_name, &rest))
284 if (options->type == OPTION_ARGUMENT) {
288 return error(_("%s takes no value"),
289 optname(options, flags));
292 p->out[p->cpidx++] = arg - 2;
293 return PARSE_OPT_DONE;
297 if (!(p->flags & PARSE_OPT_KEEP_UNKNOWN) &&
298 !strncmp(long_name, arg, arg_end - arg)) {
302 * If this is abbreviated, it is
303 * ambiguous. So when there is no
304 * exact match later, we need to
307 ambiguous_option = abbrev_option;
308 ambiguous_flags = abbrev_flags;
310 if (!(flags & OPT_UNSET) && *arg_end)
311 p->opt = arg_end + 1;
312 abbrev_option = options;
313 abbrev_flags = flags ^ opt_flags;
316 /* negation allowed? */
317 if (options->flags & PARSE_OPT_NONEG)
319 /* negated and abbreviated very much? */
320 if (starts_with("no-", arg)) {
325 if (!starts_with(arg, "no-")) {
326 if (starts_with(long_name, "no-")) {
328 opt_flags |= OPT_UNSET;
334 if (!skip_prefix(arg + 3, long_name, &rest)) {
335 /* abbreviated and negated? */
336 if (starts_with(long_name, arg + 3))
347 return get_value(p, options, all_opts, flags ^ opt_flags);
350 if (ambiguous_option) {
351 error(_("ambiguous option: %s "
352 "(could be --%s%s or --%s%s)"),
354 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
355 ambiguous_option->long_name,
356 (abbrev_flags & OPT_UNSET) ? "no-" : "",
357 abbrev_option->long_name);
358 return PARSE_OPT_HELP;
361 return get_value(p, abbrev_option, all_opts, abbrev_flags);
362 return PARSE_OPT_UNKNOWN;
365 static int parse_nodash_opt(struct parse_opt_ctx_t *p, const char *arg,
366 const struct option *options)
368 const struct option *all_opts = options;
370 for (; options->type != OPTION_END; options++) {
371 if (!(options->flags & PARSE_OPT_NODASH))
373 if (options->short_name == arg[0] && arg[1] == '\0')
374 return get_value(p, options, all_opts, OPT_SHORT);
379 static void check_typos(const char *arg, const struct option *options)
384 if (starts_with(arg, "no-")) {
385 error(_("did you mean `--%s` (with two dashes ?)"), arg);
389 for (; options->type != OPTION_END; options++) {
390 if (!options->long_name)
392 if (starts_with(options->long_name, arg)) {
393 error(_("did you mean `--%s` (with two dashes ?)"), arg);
399 static void parse_options_check(const struct option *opts)
402 char short_opts[128];
404 memset(short_opts, '\0', sizeof(short_opts));
405 for (; opts->type != OPTION_END; opts++) {
406 if ((opts->flags & PARSE_OPT_LASTARG_DEFAULT) &&
407 (opts->flags & PARSE_OPT_OPTARG))
408 err |= optbug(opts, "uses incompatible flags "
409 "LASTARG_DEFAULT and OPTARG");
410 if (opts->short_name) {
411 if (0x7F <= opts->short_name)
412 err |= optbug(opts, "invalid short name");
413 else if (short_opts[opts->short_name]++)
414 err |= optbug(opts, "short name already used");
416 if (opts->flags & PARSE_OPT_NODASH &&
417 ((opts->flags & PARSE_OPT_OPTARG) ||
418 !(opts->flags & PARSE_OPT_NOARG) ||
419 !(opts->flags & PARSE_OPT_NONEG) ||
421 err |= optbug(opts, "uses feature "
422 "not supported for dashless options");
423 switch (opts->type) {
429 if ((opts->flags & PARSE_OPT_OPTARG) ||
430 !(opts->flags & PARSE_OPT_NOARG))
431 err |= optbug(opts, "should not accept an argument");
433 case OPTION_CALLBACK:
434 if (!opts->callback && !opts->ll_callback)
435 BUG("OPTION_CALLBACK needs one callback");
436 if (opts->callback && opts->ll_callback)
437 BUG("OPTION_CALLBACK can't have two callbacks");
439 case OPTION_LOWLEVEL_CALLBACK:
440 if (!opts->ll_callback)
441 BUG("OPTION_LOWLEVEL_CALLBACK needs a callback");
443 BUG("OPTION_LOWLEVEL_CALLBACK needs no high level callback");
446 ; /* ok. (usually accepts an argument) */
449 strcspn(opts->argh, " _") != strlen(opts->argh))
450 err |= optbug(opts, "multi-word argh should use dash to separate words");
456 void parse_options_start(struct parse_opt_ctx_t *ctx,
457 int argc, const char **argv, const char *prefix,
458 const struct option *options, int flags)
460 memset(ctx, 0, sizeof(*ctx));
463 if (!(flags & PARSE_OPT_ONE_SHOT)) {
467 ctx->total = ctx->argc;
469 ctx->prefix = prefix;
470 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
472 if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
473 (flags & PARSE_OPT_STOP_AT_NON_OPTION) &&
474 !(flags & PARSE_OPT_ONE_SHOT))
475 BUG("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
476 if ((flags & PARSE_OPT_ONE_SHOT) &&
477 (flags & PARSE_OPT_KEEP_ARGV0))
478 BUG("Can't keep argv0 if you don't have it");
479 parse_options_check(options);
482 static void show_negated_gitcomp(const struct option *opts, int nr_noopts)
484 int printed_dashdash = 0;
486 for (; opts->type != OPTION_END; opts++) {
487 int has_unset_form = 0;
490 if (!opts->long_name)
492 if (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE))
494 if (opts->flags & PARSE_OPT_NONEG)
497 switch (opts->type) {
499 case OPTION_FILENAME:
501 case OPTION_MAGNITUDE:
502 case OPTION_CALLBACK:
515 if (skip_prefix(opts->long_name, "no-", &name)) {
517 printf(" --%s", name);
518 } else if (nr_noopts >= 0) {
519 if (nr_noopts && !printed_dashdash) {
521 printed_dashdash = 1;
523 printf(" --no-%s", opts->long_name);
529 static int show_gitcomp(struct parse_opt_ctx_t *ctx,
530 const struct option *opts)
532 const struct option *original_opts = opts;
535 for (; opts->type != OPTION_END; opts++) {
536 const char *suffix = "";
538 if (!opts->long_name)
540 if (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE))
543 switch (opts->type) {
547 case OPTION_FILENAME:
549 case OPTION_MAGNITUDE:
550 case OPTION_CALLBACK:
551 if (opts->flags & PARSE_OPT_NOARG)
553 if (opts->flags & PARSE_OPT_OPTARG)
555 if (opts->flags & PARSE_OPT_LASTARG_DEFAULT)
562 if (opts->flags & PARSE_OPT_COMP_ARG)
564 if (starts_with(opts->long_name, "no-"))
566 printf(" --%s%s", opts->long_name, suffix);
568 show_negated_gitcomp(original_opts, -1);
569 show_negated_gitcomp(original_opts, nr_noopts);
571 return PARSE_OPT_COMPLETE;
574 static int usage_with_options_internal(struct parse_opt_ctx_t *,
575 const char * const *,
576 const struct option *, int, int);
578 int parse_options_step(struct parse_opt_ctx_t *ctx,
579 const struct option *options,
580 const char * const usagestr[])
582 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
584 /* we must reset ->opt, unknown short option leave it dangling */
587 for (; ctx->argc; ctx->argc--, ctx->argv++) {
588 const char *arg = ctx->argv[0];
590 if (ctx->flags & PARSE_OPT_ONE_SHOT &&
591 ctx->argc != ctx->total)
594 if (*arg != '-' || !arg[1]) {
595 if (parse_nodash_opt(ctx, arg, options) == 0)
597 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
598 return PARSE_OPT_NON_OPTION;
599 ctx->out[ctx->cpidx++] = ctx->argv[0];
603 /* lone -h asks for help */
604 if (internal_help && ctx->total == 1 && !strcmp(arg + 1, "h"))
607 /* lone --git-completion-helper is asked by git-completion.bash */
608 if (ctx->total == 1 && !strcmp(arg + 1, "-git-completion-helper"))
609 return show_gitcomp(ctx, options);
613 switch (parse_short_opt(ctx, options)) {
614 case PARSE_OPT_ERROR:
615 return PARSE_OPT_ERROR;
616 case PARSE_OPT_UNKNOWN:
618 check_typos(arg + 1, options);
619 if (internal_help && *ctx->opt == 'h')
622 case PARSE_OPT_NON_OPTION:
624 case PARSE_OPT_COMPLETE:
625 BUG("parse_short_opt() cannot return these");
630 check_typos(arg + 1, options);
632 switch (parse_short_opt(ctx, options)) {
633 case PARSE_OPT_ERROR:
634 return PARSE_OPT_ERROR;
635 case PARSE_OPT_UNKNOWN:
636 if (internal_help && *ctx->opt == 'h')
639 /* fake a short option thing to hide the fact that we may have
640 * started to parse aggregated stuff
642 * This is leaky, too bad.
644 ctx->argv[0] = xstrdup(ctx->opt - 1);
645 *(char *)ctx->argv[0] = '-';
647 case PARSE_OPT_NON_OPTION:
648 case PARSE_OPT_COMPLETE:
650 BUG("parse_short_opt() cannot return these");
658 if (!arg[2]) { /* "--" */
659 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
666 if (internal_help && !strcmp(arg + 2, "help-all"))
667 return usage_with_options_internal(ctx, usagestr, options, 1, 0);
668 if (internal_help && !strcmp(arg + 2, "help"))
670 switch (parse_long_opt(ctx, arg + 2, options)) {
671 case PARSE_OPT_ERROR:
672 return PARSE_OPT_ERROR;
673 case PARSE_OPT_UNKNOWN:
677 case PARSE_OPT_NON_OPTION:
678 case PARSE_OPT_COMPLETE:
679 BUG("parse_long_opt() cannot return these");
685 if (ctx->flags & PARSE_OPT_ONE_SHOT)
687 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
688 return PARSE_OPT_UNKNOWN;
689 ctx->out[ctx->cpidx++] = ctx->argv[0];
692 return PARSE_OPT_DONE;
695 return usage_with_options_internal(ctx, usagestr, options, 0, 0);
698 int parse_options_end(struct parse_opt_ctx_t *ctx)
700 if (ctx->flags & PARSE_OPT_ONE_SHOT)
701 return ctx->total - ctx->argc;
703 MOVE_ARRAY(ctx->out + ctx->cpidx, ctx->argv, ctx->argc);
704 ctx->out[ctx->cpidx + ctx->argc] = NULL;
705 return ctx->cpidx + ctx->argc;
708 int parse_options(int argc, const char **argv, const char *prefix,
709 const struct option *options, const char * const usagestr[],
712 struct parse_opt_ctx_t ctx;
714 parse_options_start(&ctx, argc, argv, prefix, options, flags);
715 switch (parse_options_step(&ctx, options, usagestr)) {
717 case PARSE_OPT_ERROR:
719 case PARSE_OPT_COMPLETE:
721 case PARSE_OPT_NON_OPTION:
724 default: /* PARSE_OPT_UNKNOWN */
725 if (ctx.argv[0][1] == '-') {
726 error(_("unknown option `%s'"), ctx.argv[0] + 2);
727 } else if (isascii(*ctx.opt)) {
728 error(_("unknown switch `%c'"), *ctx.opt);
730 error(_("unknown non-ascii option in string: `%s'"),
733 usage_with_options(usagestr, options);
736 precompose_argv(argc, argv);
737 return parse_options_end(&ctx);
740 static int usage_argh(const struct option *opts, FILE *outfile)
743 int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
744 !opts->argh || !!strpbrk(opts->argh, "()<>[]|");
745 if (opts->flags & PARSE_OPT_OPTARG)
747 s = literal ? "[=%s]" : "[=<%s>]";
749 s = literal ? "[%s]" : "[<%s>]";
751 s = literal ? " %s" : " <%s>";
752 return utf8_fprintf(outfile, s, opts->argh ? _(opts->argh) : _("..."));
755 #define USAGE_OPTS_WIDTH 24
758 static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
759 const char * const *usagestr,
760 const struct option *opts, int full, int err)
762 FILE *outfile = err ? stderr : stdout;
766 return PARSE_OPT_HELP;
768 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
769 fprintf(outfile, "cat <<\\EOF\n");
771 fprintf_ln(outfile, _("usage: %s"), _(*usagestr++));
772 while (*usagestr && **usagestr)
774 * TRANSLATORS: the colon here should align with the
775 * one in "usage: %s" translation.
777 fprintf_ln(outfile, _(" or: %s"), _(*usagestr++));
780 fprintf_ln(outfile, _(" %s"), _(*usagestr));
782 fputc('\n', outfile);
788 for (; opts->type != OPTION_END; opts++) {
792 if (opts->type == OPTION_GROUP) {
793 fputc('\n', outfile);
796 fprintf(outfile, "%s\n", _(opts->help));
799 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
803 fputc('\n', outfile);
807 pos = fprintf(outfile, " ");
808 if (opts->short_name) {
809 if (opts->flags & PARSE_OPT_NODASH)
810 pos += fprintf(outfile, "%c", opts->short_name);
812 pos += fprintf(outfile, "-%c", opts->short_name);
814 if (opts->long_name && opts->short_name)
815 pos += fprintf(outfile, ", ");
817 pos += fprintf(outfile, "--%s", opts->long_name);
818 if (opts->type == OPTION_NUMBER)
819 pos += utf8_fprintf(outfile, _("-NUM"));
821 if ((opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
822 !(opts->flags & PARSE_OPT_NOARG))
823 pos += usage_argh(opts, outfile);
825 if (pos <= USAGE_OPTS_WIDTH)
826 pad = USAGE_OPTS_WIDTH - pos;
828 fputc('\n', outfile);
829 pad = USAGE_OPTS_WIDTH;
831 fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", _(opts->help));
833 fputc('\n', outfile);
835 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
836 fputs("EOF\n", outfile);
838 return PARSE_OPT_HELP;
841 void NORETURN usage_with_options(const char * const *usagestr,
842 const struct option *opts)
844 usage_with_options_internal(NULL, usagestr, opts, 0, 1);
848 void NORETURN usage_msg_opt(const char *msg,
849 const char * const *usagestr,
850 const struct option *options)
852 fprintf(stderr, "fatal: %s\n\n", msg);
853 usage_with_options(usagestr, options);
856 const char *optname(const struct option *opt, int flags)
858 static struct strbuf sb = STRBUF_INIT;
861 if (flags & OPT_SHORT)
862 strbuf_addf(&sb, "switch `%c'", opt->short_name);
863 else if (flags & OPT_UNSET)
864 strbuf_addf(&sb, "option `no-%s'", opt->long_name);
866 strbuf_addf(&sb, "option `%s'", opt->long_name);