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))
196 *(int *)opt->value = strtol(arg, (char **)&s, 10);
198 return error(_("%s expects a numerical value"),
199 optname(opt, flags));
202 case OPTION_MAGNITUDE:
204 *(unsigned long *)opt->value = 0;
207 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
208 *(unsigned long *)opt->value = opt->defval;
211 if (get_arg(p, opt, flags, &arg))
213 if (!git_parse_ulong(arg, opt->value))
214 return error(_("%s expects a non-negative integer value"
215 " with an optional k/m/g suffix"),
216 optname(opt, flags));
220 BUG("opt->type %d should not happen", opt->type);
224 static enum parse_opt_result parse_short_opt(struct parse_opt_ctx_t *p,
225 const struct option *options)
227 const struct option *all_opts = options;
228 const struct option *numopt = NULL;
230 for (; options->type != OPTION_END; options++) {
231 if (options->short_name == *p->opt) {
232 p->opt = p->opt[1] ? p->opt + 1 : NULL;
233 return get_value(p, options, all_opts, OPT_SHORT);
237 * Handle the numerical option later, explicit one-digit
238 * options take precedence over it.
240 if (options->type == OPTION_NUMBER)
243 if (numopt && isdigit(*p->opt)) {
248 while (isdigit(p->opt[len]))
250 arg = xmemdupz(p->opt, len);
251 p->opt = p->opt[len] ? p->opt + len : NULL;
252 if (numopt->callback)
253 rc = (*numopt->callback)(numopt, arg, 0) ? (-1) : 0;
255 rc = (*numopt->ll_callback)(p, numopt, arg, 0);
259 return PARSE_OPT_UNKNOWN;
262 static enum parse_opt_result parse_long_opt(
263 struct parse_opt_ctx_t *p, const char *arg,
264 const struct option *options)
266 const struct option *all_opts = options;
267 const char *arg_end = strchrnul(arg, '=');
268 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
269 int abbrev_flags = 0, ambiguous_flags = 0;
271 for (; options->type != OPTION_END; options++) {
272 const char *rest, *long_name = options->long_name;
273 int flags = 0, opt_flags = 0;
279 if (!skip_prefix(arg, long_name, &rest))
281 if (options->type == OPTION_ARGUMENT) {
285 return error(_("%s takes no value"),
286 optname(options, flags));
289 p->out[p->cpidx++] = arg - 2;
290 return PARSE_OPT_DONE;
294 if (!(p->flags & PARSE_OPT_KEEP_UNKNOWN) &&
295 !strncmp(long_name, arg, arg_end - arg)) {
299 * If this is abbreviated, it is
300 * ambiguous. So when there is no
301 * exact match later, we need to
304 ambiguous_option = abbrev_option;
305 ambiguous_flags = abbrev_flags;
307 if (!(flags & OPT_UNSET) && *arg_end)
308 p->opt = arg_end + 1;
309 abbrev_option = options;
310 abbrev_flags = flags ^ opt_flags;
313 /* negation allowed? */
314 if (options->flags & PARSE_OPT_NONEG)
316 /* negated and abbreviated very much? */
317 if (starts_with("no-", arg)) {
322 if (!starts_with(arg, "no-")) {
323 if (starts_with(long_name, "no-")) {
325 opt_flags |= OPT_UNSET;
331 if (!skip_prefix(arg + 3, long_name, &rest)) {
332 /* abbreviated and negated? */
333 if (starts_with(long_name, arg + 3))
344 return get_value(p, options, all_opts, flags ^ opt_flags);
347 if (ambiguous_option) {
348 error(_("ambiguous option: %s "
349 "(could be --%s%s or --%s%s)"),
351 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
352 ambiguous_option->long_name,
353 (abbrev_flags & OPT_UNSET) ? "no-" : "",
354 abbrev_option->long_name);
355 return PARSE_OPT_HELP;
358 return get_value(p, abbrev_option, all_opts, abbrev_flags);
359 return PARSE_OPT_UNKNOWN;
362 static int parse_nodash_opt(struct parse_opt_ctx_t *p, const char *arg,
363 const struct option *options)
365 const struct option *all_opts = options;
367 for (; options->type != OPTION_END; options++) {
368 if (!(options->flags & PARSE_OPT_NODASH))
370 if (options->short_name == arg[0] && arg[1] == '\0')
371 return get_value(p, options, all_opts, OPT_SHORT);
376 static void check_typos(const char *arg, const struct option *options)
381 if (starts_with(arg, "no-")) {
382 error(_("did you mean `--%s` (with two dashes ?)"), arg);
386 for (; options->type != OPTION_END; options++) {
387 if (!options->long_name)
389 if (starts_with(options->long_name, arg)) {
390 error(_("did you mean `--%s` (with two dashes ?)"), arg);
396 static void parse_options_check(const struct option *opts)
399 char short_opts[128];
401 memset(short_opts, '\0', sizeof(short_opts));
402 for (; opts->type != OPTION_END; opts++) {
403 if ((opts->flags & PARSE_OPT_LASTARG_DEFAULT) &&
404 (opts->flags & PARSE_OPT_OPTARG))
405 err |= optbug(opts, "uses incompatible flags "
406 "LASTARG_DEFAULT and OPTARG");
407 if (opts->short_name) {
408 if (0x7F <= opts->short_name)
409 err |= optbug(opts, "invalid short name");
410 else if (short_opts[opts->short_name]++)
411 err |= optbug(opts, "short name already used");
413 if (opts->flags & PARSE_OPT_NODASH &&
414 ((opts->flags & PARSE_OPT_OPTARG) ||
415 !(opts->flags & PARSE_OPT_NOARG) ||
416 !(opts->flags & PARSE_OPT_NONEG) ||
418 err |= optbug(opts, "uses feature "
419 "not supported for dashless options");
420 switch (opts->type) {
426 if ((opts->flags & PARSE_OPT_OPTARG) ||
427 !(opts->flags & PARSE_OPT_NOARG))
428 err |= optbug(opts, "should not accept an argument");
430 case OPTION_CALLBACK:
431 if (!opts->callback && !opts->ll_callback)
432 BUG("OPTION_CALLBACK needs one callback");
433 if (opts->callback && opts->ll_callback)
434 BUG("OPTION_CALLBACK can't have two callbacks");
436 case OPTION_LOWLEVEL_CALLBACK:
437 if (!opts->ll_callback)
438 BUG("OPTION_LOWLEVEL_CALLBACK needs a callback");
440 BUG("OPTION_LOWLEVEL_CALLBACK needs no high level callback");
443 ; /* ok. (usually accepts an argument) */
446 strcspn(opts->argh, " _") != strlen(opts->argh))
447 err |= optbug(opts, "multi-word argh should use dash to separate words");
453 void parse_options_start(struct parse_opt_ctx_t *ctx,
454 int argc, const char **argv, const char *prefix,
455 const struct option *options, int flags)
457 memset(ctx, 0, sizeof(*ctx));
460 if (!(flags & PARSE_OPT_ONE_SHOT)) {
464 ctx->total = ctx->argc;
466 ctx->prefix = prefix;
467 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
469 if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
470 (flags & PARSE_OPT_STOP_AT_NON_OPTION) &&
471 !(flags & PARSE_OPT_ONE_SHOT))
472 BUG("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
473 if ((flags & PARSE_OPT_ONE_SHOT) &&
474 (flags & PARSE_OPT_KEEP_ARGV0))
475 BUG("Can't keep argv0 if you don't have it");
476 parse_options_check(options);
479 static void show_negated_gitcomp(const struct option *opts, int nr_noopts)
481 int printed_dashdash = 0;
483 for (; opts->type != OPTION_END; opts++) {
484 int has_unset_form = 0;
487 if (!opts->long_name)
489 if (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE))
491 if (opts->flags & PARSE_OPT_NONEG)
494 switch (opts->type) {
496 case OPTION_FILENAME:
498 case OPTION_MAGNITUDE:
499 case OPTION_CALLBACK:
512 if (skip_prefix(opts->long_name, "no-", &name)) {
514 printf(" --%s", name);
515 } else if (nr_noopts >= 0) {
516 if (nr_noopts && !printed_dashdash) {
518 printed_dashdash = 1;
520 printf(" --no-%s", opts->long_name);
526 static int show_gitcomp(struct parse_opt_ctx_t *ctx,
527 const struct option *opts)
529 const struct option *original_opts = opts;
532 for (; opts->type != OPTION_END; opts++) {
533 const char *suffix = "";
535 if (!opts->long_name)
537 if (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE))
540 switch (opts->type) {
544 case OPTION_FILENAME:
546 case OPTION_MAGNITUDE:
547 case OPTION_CALLBACK:
548 if (opts->flags & PARSE_OPT_NOARG)
550 if (opts->flags & PARSE_OPT_OPTARG)
552 if (opts->flags & PARSE_OPT_LASTARG_DEFAULT)
559 if (opts->flags & PARSE_OPT_COMP_ARG)
561 if (starts_with(opts->long_name, "no-"))
563 printf(" --%s%s", opts->long_name, suffix);
565 show_negated_gitcomp(original_opts, -1);
566 show_negated_gitcomp(original_opts, nr_noopts);
568 return PARSE_OPT_COMPLETE;
571 static int usage_with_options_internal(struct parse_opt_ctx_t *,
572 const char * const *,
573 const struct option *, int, int);
575 int parse_options_step(struct parse_opt_ctx_t *ctx,
576 const struct option *options,
577 const char * const usagestr[])
579 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
581 /* we must reset ->opt, unknown short option leave it dangling */
584 for (; ctx->argc; ctx->argc--, ctx->argv++) {
585 const char *arg = ctx->argv[0];
587 if (ctx->flags & PARSE_OPT_ONE_SHOT &&
588 ctx->argc != ctx->total)
591 if (*arg != '-' || !arg[1]) {
592 if (parse_nodash_opt(ctx, arg, options) == 0)
594 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
595 return PARSE_OPT_NON_OPTION;
596 ctx->out[ctx->cpidx++] = ctx->argv[0];
600 /* lone -h asks for help */
601 if (internal_help && ctx->total == 1 && !strcmp(arg + 1, "h"))
604 /* lone --git-completion-helper is asked by git-completion.bash */
605 if (ctx->total == 1 && !strcmp(arg + 1, "-git-completion-helper"))
606 return show_gitcomp(ctx, options);
610 switch (parse_short_opt(ctx, options)) {
611 case PARSE_OPT_ERROR:
612 return PARSE_OPT_ERROR;
613 case PARSE_OPT_UNKNOWN:
615 check_typos(arg + 1, options);
616 if (internal_help && *ctx->opt == 'h')
619 case PARSE_OPT_NON_OPTION:
621 case PARSE_OPT_COMPLETE:
622 BUG("parse_short_opt() cannot return these");
627 check_typos(arg + 1, options);
629 switch (parse_short_opt(ctx, options)) {
630 case PARSE_OPT_ERROR:
631 return PARSE_OPT_ERROR;
632 case PARSE_OPT_UNKNOWN:
633 if (internal_help && *ctx->opt == 'h')
636 /* fake a short option thing to hide the fact that we may have
637 * started to parse aggregated stuff
639 * This is leaky, too bad.
641 ctx->argv[0] = xstrdup(ctx->opt - 1);
642 *(char *)ctx->argv[0] = '-';
644 case PARSE_OPT_NON_OPTION:
645 case PARSE_OPT_COMPLETE:
647 BUG("parse_short_opt() cannot return these");
655 if (!arg[2]) { /* "--" */
656 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
663 if (internal_help && !strcmp(arg + 2, "help-all"))
664 return usage_with_options_internal(ctx, usagestr, options, 1, 0);
665 if (internal_help && !strcmp(arg + 2, "help"))
667 switch (parse_long_opt(ctx, arg + 2, options)) {
668 case PARSE_OPT_ERROR:
669 return PARSE_OPT_ERROR;
670 case PARSE_OPT_UNKNOWN:
674 case PARSE_OPT_NON_OPTION:
675 case PARSE_OPT_COMPLETE:
676 BUG("parse_long_opt() cannot return these");
682 if (ctx->flags & PARSE_OPT_ONE_SHOT)
684 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
685 return PARSE_OPT_UNKNOWN;
686 ctx->out[ctx->cpidx++] = ctx->argv[0];
689 return PARSE_OPT_DONE;
692 return usage_with_options_internal(ctx, usagestr, options, 0, 0);
695 int parse_options_end(struct parse_opt_ctx_t *ctx)
697 if (ctx->flags & PARSE_OPT_ONE_SHOT)
698 return ctx->total - ctx->argc;
700 MOVE_ARRAY(ctx->out + ctx->cpidx, ctx->argv, ctx->argc);
701 ctx->out[ctx->cpidx + ctx->argc] = NULL;
702 return ctx->cpidx + ctx->argc;
705 int parse_options(int argc, const char **argv, const char *prefix,
706 const struct option *options, const char * const usagestr[],
709 struct parse_opt_ctx_t ctx;
711 parse_options_start(&ctx, argc, argv, prefix, options, flags);
712 switch (parse_options_step(&ctx, options, usagestr)) {
714 case PARSE_OPT_ERROR:
716 case PARSE_OPT_COMPLETE:
718 case PARSE_OPT_NON_OPTION:
721 default: /* PARSE_OPT_UNKNOWN */
722 if (ctx.argv[0][1] == '-') {
723 error(_("unknown option `%s'"), ctx.argv[0] + 2);
724 } else if (isascii(*ctx.opt)) {
725 error(_("unknown switch `%c'"), *ctx.opt);
727 error(_("unknown non-ascii option in string: `%s'"),
730 usage_with_options(usagestr, options);
733 precompose_argv(argc, argv);
734 return parse_options_end(&ctx);
737 static int usage_argh(const struct option *opts, FILE *outfile)
740 int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
741 !opts->argh || !!strpbrk(opts->argh, "()<>[]|");
742 if (opts->flags & PARSE_OPT_OPTARG)
744 s = literal ? "[=%s]" : "[=<%s>]";
746 s = literal ? "[%s]" : "[<%s>]";
748 s = literal ? " %s" : " <%s>";
749 return utf8_fprintf(outfile, s, opts->argh ? _(opts->argh) : _("..."));
752 #define USAGE_OPTS_WIDTH 24
755 static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
756 const char * const *usagestr,
757 const struct option *opts, int full, int err)
759 FILE *outfile = err ? stderr : stdout;
763 return PARSE_OPT_HELP;
765 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
766 fprintf(outfile, "cat <<\\EOF\n");
768 fprintf_ln(outfile, _("usage: %s"), _(*usagestr++));
769 while (*usagestr && **usagestr)
771 * TRANSLATORS: the colon here should align with the
772 * one in "usage: %s" translation.
774 fprintf_ln(outfile, _(" or: %s"), _(*usagestr++));
777 fprintf_ln(outfile, _(" %s"), _(*usagestr));
779 fputc('\n', outfile);
785 for (; opts->type != OPTION_END; opts++) {
789 if (opts->type == OPTION_GROUP) {
790 fputc('\n', outfile);
793 fprintf(outfile, "%s\n", _(opts->help));
796 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
800 fputc('\n', outfile);
804 pos = fprintf(outfile, " ");
805 if (opts->short_name) {
806 if (opts->flags & PARSE_OPT_NODASH)
807 pos += fprintf(outfile, "%c", opts->short_name);
809 pos += fprintf(outfile, "-%c", opts->short_name);
811 if (opts->long_name && opts->short_name)
812 pos += fprintf(outfile, ", ");
814 pos += fprintf(outfile, "--%s", opts->long_name);
815 if (opts->type == OPTION_NUMBER)
816 pos += utf8_fprintf(outfile, _("-NUM"));
818 if ((opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
819 !(opts->flags & PARSE_OPT_NOARG))
820 pos += usage_argh(opts, outfile);
822 if (pos <= USAGE_OPTS_WIDTH)
823 pad = USAGE_OPTS_WIDTH - pos;
825 fputc('\n', outfile);
826 pad = USAGE_OPTS_WIDTH;
828 fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", _(opts->help));
830 fputc('\n', outfile);
832 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
833 fputs("EOF\n", outfile);
835 return PARSE_OPT_HELP;
838 void NORETURN usage_with_options(const char * const *usagestr,
839 const struct option *opts)
841 usage_with_options_internal(NULL, usagestr, opts, 0, 1);
845 void NORETURN usage_msg_opt(const char *msg,
846 const char * const *usagestr,
847 const struct option *options)
849 fprintf(stderr, "fatal: %s\n\n", msg);
850 usage_with_options(usagestr, options);
853 const char *optname(const struct option *opt, int flags)
855 static struct strbuf sb = STRBUF_INIT;
858 if (flags & OPT_SHORT)
859 strbuf_addf(&sb, "switch `%c'", opt->short_name);
860 else if (flags & OPT_UNSET)
861 strbuf_addf(&sb, "option `no-%s'", opt->long_name);
863 strbuf_addf(&sb, "option `%s'", opt->long_name);