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 || is_absolute_path(*file) ||
45 *file = expand_user_path(*file, 0);
47 *file = prefix_filename(prefix, *file);
50 static int opt_command_mode_error(const struct option *opt,
51 const struct option *all_opts,
54 const struct option *that;
55 struct strbuf message = STRBUF_INIT;
56 struct strbuf that_name = STRBUF_INIT;
59 * Find the other option that was used to set the variable
60 * already, and report that this is not compatible with it.
62 for (that = all_opts; that->type != OPTION_END; that++) {
64 that->type != OPTION_CMDMODE ||
65 that->value != opt->value ||
66 that->defval != *(int *)opt->value)
70 strbuf_addf(&that_name, "--%s", that->long_name);
72 strbuf_addf(&that_name, "-%c", that->short_name);
73 strbuf_addf(&message, ": incompatible with %s", that_name.buf);
74 strbuf_release(&that_name);
75 opterror(opt, message.buf, flags);
76 strbuf_release(&message);
79 return opterror(opt, ": incompatible with something else", flags);
82 static int get_value(struct parse_opt_ctx_t *p,
83 const struct option *opt,
84 const struct option *all_opts,
88 const int unset = flags & OPT_UNSET;
92 return opterror(opt, "takes no value", flags);
93 if (unset && (opt->flags & PARSE_OPT_NONEG))
94 return opterror(opt, "isn't available", flags);
95 if (!(flags & OPT_SHORT) && p->opt && (opt->flags & PARSE_OPT_NOARG))
96 return opterror(opt, "takes no value", flags);
99 case OPTION_LOWLEVEL_CALLBACK:
100 return (*(parse_opt_ll_cb *)opt->callback)(p, opt, unset);
104 *(int *)opt->value &= ~opt->defval;
106 *(int *)opt->value |= opt->defval;
111 *(int *)opt->value |= opt->defval;
113 *(int *)opt->value &= ~opt->defval;
117 if (*(int *)opt->value < 0)
118 *(int *)opt->value = 0;
119 *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
123 *(int *)opt->value = unset ? 0 : opt->defval;
128 * Giving the same mode option twice, although is unnecessary,
129 * is not a grave error, so let it pass.
131 if (*(int *)opt->value && *(int *)opt->value != opt->defval)
132 return opt_command_mode_error(opt, all_opts, flags);
133 *(int *)opt->value = opt->defval;
138 *(const char **)opt->value = NULL;
139 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
140 *(const char **)opt->value = (const char *)opt->defval;
142 return get_arg(p, opt, flags, (const char **)opt->value);
145 case OPTION_FILENAME:
148 *(const char **)opt->value = NULL;
149 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
150 *(const char **)opt->value = (const char *)opt->defval;
152 err = get_arg(p, opt, flags, (const char **)opt->value);
155 fix_filename(p->prefix, (const char **)opt->value);
158 case OPTION_CALLBACK:
160 return (*opt->callback)(opt, NULL, 1) ? (-1) : 0;
161 if (opt->flags & PARSE_OPT_NOARG)
162 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
163 if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
164 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
165 if (get_arg(p, opt, flags, &arg))
167 return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
171 *(int *)opt->value = 0;
174 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
175 *(int *)opt->value = opt->defval;
178 if (get_arg(p, opt, flags, &arg))
180 *(int *)opt->value = strtol(arg, (char **)&s, 10);
182 return opterror(opt, "expects a numerical value", flags);
185 case OPTION_MAGNITUDE:
187 *(unsigned long *)opt->value = 0;
190 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
191 *(unsigned long *)opt->value = opt->defval;
194 if (get_arg(p, opt, flags, &arg))
196 if (!git_parse_ulong(arg, opt->value))
198 "expects a non-negative integer value with an optional k/m/g suffix",
203 die("should not happen, someone must be hit on the forehead");
207 static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)
209 const struct option *all_opts = options;
210 const struct option *numopt = NULL;
212 for (; options->type != OPTION_END; options++) {
213 if (options->short_name == *p->opt) {
214 p->opt = p->opt[1] ? p->opt + 1 : NULL;
215 return get_value(p, options, all_opts, OPT_SHORT);
219 * Handle the numerical option later, explicit one-digit
220 * options take precedence over it.
222 if (options->type == OPTION_NUMBER)
225 if (numopt && isdigit(*p->opt)) {
230 while (isdigit(p->opt[len]))
232 arg = xmemdupz(p->opt, len);
233 p->opt = p->opt[len] ? p->opt + len : NULL;
234 rc = (*numopt->callback)(numopt, arg, 0) ? (-1) : 0;
241 static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
242 const struct option *options)
244 const struct option *all_opts = options;
245 const char *arg_end = strchrnul(arg, '=');
246 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
247 int abbrev_flags = 0, ambiguous_flags = 0;
249 for (; options->type != OPTION_END; options++) {
250 const char *rest, *long_name = options->long_name;
251 int flags = 0, opt_flags = 0;
257 if (!skip_prefix(arg, long_name, &rest))
259 if (options->type == OPTION_ARGUMENT) {
263 return opterror(options, "takes no value", flags);
266 p->out[p->cpidx++] = arg - 2;
271 if (!strncmp(long_name, arg, arg_end - arg)) {
275 * If this is abbreviated, it is
276 * ambiguous. So when there is no
277 * exact match later, we need to
280 ambiguous_option = abbrev_option;
281 ambiguous_flags = abbrev_flags;
283 if (!(flags & OPT_UNSET) && *arg_end)
284 p->opt = arg_end + 1;
285 abbrev_option = options;
286 abbrev_flags = flags ^ opt_flags;
289 /* negation allowed? */
290 if (options->flags & PARSE_OPT_NONEG)
292 /* negated and abbreviated very much? */
293 if (starts_with("no-", arg)) {
298 if (!starts_with(arg, "no-")) {
299 if (starts_with(long_name, "no-")) {
301 opt_flags |= OPT_UNSET;
307 if (!skip_prefix(arg + 3, long_name, &rest)) {
308 /* abbreviated and negated? */
309 if (starts_with(long_name, arg + 3))
320 return get_value(p, options, all_opts, flags ^ opt_flags);
323 if (ambiguous_option)
324 return error("Ambiguous option: %s "
325 "(could be --%s%s or --%s%s)",
327 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
328 ambiguous_option->long_name,
329 (abbrev_flags & OPT_UNSET) ? "no-" : "",
330 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 die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
428 parse_options_check(options);
432 * TODO: we are not completing the --no-XXX form yet because there are
433 * many options that do not suppress it properly.
435 static int show_gitcomp(struct parse_opt_ctx_t *ctx,
436 const struct option *opts)
438 for (; opts->type != OPTION_END; opts++) {
439 const char *suffix = "";
441 if (!opts->long_name)
443 if (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE))
446 switch (opts->type) {
450 case OPTION_FILENAME:
452 case OPTION_MAGNITUDE:
453 case OPTION_CALLBACK:
454 if (opts->flags & PARSE_OPT_NOARG)
456 if (opts->flags & PARSE_OPT_OPTARG)
458 if (opts->flags & PARSE_OPT_LASTARG_DEFAULT)
465 if (opts->flags & PARSE_OPT_COMP_ARG)
467 printf(" --%s%s", opts->long_name, suffix);
473 static int usage_with_options_internal(struct parse_opt_ctx_t *,
474 const char * const *,
475 const struct option *, int, int);
477 int parse_options_step(struct parse_opt_ctx_t *ctx,
478 const struct option *options,
479 const char * const usagestr[])
481 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
484 /* we must reset ->opt, unknown short option leave it dangling */
487 for (; ctx->argc; ctx->argc--, ctx->argv++) {
488 const char *arg = ctx->argv[0];
490 if (*arg != '-' || !arg[1]) {
491 if (parse_nodash_opt(ctx, arg, options) == 0)
493 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
494 return PARSE_OPT_NON_OPTION;
495 ctx->out[ctx->cpidx++] = ctx->argv[0];
499 /* lone -h asks for help */
500 if (internal_help && ctx->total == 1 && !strcmp(arg + 1, "h"))
503 /* lone --git-completion-helper is asked by git-completion.bash */
504 if (ctx->total == 1 && !strcmp(arg + 1, "-git-completion-helper"))
505 return show_gitcomp(ctx, options);
509 switch (parse_short_opt(ctx, options)) {
511 goto show_usage_error;
514 check_typos(arg + 1, options);
515 if (internal_help && *ctx->opt == 'h')
520 check_typos(arg + 1, options);
522 switch (parse_short_opt(ctx, options)) {
524 goto show_usage_error;
526 if (internal_help && *ctx->opt == 'h')
529 /* fake a short option thing to hide the fact that we may have
530 * started to parse aggregated stuff
532 * This is leaky, too bad.
534 ctx->argv[0] = xstrdup(ctx->opt - 1);
535 *(char *)ctx->argv[0] = '-';
542 if (!arg[2]) { /* "--" */
543 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
550 if (internal_help && !strcmp(arg + 2, "help-all"))
551 return usage_with_options_internal(ctx, usagestr, options, 1, 0);
552 if (internal_help && !strcmp(arg + 2, "help"))
554 switch (parse_long_opt(ctx, arg + 2, options)) {
556 goto show_usage_error;
562 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
563 return PARSE_OPT_UNKNOWN;
564 ctx->out[ctx->cpidx++] = ctx->argv[0];
567 return PARSE_OPT_DONE;
572 return usage_with_options_internal(ctx, usagestr, options, 0, err);
575 int parse_options_end(struct parse_opt_ctx_t *ctx)
577 MOVE_ARRAY(ctx->out + ctx->cpidx, ctx->argv, ctx->argc);
578 ctx->out[ctx->cpidx + ctx->argc] = NULL;
579 return ctx->cpidx + ctx->argc;
582 int parse_options(int argc, const char **argv, const char *prefix,
583 const struct option *options, const char * const usagestr[],
586 struct parse_opt_ctx_t ctx;
588 parse_options_start(&ctx, argc, argv, prefix, options, flags);
589 switch (parse_options_step(&ctx, options, usagestr)) {
592 case PARSE_OPT_NON_OPTION:
595 default: /* PARSE_OPT_UNKNOWN */
596 if (ctx.argv[0][1] == '-') {
597 error("unknown option `%s'", ctx.argv[0] + 2);
598 } else if (isascii(*ctx.opt)) {
599 error("unknown switch `%c'", *ctx.opt);
601 error("unknown non-ascii option in string: `%s'",
604 usage_with_options(usagestr, options);
607 precompose_argv(argc, argv);
608 return parse_options_end(&ctx);
611 static int usage_argh(const struct option *opts, FILE *outfile)
614 int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) || !opts->argh;
615 if (opts->flags & PARSE_OPT_OPTARG)
617 s = literal ? "[=%s]" : "[=<%s>]";
619 s = literal ? "[%s]" : "[<%s>]";
621 s = literal ? " %s" : " <%s>";
622 return utf8_fprintf(outfile, s, opts->argh ? _(opts->argh) : _("..."));
625 #define USAGE_OPTS_WIDTH 24
628 static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
629 const char * const *usagestr,
630 const struct option *opts, int full, int err)
632 FILE *outfile = err ? stderr : stdout;
636 return PARSE_OPT_HELP;
638 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
639 fprintf(outfile, "cat <<\\EOF\n");
641 fprintf_ln(outfile, _("usage: %s"), _(*usagestr++));
642 while (*usagestr && **usagestr)
644 * TRANSLATORS: the colon here should align with the
645 * one in "usage: %s" translation.
647 fprintf_ln(outfile, _(" or: %s"), _(*usagestr++));
650 fprintf_ln(outfile, _(" %s"), _(*usagestr));
652 fputc('\n', outfile);
658 for (; opts->type != OPTION_END; opts++) {
662 if (opts->type == OPTION_GROUP) {
663 fputc('\n', outfile);
666 fprintf(outfile, "%s\n", _(opts->help));
669 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
673 fputc('\n', outfile);
677 pos = fprintf(outfile, " ");
678 if (opts->short_name) {
679 if (opts->flags & PARSE_OPT_NODASH)
680 pos += fprintf(outfile, "%c", opts->short_name);
682 pos += fprintf(outfile, "-%c", opts->short_name);
684 if (opts->long_name && opts->short_name)
685 pos += fprintf(outfile, ", ");
687 pos += fprintf(outfile, "--%s", opts->long_name);
688 if (opts->type == OPTION_NUMBER)
689 pos += utf8_fprintf(outfile, _("-NUM"));
691 if ((opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
692 !(opts->flags & PARSE_OPT_NOARG))
693 pos += usage_argh(opts, outfile);
695 if (pos <= USAGE_OPTS_WIDTH)
696 pad = USAGE_OPTS_WIDTH - pos;
698 fputc('\n', outfile);
699 pad = USAGE_OPTS_WIDTH;
701 fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", _(opts->help));
703 fputc('\n', outfile);
705 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
706 fputs("EOF\n", outfile);
708 return PARSE_OPT_HELP;
711 void NORETURN usage_with_options(const char * const *usagestr,
712 const struct option *opts)
714 usage_with_options_internal(NULL, usagestr, opts, 0, 1);
718 void NORETURN usage_msg_opt(const char *msg,
719 const char * const *usagestr,
720 const struct option *options)
722 fprintf(stderr, "fatal: %s\n\n", msg);
723 usage_with_options(usagestr, options);
727 int opterror(const struct option *opt, const char *reason, int flags)
729 if (flags & OPT_SHORT)
730 return error("switch `%c' %s", opt->short_name, reason);
731 if (flags & OPT_UNSET)
732 return error("option `no-%s' %s", opt->long_name, reason);
733 return error("option `%s' %s", opt->long_name, reason);