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)
18 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);
191 die("should not happen, someone must be hit on the forehead");
195 static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)
197 const struct option *all_opts = options;
198 const struct option *numopt = NULL;
200 for (; options->type != OPTION_END; options++) {
201 if (options->short_name == *p->opt) {
202 p->opt = p->opt[1] ? p->opt + 1 : NULL;
203 return get_value(p, options, all_opts, OPT_SHORT);
207 * Handle the numerical option later, explicit one-digit
208 * options take precedence over it.
210 if (options->type == OPTION_NUMBER)
213 if (numopt && isdigit(*p->opt)) {
218 while (isdigit(p->opt[len]))
220 arg = xmemdupz(p->opt, len);
221 p->opt = p->opt[len] ? p->opt + len : NULL;
222 rc = (*numopt->callback)(numopt, arg, 0) ? (-1) : 0;
229 static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
230 const struct option *options)
232 const struct option *all_opts = options;
233 const char *arg_end = strchrnul(arg, '=');
234 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
235 int abbrev_flags = 0, ambiguous_flags = 0;
237 for (; options->type != OPTION_END; options++) {
238 const char *rest, *long_name = options->long_name;
239 int flags = 0, opt_flags = 0;
245 rest = skip_prefix(arg, long_name);
246 if (options->type == OPTION_ARGUMENT) {
250 return opterror(options, "takes no value", flags);
253 p->out[p->cpidx++] = arg - 2;
258 if (!strncmp(long_name, arg, arg_end - arg)) {
262 * If this is abbreviated, it is
263 * ambiguous. So when there is no
264 * exact match later, we need to
267 ambiguous_option = abbrev_option;
268 ambiguous_flags = abbrev_flags;
270 if (!(flags & OPT_UNSET) && *arg_end)
271 p->opt = arg_end + 1;
272 abbrev_option = options;
273 abbrev_flags = flags ^ opt_flags;
276 /* negation allowed? */
277 if (options->flags & PARSE_OPT_NONEG)
279 /* negated and abbreviated very much? */
280 if (starts_with("no-", arg)) {
285 if (!starts_with(arg, "no-")) {
286 if (starts_with(long_name, "no-")) {
288 opt_flags |= OPT_UNSET;
294 rest = skip_prefix(arg + 3, long_name);
295 /* abbreviated and negated? */
296 if (!rest && starts_with(long_name, arg + 3))
306 return get_value(p, options, all_opts, flags ^ opt_flags);
309 if (ambiguous_option)
310 return error("Ambiguous option: %s "
311 "(could be --%s%s or --%s%s)",
313 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
314 ambiguous_option->long_name,
315 (abbrev_flags & OPT_UNSET) ? "no-" : "",
316 abbrev_option->long_name);
318 return get_value(p, abbrev_option, all_opts, abbrev_flags);
322 static int parse_nodash_opt(struct parse_opt_ctx_t *p, const char *arg,
323 const struct option *options)
325 const struct option *all_opts = options;
327 for (; options->type != OPTION_END; options++) {
328 if (!(options->flags & PARSE_OPT_NODASH))
330 if (options->short_name == arg[0] && arg[1] == '\0')
331 return get_value(p, options, all_opts, OPT_SHORT);
336 static void check_typos(const char *arg, const struct option *options)
341 if (starts_with(arg, "no-")) {
342 error ("did you mean `--%s` (with two dashes ?)", arg);
346 for (; options->type != OPTION_END; options++) {
347 if (!options->long_name)
349 if (starts_with(options->long_name, arg)) {
350 error ("did you mean `--%s` (with two dashes ?)", arg);
356 static void parse_options_check(const struct option *opts)
360 for (; opts->type != OPTION_END; opts++) {
361 if ((opts->flags & PARSE_OPT_LASTARG_DEFAULT) &&
362 (opts->flags & PARSE_OPT_OPTARG))
363 err |= optbug(opts, "uses incompatible flags "
364 "LASTARG_DEFAULT and OPTARG");
365 if (opts->flags & PARSE_OPT_NODASH &&
366 ((opts->flags & PARSE_OPT_OPTARG) ||
367 !(opts->flags & PARSE_OPT_NOARG) ||
368 !(opts->flags & PARSE_OPT_NONEG) ||
370 err |= optbug(opts, "uses feature "
371 "not supported for dashless options");
372 switch (opts->type) {
378 if ((opts->flags & PARSE_OPT_OPTARG) ||
379 !(opts->flags & PARSE_OPT_NOARG))
380 err |= optbug(opts, "should not accept an argument");
382 ; /* ok. (usually accepts an argument) */
385 strcspn(opts->argh, " _") != strlen(opts->argh))
386 err |= optbug(opts, "multi-word argh should use dash to separate words");
392 void parse_options_start(struct parse_opt_ctx_t *ctx,
393 int argc, const char **argv, const char *prefix,
394 const struct option *options, int flags)
396 memset(ctx, 0, sizeof(*ctx));
397 ctx->argc = argc - 1;
398 ctx->argv = argv + 1;
400 ctx->prefix = prefix;
401 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
403 if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
404 (flags & PARSE_OPT_STOP_AT_NON_OPTION))
405 die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
406 parse_options_check(options);
409 static int usage_with_options_internal(struct parse_opt_ctx_t *,
410 const char * const *,
411 const struct option *, int, int);
413 int parse_options_step(struct parse_opt_ctx_t *ctx,
414 const struct option *options,
415 const char * const usagestr[])
417 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
419 /* we must reset ->opt, unknown short option leave it dangling */
422 for (; ctx->argc; ctx->argc--, ctx->argv++) {
423 const char *arg = ctx->argv[0];
425 if (*arg != '-' || !arg[1]) {
426 if (parse_nodash_opt(ctx, arg, options) == 0)
428 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
429 return PARSE_OPT_NON_OPTION;
430 ctx->out[ctx->cpidx++] = ctx->argv[0];
436 if (internal_help && *ctx->opt == 'h')
437 return parse_options_usage(ctx, usagestr, options, 0);
438 switch (parse_short_opt(ctx, options)) {
440 return parse_options_usage(ctx, usagestr, options, 1);
443 check_typos(arg + 1, options);
447 check_typos(arg + 1, options);
449 if (internal_help && *ctx->opt == 'h')
450 return parse_options_usage(ctx, usagestr, options, 0);
451 switch (parse_short_opt(ctx, options)) {
453 return parse_options_usage(ctx, usagestr, options, 1);
455 /* fake a short option thing to hide the fact that we may have
456 * started to parse aggregated stuff
458 * This is leaky, too bad.
460 ctx->argv[0] = xstrdup(ctx->opt - 1);
461 *(char *)ctx->argv[0] = '-';
468 if (!arg[2]) { /* "--" */
469 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
476 if (internal_help && !strcmp(arg + 2, "help-all"))
477 return usage_with_options_internal(ctx, usagestr, options, 1, 0);
478 if (internal_help && !strcmp(arg + 2, "help"))
479 return parse_options_usage(ctx, usagestr, options, 0);
480 switch (parse_long_opt(ctx, arg + 2, options)) {
482 return parse_options_usage(ctx, usagestr, options, 1);
488 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
489 return PARSE_OPT_UNKNOWN;
490 ctx->out[ctx->cpidx++] = ctx->argv[0];
493 return PARSE_OPT_DONE;
496 int parse_options_end(struct parse_opt_ctx_t *ctx)
498 memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out));
499 ctx->out[ctx->cpidx + ctx->argc] = NULL;
500 return ctx->cpidx + ctx->argc;
503 int parse_options(int argc, const char **argv, const char *prefix,
504 const struct option *options, const char * const usagestr[],
507 struct parse_opt_ctx_t ctx;
509 parse_options_start(&ctx, argc, argv, prefix, options, flags);
510 switch (parse_options_step(&ctx, options, usagestr)) {
513 case PARSE_OPT_NON_OPTION:
516 default: /* PARSE_OPT_UNKNOWN */
517 if (ctx.argv[0][1] == '-') {
518 error("unknown option `%s'", ctx.argv[0] + 2);
519 } else if (isascii(*ctx.opt)) {
520 error("unknown switch `%c'", *ctx.opt);
522 error("unknown non-ascii option in string: `%s'",
525 usage_with_options(usagestr, options);
528 precompose_argv(argc, argv);
529 return parse_options_end(&ctx);
532 static int usage_argh(const struct option *opts, FILE *outfile)
535 int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) || !opts->argh;
536 if (opts->flags & PARSE_OPT_OPTARG)
538 s = literal ? "[=%s]" : "[=<%s>]";
540 s = literal ? "[%s]" : "[<%s>]";
542 s = literal ? " %s" : " <%s>";
543 return utf8_fprintf(outfile, s, opts->argh ? _(opts->argh) : _("..."));
546 #define USAGE_OPTS_WIDTH 24
549 static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
550 const char * const *usagestr,
551 const struct option *opts, int full, int err)
553 FILE *outfile = err ? stderr : stdout;
556 return PARSE_OPT_HELP;
558 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
559 fprintf(outfile, "cat <<\\EOF\n");
561 fprintf_ln(outfile, _("usage: %s"), _(*usagestr++));
562 while (*usagestr && **usagestr)
563 /* TRANSLATORS: the colon here should align with the
564 one in "usage: %s" translation */
565 fprintf_ln(outfile, _(" or: %s"), _(*usagestr++));
568 fprintf_ln(outfile, _(" %s"), _(*usagestr));
574 if (opts->type != OPTION_GROUP)
575 fputc('\n', outfile);
577 for (; opts->type != OPTION_END; opts++) {
581 if (opts->type == OPTION_GROUP) {
582 fputc('\n', outfile);
584 fprintf(outfile, "%s\n", _(opts->help));
587 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
590 pos = fprintf(outfile, " ");
591 if (opts->short_name) {
592 if (opts->flags & PARSE_OPT_NODASH)
593 pos += fprintf(outfile, "%c", opts->short_name);
595 pos += fprintf(outfile, "-%c", opts->short_name);
597 if (opts->long_name && opts->short_name)
598 pos += fprintf(outfile, ", ");
600 pos += fprintf(outfile, "--%s", opts->long_name);
601 if (opts->type == OPTION_NUMBER)
602 pos += utf8_fprintf(outfile, _("-NUM"));
604 if ((opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
605 !(opts->flags & PARSE_OPT_NOARG))
606 pos += usage_argh(opts, outfile);
608 if (pos <= USAGE_OPTS_WIDTH)
609 pad = USAGE_OPTS_WIDTH - pos;
611 fputc('\n', outfile);
612 pad = USAGE_OPTS_WIDTH;
614 fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", _(opts->help));
616 fputc('\n', outfile);
618 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
619 fputs("EOF\n", outfile);
621 return PARSE_OPT_HELP;
624 void NORETURN usage_with_options(const char * const *usagestr,
625 const struct option *opts)
627 usage_with_options_internal(NULL, usagestr, opts, 0, 1);
631 void NORETURN usage_msg_opt(const char *msg,
632 const char * const *usagestr,
633 const struct option *options)
635 fprintf(stderr, "%s\n\n", msg);
636 usage_with_options(usagestr, options);
639 static int parse_options_usage(struct parse_opt_ctx_t *ctx,
640 const char * const *usagestr,
641 const struct option *opts, int err)
643 return usage_with_options_internal(ctx, usagestr, opts, 0, err);