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 static int get_arg(struct parse_opt_ctx_t *p, const struct option *opt,
23 int flags, const char **arg)
28 } else if (p->argc == 1 && (opt->flags & PARSE_OPT_LASTARG_DEFAULT)) {
29 *arg = (const char *)opt->defval;
30 } else if (p->argc > 1) {
34 return opterror(opt, "requires a value", flags);
38 static void fix_filename(const char *prefix, const char **file)
40 if (!file || !*file || !prefix || is_absolute_path(*file)
41 || !strcmp("-", *file))
43 *file = xstrdup(prefix_filename(prefix, strlen(prefix), *file));
46 static int opt_command_mode_error(const struct option *opt,
47 const struct option *all_opts,
50 const struct option *that;
51 struct strbuf message = STRBUF_INIT;
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 strbuf_addf(&message, ": incompatible with %s", that_name.buf);
70 strbuf_release(&that_name);
71 opterror(opt, message.buf, flags);
72 strbuf_release(&message);
75 return opterror(opt, ": incompatible with something else", flags);
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 opterror(opt, "takes no value", flags);
89 if (unset && (opt->flags & PARSE_OPT_NONEG))
90 return opterror(opt, "isn't available", flags);
91 if (!(flags & OPT_SHORT) && p->opt && (opt->flags & PARSE_OPT_NOARG))
92 return opterror(opt, "takes no value", 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 *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
117 *(int *)opt->value = unset ? 0 : opt->defval;
122 * Giving the same mode option twice, although is unnecessary,
123 * is not a grave error, so let it pass.
125 if (*(int *)opt->value && *(int *)opt->value != opt->defval)
126 return opt_command_mode_error(opt, all_opts, flags);
127 *(int *)opt->value = opt->defval;
132 *(const char **)opt->value = NULL;
133 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
134 *(const char **)opt->value = (const char *)opt->defval;
136 return get_arg(p, opt, flags, (const char **)opt->value);
139 case OPTION_FILENAME:
142 *(const char **)opt->value = NULL;
143 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
144 *(const char **)opt->value = (const char *)opt->defval;
146 err = get_arg(p, opt, flags, (const char **)opt->value);
149 fix_filename(p->prefix, (const char **)opt->value);
152 case OPTION_CALLBACK:
154 return (*opt->callback)(opt, NULL, 1) ? (-1) : 0;
155 if (opt->flags & PARSE_OPT_NOARG)
156 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
157 if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
158 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
159 if (get_arg(p, opt, flags, &arg))
161 return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
165 *(int *)opt->value = 0;
168 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
169 *(int *)opt->value = opt->defval;
172 if (get_arg(p, opt, flags, &arg))
174 *(int *)opt->value = strtol(arg, (char **)&s, 10);
176 return opterror(opt, "expects a numerical value", flags);
179 case OPTION_MAGNITUDE:
181 *(unsigned long *)opt->value = 0;
184 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
185 *(unsigned long *)opt->value = opt->defval;
188 if (get_arg(p, opt, flags, &arg))
190 if (!git_parse_ulong(arg, opt->value))
192 "expects a non-negative integer value with an optional k/m/g suffix",
197 die("should not happen, someone must be hit on the forehead");
201 static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)
203 const struct option *all_opts = options;
204 const struct option *numopt = NULL;
206 for (; options->type != OPTION_END; options++) {
207 if (options->short_name == *p->opt) {
208 p->opt = p->opt[1] ? p->opt + 1 : NULL;
209 return get_value(p, options, all_opts, OPT_SHORT);
213 * Handle the numerical option later, explicit one-digit
214 * options take precedence over it.
216 if (options->type == OPTION_NUMBER)
219 if (numopt && isdigit(*p->opt)) {
224 while (isdigit(p->opt[len]))
226 arg = xmemdupz(p->opt, len);
227 p->opt = p->opt[len] ? p->opt + len : NULL;
228 rc = (*numopt->callback)(numopt, arg, 0) ? (-1) : 0;
235 static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
236 const struct option *options)
238 const struct option *all_opts = options;
239 const char *arg_end = strchrnul(arg, '=');
240 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
241 int abbrev_flags = 0, ambiguous_flags = 0;
243 for (; options->type != OPTION_END; options++) {
244 const char *rest, *long_name = options->long_name;
245 int flags = 0, opt_flags = 0;
251 if (!skip_prefix(arg, long_name, &rest))
253 if (options->type == OPTION_ARGUMENT) {
257 return opterror(options, "takes no value", flags);
260 p->out[p->cpidx++] = arg - 2;
265 if (!strncmp(long_name, arg, arg_end - arg)) {
269 * If this is abbreviated, it is
270 * ambiguous. So when there is no
271 * exact match later, we need to
274 ambiguous_option = abbrev_option;
275 ambiguous_flags = abbrev_flags;
277 if (!(flags & OPT_UNSET) && *arg_end)
278 p->opt = arg_end + 1;
279 abbrev_option = options;
280 abbrev_flags = flags ^ opt_flags;
283 /* negation allowed? */
284 if (options->flags & PARSE_OPT_NONEG)
286 /* negated and abbreviated very much? */
287 if (starts_with("no-", arg)) {
292 if (!starts_with(arg, "no-")) {
293 if (starts_with(long_name, "no-")) {
295 opt_flags |= OPT_UNSET;
301 if (!skip_prefix(arg + 3, long_name, &rest)) {
302 /* abbreviated and negated? */
303 if (starts_with(long_name, arg + 3))
314 return get_value(p, options, all_opts, flags ^ opt_flags);
317 if (ambiguous_option)
318 return error("Ambiguous option: %s "
319 "(could be --%s%s or --%s%s)",
321 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
322 ambiguous_option->long_name,
323 (abbrev_flags & OPT_UNSET) ? "no-" : "",
324 abbrev_option->long_name);
326 return get_value(p, abbrev_option, all_opts, abbrev_flags);
330 static int parse_nodash_opt(struct parse_opt_ctx_t *p, const char *arg,
331 const struct option *options)
333 const struct option *all_opts = options;
335 for (; options->type != OPTION_END; options++) {
336 if (!(options->flags & PARSE_OPT_NODASH))
338 if (options->short_name == arg[0] && arg[1] == '\0')
339 return get_value(p, options, all_opts, OPT_SHORT);
344 static void check_typos(const char *arg, const struct option *options)
349 if (starts_with(arg, "no-")) {
350 error ("did you mean `--%s` (with two dashes ?)", arg);
354 for (; options->type != OPTION_END; options++) {
355 if (!options->long_name)
357 if (starts_with(options->long_name, arg)) {
358 error ("did you mean `--%s` (with two dashes ?)", arg);
364 static void parse_options_check(const struct option *opts)
367 char short_opts[128];
369 memset(short_opts, '\0', sizeof(short_opts));
370 for (; opts->type != OPTION_END; opts++) {
371 if ((opts->flags & PARSE_OPT_LASTARG_DEFAULT) &&
372 (opts->flags & PARSE_OPT_OPTARG))
373 err |= optbug(opts, "uses incompatible flags "
374 "LASTARG_DEFAULT and OPTARG");
375 if (opts->short_name) {
376 if (0x7F <= opts->short_name)
377 err |= optbug(opts, "invalid short name");
378 else if (short_opts[opts->short_name]++)
379 err |= optbug(opts, "short name already used");
381 if (opts->flags & PARSE_OPT_NODASH &&
382 ((opts->flags & PARSE_OPT_OPTARG) ||
383 !(opts->flags & PARSE_OPT_NOARG) ||
384 !(opts->flags & PARSE_OPT_NONEG) ||
386 err |= optbug(opts, "uses feature "
387 "not supported for dashless options");
388 switch (opts->type) {
394 if ((opts->flags & PARSE_OPT_OPTARG) ||
395 !(opts->flags & PARSE_OPT_NOARG))
396 err |= optbug(opts, "should not accept an argument");
398 ; /* ok. (usually accepts an argument) */
401 strcspn(opts->argh, " _") != strlen(opts->argh))
402 err |= optbug(opts, "multi-word argh should use dash to separate words");
408 void parse_options_start(struct parse_opt_ctx_t *ctx,
409 int argc, const char **argv, const char *prefix,
410 const struct option *options, int flags)
412 memset(ctx, 0, sizeof(*ctx));
413 ctx->argc = ctx->total = argc - 1;
414 ctx->argv = argv + 1;
416 ctx->prefix = prefix;
417 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
419 if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
420 (flags & PARSE_OPT_STOP_AT_NON_OPTION))
421 die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
422 parse_options_check(options);
425 static int usage_with_options_internal(struct parse_opt_ctx_t *,
426 const char * const *,
427 const struct option *, int, int);
429 int parse_options_step(struct parse_opt_ctx_t *ctx,
430 const struct option *options,
431 const char * const usagestr[])
433 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
436 /* we must reset ->opt, unknown short option leave it dangling */
439 for (; ctx->argc; ctx->argc--, ctx->argv++) {
440 const char *arg = ctx->argv[0];
442 if (*arg != '-' || !arg[1]) {
443 if (parse_nodash_opt(ctx, arg, options) == 0)
445 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
446 return PARSE_OPT_NON_OPTION;
447 ctx->out[ctx->cpidx++] = ctx->argv[0];
451 /* lone -h asks for help */
452 if (internal_help && ctx->total == 1 && !strcmp(arg + 1, "h"))
457 switch (parse_short_opt(ctx, options)) {
459 goto show_usage_error;
462 check_typos(arg + 1, options);
463 if (internal_help && *ctx->opt == 'h')
468 check_typos(arg + 1, options);
470 switch (parse_short_opt(ctx, options)) {
472 goto show_usage_error;
474 if (internal_help && *ctx->opt == 'h')
477 /* fake a short option thing to hide the fact that we may have
478 * started to parse aggregated stuff
480 * This is leaky, too bad.
482 ctx->argv[0] = xstrdup(ctx->opt - 1);
483 *(char *)ctx->argv[0] = '-';
490 if (!arg[2]) { /* "--" */
491 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
498 if (internal_help && !strcmp(arg + 2, "help-all"))
499 return usage_with_options_internal(ctx, usagestr, options, 1, 0);
500 if (internal_help && !strcmp(arg + 2, "help"))
502 switch (parse_long_opt(ctx, arg + 2, options)) {
504 goto show_usage_error;
510 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
511 return PARSE_OPT_UNKNOWN;
512 ctx->out[ctx->cpidx++] = ctx->argv[0];
515 return PARSE_OPT_DONE;
520 return usage_with_options_internal(ctx, usagestr, options, 0, err);
523 int parse_options_end(struct parse_opt_ctx_t *ctx)
525 memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out));
526 ctx->out[ctx->cpidx + ctx->argc] = NULL;
527 return ctx->cpidx + ctx->argc;
530 int parse_options(int argc, const char **argv, const char *prefix,
531 const struct option *options, const char * const usagestr[],
534 struct parse_opt_ctx_t ctx;
536 parse_options_start(&ctx, argc, argv, prefix, options, flags);
537 switch (parse_options_step(&ctx, options, usagestr)) {
540 case PARSE_OPT_NON_OPTION:
543 default: /* PARSE_OPT_UNKNOWN */
544 if (ctx.argv[0][1] == '-') {
545 error("unknown option `%s'", ctx.argv[0] + 2);
546 } else if (isascii(*ctx.opt)) {
547 error("unknown switch `%c'", *ctx.opt);
549 error("unknown non-ascii option in string: `%s'",
552 usage_with_options(usagestr, options);
555 precompose_argv(argc, argv);
556 return parse_options_end(&ctx);
559 static int usage_argh(const struct option *opts, FILE *outfile)
562 int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) || !opts->argh;
563 if (opts->flags & PARSE_OPT_OPTARG)
565 s = literal ? "[=%s]" : "[=<%s>]";
567 s = literal ? "[%s]" : "[<%s>]";
569 s = literal ? " %s" : " <%s>";
570 return utf8_fprintf(outfile, s, opts->argh ? _(opts->argh) : _("..."));
573 #define USAGE_OPTS_WIDTH 24
576 static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
577 const char * const *usagestr,
578 const struct option *opts, int full, int err)
580 FILE *outfile = err ? stderr : stdout;
583 return PARSE_OPT_HELP;
585 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
586 fprintf(outfile, "cat <<\\EOF\n");
588 fprintf_ln(outfile, _("usage: %s"), _(*usagestr++));
589 while (*usagestr && **usagestr)
590 /* TRANSLATORS: the colon here should align with the
591 one in "usage: %s" translation */
592 fprintf_ln(outfile, _(" or: %s"), _(*usagestr++));
595 fprintf_ln(outfile, _(" %s"), _(*usagestr));
601 if (opts->type != OPTION_GROUP)
602 fputc('\n', outfile);
604 for (; opts->type != OPTION_END; opts++) {
608 if (opts->type == OPTION_GROUP) {
609 fputc('\n', outfile);
611 fprintf(outfile, "%s\n", _(opts->help));
614 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
617 pos = fprintf(outfile, " ");
618 if (opts->short_name) {
619 if (opts->flags & PARSE_OPT_NODASH)
620 pos += fprintf(outfile, "%c", opts->short_name);
622 pos += fprintf(outfile, "-%c", opts->short_name);
624 if (opts->long_name && opts->short_name)
625 pos += fprintf(outfile, ", ");
627 pos += fprintf(outfile, "--%s", opts->long_name);
628 if (opts->type == OPTION_NUMBER)
629 pos += utf8_fprintf(outfile, _("-NUM"));
631 if ((opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
632 !(opts->flags & PARSE_OPT_NOARG))
633 pos += usage_argh(opts, outfile);
635 if (pos <= USAGE_OPTS_WIDTH)
636 pad = USAGE_OPTS_WIDTH - pos;
638 fputc('\n', outfile);
639 pad = USAGE_OPTS_WIDTH;
641 fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", _(opts->help));
643 fputc('\n', outfile);
645 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
646 fputs("EOF\n", outfile);
648 return PARSE_OPT_HELP;
651 void NORETURN usage_with_options(const char * const *usagestr,
652 const struct option *opts)
654 usage_with_options_internal(NULL, usagestr, opts, 0, 1);
658 void NORETURN usage_msg_opt(const char *msg,
659 const char * const *usagestr,
660 const struct option *options)
662 fprintf(stderr, "%s\n\n", msg);
663 usage_with_options(usagestr, options);
667 int opterror(const struct option *opt, const char *reason, int flags)
669 if (flags & OPT_SHORT)
670 return error("switch `%c' %s", opt->short_name, reason);
671 if (flags & OPT_UNSET)
672 return error("option `no-%s' %s", opt->long_name, reason);
673 return error("option `%s' %s", opt->long_name, reason);