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 static 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 opterror(const struct option *opt, const char *reason, int flags)
23 if (flags & OPT_SHORT)
24 return error("switch `%c' %s", opt->short_name, reason);
25 if (flags & OPT_UNSET)
26 return error("option `no-%s' %s", opt->long_name, reason);
27 return error("option `%s' %s", opt->long_name, reason);
30 static int get_arg(struct parse_opt_ctx_t *p, const struct option *opt,
31 int flags, const char **arg)
36 } else if (p->argc == 1 && (opt->flags & PARSE_OPT_LASTARG_DEFAULT)) {
37 *arg = (const char *)opt->defval;
38 } else if (p->argc > 1) {
42 return opterror(opt, "requires a value", flags);
46 static void fix_filename(const char *prefix, const char **file)
48 if (!file || !*file || !prefix || is_absolute_path(*file)
49 || !strcmp("-", *file))
51 *file = xstrdup(prefix_filename(prefix, strlen(prefix), *file));
54 static int get_value(struct parse_opt_ctx_t *p,
55 const struct option *opt, int flags)
58 const int unset = flags & OPT_UNSET;
62 return opterror(opt, "takes no value", flags);
63 if (unset && (opt->flags & PARSE_OPT_NONEG))
64 return opterror(opt, "isn't available", flags);
65 if (!(flags & OPT_SHORT) && p->opt && (opt->flags & PARSE_OPT_NOARG))
66 return opterror(opt, "takes no value", flags);
71 *(int *)opt->value &= ~opt->defval;
73 *(int *)opt->value |= opt->defval;
78 *(int *)opt->value |= opt->defval;
80 *(int *)opt->value &= ~opt->defval;
84 *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
88 *(int *)opt->value = unset ? 0 : opt->defval;
92 *(void **)opt->value = unset ? NULL : (void *)opt->defval;
97 *(const char **)opt->value = NULL;
98 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
99 *(const char **)opt->value = (const char *)opt->defval;
101 return get_arg(p, opt, flags, (const char **)opt->value);
104 case OPTION_FILENAME:
107 *(const char **)opt->value = NULL;
108 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
109 *(const char **)opt->value = (const char *)opt->defval;
111 err = get_arg(p, opt, flags, (const char **)opt->value);
114 fix_filename(p->prefix, (const char **)opt->value);
117 case OPTION_CALLBACK:
119 return (*opt->callback)(opt, NULL, 1) ? (-1) : 0;
120 if (opt->flags & PARSE_OPT_NOARG)
121 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
122 if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
123 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
124 if (get_arg(p, opt, flags, &arg))
126 return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
130 *(int *)opt->value = 0;
133 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
134 *(int *)opt->value = opt->defval;
137 if (get_arg(p, opt, flags, &arg))
139 *(int *)opt->value = strtol(arg, (char **)&s, 10);
141 return opterror(opt, "expects a numerical value", flags);
145 die("should not happen, someone must be hit on the forehead");
149 static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)
151 const struct option *numopt = NULL;
153 for (; options->type != OPTION_END; options++) {
154 if (options->short_name == *p->opt) {
155 p->opt = p->opt[1] ? p->opt + 1 : NULL;
156 return get_value(p, options, OPT_SHORT);
160 * Handle the numerical option later, explicit one-digit
161 * options take precedence over it.
163 if (options->type == OPTION_NUMBER)
166 if (numopt && isdigit(*p->opt)) {
171 while (isdigit(p->opt[len]))
173 arg = xmemdupz(p->opt, len);
174 p->opt = p->opt[len] ? p->opt + len : NULL;
175 rc = (*numopt->callback)(numopt, arg, 0) ? (-1) : 0;
182 static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
183 const struct option *options)
185 const char *arg_end = strchr(arg, '=');
186 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
187 int abbrev_flags = 0, ambiguous_flags = 0;
190 arg_end = arg + strlen(arg);
192 for (; options->type != OPTION_END; options++) {
196 if (!options->long_name)
199 rest = skip_prefix(arg, options->long_name);
200 if (options->type == OPTION_ARGUMENT) {
204 return opterror(options, "takes no value", flags);
207 p->out[p->cpidx++] = arg - 2;
212 if (!strncmp(options->long_name, arg, arg_end - arg)) {
216 * If this is abbreviated, it is
217 * ambiguous. So when there is no
218 * exact match later, we need to
221 ambiguous_option = abbrev_option;
222 ambiguous_flags = abbrev_flags;
224 if (!(flags & OPT_UNSET) && *arg_end)
225 p->opt = arg_end + 1;
226 abbrev_option = options;
227 abbrev_flags = flags;
230 /* negation allowed? */
231 if (options->flags & PARSE_OPT_NONEG)
233 /* negated and abbreviated very much? */
234 if (!prefixcmp("no-", arg)) {
239 if (strncmp(arg, "no-", 3))
242 rest = skip_prefix(arg + 3, options->long_name);
243 /* abbreviated and negated? */
244 if (!rest && !prefixcmp(options->long_name, arg + 3))
254 return get_value(p, options, flags);
257 if (ambiguous_option)
258 return error("Ambiguous option: %s "
259 "(could be --%s%s or --%s%s)",
261 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
262 ambiguous_option->long_name,
263 (abbrev_flags & OPT_UNSET) ? "no-" : "",
264 abbrev_option->long_name);
266 return get_value(p, abbrev_option, abbrev_flags);
270 static int parse_nodash_opt(struct parse_opt_ctx_t *p, const char *arg,
271 const struct option *options)
273 for (; options->type != OPTION_END; options++) {
274 if (!(options->flags & PARSE_OPT_NODASH))
276 if (options->short_name == arg[0] && arg[1] == '\0')
277 return get_value(p, options, OPT_SHORT);
282 static void check_typos(const char *arg, const struct option *options)
287 if (!prefixcmp(arg, "no-")) {
288 error ("did you mean `--%s` (with two dashes ?)", arg);
292 for (; options->type != OPTION_END; options++) {
293 if (!options->long_name)
295 if (!prefixcmp(options->long_name, arg)) {
296 error ("did you mean `--%s` (with two dashes ?)", arg);
302 static void parse_options_check(const struct option *opts)
306 for (; opts->type != OPTION_END; opts++) {
307 if ((opts->flags & PARSE_OPT_LASTARG_DEFAULT) &&
308 (opts->flags & PARSE_OPT_OPTARG))
309 err |= optbug(opts, "uses incompatible flags "
310 "LASTARG_DEFAULT and OPTARG");
311 if (opts->flags & PARSE_OPT_NODASH &&
312 ((opts->flags & PARSE_OPT_OPTARG) ||
313 !(opts->flags & PARSE_OPT_NOARG) ||
314 !(opts->flags & PARSE_OPT_NONEG) ||
316 err |= optbug(opts, "uses feature "
317 "not supported for dashless options");
318 switch (opts->type) {
325 if ((opts->flags & PARSE_OPT_OPTARG) ||
326 !(opts->flags & PARSE_OPT_NOARG))
327 err |= optbug(opts, "should not accept an argument");
329 ; /* ok. (usually accepts an argument) */
336 void parse_options_start(struct parse_opt_ctx_t *ctx,
337 int argc, const char **argv, const char *prefix,
338 const struct option *options, int flags)
340 memset(ctx, 0, sizeof(*ctx));
341 ctx->argc = argc - 1;
342 ctx->argv = argv + 1;
344 ctx->prefix = prefix;
345 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
347 if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
348 (flags & PARSE_OPT_STOP_AT_NON_OPTION))
349 die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
350 parse_options_check(options);
353 static int usage_with_options_internal(struct parse_opt_ctx_t *,
354 const char * const *,
355 const struct option *, int, int);
357 int parse_options_step(struct parse_opt_ctx_t *ctx,
358 const struct option *options,
359 const char * const usagestr[])
361 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
363 /* we must reset ->opt, unknown short option leave it dangling */
366 for (; ctx->argc; ctx->argc--, ctx->argv++) {
367 const char *arg = ctx->argv[0];
369 if (*arg != '-' || !arg[1]) {
370 if (parse_nodash_opt(ctx, arg, options) == 0)
372 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
374 ctx->out[ctx->cpidx++] = ctx->argv[0];
380 if (internal_help && *ctx->opt == 'h')
381 return parse_options_usage(ctx, usagestr, options, 0);
382 switch (parse_short_opt(ctx, options)) {
384 return parse_options_usage(ctx, usagestr, options, 1);
389 check_typos(arg + 1, options);
391 if (internal_help && *ctx->opt == 'h')
392 return parse_options_usage(ctx, usagestr, options, 0);
393 switch (parse_short_opt(ctx, options)) {
395 return parse_options_usage(ctx, usagestr, options, 1);
397 /* fake a short option thing to hide the fact that we may have
398 * started to parse aggregated stuff
400 * This is leaky, too bad.
402 ctx->argv[0] = xstrdup(ctx->opt - 1);
403 *(char *)ctx->argv[0] = '-';
410 if (!arg[2]) { /* "--" */
411 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
418 if (internal_help && !strcmp(arg + 2, "help-all"))
419 return usage_with_options_internal(ctx, usagestr, options, 1, 0);
420 if (internal_help && !strcmp(arg + 2, "help"))
421 return parse_options_usage(ctx, usagestr, options, 0);
422 switch (parse_long_opt(ctx, arg + 2, options)) {
424 return parse_options_usage(ctx, usagestr, options, 1);
430 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
431 return PARSE_OPT_UNKNOWN;
432 ctx->out[ctx->cpidx++] = ctx->argv[0];
435 return PARSE_OPT_DONE;
438 int parse_options_end(struct parse_opt_ctx_t *ctx)
440 memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out));
441 ctx->out[ctx->cpidx + ctx->argc] = NULL;
442 return ctx->cpidx + ctx->argc;
445 int parse_options(int argc, const char **argv, const char *prefix,
446 const struct option *options, const char * const usagestr[],
449 struct parse_opt_ctx_t ctx;
451 parse_options_start(&ctx, argc, argv, prefix, options, flags);
452 switch (parse_options_step(&ctx, options, usagestr)) {
457 default: /* PARSE_OPT_UNKNOWN */
458 if (ctx.argv[0][1] == '-') {
459 error("unknown option `%s'", ctx.argv[0] + 2);
461 error("unknown switch `%c'", *ctx.opt);
463 usage_with_options(usagestr, options);
466 return parse_options_end(&ctx);
469 static int usage_argh(const struct option *opts, FILE *outfile)
472 int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) || !opts->argh;
473 if (opts->flags & PARSE_OPT_OPTARG)
475 s = literal ? "[=%s]" : "[=<%s>]";
477 s = literal ? "[%s]" : "[<%s>]";
479 s = literal ? " %s" : " <%s>";
480 return fprintf(outfile, s, opts->argh ? opts->argh : "...");
483 #define USAGE_OPTS_WIDTH 24
486 static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
487 const char * const *usagestr,
488 const struct option *opts, int full, int err)
490 FILE *outfile = err ? stderr : stdout;
493 return PARSE_OPT_HELP;
495 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
496 fprintf(outfile, "cat <<\\EOF\n");
498 fprintf(outfile, "usage: %s\n", *usagestr++);
499 while (*usagestr && **usagestr)
500 fprintf(outfile, " or: %s\n", *usagestr++);
502 fprintf(outfile, "%s%s\n",
503 **usagestr ? " " : "",
508 if (opts->type != OPTION_GROUP)
509 fputc('\n', outfile);
511 for (; opts->type != OPTION_END; opts++) {
515 if (opts->type == OPTION_GROUP) {
516 fputc('\n', outfile);
518 fprintf(outfile, "%s\n", opts->help);
521 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
524 pos = fprintf(outfile, " ");
525 if (opts->short_name && !(opts->flags & PARSE_OPT_NEGHELP)) {
526 if (opts->flags & PARSE_OPT_NODASH)
527 pos += fprintf(outfile, "%c", opts->short_name);
529 pos += fprintf(outfile, "-%c", opts->short_name);
531 if (opts->long_name && opts->short_name)
532 pos += fprintf(outfile, ", ");
534 pos += fprintf(outfile, "--%s%s",
535 (opts->flags & PARSE_OPT_NEGHELP) ? "no-" : "",
537 if (opts->type == OPTION_NUMBER)
538 pos += fprintf(outfile, "-NUM");
540 if ((opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
541 !(opts->flags & PARSE_OPT_NOARG))
542 pos += usage_argh(opts, outfile);
544 if (pos <= USAGE_OPTS_WIDTH)
545 pad = USAGE_OPTS_WIDTH - pos;
547 fputc('\n', outfile);
548 pad = USAGE_OPTS_WIDTH;
550 fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", opts->help);
552 fputc('\n', outfile);
554 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
555 fputs("EOF\n", outfile);
557 return PARSE_OPT_HELP;
560 void usage_with_options(const char * const *usagestr,
561 const struct option *opts)
563 usage_with_options_internal(NULL, usagestr, opts, 0, 1);
567 void usage_msg_opt(const char *msg,
568 const char * const *usagestr,
569 const struct option *options)
571 fprintf(stderr, "%s\n\n", msg);
572 usage_with_options(usagestr, options);
575 static int parse_options_usage(struct parse_opt_ctx_t *ctx,
576 const char * const *usagestr,
577 const struct option *opts, int err)
579 return usage_with_options_internal(ctx, usagestr, opts, 0, err);
583 /*----- some often used options -----*/
586 int parse_opt_abbrev_cb(const struct option *opt, const char *arg, int unset)
591 v = unset ? 0 : DEFAULT_ABBREV;
593 v = strtol(arg, (char **)&arg, 10);
595 return opterror(opt, "expects a numerical value", 0);
596 if (v && v < MINIMUM_ABBREV)
601 *(int *)(opt->value) = v;
605 int parse_opt_approxidate_cb(const struct option *opt, const char *arg,
608 *(unsigned long *)(opt->value) = approxidate(arg);
612 int parse_opt_color_flag_cb(const struct option *opt, const char *arg,
618 arg = unset ? "never" : (const char *)opt->defval;
619 value = git_config_colorbool(NULL, arg, -1);
622 "expects \"always\", \"auto\", or \"never\"", 0);
623 *(int *)opt->value = value;
627 int parse_opt_verbosity_cb(const struct option *opt, const char *arg,
630 int *target = opt->value;
633 /* --no-quiet, --no-verbose */
635 else if (opt->short_name == 'v') {
649 int parse_opt_with_commit(const struct option *opt, const char *arg, int unset)
651 unsigned char sha1[20];
652 struct commit *commit;
656 if (get_sha1(arg, sha1))
657 return error("malformed object name %s", arg);
658 commit = lookup_commit_reference(sha1);
660 return error("no such commit %s", arg);
661 commit_list_insert(commit, opt->value);
665 int parse_opt_tertiary(const struct option *opt, const char *arg, int unset)
667 int *target = opt->value;
668 *target = unset ? 2 : 1;
672 int parse_options_concat(struct option *dst, size_t dst_size, struct option *src)
676 for (i = 0; i < dst_size; i++)
677 if (dst[i].type == OPTION_END)
679 for (j = 0; i < dst_size; i++, j++) {
681 if (src[j].type == OPTION_END)