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 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);
180 die("should not happen, someone must be hit on the forehead");
184 static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)
186 const struct option *all_opts = options;
187 const struct option *numopt = NULL;
189 for (; options->type != OPTION_END; options++) {
190 if (options->short_name == *p->opt) {
191 p->opt = p->opt[1] ? p->opt + 1 : NULL;
192 return get_value(p, options, all_opts, OPT_SHORT);
196 * Handle the numerical option later, explicit one-digit
197 * options take precedence over it.
199 if (options->type == OPTION_NUMBER)
202 if (numopt && isdigit(*p->opt)) {
207 while (isdigit(p->opt[len]))
209 arg = xmemdupz(p->opt, len);
210 p->opt = p->opt[len] ? p->opt + len : NULL;
211 rc = (*numopt->callback)(numopt, arg, 0) ? (-1) : 0;
218 static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
219 const struct option *options)
221 const struct option *all_opts = options;
222 const char *arg_end = strchrnul(arg, '=');
223 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
224 int abbrev_flags = 0, ambiguous_flags = 0;
226 for (; options->type != OPTION_END; options++) {
227 const char *rest, *long_name = options->long_name;
228 int flags = 0, opt_flags = 0;
234 if (!skip_prefix(arg, long_name, &rest))
236 if (options->type == OPTION_ARGUMENT) {
240 return opterror(options, "takes no value", flags);
243 p->out[p->cpidx++] = arg - 2;
248 if (!strncmp(long_name, arg, arg_end - arg)) {
252 * If this is abbreviated, it is
253 * ambiguous. So when there is no
254 * exact match later, we need to
257 ambiguous_option = abbrev_option;
258 ambiguous_flags = abbrev_flags;
260 if (!(flags & OPT_UNSET) && *arg_end)
261 p->opt = arg_end + 1;
262 abbrev_option = options;
263 abbrev_flags = flags ^ opt_flags;
266 /* negation allowed? */
267 if (options->flags & PARSE_OPT_NONEG)
269 /* negated and abbreviated very much? */
270 if (starts_with("no-", arg)) {
275 if (!starts_with(arg, "no-")) {
276 if (starts_with(long_name, "no-")) {
278 opt_flags |= OPT_UNSET;
284 if (!skip_prefix(arg + 3, long_name, &rest)) {
285 /* abbreviated and negated? */
286 if (starts_with(long_name, arg + 3))
297 return get_value(p, options, all_opts, flags ^ opt_flags);
300 if (ambiguous_option)
301 return error("Ambiguous option: %s "
302 "(could be --%s%s or --%s%s)",
304 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
305 ambiguous_option->long_name,
306 (abbrev_flags & OPT_UNSET) ? "no-" : "",
307 abbrev_option->long_name);
309 return get_value(p, abbrev_option, all_opts, abbrev_flags);
313 static int parse_nodash_opt(struct parse_opt_ctx_t *p, const char *arg,
314 const struct option *options)
316 const struct option *all_opts = options;
318 for (; options->type != OPTION_END; options++) {
319 if (!(options->flags & PARSE_OPT_NODASH))
321 if (options->short_name == arg[0] && arg[1] == '\0')
322 return get_value(p, options, all_opts, OPT_SHORT);
327 static void check_typos(const char *arg, const struct option *options)
332 if (starts_with(arg, "no-")) {
333 error ("did you mean `--%s` (with two dashes ?)", arg);
337 for (; options->type != OPTION_END; options++) {
338 if (!options->long_name)
340 if (starts_with(options->long_name, arg)) {
341 error ("did you mean `--%s` (with two dashes ?)", arg);
347 static void parse_options_check(const struct option *opts)
351 for (; opts->type != OPTION_END; opts++) {
352 if ((opts->flags & PARSE_OPT_LASTARG_DEFAULT) &&
353 (opts->flags & PARSE_OPT_OPTARG))
354 err |= optbug(opts, "uses incompatible flags "
355 "LASTARG_DEFAULT and OPTARG");
356 if (opts->flags & PARSE_OPT_NODASH &&
357 ((opts->flags & PARSE_OPT_OPTARG) ||
358 !(opts->flags & PARSE_OPT_NOARG) ||
359 !(opts->flags & PARSE_OPT_NONEG) ||
361 err |= optbug(opts, "uses feature "
362 "not supported for dashless options");
363 switch (opts->type) {
369 if ((opts->flags & PARSE_OPT_OPTARG) ||
370 !(opts->flags & PARSE_OPT_NOARG))
371 err |= optbug(opts, "should not accept an argument");
373 ; /* ok. (usually accepts an argument) */
376 strcspn(opts->argh, " _") != strlen(opts->argh))
377 err |= optbug(opts, "multi-word argh should use dash to separate words");
383 void parse_options_start(struct parse_opt_ctx_t *ctx,
384 int argc, const char **argv, const char *prefix,
385 const struct option *options, int flags)
387 memset(ctx, 0, sizeof(*ctx));
388 ctx->argc = argc - 1;
389 ctx->argv = argv + 1;
391 ctx->prefix = prefix;
392 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
394 if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
395 (flags & PARSE_OPT_STOP_AT_NON_OPTION))
396 die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
397 parse_options_check(options);
400 static int usage_with_options_internal(struct parse_opt_ctx_t *,
401 const char * const *,
402 const struct option *, int, int);
404 int parse_options_step(struct parse_opt_ctx_t *ctx,
405 const struct option *options,
406 const char * const usagestr[])
408 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
410 /* we must reset ->opt, unknown short option leave it dangling */
413 for (; ctx->argc; ctx->argc--, ctx->argv++) {
414 const char *arg = ctx->argv[0];
416 if (*arg != '-' || !arg[1]) {
417 if (parse_nodash_opt(ctx, arg, options) == 0)
419 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
420 return PARSE_OPT_NON_OPTION;
421 ctx->out[ctx->cpidx++] = ctx->argv[0];
427 if (internal_help && *ctx->opt == 'h')
428 return parse_options_usage(ctx, usagestr, options, 0);
429 switch (parse_short_opt(ctx, options)) {
431 return parse_options_usage(ctx, usagestr, options, 1);
434 check_typos(arg + 1, options);
438 check_typos(arg + 1, options);
440 if (internal_help && *ctx->opt == 'h')
441 return parse_options_usage(ctx, usagestr, options, 0);
442 switch (parse_short_opt(ctx, options)) {
444 return parse_options_usage(ctx, usagestr, options, 1);
446 /* fake a short option thing to hide the fact that we may have
447 * started to parse aggregated stuff
449 * This is leaky, too bad.
451 ctx->argv[0] = xstrdup(ctx->opt - 1);
452 *(char *)ctx->argv[0] = '-';
459 if (!arg[2]) { /* "--" */
460 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
467 if (internal_help && !strcmp(arg + 2, "help-all"))
468 return usage_with_options_internal(ctx, usagestr, options, 1, 0);
469 if (internal_help && !strcmp(arg + 2, "help"))
470 return parse_options_usage(ctx, usagestr, options, 0);
471 switch (parse_long_opt(ctx, arg + 2, options)) {
473 return parse_options_usage(ctx, usagestr, options, 1);
479 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
480 return PARSE_OPT_UNKNOWN;
481 ctx->out[ctx->cpidx++] = ctx->argv[0];
484 return PARSE_OPT_DONE;
487 int parse_options_end(struct parse_opt_ctx_t *ctx)
489 memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out));
490 ctx->out[ctx->cpidx + ctx->argc] = NULL;
491 return ctx->cpidx + ctx->argc;
494 int parse_options(int argc, const char **argv, const char *prefix,
495 const struct option *options, const char * const usagestr[],
498 struct parse_opt_ctx_t ctx;
500 parse_options_start(&ctx, argc, argv, prefix, options, flags);
501 switch (parse_options_step(&ctx, options, usagestr)) {
504 case PARSE_OPT_NON_OPTION:
507 default: /* PARSE_OPT_UNKNOWN */
508 if (ctx.argv[0][1] == '-') {
509 error("unknown option `%s'", ctx.argv[0] + 2);
510 } else if (isascii(*ctx.opt)) {
511 error("unknown switch `%c'", *ctx.opt);
513 error("unknown non-ascii option in string: `%s'",
516 usage_with_options(usagestr, options);
519 precompose_argv(argc, argv);
520 return parse_options_end(&ctx);
523 static int usage_argh(const struct option *opts, FILE *outfile)
526 int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) || !opts->argh;
527 if (opts->flags & PARSE_OPT_OPTARG)
529 s = literal ? "[=%s]" : "[=<%s>]";
531 s = literal ? "[%s]" : "[<%s>]";
533 s = literal ? " %s" : " <%s>";
534 return utf8_fprintf(outfile, s, opts->argh ? _(opts->argh) : _("..."));
537 #define USAGE_OPTS_WIDTH 24
540 static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
541 const char * const *usagestr,
542 const struct option *opts, int full, int err)
544 FILE *outfile = err ? stderr : stdout;
547 return PARSE_OPT_HELP;
549 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
550 fprintf(outfile, "cat <<\\EOF\n");
552 fprintf_ln(outfile, _("usage: %s"), _(*usagestr++));
553 while (*usagestr && **usagestr)
554 /* TRANSLATORS: the colon here should align with the
555 one in "usage: %s" translation */
556 fprintf_ln(outfile, _(" or: %s"), _(*usagestr++));
559 fprintf_ln(outfile, _(" %s"), _(*usagestr));
565 if (opts->type != OPTION_GROUP)
566 fputc('\n', outfile);
568 for (; opts->type != OPTION_END; opts++) {
572 if (opts->type == OPTION_GROUP) {
573 fputc('\n', outfile);
575 fprintf(outfile, "%s\n", _(opts->help));
578 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
581 pos = fprintf(outfile, " ");
582 if (opts->short_name) {
583 if (opts->flags & PARSE_OPT_NODASH)
584 pos += fprintf(outfile, "%c", opts->short_name);
586 pos += fprintf(outfile, "-%c", opts->short_name);
588 if (opts->long_name && opts->short_name)
589 pos += fprintf(outfile, ", ");
591 pos += fprintf(outfile, "--%s", opts->long_name);
592 if (opts->type == OPTION_NUMBER)
593 pos += utf8_fprintf(outfile, _("-NUM"));
595 if ((opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
596 !(opts->flags & PARSE_OPT_NOARG))
597 pos += usage_argh(opts, outfile);
599 if (pos <= USAGE_OPTS_WIDTH)
600 pad = USAGE_OPTS_WIDTH - pos;
602 fputc('\n', outfile);
603 pad = USAGE_OPTS_WIDTH;
605 fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", _(opts->help));
607 fputc('\n', outfile);
609 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
610 fputs("EOF\n", outfile);
612 return PARSE_OPT_HELP;
615 void NORETURN usage_with_options(const char * const *usagestr,
616 const struct option *opts)
618 usage_with_options_internal(NULL, usagestr, opts, 0, 1);
622 void NORETURN usage_msg_opt(const char *msg,
623 const char * const *usagestr,
624 const struct option *options)
626 fprintf(stderr, "%s\n\n", msg);
627 usage_with_options(usagestr, options);
630 static int parse_options_usage(struct parse_opt_ctx_t *ctx,
631 const char * const *usagestr,
632 const struct option *opts, int err)
634 return usage_with_options_internal(ctx, usagestr, opts, 0, err);
638 int opterror(const struct option *opt, const char *reason, int flags)
640 if (flags & OPT_SHORT)
641 return error("switch `%c' %s", opt->short_name, reason);
642 if (flags & OPT_UNSET)
643 return error("option `no-%s' %s", opt->long_name, reason);
644 return error("option `%s' %s", opt->long_name, reason);