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));
419 ctx->argc = ctx->total = argc - 1;
420 ctx->argv = argv + 1;
422 ctx->prefix = prefix;
423 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
425 if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
426 (flags & PARSE_OPT_STOP_AT_NON_OPTION))
427 BUG("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
428 parse_options_check(options);
431 static void show_negated_gitcomp(const struct option *opts, int nr_noopts)
433 int printed_dashdash = 0;
435 for (; opts->type != OPTION_END; opts++) {
436 int has_unset_form = 0;
439 if (!opts->long_name)
441 if (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE))
443 if (opts->flags & PARSE_OPT_NONEG)
446 switch (opts->type) {
448 case OPTION_FILENAME:
450 case OPTION_MAGNITUDE:
451 case OPTION_CALLBACK:
464 if (skip_prefix(opts->long_name, "no-", &name)) {
466 printf(" --%s", name);
467 } else if (nr_noopts >= 0) {
468 if (nr_noopts && !printed_dashdash) {
470 printed_dashdash = 1;
472 printf(" --no-%s", opts->long_name);
478 static int show_gitcomp(struct parse_opt_ctx_t *ctx,
479 const struct option *opts)
481 const struct option *original_opts = opts;
484 for (; opts->type != OPTION_END; opts++) {
485 const char *suffix = "";
487 if (!opts->long_name)
489 if (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE))
492 switch (opts->type) {
496 case OPTION_FILENAME:
498 case OPTION_MAGNITUDE:
499 case OPTION_CALLBACK:
500 if (opts->flags & PARSE_OPT_NOARG)
502 if (opts->flags & PARSE_OPT_OPTARG)
504 if (opts->flags & PARSE_OPT_LASTARG_DEFAULT)
511 if (opts->flags & PARSE_OPT_COMP_ARG)
513 if (starts_with(opts->long_name, "no-"))
515 printf(" --%s%s", opts->long_name, suffix);
517 show_negated_gitcomp(original_opts, -1);
518 show_negated_gitcomp(original_opts, nr_noopts);
520 return PARSE_OPT_COMPLETE;
523 static int usage_with_options_internal(struct parse_opt_ctx_t *,
524 const char * const *,
525 const struct option *, int, int);
527 int parse_options_step(struct parse_opt_ctx_t *ctx,
528 const struct option *options,
529 const char * const usagestr[])
531 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
533 /* we must reset ->opt, unknown short option leave it dangling */
536 for (; ctx->argc; ctx->argc--, ctx->argv++) {
537 const char *arg = ctx->argv[0];
539 if (*arg != '-' || !arg[1]) {
540 if (parse_nodash_opt(ctx, arg, options) == 0)
542 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
543 return PARSE_OPT_NON_OPTION;
544 ctx->out[ctx->cpidx++] = ctx->argv[0];
548 /* lone -h asks for help */
549 if (internal_help && ctx->total == 1 && !strcmp(arg + 1, "h"))
552 /* lone --git-completion-helper is asked by git-completion.bash */
553 if (ctx->total == 1 && !strcmp(arg + 1, "-git-completion-helper"))
554 return show_gitcomp(ctx, options);
558 switch (parse_short_opt(ctx, options)) {
560 return PARSE_OPT_ERROR;
563 check_typos(arg + 1, options);
564 if (internal_help && *ctx->opt == 'h')
569 check_typos(arg + 1, options);
571 switch (parse_short_opt(ctx, options)) {
573 return PARSE_OPT_ERROR;
575 if (internal_help && *ctx->opt == 'h')
578 /* fake a short option thing to hide the fact that we may have
579 * started to parse aggregated stuff
581 * This is leaky, too bad.
583 ctx->argv[0] = xstrdup(ctx->opt - 1);
584 *(char *)ctx->argv[0] = '-';
591 if (!arg[2]) { /* "--" */
592 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
599 if (internal_help && !strcmp(arg + 2, "help-all"))
600 return usage_with_options_internal(ctx, usagestr, options, 1, 0);
601 if (internal_help && !strcmp(arg + 2, "help"))
603 switch (parse_long_opt(ctx, arg + 2, options)) {
605 return PARSE_OPT_ERROR;
613 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
614 return PARSE_OPT_UNKNOWN;
615 ctx->out[ctx->cpidx++] = ctx->argv[0];
618 return PARSE_OPT_DONE;
621 return usage_with_options_internal(ctx, usagestr, options, 0, 0);
624 int parse_options_end(struct parse_opt_ctx_t *ctx)
626 MOVE_ARRAY(ctx->out + ctx->cpidx, ctx->argv, ctx->argc);
627 ctx->out[ctx->cpidx + ctx->argc] = NULL;
628 return ctx->cpidx + ctx->argc;
631 int parse_options(int argc, const char **argv, const char *prefix,
632 const struct option *options, const char * const usagestr[],
635 struct parse_opt_ctx_t ctx;
637 parse_options_start(&ctx, argc, argv, prefix, options, flags);
638 switch (parse_options_step(&ctx, options, usagestr)) {
640 case PARSE_OPT_ERROR:
642 case PARSE_OPT_COMPLETE:
644 case PARSE_OPT_NON_OPTION:
647 default: /* PARSE_OPT_UNKNOWN */
648 if (ctx.argv[0][1] == '-') {
649 error(_("unknown option `%s'"), ctx.argv[0] + 2);
650 } else if (isascii(*ctx.opt)) {
651 error(_("unknown switch `%c'"), *ctx.opt);
653 error(_("unknown non-ascii option in string: `%s'"),
656 usage_with_options(usagestr, options);
659 precompose_argv(argc, argv);
660 return parse_options_end(&ctx);
663 static int usage_argh(const struct option *opts, FILE *outfile)
666 int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
667 !opts->argh || !!strpbrk(opts->argh, "()<>[]|");
668 if (opts->flags & PARSE_OPT_OPTARG)
670 s = literal ? "[=%s]" : "[=<%s>]";
672 s = literal ? "[%s]" : "[<%s>]";
674 s = literal ? " %s" : " <%s>";
675 return utf8_fprintf(outfile, s, opts->argh ? _(opts->argh) : _("..."));
678 #define USAGE_OPTS_WIDTH 24
681 static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
682 const char * const *usagestr,
683 const struct option *opts, int full, int err)
685 FILE *outfile = err ? stderr : stdout;
689 return PARSE_OPT_HELP;
691 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
692 fprintf(outfile, "cat <<\\EOF\n");
694 fprintf_ln(outfile, _("usage: %s"), _(*usagestr++));
695 while (*usagestr && **usagestr)
697 * TRANSLATORS: the colon here should align with the
698 * one in "usage: %s" translation.
700 fprintf_ln(outfile, _(" or: %s"), _(*usagestr++));
703 fprintf_ln(outfile, _(" %s"), _(*usagestr));
705 fputc('\n', outfile);
711 for (; opts->type != OPTION_END; opts++) {
715 if (opts->type == OPTION_GROUP) {
716 fputc('\n', outfile);
719 fprintf(outfile, "%s\n", _(opts->help));
722 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
726 fputc('\n', outfile);
730 pos = fprintf(outfile, " ");
731 if (opts->short_name) {
732 if (opts->flags & PARSE_OPT_NODASH)
733 pos += fprintf(outfile, "%c", opts->short_name);
735 pos += fprintf(outfile, "-%c", opts->short_name);
737 if (opts->long_name && opts->short_name)
738 pos += fprintf(outfile, ", ");
740 pos += fprintf(outfile, "--%s", opts->long_name);
741 if (opts->type == OPTION_NUMBER)
742 pos += utf8_fprintf(outfile, _("-NUM"));
744 if ((opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
745 !(opts->flags & PARSE_OPT_NOARG))
746 pos += usage_argh(opts, outfile);
748 if (pos <= USAGE_OPTS_WIDTH)
749 pad = USAGE_OPTS_WIDTH - pos;
751 fputc('\n', outfile);
752 pad = USAGE_OPTS_WIDTH;
754 fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", _(opts->help));
756 fputc('\n', outfile);
758 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
759 fputs("EOF\n", outfile);
761 return PARSE_OPT_HELP;
764 void NORETURN usage_with_options(const char * const *usagestr,
765 const struct option *opts)
767 usage_with_options_internal(NULL, usagestr, opts, 0, 1);
771 void NORETURN usage_msg_opt(const char *msg,
772 const char * const *usagestr,
773 const struct option *options)
775 fprintf(stderr, "fatal: %s\n\n", msg);
776 usage_with_options(usagestr, options);
779 const char *optname(const struct option *opt, int flags)
781 static struct strbuf sb = STRBUF_INIT;
784 if (flags & OPT_SHORT)
785 strbuf_addf(&sb, "switch `%c'", opt->short_name);
786 else if (flags & OPT_UNSET)
787 strbuf_addf(&sb, "option `no-%s'", opt->long_name);
789 strbuf_addf(&sb, "option `%s'", opt->long_name);