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;
142 *(void **)opt->value = unset ? NULL : (void *)opt->defval;
147 *(const char **)opt->value = NULL;
148 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
149 *(const char **)opt->value = (const char *)opt->defval;
151 return get_arg(p, opt, flags, (const char **)opt->value);
154 case OPTION_FILENAME:
157 *(const char **)opt->value = NULL;
158 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
159 *(const char **)opt->value = (const char *)opt->defval;
161 err = get_arg(p, opt, flags, (const char **)opt->value);
164 fix_filename(p->prefix, (const char **)opt->value);
167 case OPTION_CALLBACK:
169 return (*opt->callback)(opt, NULL, 1) ? (-1) : 0;
170 if (opt->flags & PARSE_OPT_NOARG)
171 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
172 if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
173 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
174 if (get_arg(p, opt, flags, &arg))
176 return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
180 *(int *)opt->value = 0;
183 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
184 *(int *)opt->value = opt->defval;
187 if (get_arg(p, opt, flags, &arg))
189 *(int *)opt->value = strtol(arg, (char **)&s, 10);
191 return opterror(opt, "expects a numerical value", flags);
195 die("should not happen, someone must be hit on the forehead");
199 static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)
201 const struct option *all_opts = options;
202 const struct option *numopt = NULL;
204 for (; options->type != OPTION_END; options++) {
205 if (options->short_name == *p->opt) {
206 p->opt = p->opt[1] ? p->opt + 1 : NULL;
207 return get_value(p, options, all_opts, OPT_SHORT);
211 * Handle the numerical option later, explicit one-digit
212 * options take precedence over it.
214 if (options->type == OPTION_NUMBER)
217 if (numopt && isdigit(*p->opt)) {
222 while (isdigit(p->opt[len]))
224 arg = xmemdupz(p->opt, len);
225 p->opt = p->opt[len] ? p->opt + len : NULL;
226 rc = (*numopt->callback)(numopt, arg, 0) ? (-1) : 0;
233 static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
234 const struct option *options)
236 const struct option *all_opts = options;
237 const char *arg_end = strchr(arg, '=');
238 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
239 int abbrev_flags = 0, ambiguous_flags = 0;
242 arg_end = arg + strlen(arg);
244 for (; options->type != OPTION_END; options++) {
245 const char *rest, *long_name = options->long_name;
246 int flags = 0, opt_flags = 0;
252 rest = skip_prefix(arg, long_name);
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 rest = skip_prefix(arg + 3, long_name);
302 /* abbreviated and negated? */
303 if (!rest && starts_with(long_name, arg + 3))
313 return get_value(p, options, all_opts, flags ^ opt_flags);
316 if (ambiguous_option)
317 return error("Ambiguous option: %s "
318 "(could be --%s%s or --%s%s)",
320 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
321 ambiguous_option->long_name,
322 (abbrev_flags & OPT_UNSET) ? "no-" : "",
323 abbrev_option->long_name);
325 return get_value(p, abbrev_option, all_opts, abbrev_flags);
329 static int parse_nodash_opt(struct parse_opt_ctx_t *p, const char *arg,
330 const struct option *options)
332 const struct option *all_opts = options;
334 for (; options->type != OPTION_END; options++) {
335 if (!(options->flags & PARSE_OPT_NODASH))
337 if (options->short_name == arg[0] && arg[1] == '\0')
338 return get_value(p, options, all_opts, OPT_SHORT);
343 static void check_typos(const char *arg, const struct option *options)
348 if (starts_with(arg, "no-")) {
349 error ("did you mean `--%s` (with two dashes ?)", arg);
353 for (; options->type != OPTION_END; options++) {
354 if (!options->long_name)
356 if (starts_with(options->long_name, arg)) {
357 error ("did you mean `--%s` (with two dashes ?)", arg);
363 static void parse_options_check(const struct option *opts)
367 for (; opts->type != OPTION_END; opts++) {
368 if ((opts->flags & PARSE_OPT_LASTARG_DEFAULT) &&
369 (opts->flags & PARSE_OPT_OPTARG))
370 err |= optbug(opts, "uses incompatible flags "
371 "LASTARG_DEFAULT and OPTARG");
372 if (opts->flags & PARSE_OPT_NODASH &&
373 ((opts->flags & PARSE_OPT_OPTARG) ||
374 !(opts->flags & PARSE_OPT_NOARG) ||
375 !(opts->flags & PARSE_OPT_NONEG) ||
377 err |= optbug(opts, "uses feature "
378 "not supported for dashless options");
379 switch (opts->type) {
386 if ((opts->flags & PARSE_OPT_OPTARG) ||
387 !(opts->flags & PARSE_OPT_NOARG))
388 err |= optbug(opts, "should not accept an argument");
390 ; /* ok. (usually accepts an argument) */
397 void parse_options_start(struct parse_opt_ctx_t *ctx,
398 int argc, const char **argv, const char *prefix,
399 const struct option *options, int flags)
401 memset(ctx, 0, sizeof(*ctx));
402 ctx->argc = argc - 1;
403 ctx->argv = argv + 1;
405 ctx->prefix = prefix;
406 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
408 if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
409 (flags & PARSE_OPT_STOP_AT_NON_OPTION))
410 die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
411 parse_options_check(options);
414 static int usage_with_options_internal(struct parse_opt_ctx_t *,
415 const char * const *,
416 const struct option *, int, int);
418 int parse_options_step(struct parse_opt_ctx_t *ctx,
419 const struct option *options,
420 const char * const usagestr[])
422 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
424 /* we must reset ->opt, unknown short option leave it dangling */
427 for (; ctx->argc; ctx->argc--, ctx->argv++) {
428 const char *arg = ctx->argv[0];
430 if (*arg != '-' || !arg[1]) {
431 if (parse_nodash_opt(ctx, arg, options) == 0)
433 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
434 return PARSE_OPT_NON_OPTION;
435 ctx->out[ctx->cpidx++] = ctx->argv[0];
441 if (internal_help && *ctx->opt == 'h')
442 return parse_options_usage(ctx, usagestr, options, 0);
443 switch (parse_short_opt(ctx, options)) {
445 return parse_options_usage(ctx, usagestr, options, 1);
448 check_typos(arg + 1, options);
452 check_typos(arg + 1, options);
454 if (internal_help && *ctx->opt == 'h')
455 return parse_options_usage(ctx, usagestr, options, 0);
456 switch (parse_short_opt(ctx, options)) {
458 return parse_options_usage(ctx, usagestr, options, 1);
460 /* fake a short option thing to hide the fact that we may have
461 * started to parse aggregated stuff
463 * This is leaky, too bad.
465 ctx->argv[0] = xstrdup(ctx->opt - 1);
466 *(char *)ctx->argv[0] = '-';
473 if (!arg[2]) { /* "--" */
474 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
481 if (internal_help && !strcmp(arg + 2, "help-all"))
482 return usage_with_options_internal(ctx, usagestr, options, 1, 0);
483 if (internal_help && !strcmp(arg + 2, "help"))
484 return parse_options_usage(ctx, usagestr, options, 0);
485 switch (parse_long_opt(ctx, arg + 2, options)) {
487 return parse_options_usage(ctx, usagestr, options, 1);
493 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
494 return PARSE_OPT_UNKNOWN;
495 ctx->out[ctx->cpidx++] = ctx->argv[0];
498 return PARSE_OPT_DONE;
501 int parse_options_end(struct parse_opt_ctx_t *ctx)
503 memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out));
504 ctx->out[ctx->cpidx + ctx->argc] = NULL;
505 return ctx->cpidx + ctx->argc;
508 int parse_options(int argc, const char **argv, const char *prefix,
509 const struct option *options, const char * const usagestr[],
512 struct parse_opt_ctx_t ctx;
514 parse_options_start(&ctx, argc, argv, prefix, options, flags);
515 switch (parse_options_step(&ctx, options, usagestr)) {
518 case PARSE_OPT_NON_OPTION:
521 default: /* PARSE_OPT_UNKNOWN */
522 if (ctx.argv[0][1] == '-') {
523 error("unknown option `%s'", ctx.argv[0] + 2);
524 } else if (isascii(*ctx.opt)) {
525 error("unknown switch `%c'", *ctx.opt);
527 error("unknown non-ascii option in string: `%s'",
530 usage_with_options(usagestr, options);
533 precompose_argv(argc, argv);
534 return parse_options_end(&ctx);
537 static int usage_argh(const struct option *opts, FILE *outfile)
540 int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) || !opts->argh;
541 if (opts->flags & PARSE_OPT_OPTARG)
543 s = literal ? "[=%s]" : "[=<%s>]";
545 s = literal ? "[%s]" : "[<%s>]";
547 s = literal ? " %s" : " <%s>";
548 return utf8_fprintf(outfile, s, opts->argh ? _(opts->argh) : _("..."));
551 #define USAGE_OPTS_WIDTH 24
554 static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
555 const char * const *usagestr,
556 const struct option *opts, int full, int err)
558 FILE *outfile = err ? stderr : stdout;
561 return PARSE_OPT_HELP;
563 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
564 fprintf(outfile, "cat <<\\EOF\n");
566 fprintf_ln(outfile, _("usage: %s"), _(*usagestr++));
567 while (*usagestr && **usagestr)
568 /* TRANSLATORS: the colon here should align with the
569 one in "usage: %s" translation */
570 fprintf_ln(outfile, _(" or: %s"), _(*usagestr++));
573 fprintf_ln(outfile, _(" %s"), _(*usagestr));
579 if (opts->type != OPTION_GROUP)
580 fputc('\n', outfile);
582 for (; opts->type != OPTION_END; opts++) {
586 if (opts->type == OPTION_GROUP) {
587 fputc('\n', outfile);
589 fprintf(outfile, "%s\n", _(opts->help));
592 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
595 pos = fprintf(outfile, " ");
596 if (opts->short_name) {
597 if (opts->flags & PARSE_OPT_NODASH)
598 pos += fprintf(outfile, "%c", opts->short_name);
600 pos += fprintf(outfile, "-%c", opts->short_name);
602 if (opts->long_name && opts->short_name)
603 pos += fprintf(outfile, ", ");
605 pos += fprintf(outfile, "--%s", opts->long_name);
606 if (opts->type == OPTION_NUMBER)
607 pos += utf8_fprintf(outfile, _("-NUM"));
609 if ((opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
610 !(opts->flags & PARSE_OPT_NOARG))
611 pos += usage_argh(opts, outfile);
613 if (pos <= USAGE_OPTS_WIDTH)
614 pad = USAGE_OPTS_WIDTH - pos;
616 fputc('\n', outfile);
617 pad = USAGE_OPTS_WIDTH;
619 fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", _(opts->help));
621 fputc('\n', outfile);
623 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
624 fputs("EOF\n", outfile);
626 return PARSE_OPT_HELP;
629 void NORETURN usage_with_options(const char * const *usagestr,
630 const struct option *opts)
632 usage_with_options_internal(NULL, usagestr, opts, 0, 1);
636 void NORETURN usage_msg_opt(const char *msg,
637 const char * const *usagestr,
638 const struct option *options)
640 fprintf(stderr, "%s\n\n", msg);
641 usage_with_options(usagestr, options);
644 static int parse_options_usage(struct parse_opt_ctx_t *ctx,
645 const char * const *usagestr,
646 const struct option *opts, int err)
648 return usage_with_options_internal(ctx, usagestr, opts, 0, err);