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, 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 return (*opt->callback)(opt, NULL, 1) ? (-1) : 0;
166 if (opt->flags & PARSE_OPT_NOARG)
167 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
168 if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
169 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
170 if (get_arg(p, opt, flags, &arg))
172 return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
176 *(int *)opt->value = 0;
179 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
180 *(int *)opt->value = opt->defval;
183 if (get_arg(p, opt, flags, &arg))
185 *(int *)opt->value = strtol(arg, (char **)&s, 10);
187 return error(_("%s expects a numerical value"),
188 optname(opt, flags));
191 case OPTION_MAGNITUDE:
193 *(unsigned long *)opt->value = 0;
196 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
197 *(unsigned long *)opt->value = opt->defval;
200 if (get_arg(p, opt, flags, &arg))
202 if (!git_parse_ulong(arg, opt->value))
203 return error(_("%s expects a non-negative integer value"
204 " with an optional k/m/g suffix"),
205 optname(opt, flags));
209 BUG("opt->type %d should not happen", opt->type);
213 static enum parse_opt_result parse_short_opt(struct parse_opt_ctx_t *p,
214 const struct option *options)
216 const struct option *all_opts = options;
217 const struct option *numopt = NULL;
219 for (; options->type != OPTION_END; options++) {
220 if (options->short_name == *p->opt) {
221 p->opt = p->opt[1] ? p->opt + 1 : NULL;
222 return get_value(p, options, all_opts, OPT_SHORT);
226 * Handle the numerical option later, explicit one-digit
227 * options take precedence over it.
229 if (options->type == OPTION_NUMBER)
232 if (numopt && isdigit(*p->opt)) {
237 while (isdigit(p->opt[len]))
239 arg = xmemdupz(p->opt, len);
240 p->opt = p->opt[len] ? p->opt + len : NULL;
241 rc = (*numopt->callback)(numopt, arg, 0) ? (-1) : 0;
245 return PARSE_OPT_UNKNOWN;
248 static enum parse_opt_result parse_long_opt(
249 struct parse_opt_ctx_t *p, const char *arg,
250 const struct option *options)
252 const struct option *all_opts = options;
253 const char *arg_end = strchrnul(arg, '=');
254 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
255 int abbrev_flags = 0, ambiguous_flags = 0;
257 for (; options->type != OPTION_END; options++) {
258 const char *rest, *long_name = options->long_name;
259 int flags = 0, opt_flags = 0;
265 if (!skip_prefix(arg, long_name, &rest))
267 if (options->type == OPTION_ARGUMENT) {
271 return error(_("%s takes no value"),
272 optname(options, flags));
275 p->out[p->cpidx++] = arg - 2;
276 return PARSE_OPT_DONE;
280 if (!(p->flags & PARSE_OPT_KEEP_UNKNOWN) &&
281 !strncmp(long_name, arg, arg_end - arg)) {
285 * If this is abbreviated, it is
286 * ambiguous. So when there is no
287 * exact match later, we need to
290 ambiguous_option = abbrev_option;
291 ambiguous_flags = abbrev_flags;
293 if (!(flags & OPT_UNSET) && *arg_end)
294 p->opt = arg_end + 1;
295 abbrev_option = options;
296 abbrev_flags = flags ^ opt_flags;
299 /* negation allowed? */
300 if (options->flags & PARSE_OPT_NONEG)
302 /* negated and abbreviated very much? */
303 if (starts_with("no-", arg)) {
308 if (!starts_with(arg, "no-")) {
309 if (starts_with(long_name, "no-")) {
311 opt_flags |= OPT_UNSET;
317 if (!skip_prefix(arg + 3, long_name, &rest)) {
318 /* abbreviated and negated? */
319 if (starts_with(long_name, arg + 3))
330 return get_value(p, options, all_opts, flags ^ opt_flags);
333 if (ambiguous_option) {
334 error(_("ambiguous option: %s "
335 "(could be --%s%s or --%s%s)"),
337 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
338 ambiguous_option->long_name,
339 (abbrev_flags & OPT_UNSET) ? "no-" : "",
340 abbrev_option->long_name);
341 return PARSE_OPT_HELP;
344 return get_value(p, abbrev_option, all_opts, abbrev_flags);
345 return PARSE_OPT_UNKNOWN;
348 static int parse_nodash_opt(struct parse_opt_ctx_t *p, const char *arg,
349 const struct option *options)
351 const struct option *all_opts = options;
353 for (; options->type != OPTION_END; options++) {
354 if (!(options->flags & PARSE_OPT_NODASH))
356 if (options->short_name == arg[0] && arg[1] == '\0')
357 return get_value(p, options, all_opts, OPT_SHORT);
362 static void check_typos(const char *arg, const struct option *options)
367 if (starts_with(arg, "no-")) {
368 error(_("did you mean `--%s` (with two dashes ?)"), arg);
372 for (; options->type != OPTION_END; options++) {
373 if (!options->long_name)
375 if (starts_with(options->long_name, arg)) {
376 error(_("did you mean `--%s` (with two dashes ?)"), arg);
382 static void parse_options_check(const struct option *opts)
385 char short_opts[128];
387 memset(short_opts, '\0', sizeof(short_opts));
388 for (; opts->type != OPTION_END; opts++) {
389 if ((opts->flags & PARSE_OPT_LASTARG_DEFAULT) &&
390 (opts->flags & PARSE_OPT_OPTARG))
391 err |= optbug(opts, "uses incompatible flags "
392 "LASTARG_DEFAULT and OPTARG");
393 if (opts->short_name) {
394 if (0x7F <= opts->short_name)
395 err |= optbug(opts, "invalid short name");
396 else if (short_opts[opts->short_name]++)
397 err |= optbug(opts, "short name already used");
399 if (opts->flags & PARSE_OPT_NODASH &&
400 ((opts->flags & PARSE_OPT_OPTARG) ||
401 !(opts->flags & PARSE_OPT_NOARG) ||
402 !(opts->flags & PARSE_OPT_NONEG) ||
404 err |= optbug(opts, "uses feature "
405 "not supported for dashless options");
406 switch (opts->type) {
412 if ((opts->flags & PARSE_OPT_OPTARG) ||
413 !(opts->flags & PARSE_OPT_NOARG))
414 err |= optbug(opts, "should not accept an argument");
416 case OPTION_CALLBACK:
418 BUG("OPTION_CALLBACK needs a callback");
419 if (opts->ll_callback)
420 BUG("OPTION_CALLBACK needs no ll_callback");
422 case OPTION_LOWLEVEL_CALLBACK:
423 if (!opts->ll_callback)
424 BUG("OPTION_LOWLEVEL_CALLBACK needs a callback");
426 BUG("OPTION_LOWLEVEL_CALLBACK needs no high level callback");
429 ; /* ok. (usually accepts an argument) */
432 strcspn(opts->argh, " _") != strlen(opts->argh))
433 err |= optbug(opts, "multi-word argh should use dash to separate words");
439 void parse_options_start(struct parse_opt_ctx_t *ctx,
440 int argc, const char **argv, const char *prefix,
441 const struct option *options, int flags)
443 memset(ctx, 0, sizeof(*ctx));
446 if (!(flags & PARSE_OPT_ONE_SHOT)) {
450 ctx->total = ctx->argc;
452 ctx->prefix = prefix;
453 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
455 if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
456 (flags & PARSE_OPT_STOP_AT_NON_OPTION) &&
457 !(flags & PARSE_OPT_ONE_SHOT))
458 BUG("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
459 if ((flags & PARSE_OPT_ONE_SHOT) &&
460 (flags & PARSE_OPT_KEEP_ARGV0))
461 BUG("Can't keep argv0 if you don't have it");
462 parse_options_check(options);
465 static void show_negated_gitcomp(const struct option *opts, int nr_noopts)
467 int printed_dashdash = 0;
469 for (; opts->type != OPTION_END; opts++) {
470 int has_unset_form = 0;
473 if (!opts->long_name)
475 if (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE))
477 if (opts->flags & PARSE_OPT_NONEG)
480 switch (opts->type) {
482 case OPTION_FILENAME:
484 case OPTION_MAGNITUDE:
485 case OPTION_CALLBACK:
498 if (skip_prefix(opts->long_name, "no-", &name)) {
500 printf(" --%s", name);
501 } else if (nr_noopts >= 0) {
502 if (nr_noopts && !printed_dashdash) {
504 printed_dashdash = 1;
506 printf(" --no-%s", opts->long_name);
512 static int show_gitcomp(struct parse_opt_ctx_t *ctx,
513 const struct option *opts)
515 const struct option *original_opts = opts;
518 for (; opts->type != OPTION_END; opts++) {
519 const char *suffix = "";
521 if (!opts->long_name)
523 if (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE))
526 switch (opts->type) {
530 case OPTION_FILENAME:
532 case OPTION_MAGNITUDE:
533 case OPTION_CALLBACK:
534 if (opts->flags & PARSE_OPT_NOARG)
536 if (opts->flags & PARSE_OPT_OPTARG)
538 if (opts->flags & PARSE_OPT_LASTARG_DEFAULT)
545 if (opts->flags & PARSE_OPT_COMP_ARG)
547 if (starts_with(opts->long_name, "no-"))
549 printf(" --%s%s", opts->long_name, suffix);
551 show_negated_gitcomp(original_opts, -1);
552 show_negated_gitcomp(original_opts, nr_noopts);
554 return PARSE_OPT_COMPLETE;
557 static int usage_with_options_internal(struct parse_opt_ctx_t *,
558 const char * const *,
559 const struct option *, int, int);
561 int parse_options_step(struct parse_opt_ctx_t *ctx,
562 const struct option *options,
563 const char * const usagestr[])
565 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
567 /* we must reset ->opt, unknown short option leave it dangling */
570 for (; ctx->argc; ctx->argc--, ctx->argv++) {
571 const char *arg = ctx->argv[0];
573 if (ctx->flags & PARSE_OPT_ONE_SHOT &&
574 ctx->argc != ctx->total)
577 if (*arg != '-' || !arg[1]) {
578 if (parse_nodash_opt(ctx, arg, options) == 0)
580 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
581 return PARSE_OPT_NON_OPTION;
582 ctx->out[ctx->cpidx++] = ctx->argv[0];
586 /* lone -h asks for help */
587 if (internal_help && ctx->total == 1 && !strcmp(arg + 1, "h"))
590 /* lone --git-completion-helper is asked by git-completion.bash */
591 if (ctx->total == 1 && !strcmp(arg + 1, "-git-completion-helper"))
592 return show_gitcomp(ctx, options);
596 switch (parse_short_opt(ctx, options)) {
597 case PARSE_OPT_ERROR:
598 return PARSE_OPT_ERROR;
599 case PARSE_OPT_UNKNOWN:
601 check_typos(arg + 1, options);
602 if (internal_help && *ctx->opt == 'h')
605 case PARSE_OPT_NON_OPTION:
607 case PARSE_OPT_COMPLETE:
608 BUG("parse_short_opt() cannot return these");
613 check_typos(arg + 1, options);
615 switch (parse_short_opt(ctx, options)) {
616 case PARSE_OPT_ERROR:
617 return PARSE_OPT_ERROR;
618 case PARSE_OPT_UNKNOWN:
619 if (internal_help && *ctx->opt == 'h')
622 /* fake a short option thing to hide the fact that we may have
623 * started to parse aggregated stuff
625 * This is leaky, too bad.
627 ctx->argv[0] = xstrdup(ctx->opt - 1);
628 *(char *)ctx->argv[0] = '-';
630 case PARSE_OPT_NON_OPTION:
631 case PARSE_OPT_COMPLETE:
633 BUG("parse_short_opt() cannot return these");
641 if (!arg[2]) { /* "--" */
642 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
649 if (internal_help && !strcmp(arg + 2, "help-all"))
650 return usage_with_options_internal(ctx, usagestr, options, 1, 0);
651 if (internal_help && !strcmp(arg + 2, "help"))
653 switch (parse_long_opt(ctx, arg + 2, options)) {
654 case PARSE_OPT_ERROR:
655 return PARSE_OPT_ERROR;
656 case PARSE_OPT_UNKNOWN:
660 case PARSE_OPT_NON_OPTION:
661 case PARSE_OPT_COMPLETE:
662 BUG("parse_long_opt() cannot return these");
668 if (ctx->flags & PARSE_OPT_ONE_SHOT)
670 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
671 return PARSE_OPT_UNKNOWN;
672 ctx->out[ctx->cpidx++] = ctx->argv[0];
675 return PARSE_OPT_DONE;
678 return usage_with_options_internal(ctx, usagestr, options, 0, 0);
681 int parse_options_end(struct parse_opt_ctx_t *ctx)
683 if (ctx->flags & PARSE_OPT_ONE_SHOT)
684 return ctx->total - ctx->argc;
686 MOVE_ARRAY(ctx->out + ctx->cpidx, ctx->argv, ctx->argc);
687 ctx->out[ctx->cpidx + ctx->argc] = NULL;
688 return ctx->cpidx + ctx->argc;
691 int parse_options(int argc, const char **argv, const char *prefix,
692 const struct option *options, const char * const usagestr[],
695 struct parse_opt_ctx_t ctx;
697 parse_options_start(&ctx, argc, argv, prefix, options, flags);
698 switch (parse_options_step(&ctx, options, usagestr)) {
700 case PARSE_OPT_ERROR:
702 case PARSE_OPT_COMPLETE:
704 case PARSE_OPT_NON_OPTION:
707 default: /* PARSE_OPT_UNKNOWN */
708 if (ctx.argv[0][1] == '-') {
709 error(_("unknown option `%s'"), ctx.argv[0] + 2);
710 } else if (isascii(*ctx.opt)) {
711 error(_("unknown switch `%c'"), *ctx.opt);
713 error(_("unknown non-ascii option in string: `%s'"),
716 usage_with_options(usagestr, options);
719 precompose_argv(argc, argv);
720 return parse_options_end(&ctx);
723 static int usage_argh(const struct option *opts, FILE *outfile)
726 int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
727 !opts->argh || !!strpbrk(opts->argh, "()<>[]|");
728 if (opts->flags & PARSE_OPT_OPTARG)
730 s = literal ? "[=%s]" : "[=<%s>]";
732 s = literal ? "[%s]" : "[<%s>]";
734 s = literal ? " %s" : " <%s>";
735 return utf8_fprintf(outfile, s, opts->argh ? _(opts->argh) : _("..."));
738 #define USAGE_OPTS_WIDTH 24
741 static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
742 const char * const *usagestr,
743 const struct option *opts, int full, int err)
745 FILE *outfile = err ? stderr : stdout;
749 return PARSE_OPT_HELP;
751 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
752 fprintf(outfile, "cat <<\\EOF\n");
754 fprintf_ln(outfile, _("usage: %s"), _(*usagestr++));
755 while (*usagestr && **usagestr)
757 * TRANSLATORS: the colon here should align with the
758 * one in "usage: %s" translation.
760 fprintf_ln(outfile, _(" or: %s"), _(*usagestr++));
763 fprintf_ln(outfile, _(" %s"), _(*usagestr));
765 fputc('\n', outfile);
771 for (; opts->type != OPTION_END; opts++) {
775 if (opts->type == OPTION_GROUP) {
776 fputc('\n', outfile);
779 fprintf(outfile, "%s\n", _(opts->help));
782 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
786 fputc('\n', outfile);
790 pos = fprintf(outfile, " ");
791 if (opts->short_name) {
792 if (opts->flags & PARSE_OPT_NODASH)
793 pos += fprintf(outfile, "%c", opts->short_name);
795 pos += fprintf(outfile, "-%c", opts->short_name);
797 if (opts->long_name && opts->short_name)
798 pos += fprintf(outfile, ", ");
800 pos += fprintf(outfile, "--%s", opts->long_name);
801 if (opts->type == OPTION_NUMBER)
802 pos += utf8_fprintf(outfile, _("-NUM"));
804 if ((opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
805 !(opts->flags & PARSE_OPT_NOARG))
806 pos += usage_argh(opts, outfile);
808 if (pos <= USAGE_OPTS_WIDTH)
809 pad = USAGE_OPTS_WIDTH - pos;
811 fputc('\n', outfile);
812 pad = USAGE_OPTS_WIDTH;
814 fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", _(opts->help));
816 fputc('\n', outfile);
818 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
819 fputs("EOF\n", outfile);
821 return PARSE_OPT_HELP;
824 void NORETURN usage_with_options(const char * const *usagestr,
825 const struct option *opts)
827 usage_with_options_internal(NULL, usagestr, opts, 0, 1);
831 void NORETURN usage_msg_opt(const char *msg,
832 const char * const *usagestr,
833 const struct option *options)
835 fprintf(stderr, "fatal: %s\n\n", msg);
836 usage_with_options(usagestr, options);
839 const char *optname(const struct option *opt, int flags)
841 static struct strbuf sb = STRBUF_INIT;
844 if (flags & OPT_SHORT)
845 strbuf_addf(&sb, "switch `%c'", opt->short_name);
846 else if (flags & OPT_UNSET)
847 strbuf_addf(&sb, "option `no-%s'", opt->long_name);
849 strbuf_addf(&sb, "option `%s'", opt->long_name);