1 #include "git-compat-util.h"
2 #include "parse-options.h"
11 int optbug(const struct option *opt, const char *reason)
15 return error("BUG: switch '%c' (--%s) %s",
16 opt->short_name, opt->long_name, reason);
17 return error("BUG: option '%s' %s", opt->long_name, reason);
19 return error("BUG: switch '%c' %s", opt->short_name, reason);
22 int opterror(const struct option *opt, const char *reason, int flags)
24 if (flags & OPT_SHORT)
25 return error("switch `%c' %s", opt->short_name, reason);
26 if (flags & OPT_UNSET)
27 return error("option `no-%s' %s", opt->long_name, reason);
28 return error("option `%s' %s", opt->long_name, reason);
31 static int get_arg(struct parse_opt_ctx_t *p, const struct option *opt,
32 int flags, const char **arg)
37 } else if (p->argc == 1 && (opt->flags & PARSE_OPT_LASTARG_DEFAULT)) {
38 *arg = (const char *)opt->defval;
39 } else if (p->argc > 1) {
43 opterror(opt, "requires a value", flags);
49 static void fix_filename(const char *prefix, const char **file)
51 if (!file || !*file || !prefix || is_absolute_path(*file)
52 || !strcmp("-", *file))
54 *file = xstrdup(prefix_filename(prefix, strlen(prefix), *file));
57 static int opt_command_mode_error(const struct option *opt,
58 const struct option *all_opts,
61 const struct option *that;
62 struct strbuf message = STRBUF_INIT;
63 struct strbuf that_name = STRBUF_INIT;
66 * Find the other option that was used to set the variable
67 * already, and report that this is not compatible with it.
69 for (that = all_opts; that->type != OPTION_END; that++) {
71 that->type != OPTION_CMDMODE ||
72 that->value != opt->value ||
73 that->defval != *(int *)opt->value)
77 strbuf_addf(&that_name, "--%s", that->long_name);
79 strbuf_addf(&that_name, "-%c", that->short_name);
80 strbuf_addf(&message, ": incompatible with %s", that_name.buf);
81 strbuf_release(&that_name);
82 opterror(opt, message.buf, flags);
83 strbuf_release(&message);
86 return opterror(opt, ": incompatible with something else", flags);
89 static int get_value(struct parse_opt_ctx_t *p,
90 const struct option *opt,
91 const struct option *all_opts,
95 const int unset = flags & OPT_UNSET;
99 return opterror(opt, "takes no value", flags);
100 if (unset && (opt->flags & PARSE_OPT_NONEG))
101 return opterror(opt, "isn't available", flags);
102 if (!(flags & OPT_SHORT) && p->opt && (opt->flags & PARSE_OPT_NOARG))
103 return opterror(opt, "takes no value", flags);
106 case OPTION_LOWLEVEL_CALLBACK:
107 return (*(parse_opt_ll_cb *)opt->callback)(p, opt, unset);
111 *(int *)opt->value &= ~opt->defval;
113 *(int *)opt->value |= opt->defval;
118 *(int *)opt->value |= opt->defval;
120 *(int *)opt->value &= ~opt->defval;
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 opterror(opt, "expects a numerical value", flags);
190 case OPTION_MAGNITUDE:
192 *(unsigned long *)opt->value = 0;
195 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
196 *(unsigned long *)opt->value = opt->defval;
199 if (get_arg(p, opt, flags, &arg))
201 if (!git_parse_ulong(arg, opt->value))
203 "expects a non-negative integer value with an optional k/m/g suffix",
208 die("should not happen, someone must be hit on the forehead");
212 static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)
214 const struct option *all_opts = options;
215 const struct option *numopt = NULL;
217 for (; options->type != OPTION_END; options++) {
218 if (options->short_name == *p->opt) {
219 p->opt = p->opt[1] ? p->opt + 1 : NULL;
220 return get_value(p, options, all_opts, OPT_SHORT);
224 * Handle the numerical option later, explicit one-digit
225 * options take precedence over it.
227 if (options->type == OPTION_NUMBER)
230 if (numopt && isdigit(*p->opt)) {
235 while (isdigit(p->opt[len]))
237 arg = xmemdupz(p->opt, len);
238 p->opt = p->opt[len] ? p->opt + len : NULL;
239 rc = (*numopt->callback)(numopt, arg, 0) ? (-1) : 0;
246 static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
247 const struct option *options)
249 const struct option *all_opts = options;
250 const char *arg_end = strchrnul(arg, '=');
251 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
252 int abbrev_flags = 0, ambiguous_flags = 0;
254 for (; options->type != OPTION_END; options++) {
255 const char *rest, *long_name = options->long_name;
256 int flags = 0, opt_flags = 0;
262 if (!skip_prefix(arg, long_name, &rest))
264 if (options->type == OPTION_ARGUMENT) {
268 return opterror(options, "takes no value", flags);
271 p->out[p->cpidx++] = arg - 2;
276 if (!strncmp(long_name, arg, arg_end - arg)) {
280 * If this is abbreviated, it is
281 * ambiguous. So when there is no
282 * exact match later, we need to
285 ambiguous_option = abbrev_option;
286 ambiguous_flags = abbrev_flags;
288 if (!(flags & OPT_UNSET) && *arg_end)
289 p->opt = arg_end + 1;
290 abbrev_option = options;
291 abbrev_flags = flags ^ opt_flags;
294 /* negation allowed? */
295 if (options->flags & PARSE_OPT_NONEG)
297 /* negated and abbreviated very much? */
298 if (starts_with("no-", arg)) {
303 if (!starts_with(arg, "no-")) {
304 if (starts_with(long_name, "no-")) {
306 opt_flags |= OPT_UNSET;
312 if (!skip_prefix(arg + 3, long_name, &rest)) {
313 /* abbreviated and negated? */
314 if (starts_with(long_name, arg + 3))
325 return get_value(p, options, all_opts, flags ^ opt_flags);
328 if (ambiguous_option)
329 return error("Ambiguous option: %s "
330 "(could be --%s%s or --%s%s)",
332 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
333 ambiguous_option->long_name,
334 (abbrev_flags & OPT_UNSET) ? "no-" : "",
335 abbrev_option->long_name);
337 return get_value(p, abbrev_option, all_opts, abbrev_flags);
341 static int parse_nodash_opt(struct parse_opt_ctx_t *p, const char *arg,
342 const struct option *options)
344 const struct option *all_opts = options;
346 for (; options->type != OPTION_END; options++) {
347 if (!(options->flags & PARSE_OPT_NODASH))
349 if (options->short_name == arg[0] && arg[1] == '\0')
350 return get_value(p, options, all_opts, OPT_SHORT);
355 static void check_typos(const char *arg, const struct option *options)
360 if (starts_with(arg, "no-")) {
361 error ("did you mean `--%s` (with two dashes ?)", arg);
365 for (; options->type != OPTION_END; options++) {
366 if (!options->long_name)
368 if (starts_with(options->long_name, arg)) {
369 error ("did you mean `--%s` (with two dashes ?)", arg);
375 static void parse_options_check(const struct option *opts)
378 char short_opts[128];
380 memset(short_opts, '\0', sizeof(short_opts));
381 for (; opts->type != OPTION_END; opts++) {
382 if ((opts->flags & PARSE_OPT_LASTARG_DEFAULT) &&
383 (opts->flags & PARSE_OPT_OPTARG))
384 err |= optbug(opts, "uses incompatible flags "
385 "LASTARG_DEFAULT and OPTARG");
386 if (opts->short_name) {
387 if (0x7F <= opts->short_name)
388 err |= optbug(opts, "invalid short name");
389 else if (short_opts[opts->short_name]++)
390 err |= optbug(opts, "short name already used");
392 if (opts->flags & PARSE_OPT_NODASH &&
393 ((opts->flags & PARSE_OPT_OPTARG) ||
394 !(opts->flags & PARSE_OPT_NOARG) ||
395 !(opts->flags & PARSE_OPT_NONEG) ||
397 err |= optbug(opts, "uses feature "
398 "not supported for dashless options");
399 switch (opts->type) {
405 if ((opts->flags & PARSE_OPT_OPTARG) ||
406 !(opts->flags & PARSE_OPT_NOARG))
407 err |= optbug(opts, "should not accept an argument");
409 ; /* ok. (usually accepts an argument) */
412 strcspn(opts->argh, " _") != strlen(opts->argh))
413 err |= optbug(opts, "multi-word argh should use dash to separate words");
419 void parse_options_start(struct parse_opt_ctx_t *ctx,
420 int argc, const char **argv, const char *prefix,
421 const struct option *options, int flags)
423 memset(ctx, 0, sizeof(*ctx));
424 ctx->argc = ctx->total = argc - 1;
425 ctx->argv = argv + 1;
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 die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
433 parse_options_check(options);
436 static int usage_with_options_internal(struct parse_opt_ctx_t *,
437 const char * const *,
438 const struct option *, int, int);
440 int parse_options_step(struct parse_opt_ctx_t *ctx,
441 const struct option *options,
442 const char * const usagestr[])
444 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
447 /* we must reset ->opt, unknown short option leave it dangling */
450 for (; ctx->argc; ctx->argc--, ctx->argv++) {
451 const char *arg = ctx->argv[0];
453 if (*arg != '-' || !arg[1]) {
454 if (parse_nodash_opt(ctx, arg, options) == 0)
456 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
457 return PARSE_OPT_NON_OPTION;
458 ctx->out[ctx->cpidx++] = ctx->argv[0];
462 /* lone -h asks for help */
463 if (internal_help && ctx->total == 1 && !strcmp(arg + 1, "h"))
468 switch (parse_short_opt(ctx, options)) {
470 goto show_usage_error;
473 check_typos(arg + 1, options);
474 if (internal_help && *ctx->opt == 'h')
479 check_typos(arg + 1, options);
481 switch (parse_short_opt(ctx, options)) {
483 goto show_usage_error;
485 if (internal_help && *ctx->opt == 'h')
488 /* fake a short option thing to hide the fact that we may have
489 * started to parse aggregated stuff
491 * This is leaky, too bad.
493 ctx->argv[0] = xstrdup(ctx->opt - 1);
494 *(char *)ctx->argv[0] = '-';
501 if (!arg[2]) { /* "--" */
502 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
509 if (internal_help && !strcmp(arg + 2, "help-all"))
510 return usage_with_options_internal(ctx, usagestr, options, 1, 0);
511 if (internal_help && !strcmp(arg + 2, "help"))
513 switch (parse_long_opt(ctx, arg + 2, options)) {
515 goto show_usage_error;
521 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
522 return PARSE_OPT_UNKNOWN;
523 ctx->out[ctx->cpidx++] = ctx->argv[0];
526 return PARSE_OPT_DONE;
531 return usage_with_options_internal(ctx, usagestr, options, 0, err);
534 int parse_options_end(struct parse_opt_ctx_t *ctx)
536 memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out));
537 ctx->out[ctx->cpidx + ctx->argc] = NULL;
538 return ctx->cpidx + ctx->argc;
541 int parse_options(int argc, const char **argv, const char *prefix,
542 const struct option *options, const char * const usagestr[],
545 struct parse_opt_ctx_t ctx;
547 parse_options_start(&ctx, argc, argv, prefix, options, flags);
548 switch (parse_options_step(&ctx, options, usagestr)) {
551 case PARSE_OPT_NON_OPTION:
554 default: /* PARSE_OPT_UNKNOWN */
555 if (ctx.argv[0][1] == '-') {
556 error("unknown option `%s'", ctx.argv[0] + 2);
557 } else if (isascii(*ctx.opt)) {
558 error("unknown switch `%c'", *ctx.opt);
560 error("unknown non-ascii option in string: `%s'",
563 usage_with_options(usagestr, options);
566 precompose_argv(argc, argv);
567 return parse_options_end(&ctx);
570 static int usage_argh(const struct option *opts, FILE *outfile)
573 int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) || !opts->argh;
574 if (opts->flags & PARSE_OPT_OPTARG)
576 s = literal ? "[=%s]" : "[=<%s>]";
578 s = literal ? "[%s]" : "[<%s>]";
580 s = literal ? " %s" : " <%s>";
581 return utf8_fprintf(outfile, s, opts->argh ? _(opts->argh) : _("..."));
584 #define USAGE_OPTS_WIDTH 24
587 static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
588 const char * const *usagestr,
589 const struct option *opts, int full, int err)
591 FILE *outfile = err ? stderr : stdout;
594 return PARSE_OPT_HELP;
596 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
597 fprintf(outfile, "cat <<\\EOF\n");
599 fprintf_ln(outfile, _("usage: %s"), _(*usagestr++));
600 while (*usagestr && **usagestr)
601 /* TRANSLATORS: the colon here should align with the
602 one in "usage: %s" translation */
603 fprintf_ln(outfile, _(" or: %s"), _(*usagestr++));
606 fprintf_ln(outfile, _(" %s"), _(*usagestr));
612 if (opts->type != OPTION_GROUP)
613 fputc('\n', outfile);
615 for (; opts->type != OPTION_END; opts++) {
619 if (opts->type == OPTION_GROUP) {
620 fputc('\n', outfile);
622 fprintf(outfile, "%s\n", _(opts->help));
625 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
628 pos = fprintf(outfile, " ");
629 if (opts->short_name) {
630 if (opts->flags & PARSE_OPT_NODASH)
631 pos += fprintf(outfile, "%c", opts->short_name);
633 pos += fprintf(outfile, "-%c", opts->short_name);
635 if (opts->long_name && opts->short_name)
636 pos += fprintf(outfile, ", ");
638 pos += fprintf(outfile, "--%s", opts->long_name);
639 if (opts->type == OPTION_NUMBER)
640 pos += utf8_fprintf(outfile, _("-NUM"));
642 if ((opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
643 !(opts->flags & PARSE_OPT_NOARG))
644 pos += usage_argh(opts, outfile);
646 if (pos <= USAGE_OPTS_WIDTH)
647 pad = USAGE_OPTS_WIDTH - pos;
649 fputc('\n', outfile);
650 pad = USAGE_OPTS_WIDTH;
652 fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", _(opts->help));
654 fputc('\n', outfile);
656 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
657 fputs("EOF\n", outfile);
659 return PARSE_OPT_HELP;
662 void NORETURN usage_with_options(const char * const *usagestr,
663 const struct option *opts)
665 usage_with_options_internal(NULL, usagestr, opts, 0, 1);
669 void NORETURN usage_msg_opt(const char *msg,
670 const char * const *usagestr,
671 const struct option *options)
673 fprintf(stderr, "%s\n\n", msg);
674 usage_with_options(usagestr, options);