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 opt->ll_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 case OPTION_CALLBACK:
414 BUG("OPTION_CALLBACK needs a callback");
415 if (opts->ll_callback)
416 BUG("OPTION_CALLBACK needs no ll_callback");
418 case OPTION_LOWLEVEL_CALLBACK:
419 if (!opts->ll_callback)
420 BUG("OPTION_LOWLEVEL_CALLBACK needs a callback");
422 BUG("OPTION_LOWLEVEL_CALLBACK needs no high level callback");
425 ; /* ok. (usually accepts an argument) */
428 strcspn(opts->argh, " _") != strlen(opts->argh))
429 err |= optbug(opts, "multi-word argh should use dash to separate words");
435 void parse_options_start(struct parse_opt_ctx_t *ctx,
436 int argc, const char **argv, const char *prefix,
437 const struct option *options, int flags)
439 memset(ctx, 0, sizeof(*ctx));
442 if (!(flags & PARSE_OPT_ONE_SHOT)) {
446 ctx->total = ctx->argc;
448 ctx->prefix = prefix;
449 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
451 if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
452 (flags & PARSE_OPT_STOP_AT_NON_OPTION) &&
453 !(flags & PARSE_OPT_ONE_SHOT))
454 BUG("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
455 if ((flags & PARSE_OPT_ONE_SHOT) &&
456 (flags & PARSE_OPT_KEEP_ARGV0))
457 BUG("Can't keep argv0 if you don't have it");
458 parse_options_check(options);
461 static void show_negated_gitcomp(const struct option *opts, int nr_noopts)
463 int printed_dashdash = 0;
465 for (; opts->type != OPTION_END; opts++) {
466 int has_unset_form = 0;
469 if (!opts->long_name)
471 if (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE))
473 if (opts->flags & PARSE_OPT_NONEG)
476 switch (opts->type) {
478 case OPTION_FILENAME:
480 case OPTION_MAGNITUDE:
481 case OPTION_CALLBACK:
494 if (skip_prefix(opts->long_name, "no-", &name)) {
496 printf(" --%s", name);
497 } else if (nr_noopts >= 0) {
498 if (nr_noopts && !printed_dashdash) {
500 printed_dashdash = 1;
502 printf(" --no-%s", opts->long_name);
508 static int show_gitcomp(struct parse_opt_ctx_t *ctx,
509 const struct option *opts)
511 const struct option *original_opts = opts;
514 for (; opts->type != OPTION_END; opts++) {
515 const char *suffix = "";
517 if (!opts->long_name)
519 if (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE))
522 switch (opts->type) {
526 case OPTION_FILENAME:
528 case OPTION_MAGNITUDE:
529 case OPTION_CALLBACK:
530 if (opts->flags & PARSE_OPT_NOARG)
532 if (opts->flags & PARSE_OPT_OPTARG)
534 if (opts->flags & PARSE_OPT_LASTARG_DEFAULT)
541 if (opts->flags & PARSE_OPT_COMP_ARG)
543 if (starts_with(opts->long_name, "no-"))
545 printf(" --%s%s", opts->long_name, suffix);
547 show_negated_gitcomp(original_opts, -1);
548 show_negated_gitcomp(original_opts, nr_noopts);
550 return PARSE_OPT_COMPLETE;
553 static int usage_with_options_internal(struct parse_opt_ctx_t *,
554 const char * const *,
555 const struct option *, int, int);
557 int parse_options_step(struct parse_opt_ctx_t *ctx,
558 const struct option *options,
559 const char * const usagestr[])
561 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
563 /* we must reset ->opt, unknown short option leave it dangling */
566 for (; ctx->argc; ctx->argc--, ctx->argv++) {
567 const char *arg = ctx->argv[0];
569 if (ctx->flags & PARSE_OPT_ONE_SHOT &&
570 ctx->argc != ctx->total)
573 if (*arg != '-' || !arg[1]) {
574 if (parse_nodash_opt(ctx, arg, options) == 0)
576 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
577 return PARSE_OPT_NON_OPTION;
578 ctx->out[ctx->cpidx++] = ctx->argv[0];
582 /* lone -h asks for help */
583 if (internal_help && ctx->total == 1 && !strcmp(arg + 1, "h"))
586 /* lone --git-completion-helper is asked by git-completion.bash */
587 if (ctx->total == 1 && !strcmp(arg + 1, "-git-completion-helper"))
588 return show_gitcomp(ctx, options);
592 switch (parse_short_opt(ctx, options)) {
594 return PARSE_OPT_ERROR;
597 check_typos(arg + 1, options);
598 if (internal_help && *ctx->opt == 'h')
603 check_typos(arg + 1, options);
605 switch (parse_short_opt(ctx, options)) {
607 return PARSE_OPT_ERROR;
609 if (internal_help && *ctx->opt == 'h')
612 /* fake a short option thing to hide the fact that we may have
613 * started to parse aggregated stuff
615 * This is leaky, too bad.
617 ctx->argv[0] = xstrdup(ctx->opt - 1);
618 *(char *)ctx->argv[0] = '-';
625 if (!arg[2]) { /* "--" */
626 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
633 if (internal_help && !strcmp(arg + 2, "help-all"))
634 return usage_with_options_internal(ctx, usagestr, options, 1, 0);
635 if (internal_help && !strcmp(arg + 2, "help"))
637 switch (parse_long_opt(ctx, arg + 2, options)) {
639 return PARSE_OPT_ERROR;
647 if (ctx->flags & PARSE_OPT_ONE_SHOT)
649 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
650 return PARSE_OPT_UNKNOWN;
651 ctx->out[ctx->cpidx++] = ctx->argv[0];
654 return PARSE_OPT_DONE;
657 return usage_with_options_internal(ctx, usagestr, options, 0, 0);
660 int parse_options_end(struct parse_opt_ctx_t *ctx)
662 if (ctx->flags & PARSE_OPT_ONE_SHOT)
663 return ctx->total - ctx->argc;
665 MOVE_ARRAY(ctx->out + ctx->cpidx, ctx->argv, ctx->argc);
666 ctx->out[ctx->cpidx + ctx->argc] = NULL;
667 return ctx->cpidx + ctx->argc;
670 int parse_options(int argc, const char **argv, const char *prefix,
671 const struct option *options, const char * const usagestr[],
674 struct parse_opt_ctx_t ctx;
676 parse_options_start(&ctx, argc, argv, prefix, options, flags);
677 switch (parse_options_step(&ctx, options, usagestr)) {
679 case PARSE_OPT_ERROR:
681 case PARSE_OPT_COMPLETE:
683 case PARSE_OPT_NON_OPTION:
686 default: /* PARSE_OPT_UNKNOWN */
687 if (ctx.argv[0][1] == '-') {
688 error(_("unknown option `%s'"), ctx.argv[0] + 2);
689 } else if (isascii(*ctx.opt)) {
690 error(_("unknown switch `%c'"), *ctx.opt);
692 error(_("unknown non-ascii option in string: `%s'"),
695 usage_with_options(usagestr, options);
698 precompose_argv(argc, argv);
699 return parse_options_end(&ctx);
702 static int usage_argh(const struct option *opts, FILE *outfile)
705 int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
706 !opts->argh || !!strpbrk(opts->argh, "()<>[]|");
707 if (opts->flags & PARSE_OPT_OPTARG)
709 s = literal ? "[=%s]" : "[=<%s>]";
711 s = literal ? "[%s]" : "[<%s>]";
713 s = literal ? " %s" : " <%s>";
714 return utf8_fprintf(outfile, s, opts->argh ? _(opts->argh) : _("..."));
717 #define USAGE_OPTS_WIDTH 24
720 static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
721 const char * const *usagestr,
722 const struct option *opts, int full, int err)
724 FILE *outfile = err ? stderr : stdout;
728 return PARSE_OPT_HELP;
730 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
731 fprintf(outfile, "cat <<\\EOF\n");
733 fprintf_ln(outfile, _("usage: %s"), _(*usagestr++));
734 while (*usagestr && **usagestr)
736 * TRANSLATORS: the colon here should align with the
737 * one in "usage: %s" translation.
739 fprintf_ln(outfile, _(" or: %s"), _(*usagestr++));
742 fprintf_ln(outfile, _(" %s"), _(*usagestr));
744 fputc('\n', outfile);
750 for (; opts->type != OPTION_END; opts++) {
754 if (opts->type == OPTION_GROUP) {
755 fputc('\n', outfile);
758 fprintf(outfile, "%s\n", _(opts->help));
761 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
765 fputc('\n', outfile);
769 pos = fprintf(outfile, " ");
770 if (opts->short_name) {
771 if (opts->flags & PARSE_OPT_NODASH)
772 pos += fprintf(outfile, "%c", opts->short_name);
774 pos += fprintf(outfile, "-%c", opts->short_name);
776 if (opts->long_name && opts->short_name)
777 pos += fprintf(outfile, ", ");
779 pos += fprintf(outfile, "--%s", opts->long_name);
780 if (opts->type == OPTION_NUMBER)
781 pos += utf8_fprintf(outfile, _("-NUM"));
783 if ((opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
784 !(opts->flags & PARSE_OPT_NOARG))
785 pos += usage_argh(opts, outfile);
787 if (pos <= USAGE_OPTS_WIDTH)
788 pad = USAGE_OPTS_WIDTH - pos;
790 fputc('\n', outfile);
791 pad = USAGE_OPTS_WIDTH;
793 fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", _(opts->help));
795 fputc('\n', outfile);
797 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
798 fputs("EOF\n", outfile);
800 return PARSE_OPT_HELP;
803 void NORETURN usage_with_options(const char * const *usagestr,
804 const struct option *opts)
806 usage_with_options_internal(NULL, usagestr, opts, 0, 1);
810 void NORETURN usage_msg_opt(const char *msg,
811 const char * const *usagestr,
812 const struct option *options)
814 fprintf(stderr, "fatal: %s\n\n", msg);
815 usage_with_options(usagestr, options);
818 const char *optname(const struct option *opt, int flags)
820 static struct strbuf sb = STRBUF_INIT;
823 if (flags & OPT_SHORT)
824 strbuf_addf(&sb, "switch `%c'", opt->short_name);
825 else if (flags & OPT_UNSET)
826 strbuf_addf(&sb, "option `no-%s'", opt->long_name);
828 strbuf_addf(&sb, "option `%s'", opt->long_name);