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 int get_arg(struct parse_opt_ctx_t *p, const struct option *opt,
24 int flags, const char **arg)
29 } else if (p->argc == 1 && (opt->flags & PARSE_OPT_LASTARG_DEFAULT)) {
30 *arg = (const char *)opt->defval;
31 } else if (p->argc > 1) {
35 return error(_("%s requires a value"), optname(opt, flags));
39 static void fix_filename(const char *prefix, const char **file)
41 if (!file || !*file || !prefix || is_absolute_path(*file)
42 || !strcmp("-", *file))
44 *file = prefix_filename(prefix, *file);
47 static int opt_command_mode_error(const struct option *opt,
48 const struct option *all_opts,
51 const struct option *that;
52 struct strbuf that_name = STRBUF_INIT;
55 * Find the other option that was used to set the variable
56 * already, and report that this is not compatible with it.
58 for (that = all_opts; that->type != OPTION_END; that++) {
60 that->type != OPTION_CMDMODE ||
61 that->value != opt->value ||
62 that->defval != *(int *)opt->value)
66 strbuf_addf(&that_name, "--%s", that->long_name);
68 strbuf_addf(&that_name, "-%c", that->short_name);
69 error(_("%s is incompatible with %s"),
70 optname(opt, flags), that_name.buf);
71 strbuf_release(&that_name);
74 return error(_("%s : incompatible with something else"),
78 static int get_value(struct parse_opt_ctx_t *p,
79 const struct option *opt,
80 const struct option *all_opts,
84 const int unset = flags & OPT_UNSET;
88 return error(_("%s takes no value"), optname(opt, flags));
89 if (unset && (opt->flags & PARSE_OPT_NONEG))
90 return error(_("%s isn't available"), optname(opt, flags));
91 if (!(flags & OPT_SHORT) && p->opt && (opt->flags & PARSE_OPT_NOARG))
92 return error(_("%s takes no value"), optname(opt, flags));
95 case OPTION_LOWLEVEL_CALLBACK:
96 return (*(parse_opt_ll_cb *)opt->callback)(p, opt, unset);
100 *(int *)opt->value &= ~opt->defval;
102 *(int *)opt->value |= opt->defval;
107 *(int *)opt->value |= opt->defval;
109 *(int *)opt->value &= ~opt->defval;
114 BUG("BITOP can't have unset form");
115 *(int *)opt->value &= ~opt->extra;
116 *(int *)opt->value |= opt->defval;
120 if (*(int *)opt->value < 0)
121 *(int *)opt->value = 0;
122 *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
126 *(int *)opt->value = unset ? 0 : opt->defval;
131 * Giving the same mode option twice, although is unnecessary,
132 * is not a grave error, so let it pass.
134 if (*(int *)opt->value && *(int *)opt->value != opt->defval)
135 return opt_command_mode_error(opt, all_opts, flags);
136 *(int *)opt->value = opt->defval;
141 *(const char **)opt->value = NULL;
142 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
143 *(const char **)opt->value = (const char *)opt->defval;
145 return get_arg(p, opt, flags, (const char **)opt->value);
148 case OPTION_FILENAME:
151 *(const char **)opt->value = NULL;
152 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
153 *(const char **)opt->value = (const char *)opt->defval;
155 err = get_arg(p, opt, flags, (const char **)opt->value);
158 fix_filename(p->prefix, (const char **)opt->value);
161 case OPTION_CALLBACK:
163 return (*opt->callback)(opt, NULL, 1) ? (-1) : 0;
164 if (opt->flags & PARSE_OPT_NOARG)
165 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
166 if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
167 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
168 if (get_arg(p, opt, flags, &arg))
170 return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
174 *(int *)opt->value = 0;
177 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
178 *(int *)opt->value = opt->defval;
181 if (get_arg(p, opt, flags, &arg))
183 *(int *)opt->value = strtol(arg, (char **)&s, 10);
185 return error(_("%s expects a numerical value"),
186 optname(opt, flags));
189 case OPTION_MAGNITUDE:
191 *(unsigned long *)opt->value = 0;
194 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
195 *(unsigned long *)opt->value = opt->defval;
198 if (get_arg(p, opt, flags, &arg))
200 if (!git_parse_ulong(arg, opt->value))
201 return error(_("%s expects a non-negative integer value"
202 " with an optional k/m/g suffix"),
203 optname(opt, flags));
207 BUG("opt->type %d should not happen", opt->type);
211 static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)
213 const struct option *all_opts = options;
214 const struct option *numopt = NULL;
216 for (; options->type != OPTION_END; options++) {
217 if (options->short_name == *p->opt) {
218 p->opt = p->opt[1] ? p->opt + 1 : NULL;
219 return get_value(p, options, all_opts, OPT_SHORT);
223 * Handle the numerical option later, explicit one-digit
224 * options take precedence over it.
226 if (options->type == OPTION_NUMBER)
229 if (numopt && isdigit(*p->opt)) {
234 while (isdigit(p->opt[len]))
236 arg = xmemdupz(p->opt, len);
237 p->opt = p->opt[len] ? p->opt + len : NULL;
238 rc = (*numopt->callback)(numopt, arg, 0) ? (-1) : 0;
245 static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
246 const struct option *options)
248 const struct option *all_opts = options;
249 const char *arg_end = strchrnul(arg, '=');
250 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
251 int abbrev_flags = 0, ambiguous_flags = 0;
253 for (; options->type != OPTION_END; options++) {
254 const char *rest, *long_name = options->long_name;
255 int flags = 0, opt_flags = 0;
261 if (!skip_prefix(arg, long_name, &rest))
263 if (options->type == OPTION_ARGUMENT) {
267 return error(_("%s takes no value"),
268 optname(options, flags));
271 p->out[p->cpidx++] = arg - 2;
276 if (!(p->flags & PARSE_OPT_KEEP_UNKNOWN) &&
277 !strncmp(long_name, arg, arg_end - arg)) {
281 * If this is abbreviated, it is
282 * ambiguous. So when there is no
283 * exact match later, we need to
286 ambiguous_option = abbrev_option;
287 ambiguous_flags = abbrev_flags;
289 if (!(flags & OPT_UNSET) && *arg_end)
290 p->opt = arg_end + 1;
291 abbrev_option = options;
292 abbrev_flags = flags ^ opt_flags;
295 /* negation allowed? */
296 if (options->flags & PARSE_OPT_NONEG)
298 /* negated and abbreviated very much? */
299 if (starts_with("no-", arg)) {
304 if (!starts_with(arg, "no-")) {
305 if (starts_with(long_name, "no-")) {
307 opt_flags |= OPT_UNSET;
313 if (!skip_prefix(arg + 3, long_name, &rest)) {
314 /* abbreviated and negated? */
315 if (starts_with(long_name, arg + 3))
326 return get_value(p, options, all_opts, flags ^ opt_flags);
329 if (ambiguous_option) {
330 error(_("ambiguous option: %s "
331 "(could be --%s%s or --%s%s)"),
333 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
334 ambiguous_option->long_name,
335 (abbrev_flags & OPT_UNSET) ? "no-" : "",
336 abbrev_option->long_name);
340 return get_value(p, abbrev_option, all_opts, abbrev_flags);
344 static int parse_nodash_opt(struct parse_opt_ctx_t *p, const char *arg,
345 const struct option *options)
347 const struct option *all_opts = options;
349 for (; options->type != OPTION_END; options++) {
350 if (!(options->flags & PARSE_OPT_NODASH))
352 if (options->short_name == arg[0] && arg[1] == '\0')
353 return get_value(p, options, all_opts, OPT_SHORT);
358 static void check_typos(const char *arg, const struct option *options)
363 if (starts_with(arg, "no-")) {
364 error(_("did you mean `--%s` (with two dashes ?)"), arg);
368 for (; options->type != OPTION_END; options++) {
369 if (!options->long_name)
371 if (starts_with(options->long_name, arg)) {
372 error(_("did you mean `--%s` (with two dashes ?)"), arg);
378 static void parse_options_check(const struct option *opts)
381 char short_opts[128];
383 memset(short_opts, '\0', sizeof(short_opts));
384 for (; opts->type != OPTION_END; opts++) {
385 if ((opts->flags & PARSE_OPT_LASTARG_DEFAULT) &&
386 (opts->flags & PARSE_OPT_OPTARG))
387 err |= optbug(opts, "uses incompatible flags "
388 "LASTARG_DEFAULT and OPTARG");
389 if (opts->short_name) {
390 if (0x7F <= opts->short_name)
391 err |= optbug(opts, "invalid short name");
392 else if (short_opts[opts->short_name]++)
393 err |= optbug(opts, "short name already used");
395 if (opts->flags & PARSE_OPT_NODASH &&
396 ((opts->flags & PARSE_OPT_OPTARG) ||
397 !(opts->flags & PARSE_OPT_NOARG) ||
398 !(opts->flags & PARSE_OPT_NONEG) ||
400 err |= optbug(opts, "uses feature "
401 "not supported for dashless options");
402 switch (opts->type) {
408 if ((opts->flags & PARSE_OPT_OPTARG) ||
409 !(opts->flags & PARSE_OPT_NOARG))
410 err |= optbug(opts, "should not accept an argument");
412 ; /* ok. (usually accepts an argument) */
415 strcspn(opts->argh, " _") != strlen(opts->argh))
416 err |= optbug(opts, "multi-word argh should use dash to separate words");
422 void parse_options_start(struct parse_opt_ctx_t *ctx,
423 int argc, const char **argv, const char *prefix,
424 const struct option *options, int flags)
426 memset(ctx, 0, sizeof(*ctx));
429 if (!(flags & PARSE_OPT_ONE_SHOT)) {
433 ctx->total = ctx->argc;
435 ctx->prefix = prefix;
436 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
438 if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
439 (flags & PARSE_OPT_STOP_AT_NON_OPTION) &&
440 !(flags & PARSE_OPT_ONE_SHOT))
441 BUG("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
442 if ((flags & PARSE_OPT_ONE_SHOT) &&
443 (flags & PARSE_OPT_KEEP_ARGV0))
444 BUG("Can't keep argv0 if you don't have it");
445 parse_options_check(options);
448 static void show_negated_gitcomp(const struct option *opts, int nr_noopts)
450 int printed_dashdash = 0;
452 for (; opts->type != OPTION_END; opts++) {
453 int has_unset_form = 0;
456 if (!opts->long_name)
458 if (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE))
460 if (opts->flags & PARSE_OPT_NONEG)
463 switch (opts->type) {
465 case OPTION_FILENAME:
467 case OPTION_MAGNITUDE:
468 case OPTION_CALLBACK:
481 if (skip_prefix(opts->long_name, "no-", &name)) {
483 printf(" --%s", name);
484 } else if (nr_noopts >= 0) {
485 if (nr_noopts && !printed_dashdash) {
487 printed_dashdash = 1;
489 printf(" --no-%s", opts->long_name);
495 static int show_gitcomp(struct parse_opt_ctx_t *ctx,
496 const struct option *opts)
498 const struct option *original_opts = opts;
501 for (; opts->type != OPTION_END; opts++) {
502 const char *suffix = "";
504 if (!opts->long_name)
506 if (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE))
509 switch (opts->type) {
513 case OPTION_FILENAME:
515 case OPTION_MAGNITUDE:
516 case OPTION_CALLBACK:
517 if (opts->flags & PARSE_OPT_NOARG)
519 if (opts->flags & PARSE_OPT_OPTARG)
521 if (opts->flags & PARSE_OPT_LASTARG_DEFAULT)
528 if (opts->flags & PARSE_OPT_COMP_ARG)
530 if (starts_with(opts->long_name, "no-"))
532 printf(" --%s%s", opts->long_name, suffix);
534 show_negated_gitcomp(original_opts, -1);
535 show_negated_gitcomp(original_opts, nr_noopts);
537 return PARSE_OPT_COMPLETE;
540 static int usage_with_options_internal(struct parse_opt_ctx_t *,
541 const char * const *,
542 const struct option *, int, int);
544 int parse_options_step(struct parse_opt_ctx_t *ctx,
545 const struct option *options,
546 const char * const usagestr[])
548 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
550 /* we must reset ->opt, unknown short option leave it dangling */
553 for (; ctx->argc; ctx->argc--, ctx->argv++) {
554 const char *arg = ctx->argv[0];
556 if (ctx->flags & PARSE_OPT_ONE_SHOT &&
557 ctx->argc != ctx->total)
560 if (*arg != '-' || !arg[1]) {
561 if (parse_nodash_opt(ctx, arg, options) == 0)
563 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
564 return PARSE_OPT_NON_OPTION;
565 ctx->out[ctx->cpidx++] = ctx->argv[0];
569 /* lone -h asks for help */
570 if (internal_help && ctx->total == 1 && !strcmp(arg + 1, "h"))
573 /* lone --git-completion-helper is asked by git-completion.bash */
574 if (ctx->total == 1 && !strcmp(arg + 1, "-git-completion-helper"))
575 return show_gitcomp(ctx, options);
579 switch (parse_short_opt(ctx, options)) {
581 return PARSE_OPT_ERROR;
584 check_typos(arg + 1, options);
585 if (internal_help && *ctx->opt == 'h')
590 check_typos(arg + 1, options);
592 switch (parse_short_opt(ctx, options)) {
594 return PARSE_OPT_ERROR;
596 if (internal_help && *ctx->opt == 'h')
599 /* fake a short option thing to hide the fact that we may have
600 * started to parse aggregated stuff
602 * This is leaky, too bad.
604 ctx->argv[0] = xstrdup(ctx->opt - 1);
605 *(char *)ctx->argv[0] = '-';
612 if (!arg[2]) { /* "--" */
613 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
620 if (internal_help && !strcmp(arg + 2, "help-all"))
621 return usage_with_options_internal(ctx, usagestr, options, 1, 0);
622 if (internal_help && !strcmp(arg + 2, "help"))
624 switch (parse_long_opt(ctx, arg + 2, options)) {
626 return PARSE_OPT_ERROR;
634 if (ctx->flags & PARSE_OPT_ONE_SHOT)
636 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
637 return PARSE_OPT_UNKNOWN;
638 ctx->out[ctx->cpidx++] = ctx->argv[0];
641 return PARSE_OPT_DONE;
644 return usage_with_options_internal(ctx, usagestr, options, 0, 0);
647 int parse_options_end(struct parse_opt_ctx_t *ctx)
649 if (ctx->flags & PARSE_OPT_ONE_SHOT)
650 return ctx->total - ctx->argc;
652 MOVE_ARRAY(ctx->out + ctx->cpidx, ctx->argv, ctx->argc);
653 ctx->out[ctx->cpidx + ctx->argc] = NULL;
654 return ctx->cpidx + ctx->argc;
657 int parse_options(int argc, const char **argv, const char *prefix,
658 const struct option *options, const char * const usagestr[],
661 struct parse_opt_ctx_t ctx;
663 parse_options_start(&ctx, argc, argv, prefix, options, flags);
664 switch (parse_options_step(&ctx, options, usagestr)) {
666 case PARSE_OPT_ERROR:
668 case PARSE_OPT_COMPLETE:
670 case PARSE_OPT_NON_OPTION:
673 default: /* PARSE_OPT_UNKNOWN */
674 if (ctx.argv[0][1] == '-') {
675 error(_("unknown option `%s'"), ctx.argv[0] + 2);
676 } else if (isascii(*ctx.opt)) {
677 error(_("unknown switch `%c'"), *ctx.opt);
679 error(_("unknown non-ascii option in string: `%s'"),
682 usage_with_options(usagestr, options);
685 precompose_argv(argc, argv);
686 return parse_options_end(&ctx);
689 static int usage_argh(const struct option *opts, FILE *outfile)
692 int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
693 !opts->argh || !!strpbrk(opts->argh, "()<>[]|");
694 if (opts->flags & PARSE_OPT_OPTARG)
696 s = literal ? "[=%s]" : "[=<%s>]";
698 s = literal ? "[%s]" : "[<%s>]";
700 s = literal ? " %s" : " <%s>";
701 return utf8_fprintf(outfile, s, opts->argh ? _(opts->argh) : _("..."));
704 #define USAGE_OPTS_WIDTH 24
707 static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
708 const char * const *usagestr,
709 const struct option *opts, int full, int err)
711 FILE *outfile = err ? stderr : stdout;
715 return PARSE_OPT_HELP;
717 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
718 fprintf(outfile, "cat <<\\EOF\n");
720 fprintf_ln(outfile, _("usage: %s"), _(*usagestr++));
721 while (*usagestr && **usagestr)
723 * TRANSLATORS: the colon here should align with the
724 * one in "usage: %s" translation.
726 fprintf_ln(outfile, _(" or: %s"), _(*usagestr++));
729 fprintf_ln(outfile, _(" %s"), _(*usagestr));
731 fputc('\n', outfile);
737 for (; opts->type != OPTION_END; opts++) {
741 if (opts->type == OPTION_GROUP) {
742 fputc('\n', outfile);
745 fprintf(outfile, "%s\n", _(opts->help));
748 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
752 fputc('\n', outfile);
756 pos = fprintf(outfile, " ");
757 if (opts->short_name) {
758 if (opts->flags & PARSE_OPT_NODASH)
759 pos += fprintf(outfile, "%c", opts->short_name);
761 pos += fprintf(outfile, "-%c", opts->short_name);
763 if (opts->long_name && opts->short_name)
764 pos += fprintf(outfile, ", ");
766 pos += fprintf(outfile, "--%s", opts->long_name);
767 if (opts->type == OPTION_NUMBER)
768 pos += utf8_fprintf(outfile, _("-NUM"));
770 if ((opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
771 !(opts->flags & PARSE_OPT_NOARG))
772 pos += usage_argh(opts, outfile);
774 if (pos <= USAGE_OPTS_WIDTH)
775 pad = USAGE_OPTS_WIDTH - pos;
777 fputc('\n', outfile);
778 pad = USAGE_OPTS_WIDTH;
780 fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", _(opts->help));
782 fputc('\n', outfile);
784 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
785 fputs("EOF\n", outfile);
787 return PARSE_OPT_HELP;
790 void NORETURN usage_with_options(const char * const *usagestr,
791 const struct option *opts)
793 usage_with_options_internal(NULL, usagestr, opts, 0, 1);
797 void NORETURN usage_msg_opt(const char *msg,
798 const char * const *usagestr,
799 const struct option *options)
801 fprintf(stderr, "fatal: %s\n\n", msg);
802 usage_with_options(usagestr, options);
805 const char *optname(const struct option *opt, int flags)
807 static struct strbuf sb = STRBUF_INIT;
810 if (flags & OPT_SHORT)
811 strbuf_addf(&sb, "switch `%c'", opt->short_name);
812 else if (flags & OPT_UNSET)
813 strbuf_addf(&sb, "option `no-%s'", opt->long_name);
815 strbuf_addf(&sb, "option `%s'", opt->long_name);