1 #include "git-compat-util.h"
2 #include "parse-options.h"
8 static int parse_options_usage(struct parse_opt_ctx_t *ctx,
9 const char * const *usagestr,
10 const struct option *opts, int err);
15 int optbug(const struct option *opt, const char *reason)
19 return error("BUG: switch '%c' (--%s) %s",
20 opt->short_name, opt->long_name, reason);
21 return error("BUG: option '%s' %s", opt->long_name, reason);
23 return error("BUG: switch '%c' %s", opt->short_name, reason);
26 static int get_arg(struct parse_opt_ctx_t *p, const struct option *opt,
27 int flags, const char **arg)
32 } else if (p->argc == 1 && (opt->flags & PARSE_OPT_LASTARG_DEFAULT)) {
33 *arg = (const char *)opt->defval;
34 } else if (p->argc > 1) {
38 return opterror(opt, "requires a value", flags);
42 static void fix_filename(const char *prefix, const char **file)
44 if (!file || !*file || !prefix || is_absolute_path(*file)
45 || !strcmp("-", *file))
47 *file = xstrdup(prefix_filename(prefix, strlen(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 *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
121 *(int *)opt->value = unset ? 0 : opt->defval;
126 * Giving the same mode option twice, although is unnecessary,
127 * is not a grave error, so let it pass.
129 if (*(int *)opt->value && *(int *)opt->value != opt->defval)
130 return opt_command_mode_error(opt, all_opts, flags);
131 *(int *)opt->value = opt->defval;
136 *(const char **)opt->value = NULL;
137 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
138 *(const char **)opt->value = (const char *)opt->defval;
140 return get_arg(p, opt, flags, (const char **)opt->value);
143 case OPTION_FILENAME:
146 *(const char **)opt->value = NULL;
147 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
148 *(const char **)opt->value = (const char *)opt->defval;
150 err = get_arg(p, opt, flags, (const char **)opt->value);
153 fix_filename(p->prefix, (const char **)opt->value);
156 case OPTION_CALLBACK:
158 return (*opt->callback)(opt, NULL, 1) ? (-1) : 0;
159 if (opt->flags & PARSE_OPT_NOARG)
160 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
161 if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
162 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
163 if (get_arg(p, opt, flags, &arg))
165 return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
169 *(int *)opt->value = 0;
172 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
173 *(int *)opt->value = opt->defval;
176 if (get_arg(p, opt, flags, &arg))
178 *(int *)opt->value = strtol(arg, (char **)&s, 10);
180 return opterror(opt, "expects a numerical value", flags);
184 die("should not happen, someone must be hit on the forehead");
188 static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)
190 const struct option *all_opts = options;
191 const struct option *numopt = NULL;
193 for (; options->type != OPTION_END; options++) {
194 if (options->short_name == *p->opt) {
195 p->opt = p->opt[1] ? p->opt + 1 : NULL;
196 return get_value(p, options, all_opts, OPT_SHORT);
200 * Handle the numerical option later, explicit one-digit
201 * options take precedence over it.
203 if (options->type == OPTION_NUMBER)
206 if (numopt && isdigit(*p->opt)) {
211 while (isdigit(p->opt[len]))
213 arg = xmemdupz(p->opt, len);
214 p->opt = p->opt[len] ? p->opt + len : NULL;
215 rc = (*numopt->callback)(numopt, arg, 0) ? (-1) : 0;
222 static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
223 const struct option *options)
225 const struct option *all_opts = options;
226 const char *arg_end = strchrnul(arg, '=');
227 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
228 int abbrev_flags = 0, ambiguous_flags = 0;
230 for (; options->type != OPTION_END; options++) {
231 const char *rest, *long_name = options->long_name;
232 int flags = 0, opt_flags = 0;
238 if (!skip_prefix(arg, long_name, &rest))
240 if (options->type == OPTION_ARGUMENT) {
244 return opterror(options, "takes no value", flags);
247 p->out[p->cpidx++] = arg - 2;
252 if (!strncmp(long_name, arg, arg_end - arg)) {
256 * If this is abbreviated, it is
257 * ambiguous. So when there is no
258 * exact match later, we need to
261 ambiguous_option = abbrev_option;
262 ambiguous_flags = abbrev_flags;
264 if (!(flags & OPT_UNSET) && *arg_end)
265 p->opt = arg_end + 1;
266 abbrev_option = options;
267 abbrev_flags = flags ^ opt_flags;
270 /* negation allowed? */
271 if (options->flags & PARSE_OPT_NONEG)
273 /* negated and abbreviated very much? */
274 if (starts_with("no-", arg)) {
279 if (!starts_with(arg, "no-")) {
280 if (starts_with(long_name, "no-")) {
282 opt_flags |= OPT_UNSET;
288 if (!skip_prefix(arg + 3, long_name, &rest)) {
289 /* abbreviated and negated? */
290 if (starts_with(long_name, arg + 3))
301 return get_value(p, options, all_opts, flags ^ opt_flags);
304 if (ambiguous_option)
305 return error("Ambiguous option: %s "
306 "(could be --%s%s or --%s%s)",
308 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
309 ambiguous_option->long_name,
310 (abbrev_flags & OPT_UNSET) ? "no-" : "",
311 abbrev_option->long_name);
313 return get_value(p, abbrev_option, all_opts, abbrev_flags);
317 static int parse_nodash_opt(struct parse_opt_ctx_t *p, const char *arg,
318 const struct option *options)
320 const struct option *all_opts = options;
322 for (; options->type != OPTION_END; options++) {
323 if (!(options->flags & PARSE_OPT_NODASH))
325 if (options->short_name == arg[0] && arg[1] == '\0')
326 return get_value(p, options, all_opts, OPT_SHORT);
331 static void check_typos(const char *arg, const struct option *options)
336 if (starts_with(arg, "no-")) {
337 error ("did you mean `--%s` (with two dashes ?)", arg);
341 for (; options->type != OPTION_END; options++) {
342 if (!options->long_name)
344 if (starts_with(options->long_name, arg)) {
345 error ("did you mean `--%s` (with two dashes ?)", arg);
351 static void parse_options_check(const struct option *opts)
354 char short_opts[128];
356 memset(short_opts, '\0', sizeof(short_opts));
357 for (; opts->type != OPTION_END; opts++) {
358 if ((opts->flags & PARSE_OPT_LASTARG_DEFAULT) &&
359 (opts->flags & PARSE_OPT_OPTARG))
360 err |= optbug(opts, "uses incompatible flags "
361 "LASTARG_DEFAULT and OPTARG");
362 if (opts->short_name) {
363 if (0x7F <= opts->short_name)
364 err |= optbug(opts, "invalid short name");
365 else if (short_opts[opts->short_name]++)
366 err |= optbug(opts, "short name already used");
368 if (opts->flags & PARSE_OPT_NODASH &&
369 ((opts->flags & PARSE_OPT_OPTARG) ||
370 !(opts->flags & PARSE_OPT_NOARG) ||
371 !(opts->flags & PARSE_OPT_NONEG) ||
373 err |= optbug(opts, "uses feature "
374 "not supported for dashless options");
375 switch (opts->type) {
381 if ((opts->flags & PARSE_OPT_OPTARG) ||
382 !(opts->flags & PARSE_OPT_NOARG))
383 err |= optbug(opts, "should not accept an argument");
385 ; /* ok. (usually accepts an argument) */
388 strcspn(opts->argh, " _") != strlen(opts->argh))
389 err |= optbug(opts, "multi-word argh should use dash to separate words");
395 void parse_options_start(struct parse_opt_ctx_t *ctx,
396 int argc, const char **argv, const char *prefix,
397 const struct option *options, int flags)
399 memset(ctx, 0, sizeof(*ctx));
400 ctx->argc = argc - 1;
401 ctx->argv = argv + 1;
403 ctx->prefix = prefix;
404 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
406 if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
407 (flags & PARSE_OPT_STOP_AT_NON_OPTION))
408 die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
409 parse_options_check(options);
412 static int usage_with_options_internal(struct parse_opt_ctx_t *,
413 const char * const *,
414 const struct option *, int, int);
416 int parse_options_step(struct parse_opt_ctx_t *ctx,
417 const struct option *options,
418 const char * const usagestr[])
420 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
422 /* we must reset ->opt, unknown short option leave it dangling */
425 for (; ctx->argc; ctx->argc--, ctx->argv++) {
426 const char *arg = ctx->argv[0];
428 if (*arg != '-' || !arg[1]) {
429 if (parse_nodash_opt(ctx, arg, options) == 0)
431 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
432 return PARSE_OPT_NON_OPTION;
433 ctx->out[ctx->cpidx++] = ctx->argv[0];
439 if (internal_help && *ctx->opt == 'h')
440 return parse_options_usage(ctx, usagestr, options, 0);
441 switch (parse_short_opt(ctx, options)) {
443 return parse_options_usage(ctx, usagestr, options, 1);
446 check_typos(arg + 1, options);
450 check_typos(arg + 1, options);
452 if (internal_help && *ctx->opt == 'h')
453 return parse_options_usage(ctx, usagestr, options, 0);
454 switch (parse_short_opt(ctx, options)) {
456 return parse_options_usage(ctx, usagestr, options, 1);
458 /* fake a short option thing to hide the fact that we may have
459 * started to parse aggregated stuff
461 * This is leaky, too bad.
463 ctx->argv[0] = xstrdup(ctx->opt - 1);
464 *(char *)ctx->argv[0] = '-';
471 if (!arg[2]) { /* "--" */
472 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
479 if (internal_help && !strcmp(arg + 2, "help-all"))
480 return usage_with_options_internal(ctx, usagestr, options, 1, 0);
481 if (internal_help && !strcmp(arg + 2, "help"))
482 return parse_options_usage(ctx, usagestr, options, 0);
483 switch (parse_long_opt(ctx, arg + 2, options)) {
485 return parse_options_usage(ctx, usagestr, options, 1);
491 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
492 return PARSE_OPT_UNKNOWN;
493 ctx->out[ctx->cpidx++] = ctx->argv[0];
496 return PARSE_OPT_DONE;
499 int parse_options_end(struct parse_opt_ctx_t *ctx)
501 memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out));
502 ctx->out[ctx->cpidx + ctx->argc] = NULL;
503 return ctx->cpidx + ctx->argc;
506 int parse_options(int argc, const char **argv, const char *prefix,
507 const struct option *options, const char * const usagestr[],
510 struct parse_opt_ctx_t ctx;
512 parse_options_start(&ctx, argc, argv, prefix, options, flags);
513 switch (parse_options_step(&ctx, options, usagestr)) {
516 case PARSE_OPT_NON_OPTION:
519 default: /* PARSE_OPT_UNKNOWN */
520 if (ctx.argv[0][1] == '-') {
521 error("unknown option `%s'", ctx.argv[0] + 2);
522 } else if (isascii(*ctx.opt)) {
523 error("unknown switch `%c'", *ctx.opt);
525 error("unknown non-ascii option in string: `%s'",
528 usage_with_options(usagestr, options);
531 precompose_argv(argc, argv);
532 return parse_options_end(&ctx);
535 static int usage_argh(const struct option *opts, FILE *outfile)
538 int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) || !opts->argh;
539 if (opts->flags & PARSE_OPT_OPTARG)
541 s = literal ? "[=%s]" : "[=<%s>]";
543 s = literal ? "[%s]" : "[<%s>]";
545 s = literal ? " %s" : " <%s>";
546 return utf8_fprintf(outfile, s, opts->argh ? _(opts->argh) : _("..."));
549 #define USAGE_OPTS_WIDTH 24
552 static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
553 const char * const *usagestr,
554 const struct option *opts, int full, int err)
556 FILE *outfile = err ? stderr : stdout;
559 return PARSE_OPT_HELP;
561 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
562 fprintf(outfile, "cat <<\\EOF\n");
564 fprintf_ln(outfile, _("usage: %s"), _(*usagestr++));
565 while (*usagestr && **usagestr)
566 /* TRANSLATORS: the colon here should align with the
567 one in "usage: %s" translation */
568 fprintf_ln(outfile, _(" or: %s"), _(*usagestr++));
571 fprintf_ln(outfile, _(" %s"), _(*usagestr));
577 if (opts->type != OPTION_GROUP)
578 fputc('\n', outfile);
580 for (; opts->type != OPTION_END; opts++) {
584 if (opts->type == OPTION_GROUP) {
585 fputc('\n', outfile);
587 fprintf(outfile, "%s\n", _(opts->help));
590 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
593 pos = fprintf(outfile, " ");
594 if (opts->short_name) {
595 if (opts->flags & PARSE_OPT_NODASH)
596 pos += fprintf(outfile, "%c", opts->short_name);
598 pos += fprintf(outfile, "-%c", opts->short_name);
600 if (opts->long_name && opts->short_name)
601 pos += fprintf(outfile, ", ");
603 pos += fprintf(outfile, "--%s", opts->long_name);
604 if (opts->type == OPTION_NUMBER)
605 pos += utf8_fprintf(outfile, _("-NUM"));
607 if ((opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
608 !(opts->flags & PARSE_OPT_NOARG))
609 pos += usage_argh(opts, outfile);
611 if (pos <= USAGE_OPTS_WIDTH)
612 pad = USAGE_OPTS_WIDTH - pos;
614 fputc('\n', outfile);
615 pad = USAGE_OPTS_WIDTH;
617 fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", _(opts->help));
619 fputc('\n', outfile);
621 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
622 fputs("EOF\n", outfile);
624 return PARSE_OPT_HELP;
627 void NORETURN usage_with_options(const char * const *usagestr,
628 const struct option *opts)
630 usage_with_options_internal(NULL, usagestr, opts, 0, 1);
634 void NORETURN usage_msg_opt(const char *msg,
635 const char * const *usagestr,
636 const struct option *options)
638 fprintf(stderr, "%s\n\n", msg);
639 usage_with_options(usagestr, options);
642 static int parse_options_usage(struct parse_opt_ctx_t *ctx,
643 const char * const *usagestr,
644 const struct option *opts, int err)
646 return usage_with_options_internal(ctx, usagestr, opts, 0, err);
650 int opterror(const struct option *opt, const char *reason, int flags)
652 if (flags & OPT_SHORT)
653 return error("switch `%c' %s", opt->short_name, reason);
654 if (flags & OPT_UNSET)
655 return error("option `no-%s' %s", opt->long_name, reason);
656 return error("option `%s' %s", opt->long_name, reason);