1 #include "git-compat-util.h"
2 #include "parse-options.h"
7 static int parse_options_usage(struct parse_opt_ctx_t *ctx,
8 const char * const *usagestr,
9 const struct option *opts, int err);
14 int optbug(const struct option *opt, const char *reason)
17 return error("BUG: option '%s' %s", opt->long_name, reason);
18 return error("BUG: switch '%c' %s", opt->short_name, reason);
21 static int get_arg(struct parse_opt_ctx_t *p, const struct option *opt,
22 int flags, const char **arg)
27 } else if (p->argc == 1 && (opt->flags & PARSE_OPT_LASTARG_DEFAULT)) {
28 *arg = (const char *)opt->defval;
29 } else if (p->argc > 1) {
33 return opterror(opt, "requires a value", flags);
37 static void fix_filename(const char *prefix, const char **file)
39 if (!file || !*file || !prefix || is_absolute_path(*file)
40 || !strcmp("-", *file))
42 *file = xstrdup(prefix_filename(prefix, strlen(prefix), *file));
45 static int get_value(struct parse_opt_ctx_t *p,
46 const struct option *opt, int flags)
49 const int unset = flags & OPT_UNSET;
53 return opterror(opt, "takes no value", flags);
54 if (unset && (opt->flags & PARSE_OPT_NONEG))
55 return opterror(opt, "isn't available", flags);
56 if (!(flags & OPT_SHORT) && p->opt && (opt->flags & PARSE_OPT_NOARG))
57 return opterror(opt, "takes no value", flags);
60 case OPTION_LOWLEVEL_CALLBACK:
61 return (*(parse_opt_ll_cb *)opt->callback)(p, opt, unset);
65 *(int *)opt->value &= ~opt->defval;
67 *(int *)opt->value |= opt->defval;
72 *(int *)opt->value |= opt->defval;
74 *(int *)opt->value &= ~opt->defval;
78 *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
82 *(int *)opt->value = unset ? 0 : opt->defval;
86 *(void **)opt->value = unset ? NULL : (void *)opt->defval;
91 *(const char **)opt->value = NULL;
92 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
93 *(const char **)opt->value = (const char *)opt->defval;
95 return get_arg(p, opt, flags, (const char **)opt->value);
101 *(const char **)opt->value = NULL;
102 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
103 *(const char **)opt->value = (const char *)opt->defval;
105 err = get_arg(p, opt, flags, (const char **)opt->value);
108 fix_filename(p->prefix, (const char **)opt->value);
111 case OPTION_CALLBACK:
113 return (*opt->callback)(opt, NULL, 1) ? (-1) : 0;
114 if (opt->flags & PARSE_OPT_NOARG)
115 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
116 if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
117 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
118 if (get_arg(p, opt, flags, &arg))
120 return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
124 *(int *)opt->value = 0;
127 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
128 *(int *)opt->value = opt->defval;
131 if (get_arg(p, opt, flags, &arg))
133 *(int *)opt->value = strtol(arg, (char **)&s, 10);
135 return opterror(opt, "expects a numerical value", flags);
139 die("should not happen, someone must be hit on the forehead");
143 static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)
145 const struct option *numopt = NULL;
147 for (; options->type != OPTION_END; options++) {
148 if (options->short_name == *p->opt) {
149 p->opt = p->opt[1] ? p->opt + 1 : NULL;
150 return get_value(p, options, OPT_SHORT);
154 * Handle the numerical option later, explicit one-digit
155 * options take precedence over it.
157 if (options->type == OPTION_NUMBER)
160 if (numopt && isdigit(*p->opt)) {
165 while (isdigit(p->opt[len]))
167 arg = xmemdupz(p->opt, len);
168 p->opt = p->opt[len] ? p->opt + len : NULL;
169 rc = (*numopt->callback)(numopt, arg, 0) ? (-1) : 0;
176 static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
177 const struct option *options)
179 const char *arg_end = strchr(arg, '=');
180 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
181 int abbrev_flags = 0, ambiguous_flags = 0;
184 arg_end = arg + strlen(arg);
186 for (; options->type != OPTION_END; options++) {
187 const char *rest, *long_name = options->long_name;
188 int flags = 0, opt_flags = 0;
194 rest = skip_prefix(arg, long_name);
195 if (options->type == OPTION_ARGUMENT) {
199 return opterror(options, "takes no value", flags);
202 p->out[p->cpidx++] = arg - 2;
207 if (!strncmp(long_name, arg, arg_end - arg)) {
211 * If this is abbreviated, it is
212 * ambiguous. So when there is no
213 * exact match later, we need to
216 ambiguous_option = abbrev_option;
217 ambiguous_flags = abbrev_flags;
219 if (!(flags & OPT_UNSET) && *arg_end)
220 p->opt = arg_end + 1;
221 abbrev_option = options;
222 abbrev_flags = flags ^ opt_flags;
225 /* negation allowed? */
226 if (options->flags & PARSE_OPT_NONEG)
228 /* negated and abbreviated very much? */
229 if (!prefixcmp("no-", arg)) {
234 if (prefixcmp(arg, "no-")) {
235 if (!prefixcmp(long_name, "no-")) {
237 opt_flags |= OPT_UNSET;
243 rest = skip_prefix(arg + 3, long_name);
244 /* abbreviated and negated? */
245 if (!rest && !prefixcmp(long_name, arg + 3))
255 return get_value(p, options, flags ^ opt_flags);
258 if (ambiguous_option)
259 return error("Ambiguous option: %s "
260 "(could be --%s%s or --%s%s)",
262 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
263 ambiguous_option->long_name,
264 (abbrev_flags & OPT_UNSET) ? "no-" : "",
265 abbrev_option->long_name);
267 return get_value(p, abbrev_option, abbrev_flags);
271 static int parse_nodash_opt(struct parse_opt_ctx_t *p, const char *arg,
272 const struct option *options)
274 for (; options->type != OPTION_END; options++) {
275 if (!(options->flags & PARSE_OPT_NODASH))
277 if (options->short_name == arg[0] && arg[1] == '\0')
278 return get_value(p, options, OPT_SHORT);
283 static void check_typos(const char *arg, const struct option *options)
288 if (!prefixcmp(arg, "no-")) {
289 error ("did you mean `--%s` (with two dashes ?)", arg);
293 for (; options->type != OPTION_END; options++) {
294 if (!options->long_name)
296 if (!prefixcmp(options->long_name, arg)) {
297 error ("did you mean `--%s` (with two dashes ?)", arg);
303 static void parse_options_check(const struct option *opts)
307 for (; opts->type != OPTION_END; opts++) {
308 if ((opts->flags & PARSE_OPT_LASTARG_DEFAULT) &&
309 (opts->flags & PARSE_OPT_OPTARG))
310 err |= optbug(opts, "uses incompatible flags "
311 "LASTARG_DEFAULT and OPTARG");
312 if (opts->flags & PARSE_OPT_NODASH &&
313 ((opts->flags & PARSE_OPT_OPTARG) ||
314 !(opts->flags & PARSE_OPT_NOARG) ||
315 !(opts->flags & PARSE_OPT_NONEG) ||
317 err |= optbug(opts, "uses feature "
318 "not supported for dashless options");
319 switch (opts->type) {
326 if ((opts->flags & PARSE_OPT_OPTARG) ||
327 !(opts->flags & PARSE_OPT_NOARG))
328 err |= optbug(opts, "should not accept an argument");
330 ; /* ok. (usually accepts an argument) */
337 void parse_options_start(struct parse_opt_ctx_t *ctx,
338 int argc, const char **argv, const char *prefix,
339 const struct option *options, int flags)
341 memset(ctx, 0, sizeof(*ctx));
342 ctx->argc = argc - 1;
343 ctx->argv = argv + 1;
345 ctx->prefix = prefix;
346 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
348 if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
349 (flags & PARSE_OPT_STOP_AT_NON_OPTION))
350 die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
351 parse_options_check(options);
354 static int usage_with_options_internal(struct parse_opt_ctx_t *,
355 const char * const *,
356 const struct option *, int, int);
358 int parse_options_step(struct parse_opt_ctx_t *ctx,
359 const struct option *options,
360 const char * const usagestr[])
362 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
364 /* we must reset ->opt, unknown short option leave it dangling */
367 for (; ctx->argc; ctx->argc--, ctx->argv++) {
368 const char *arg = ctx->argv[0];
370 if (*arg != '-' || !arg[1]) {
371 if (parse_nodash_opt(ctx, arg, options) == 0)
373 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
374 return PARSE_OPT_NON_OPTION;
375 ctx->out[ctx->cpidx++] = ctx->argv[0];
381 if (internal_help && *ctx->opt == 'h')
382 return parse_options_usage(ctx, usagestr, options, 0);
383 switch (parse_short_opt(ctx, options)) {
385 return parse_options_usage(ctx, usagestr, options, 1);
388 check_typos(arg + 1, options);
392 check_typos(arg + 1, options);
394 if (internal_help && *ctx->opt == 'h')
395 return parse_options_usage(ctx, usagestr, options, 0);
396 switch (parse_short_opt(ctx, options)) {
398 return parse_options_usage(ctx, usagestr, options, 1);
400 /* fake a short option thing to hide the fact that we may have
401 * started to parse aggregated stuff
403 * This is leaky, too bad.
405 ctx->argv[0] = xstrdup(ctx->opt - 1);
406 *(char *)ctx->argv[0] = '-';
413 if (!arg[2]) { /* "--" */
414 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
421 if (internal_help && !strcmp(arg + 2, "help-all"))
422 return usage_with_options_internal(ctx, usagestr, options, 1, 0);
423 if (internal_help && !strcmp(arg + 2, "help"))
424 return parse_options_usage(ctx, usagestr, options, 0);
425 switch (parse_long_opt(ctx, arg + 2, options)) {
427 return parse_options_usage(ctx, usagestr, options, 1);
433 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
434 return PARSE_OPT_UNKNOWN;
435 ctx->out[ctx->cpidx++] = ctx->argv[0];
438 return PARSE_OPT_DONE;
441 int parse_options_end(struct parse_opt_ctx_t *ctx)
443 memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out));
444 ctx->out[ctx->cpidx + ctx->argc] = NULL;
445 return ctx->cpidx + ctx->argc;
448 int parse_options(int argc, const char **argv, const char *prefix,
449 const struct option *options, const char * const usagestr[],
452 struct parse_opt_ctx_t ctx;
454 parse_options_start(&ctx, argc, argv, prefix, options, flags);
455 switch (parse_options_step(&ctx, options, usagestr)) {
458 case PARSE_OPT_NON_OPTION:
461 default: /* PARSE_OPT_UNKNOWN */
462 if (ctx.argv[0][1] == '-') {
463 error("unknown option `%s'", ctx.argv[0] + 2);
465 error("unknown switch `%c'", *ctx.opt);
467 usage_with_options(usagestr, options);
470 precompose_argv(argc, argv);
471 return parse_options_end(&ctx);
474 static int usage_argh(const struct option *opts, FILE *outfile)
477 int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) || !opts->argh;
478 if (opts->flags & PARSE_OPT_OPTARG)
480 s = literal ? "[=%s]" : "[=<%s>]";
482 s = literal ? "[%s]" : "[<%s>]";
484 s = literal ? " %s" : " <%s>";
485 return fprintf(outfile, s, opts->argh ? _(opts->argh) : _("..."));
488 #define USAGE_OPTS_WIDTH 24
491 static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
492 const char * const *usagestr,
493 const struct option *opts, int full, int err)
495 FILE *outfile = err ? stderr : stdout;
498 return PARSE_OPT_HELP;
500 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
501 fprintf(outfile, "cat <<\\EOF\n");
503 fprintf_ln(outfile, _("usage: %s"), _(*usagestr++));
504 while (*usagestr && **usagestr)
505 /* TRANSLATORS: the colon here should align with the
506 one in "usage: %s" translation */
507 fprintf_ln(outfile, _(" or: %s"), _(*usagestr++));
510 fprintf_ln(outfile, _(" %s"), _(*usagestr));
516 if (opts->type != OPTION_GROUP)
517 fputc('\n', outfile);
519 for (; opts->type != OPTION_END; opts++) {
523 if (opts->type == OPTION_GROUP) {
524 fputc('\n', outfile);
526 fprintf(outfile, "%s\n", _(opts->help));
529 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
532 pos = fprintf(outfile, " ");
533 if (opts->short_name) {
534 if (opts->flags & PARSE_OPT_NODASH)
535 pos += fprintf(outfile, "%c", opts->short_name);
537 pos += fprintf(outfile, "-%c", opts->short_name);
539 if (opts->long_name && opts->short_name)
540 pos += fprintf(outfile, ", ");
542 pos += fprintf(outfile, "--%s", opts->long_name);
543 if (opts->type == OPTION_NUMBER)
544 pos += fprintf(outfile, "-NUM");
546 if ((opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
547 !(opts->flags & PARSE_OPT_NOARG))
548 pos += usage_argh(opts, outfile);
550 if (pos <= USAGE_OPTS_WIDTH)
551 pad = USAGE_OPTS_WIDTH - pos;
553 fputc('\n', outfile);
554 pad = USAGE_OPTS_WIDTH;
556 fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", _(opts->help));
558 fputc('\n', outfile);
560 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
561 fputs("EOF\n", outfile);
563 return PARSE_OPT_HELP;
566 void NORETURN usage_with_options(const char * const *usagestr,
567 const struct option *opts)
569 usage_with_options_internal(NULL, usagestr, opts, 0, 1);
573 void NORETURN usage_msg_opt(const char *msg,
574 const char * const *usagestr,
575 const struct option *options)
577 fprintf(stderr, "%s\n\n", msg);
578 usage_with_options(usagestr, options);
581 static int parse_options_usage(struct parse_opt_ctx_t *ctx,
582 const char * const *usagestr,
583 const struct option *opts, int err)
585 return usage_with_options_internal(ctx, usagestr, opts, 0, err);
589 int opterror(const struct option *opt, const char *reason, int flags)
591 if (flags & OPT_SHORT)
592 return error("switch `%c' %s", opt->short_name, reason);
593 if (flags & OPT_UNSET)
594 return error("option `no-%s' %s", opt->long_name, reason);
595 return error("option `%s' %s", opt->long_name, reason);