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 opterror(opt, "requires a value", 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 message = STRBUF_INIT;
53 struct strbuf that_name = STRBUF_INIT;
56 * Find the other option that was used to set the variable
57 * already, and report that this is not compatible with it.
59 for (that = all_opts; that->type != OPTION_END; that++) {
61 that->type != OPTION_CMDMODE ||
62 that->value != opt->value ||
63 that->defval != *(int *)opt->value)
67 strbuf_addf(&that_name, "--%s", that->long_name);
69 strbuf_addf(&that_name, "-%c", that->short_name);
70 strbuf_addf(&message, ": incompatible with %s", that_name.buf);
71 strbuf_release(&that_name);
72 opterror(opt, message.buf, flags);
73 strbuf_release(&message);
76 return opterror(opt, ": incompatible with something else", flags);
79 static int get_value(struct parse_opt_ctx_t *p,
80 const struct option *opt,
81 const struct option *all_opts,
85 const int unset = flags & OPT_UNSET;
89 return opterror(opt, "takes no value", flags);
90 if (unset && (opt->flags & PARSE_OPT_NONEG))
91 return opterror(opt, "isn't available", flags);
92 if (!(flags & OPT_SHORT) && p->opt && (opt->flags & PARSE_OPT_NOARG))
93 return opterror(opt, "takes no value", flags);
96 case OPTION_LOWLEVEL_CALLBACK:
97 return (*(parse_opt_ll_cb *)opt->callback)(p, opt, unset);
101 *(int *)opt->value &= ~opt->defval;
103 *(int *)opt->value |= opt->defval;
108 *(int *)opt->value |= opt->defval;
110 *(int *)opt->value &= ~opt->defval;
114 if (*(int *)opt->value < 0)
115 *(int *)opt->value = 0;
116 *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
120 *(int *)opt->value = unset ? 0 : opt->defval;
125 * Giving the same mode option twice, although is unnecessary,
126 * is not a grave error, so let it pass.
128 if (*(int *)opt->value && *(int *)opt->value != opt->defval)
129 return opt_command_mode_error(opt, all_opts, flags);
130 *(int *)opt->value = opt->defval;
135 *(const char **)opt->value = NULL;
136 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
137 *(const char **)opt->value = (const char *)opt->defval;
139 return get_arg(p, opt, flags, (const char **)opt->value);
142 case OPTION_FILENAME:
145 *(const char **)opt->value = NULL;
146 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
147 *(const char **)opt->value = (const char *)opt->defval;
149 err = get_arg(p, opt, flags, (const char **)opt->value);
152 fix_filename(p->prefix, (const char **)opt->value);
155 case OPTION_CALLBACK:
157 return (*opt->callback)(opt, NULL, 1) ? (-1) : 0;
158 if (opt->flags & PARSE_OPT_NOARG)
159 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
160 if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
161 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
162 if (get_arg(p, opt, flags, &arg))
164 return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
168 *(int *)opt->value = 0;
171 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
172 *(int *)opt->value = opt->defval;
175 if (get_arg(p, opt, flags, &arg))
177 *(int *)opt->value = strtol(arg, (char **)&s, 10);
179 return opterror(opt, "expects a numerical value", 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))
195 "expects a non-negative integer value with an optional k/m/g suffix",
200 die("should not happen, someone must be hit on the forehead");
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 opterror(options, "takes no value", flags);
263 p->out[p->cpidx++] = arg - 2;
268 if (!strncmp(long_name, arg, arg_end - arg)) {
272 * If this is abbreviated, it is
273 * ambiguous. So when there is no
274 * exact match later, we need to
277 ambiguous_option = abbrev_option;
278 ambiguous_flags = abbrev_flags;
280 if (!(flags & OPT_UNSET) && *arg_end)
281 p->opt = arg_end + 1;
282 abbrev_option = options;
283 abbrev_flags = flags ^ opt_flags;
286 /* negation allowed? */
287 if (options->flags & PARSE_OPT_NONEG)
289 /* negated and abbreviated very much? */
290 if (starts_with("no-", arg)) {
295 if (!starts_with(arg, "no-")) {
296 if (starts_with(long_name, "no-")) {
298 opt_flags |= OPT_UNSET;
304 if (!skip_prefix(arg + 3, long_name, &rest)) {
305 /* abbreviated and negated? */
306 if (starts_with(long_name, arg + 3))
317 return get_value(p, options, all_opts, flags ^ opt_flags);
320 if (ambiguous_option) {
321 error("Ambiguous option: %s "
322 "(could be --%s%s or --%s%s)",
324 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
325 ambiguous_option->long_name,
326 (abbrev_flags & OPT_UNSET) ? "no-" : "",
327 abbrev_option->long_name);
331 return get_value(p, abbrev_option, all_opts, abbrev_flags);
335 static int parse_nodash_opt(struct parse_opt_ctx_t *p, const char *arg,
336 const struct option *options)
338 const struct option *all_opts = options;
340 for (; options->type != OPTION_END; options++) {
341 if (!(options->flags & PARSE_OPT_NODASH))
343 if (options->short_name == arg[0] && arg[1] == '\0')
344 return get_value(p, options, all_opts, OPT_SHORT);
349 static void check_typos(const char *arg, const struct option *options)
354 if (starts_with(arg, "no-")) {
355 error ("did you mean `--%s` (with two dashes ?)", arg);
359 for (; options->type != OPTION_END; options++) {
360 if (!options->long_name)
362 if (starts_with(options->long_name, arg)) {
363 error ("did you mean `--%s` (with two dashes ?)", arg);
369 static void parse_options_check(const struct option *opts)
372 char short_opts[128];
374 memset(short_opts, '\0', sizeof(short_opts));
375 for (; opts->type != OPTION_END; opts++) {
376 if ((opts->flags & PARSE_OPT_LASTARG_DEFAULT) &&
377 (opts->flags & PARSE_OPT_OPTARG))
378 err |= optbug(opts, "uses incompatible flags "
379 "LASTARG_DEFAULT and OPTARG");
380 if (opts->short_name) {
381 if (0x7F <= opts->short_name)
382 err |= optbug(opts, "invalid short name");
383 else if (short_opts[opts->short_name]++)
384 err |= optbug(opts, "short name already used");
386 if (opts->flags & PARSE_OPT_NODASH &&
387 ((opts->flags & PARSE_OPT_OPTARG) ||
388 !(opts->flags & PARSE_OPT_NOARG) ||
389 !(opts->flags & PARSE_OPT_NONEG) ||
391 err |= optbug(opts, "uses feature "
392 "not supported for dashless options");
393 switch (opts->type) {
399 if ((opts->flags & PARSE_OPT_OPTARG) ||
400 !(opts->flags & PARSE_OPT_NOARG))
401 err |= optbug(opts, "should not accept an argument");
403 ; /* ok. (usually accepts an argument) */
406 strcspn(opts->argh, " _") != strlen(opts->argh))
407 err |= optbug(opts, "multi-word argh should use dash to separate words");
413 void parse_options_start(struct parse_opt_ctx_t *ctx,
414 int argc, const char **argv, const char *prefix,
415 const struct option *options, int flags)
417 memset(ctx, 0, sizeof(*ctx));
418 ctx->argc = ctx->total = argc - 1;
419 ctx->argv = argv + 1;
421 ctx->prefix = prefix;
422 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
424 if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
425 (flags & PARSE_OPT_STOP_AT_NON_OPTION))
426 die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
427 parse_options_check(options);
430 static void show_negated_gitcomp(const struct option *opts, int nr_noopts)
432 int printed_dashdash = 0;
434 for (; opts->type != OPTION_END; opts++) {
435 int has_unset_form = 0;
438 if (!opts->long_name)
440 if (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE))
442 if (opts->flags & PARSE_OPT_NONEG)
445 switch (opts->type) {
447 case OPTION_FILENAME:
449 case OPTION_MAGNITUDE:
450 case OPTION_CALLBACK:
463 if (skip_prefix(opts->long_name, "no-", &name)) {
465 printf(" --%s", name);
466 } else if (nr_noopts >= 0) {
467 if (nr_noopts && !printed_dashdash) {
469 printed_dashdash = 1;
471 printf(" --no-%s", opts->long_name);
477 static int show_gitcomp(struct parse_opt_ctx_t *ctx,
478 const struct option *opts)
480 const struct option *original_opts = opts;
483 for (; opts->type != OPTION_END; opts++) {
484 const char *suffix = "";
486 if (!opts->long_name)
488 if (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE))
491 switch (opts->type) {
495 case OPTION_FILENAME:
497 case OPTION_MAGNITUDE:
498 case OPTION_CALLBACK:
499 if (opts->flags & PARSE_OPT_NOARG)
501 if (opts->flags & PARSE_OPT_OPTARG)
503 if (opts->flags & PARSE_OPT_LASTARG_DEFAULT)
510 if (opts->flags & PARSE_OPT_COMP_ARG)
512 if (starts_with(opts->long_name, "no-"))
514 printf(" --%s%s", opts->long_name, suffix);
516 show_negated_gitcomp(original_opts, -1);
517 show_negated_gitcomp(original_opts, nr_noopts);
522 static int usage_with_options_internal(struct parse_opt_ctx_t *,
523 const char * const *,
524 const struct option *, int, int);
526 int parse_options_step(struct parse_opt_ctx_t *ctx,
527 const struct option *options,
528 const char * const usagestr[])
530 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
532 /* we must reset ->opt, unknown short option leave it dangling */
535 for (; ctx->argc; ctx->argc--, ctx->argv++) {
536 const char *arg = ctx->argv[0];
538 if (*arg != '-' || !arg[1]) {
539 if (parse_nodash_opt(ctx, arg, options) == 0)
541 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
542 return PARSE_OPT_NON_OPTION;
543 ctx->out[ctx->cpidx++] = ctx->argv[0];
547 /* lone -h asks for help */
548 if (internal_help && ctx->total == 1 && !strcmp(arg + 1, "h"))
551 /* lone --git-completion-helper is asked by git-completion.bash */
552 if (ctx->total == 1 && !strcmp(arg + 1, "-git-completion-helper"))
553 return show_gitcomp(ctx, options);
557 switch (parse_short_opt(ctx, options)) {
559 return PARSE_OPT_ERROR;
562 check_typos(arg + 1, options);
563 if (internal_help && *ctx->opt == 'h')
568 check_typos(arg + 1, options);
570 switch (parse_short_opt(ctx, options)) {
572 return PARSE_OPT_ERROR;
574 if (internal_help && *ctx->opt == 'h')
577 /* fake a short option thing to hide the fact that we may have
578 * started to parse aggregated stuff
580 * This is leaky, too bad.
582 ctx->argv[0] = xstrdup(ctx->opt - 1);
583 *(char *)ctx->argv[0] = '-';
590 if (!arg[2]) { /* "--" */
591 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
598 if (internal_help && !strcmp(arg + 2, "help-all"))
599 return usage_with_options_internal(ctx, usagestr, options, 1, 0);
600 if (internal_help && !strcmp(arg + 2, "help"))
602 switch (parse_long_opt(ctx, arg + 2, options)) {
604 return PARSE_OPT_ERROR;
612 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
613 return PARSE_OPT_UNKNOWN;
614 ctx->out[ctx->cpidx++] = ctx->argv[0];
617 return PARSE_OPT_DONE;
620 return usage_with_options_internal(ctx, usagestr, options, 0, 0);
623 int parse_options_end(struct parse_opt_ctx_t *ctx)
625 MOVE_ARRAY(ctx->out + ctx->cpidx, ctx->argv, ctx->argc);
626 ctx->out[ctx->cpidx + ctx->argc] = NULL;
627 return ctx->cpidx + ctx->argc;
630 int parse_options(int argc, const char **argv, const char *prefix,
631 const struct option *options, const char * const usagestr[],
634 struct parse_opt_ctx_t ctx;
636 parse_options_start(&ctx, argc, argv, prefix, options, flags);
637 switch (parse_options_step(&ctx, options, usagestr)) {
639 case PARSE_OPT_ERROR:
641 case PARSE_OPT_NON_OPTION:
644 default: /* PARSE_OPT_UNKNOWN */
645 if (ctx.argv[0][1] == '-') {
646 error("unknown option `%s'", ctx.argv[0] + 2);
647 } else if (isascii(*ctx.opt)) {
648 error("unknown switch `%c'", *ctx.opt);
650 error("unknown non-ascii option in string: `%s'",
653 usage_with_options(usagestr, options);
656 precompose_argv(argc, argv);
657 return parse_options_end(&ctx);
660 static int usage_argh(const struct option *opts, FILE *outfile)
663 int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
664 !opts->argh || !!strpbrk(opts->argh, "()<>[]|");
665 if (opts->flags & PARSE_OPT_OPTARG)
667 s = literal ? "[=%s]" : "[=<%s>]";
669 s = literal ? "[%s]" : "[<%s>]";
671 s = literal ? " %s" : " <%s>";
672 return utf8_fprintf(outfile, s, opts->argh ? _(opts->argh) : _("..."));
675 #define USAGE_OPTS_WIDTH 24
678 static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
679 const char * const *usagestr,
680 const struct option *opts, int full, int err)
682 FILE *outfile = err ? stderr : stdout;
686 return PARSE_OPT_HELP;
688 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
689 fprintf(outfile, "cat <<\\EOF\n");
691 fprintf_ln(outfile, _("usage: %s"), _(*usagestr++));
692 while (*usagestr && **usagestr)
694 * TRANSLATORS: the colon here should align with the
695 * one in "usage: %s" translation.
697 fprintf_ln(outfile, _(" or: %s"), _(*usagestr++));
700 fprintf_ln(outfile, _(" %s"), _(*usagestr));
702 fputc('\n', outfile);
708 for (; opts->type != OPTION_END; opts++) {
712 if (opts->type == OPTION_GROUP) {
713 fputc('\n', outfile);
716 fprintf(outfile, "%s\n", _(opts->help));
719 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
723 fputc('\n', outfile);
727 pos = fprintf(outfile, " ");
728 if (opts->short_name) {
729 if (opts->flags & PARSE_OPT_NODASH)
730 pos += fprintf(outfile, "%c", opts->short_name);
732 pos += fprintf(outfile, "-%c", opts->short_name);
734 if (opts->long_name && opts->short_name)
735 pos += fprintf(outfile, ", ");
737 pos += fprintf(outfile, "--%s", opts->long_name);
738 if (opts->type == OPTION_NUMBER)
739 pos += utf8_fprintf(outfile, _("-NUM"));
741 if ((opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
742 !(opts->flags & PARSE_OPT_NOARG))
743 pos += usage_argh(opts, outfile);
745 if (pos <= USAGE_OPTS_WIDTH)
746 pad = USAGE_OPTS_WIDTH - pos;
748 fputc('\n', outfile);
749 pad = USAGE_OPTS_WIDTH;
751 fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", _(opts->help));
753 fputc('\n', outfile);
755 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
756 fputs("EOF\n", outfile);
758 return PARSE_OPT_HELP;
761 void NORETURN usage_with_options(const char * const *usagestr,
762 const struct option *opts)
764 usage_with_options_internal(NULL, usagestr, opts, 0, 1);
768 void NORETURN usage_msg_opt(const char *msg,
769 const char * const *usagestr,
770 const struct option *options)
772 fprintf(stderr, "fatal: %s\n\n", msg);
773 usage_with_options(usagestr, options);
777 int opterror(const struct option *opt, const char *reason, int flags)
779 if (flags & OPT_SHORT)
780 return error("switch `%c' %s", opt->short_name, reason);
781 if (flags & OPT_UNSET)
782 return error("option `no-%s' %s", opt->long_name, reason);
783 return error("option `%s' %s", opt->long_name, reason);