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;
113 if (*(int *)opt->value < 0)
114 *(int *)opt->value = 0;
115 *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
119 *(int *)opt->value = unset ? 0 : opt->defval;
124 * Giving the same mode option twice, although is unnecessary,
125 * is not a grave error, so let it pass.
127 if (*(int *)opt->value && *(int *)opt->value != opt->defval)
128 return opt_command_mode_error(opt, all_opts, flags);
129 *(int *)opt->value = opt->defval;
134 *(const char **)opt->value = NULL;
135 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
136 *(const char **)opt->value = (const char *)opt->defval;
138 return get_arg(p, opt, flags, (const char **)opt->value);
141 case OPTION_FILENAME:
144 *(const char **)opt->value = NULL;
145 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
146 *(const char **)opt->value = (const char *)opt->defval;
148 err = get_arg(p, opt, flags, (const char **)opt->value);
151 fix_filename(p->prefix, (const char **)opt->value);
154 case OPTION_CALLBACK:
156 return (*opt->callback)(opt, NULL, 1) ? (-1) : 0;
157 if (opt->flags & PARSE_OPT_NOARG)
158 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
159 if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
160 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
161 if (get_arg(p, opt, flags, &arg))
163 return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
167 *(int *)opt->value = 0;
170 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
171 *(int *)opt->value = opt->defval;
174 if (get_arg(p, opt, flags, &arg))
176 *(int *)opt->value = strtol(arg, (char **)&s, 10);
178 return error(_("%s expects a numerical value"),
179 optname(opt, flags));
182 case OPTION_MAGNITUDE:
184 *(unsigned long *)opt->value = 0;
187 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
188 *(unsigned long *)opt->value = opt->defval;
191 if (get_arg(p, opt, flags, &arg))
193 if (!git_parse_ulong(arg, opt->value))
194 return error(_("%s expects a non-negative integer value"
195 " with an optional k/m/g suffix"),
196 optname(opt, flags));
200 BUG("opt->type %d should not happen", opt->type);
204 static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)
206 const struct option *all_opts = options;
207 const struct option *numopt = NULL;
209 for (; options->type != OPTION_END; options++) {
210 if (options->short_name == *p->opt) {
211 p->opt = p->opt[1] ? p->opt + 1 : NULL;
212 return get_value(p, options, all_opts, OPT_SHORT);
216 * Handle the numerical option later, explicit one-digit
217 * options take precedence over it.
219 if (options->type == OPTION_NUMBER)
222 if (numopt && isdigit(*p->opt)) {
227 while (isdigit(p->opt[len]))
229 arg = xmemdupz(p->opt, len);
230 p->opt = p->opt[len] ? p->opt + len : NULL;
231 rc = (*numopt->callback)(numopt, arg, 0) ? (-1) : 0;
238 static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
239 const struct option *options)
241 const struct option *all_opts = options;
242 const char *arg_end = strchrnul(arg, '=');
243 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
244 int abbrev_flags = 0, ambiguous_flags = 0;
246 for (; options->type != OPTION_END; options++) {
247 const char *rest, *long_name = options->long_name;
248 int flags = 0, opt_flags = 0;
254 if (!skip_prefix(arg, long_name, &rest))
256 if (options->type == OPTION_ARGUMENT) {
260 return error(_("%s takes no value"),
261 optname(options, flags));
264 p->out[p->cpidx++] = arg - 2;
269 if (!strncmp(long_name, arg, arg_end - arg)) {
273 * If this is abbreviated, it is
274 * ambiguous. So when there is no
275 * exact match later, we need to
278 ambiguous_option = abbrev_option;
279 ambiguous_flags = abbrev_flags;
281 if (!(flags & OPT_UNSET) && *arg_end)
282 p->opt = arg_end + 1;
283 abbrev_option = options;
284 abbrev_flags = flags ^ opt_flags;
287 /* negation allowed? */
288 if (options->flags & PARSE_OPT_NONEG)
290 /* negated and abbreviated very much? */
291 if (starts_with("no-", arg)) {
296 if (!starts_with(arg, "no-")) {
297 if (starts_with(long_name, "no-")) {
299 opt_flags |= OPT_UNSET;
305 if (!skip_prefix(arg + 3, long_name, &rest)) {
306 /* abbreviated and negated? */
307 if (starts_with(long_name, arg + 3))
318 return get_value(p, options, all_opts, flags ^ opt_flags);
321 if (ambiguous_option) {
322 error(_("ambiguous option: %s "
323 "(could be --%s%s or --%s%s)"),
325 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
326 ambiguous_option->long_name,
327 (abbrev_flags & OPT_UNSET) ? "no-" : "",
328 abbrev_option->long_name);
332 return get_value(p, abbrev_option, all_opts, abbrev_flags);
336 static int parse_nodash_opt(struct parse_opt_ctx_t *p, const char *arg,
337 const struct option *options)
339 const struct option *all_opts = options;
341 for (; options->type != OPTION_END; options++) {
342 if (!(options->flags & PARSE_OPT_NODASH))
344 if (options->short_name == arg[0] && arg[1] == '\0')
345 return get_value(p, options, all_opts, OPT_SHORT);
350 static void check_typos(const char *arg, const struct option *options)
355 if (starts_with(arg, "no-")) {
356 error(_("did you mean `--%s` (with two dashes ?)"), arg);
360 for (; options->type != OPTION_END; options++) {
361 if (!options->long_name)
363 if (starts_with(options->long_name, arg)) {
364 error(_("did you mean `--%s` (with two dashes ?)"), arg);
370 static void parse_options_check(const struct option *opts)
373 char short_opts[128];
375 memset(short_opts, '\0', sizeof(short_opts));
376 for (; opts->type != OPTION_END; opts++) {
377 if ((opts->flags & PARSE_OPT_LASTARG_DEFAULT) &&
378 (opts->flags & PARSE_OPT_OPTARG))
379 err |= optbug(opts, "uses incompatible flags "
380 "LASTARG_DEFAULT and OPTARG");
381 if (opts->short_name) {
382 if (0x7F <= opts->short_name)
383 err |= optbug(opts, "invalid short name");
384 else if (short_opts[opts->short_name]++)
385 err |= optbug(opts, "short name already used");
387 if (opts->flags & PARSE_OPT_NODASH &&
388 ((opts->flags & PARSE_OPT_OPTARG) ||
389 !(opts->flags & PARSE_OPT_NOARG) ||
390 !(opts->flags & PARSE_OPT_NONEG) ||
392 err |= optbug(opts, "uses feature "
393 "not supported for dashless options");
394 switch (opts->type) {
400 if ((opts->flags & PARSE_OPT_OPTARG) ||
401 !(opts->flags & PARSE_OPT_NOARG))
402 err |= optbug(opts, "should not accept an argument");
404 ; /* ok. (usually accepts an argument) */
407 strcspn(opts->argh, " _") != strlen(opts->argh))
408 err |= optbug(opts, "multi-word argh should use dash to separate words");
414 void parse_options_start(struct parse_opt_ctx_t *ctx,
415 int argc, const char **argv, const char *prefix,
416 const struct option *options, int flags)
418 memset(ctx, 0, sizeof(*ctx));
421 if (!(flags & PARSE_OPT_ONE_SHOT)) {
425 ctx->total = ctx->argc;
427 ctx->prefix = prefix;
428 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
430 if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
431 (flags & PARSE_OPT_STOP_AT_NON_OPTION) &&
432 !(flags & PARSE_OPT_ONE_SHOT))
433 BUG("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
434 if ((flags & PARSE_OPT_ONE_SHOT) &&
435 (flags & PARSE_OPT_KEEP_ARGV0))
436 BUG("Can't keep argv0 if you don't have it");
437 parse_options_check(options);
440 static void show_negated_gitcomp(const struct option *opts, int nr_noopts)
442 int printed_dashdash = 0;
444 for (; opts->type != OPTION_END; opts++) {
445 int has_unset_form = 0;
448 if (!opts->long_name)
450 if (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE))
452 if (opts->flags & PARSE_OPT_NONEG)
455 switch (opts->type) {
457 case OPTION_FILENAME:
459 case OPTION_MAGNITUDE:
460 case OPTION_CALLBACK:
473 if (skip_prefix(opts->long_name, "no-", &name)) {
475 printf(" --%s", name);
476 } else if (nr_noopts >= 0) {
477 if (nr_noopts && !printed_dashdash) {
479 printed_dashdash = 1;
481 printf(" --no-%s", opts->long_name);
487 static int show_gitcomp(struct parse_opt_ctx_t *ctx,
488 const struct option *opts)
490 const struct option *original_opts = opts;
493 for (; opts->type != OPTION_END; opts++) {
494 const char *suffix = "";
496 if (!opts->long_name)
498 if (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE))
501 switch (opts->type) {
505 case OPTION_FILENAME:
507 case OPTION_MAGNITUDE:
508 case OPTION_CALLBACK:
509 if (opts->flags & PARSE_OPT_NOARG)
511 if (opts->flags & PARSE_OPT_OPTARG)
513 if (opts->flags & PARSE_OPT_LASTARG_DEFAULT)
520 if (opts->flags & PARSE_OPT_COMP_ARG)
522 if (starts_with(opts->long_name, "no-"))
524 printf(" --%s%s", opts->long_name, suffix);
526 show_negated_gitcomp(original_opts, -1);
527 show_negated_gitcomp(original_opts, nr_noopts);
529 return PARSE_OPT_COMPLETE;
532 static int usage_with_options_internal(struct parse_opt_ctx_t *,
533 const char * const *,
534 const struct option *, int, int);
536 int parse_options_step(struct parse_opt_ctx_t *ctx,
537 const struct option *options,
538 const char * const usagestr[])
540 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
542 /* we must reset ->opt, unknown short option leave it dangling */
545 for (; ctx->argc; ctx->argc--, ctx->argv++) {
546 const char *arg = ctx->argv[0];
548 if (ctx->flags & PARSE_OPT_ONE_SHOT &&
549 ctx->argc != ctx->total)
552 if (*arg != '-' || !arg[1]) {
553 if (parse_nodash_opt(ctx, arg, options) == 0)
555 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
556 return PARSE_OPT_NON_OPTION;
557 ctx->out[ctx->cpidx++] = ctx->argv[0];
561 /* lone -h asks for help */
562 if (internal_help && ctx->total == 1 && !strcmp(arg + 1, "h"))
565 /* lone --git-completion-helper is asked by git-completion.bash */
566 if (ctx->total == 1 && !strcmp(arg + 1, "-git-completion-helper"))
567 return show_gitcomp(ctx, options);
571 switch (parse_short_opt(ctx, options)) {
573 return PARSE_OPT_ERROR;
576 check_typos(arg + 1, options);
577 if (internal_help && *ctx->opt == 'h')
582 check_typos(arg + 1, options);
584 switch (parse_short_opt(ctx, options)) {
586 return PARSE_OPT_ERROR;
588 if (internal_help && *ctx->opt == 'h')
591 /* fake a short option thing to hide the fact that we may have
592 * started to parse aggregated stuff
594 * This is leaky, too bad.
596 ctx->argv[0] = xstrdup(ctx->opt - 1);
597 *(char *)ctx->argv[0] = '-';
604 if (!arg[2]) { /* "--" */
605 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
612 if (internal_help && !strcmp(arg + 2, "help-all"))
613 return usage_with_options_internal(ctx, usagestr, options, 1, 0);
614 if (internal_help && !strcmp(arg + 2, "help"))
616 switch (parse_long_opt(ctx, arg + 2, options)) {
618 return PARSE_OPT_ERROR;
626 if (ctx->flags & PARSE_OPT_ONE_SHOT)
628 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
629 return PARSE_OPT_UNKNOWN;
630 ctx->out[ctx->cpidx++] = ctx->argv[0];
633 return PARSE_OPT_DONE;
636 return usage_with_options_internal(ctx, usagestr, options, 0, 0);
639 int parse_options_end(struct parse_opt_ctx_t *ctx)
641 if (ctx->flags & PARSE_OPT_ONE_SHOT)
642 return ctx->total - ctx->argc;
644 MOVE_ARRAY(ctx->out + ctx->cpidx, ctx->argv, ctx->argc);
645 ctx->out[ctx->cpidx + ctx->argc] = NULL;
646 return ctx->cpidx + ctx->argc;
649 int parse_options(int argc, const char **argv, const char *prefix,
650 const struct option *options, const char * const usagestr[],
653 struct parse_opt_ctx_t ctx;
655 parse_options_start(&ctx, argc, argv, prefix, options, flags);
656 switch (parse_options_step(&ctx, options, usagestr)) {
658 case PARSE_OPT_ERROR:
660 case PARSE_OPT_COMPLETE:
662 case PARSE_OPT_NON_OPTION:
665 default: /* PARSE_OPT_UNKNOWN */
666 if (ctx.argv[0][1] == '-') {
667 error(_("unknown option `%s'"), ctx.argv[0] + 2);
668 } else if (isascii(*ctx.opt)) {
669 error(_("unknown switch `%c'"), *ctx.opt);
671 error(_("unknown non-ascii option in string: `%s'"),
674 usage_with_options(usagestr, options);
677 precompose_argv(argc, argv);
678 return parse_options_end(&ctx);
681 static int usage_argh(const struct option *opts, FILE *outfile)
684 int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
685 !opts->argh || !!strpbrk(opts->argh, "()<>[]|");
686 if (opts->flags & PARSE_OPT_OPTARG)
688 s = literal ? "[=%s]" : "[=<%s>]";
690 s = literal ? "[%s]" : "[<%s>]";
692 s = literal ? " %s" : " <%s>";
693 return utf8_fprintf(outfile, s, opts->argh ? _(opts->argh) : _("..."));
696 #define USAGE_OPTS_WIDTH 24
699 static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
700 const char * const *usagestr,
701 const struct option *opts, int full, int err)
703 FILE *outfile = err ? stderr : stdout;
707 return PARSE_OPT_HELP;
709 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
710 fprintf(outfile, "cat <<\\EOF\n");
712 fprintf_ln(outfile, _("usage: %s"), _(*usagestr++));
713 while (*usagestr && **usagestr)
715 * TRANSLATORS: the colon here should align with the
716 * one in "usage: %s" translation.
718 fprintf_ln(outfile, _(" or: %s"), _(*usagestr++));
721 fprintf_ln(outfile, _(" %s"), _(*usagestr));
723 fputc('\n', outfile);
729 for (; opts->type != OPTION_END; opts++) {
733 if (opts->type == OPTION_GROUP) {
734 fputc('\n', outfile);
737 fprintf(outfile, "%s\n", _(opts->help));
740 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
744 fputc('\n', outfile);
748 pos = fprintf(outfile, " ");
749 if (opts->short_name) {
750 if (opts->flags & PARSE_OPT_NODASH)
751 pos += fprintf(outfile, "%c", opts->short_name);
753 pos += fprintf(outfile, "-%c", opts->short_name);
755 if (opts->long_name && opts->short_name)
756 pos += fprintf(outfile, ", ");
758 pos += fprintf(outfile, "--%s", opts->long_name);
759 if (opts->type == OPTION_NUMBER)
760 pos += utf8_fprintf(outfile, _("-NUM"));
762 if ((opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
763 !(opts->flags & PARSE_OPT_NOARG))
764 pos += usage_argh(opts, outfile);
766 if (pos <= USAGE_OPTS_WIDTH)
767 pad = USAGE_OPTS_WIDTH - pos;
769 fputc('\n', outfile);
770 pad = USAGE_OPTS_WIDTH;
772 fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", _(opts->help));
774 fputc('\n', outfile);
776 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
777 fputs("EOF\n", outfile);
779 return PARSE_OPT_HELP;
782 void NORETURN usage_with_options(const char * const *usagestr,
783 const struct option *opts)
785 usage_with_options_internal(NULL, usagestr, opts, 0, 1);
789 void NORETURN usage_msg_opt(const char *msg,
790 const char * const *usagestr,
791 const struct option *options)
793 fprintf(stderr, "fatal: %s\n\n", msg);
794 usage_with_options(usagestr, options);
797 const char *optname(const struct option *opt, int flags)
799 static struct strbuf sb = STRBUF_INIT;
802 if (flags & OPT_SHORT)
803 strbuf_addf(&sb, "switch `%c'", opt->short_name);
804 else if (flags & OPT_UNSET)
805 strbuf_addf(&sb, "option `no-%s'", opt->long_name);
807 strbuf_addf(&sb, "option `%s'", opt->long_name);