1 #include "git-compat-util.h"
2 #include "parse-options.h"
6 #include "string-list.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 static 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 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 return opterror(opt, "requires a value", flags);
47 static void fix_filename(const char *prefix, const char **file)
49 if (!file || !*file || !prefix || is_absolute_path(*file)
50 || !strcmp("-", *file))
52 *file = xstrdup(prefix_filename(prefix, strlen(prefix), *file));
55 static int get_value(struct parse_opt_ctx_t *p,
56 const struct option *opt, int flags)
59 const int unset = flags & OPT_UNSET;
63 return opterror(opt, "takes no value", flags);
64 if (unset && (opt->flags & PARSE_OPT_NONEG))
65 return opterror(opt, "isn't available", flags);
66 if (!(flags & OPT_SHORT) && p->opt && (opt->flags & PARSE_OPT_NOARG))
67 return opterror(opt, "takes no value", flags);
70 case OPTION_LOWLEVEL_CALLBACK:
71 return (*(parse_opt_ll_cb *)opt->callback)(p, opt, unset);
75 *(int *)opt->value &= ~opt->defval;
77 *(int *)opt->value |= opt->defval;
82 *(int *)opt->value |= opt->defval;
84 *(int *)opt->value &= ~opt->defval;
88 *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
92 *(int *)opt->value = unset ? 0 : opt->defval;
96 *(void **)opt->value = unset ? NULL : (void *)opt->defval;
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 return get_arg(p, opt, flags, (const char **)opt->value);
108 case OPTION_FILENAME:
111 *(const char **)opt->value = NULL;
112 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
113 *(const char **)opt->value = (const char *)opt->defval;
115 err = get_arg(p, opt, flags, (const char **)opt->value);
118 fix_filename(p->prefix, (const char **)opt->value);
121 case OPTION_CALLBACK:
123 return (*opt->callback)(opt, NULL, 1) ? (-1) : 0;
124 if (opt->flags & PARSE_OPT_NOARG)
125 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
126 if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
127 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
128 if (get_arg(p, opt, flags, &arg))
130 return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
134 *(int *)opt->value = 0;
137 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
138 *(int *)opt->value = opt->defval;
141 if (get_arg(p, opt, flags, &arg))
143 *(int *)opt->value = strtol(arg, (char **)&s, 10);
145 return opterror(opt, "expects a numerical value", flags);
149 die("should not happen, someone must be hit on the forehead");
153 static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)
155 const struct option *numopt = NULL;
157 for (; options->type != OPTION_END; options++) {
158 if (options->short_name == *p->opt) {
159 p->opt = p->opt[1] ? p->opt + 1 : NULL;
160 return get_value(p, options, OPT_SHORT);
164 * Handle the numerical option later, explicit one-digit
165 * options take precedence over it.
167 if (options->type == OPTION_NUMBER)
170 if (numopt && isdigit(*p->opt)) {
175 while (isdigit(p->opt[len]))
177 arg = xmemdupz(p->opt, len);
178 p->opt = p->opt[len] ? p->opt + len : NULL;
179 rc = (*numopt->callback)(numopt, arg, 0) ? (-1) : 0;
186 static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
187 const struct option *options)
189 const char *arg_end = strchr(arg, '=');
190 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
191 int abbrev_flags = 0, ambiguous_flags = 0;
194 arg_end = arg + strlen(arg);
196 for (; options->type != OPTION_END; options++) {
200 if (!options->long_name)
203 rest = skip_prefix(arg, options->long_name);
204 if (options->type == OPTION_ARGUMENT) {
208 return opterror(options, "takes no value", flags);
211 p->out[p->cpidx++] = arg - 2;
216 if (!strncmp(options->long_name, arg, arg_end - arg)) {
220 * If this is abbreviated, it is
221 * ambiguous. So when there is no
222 * exact match later, we need to
225 ambiguous_option = abbrev_option;
226 ambiguous_flags = abbrev_flags;
228 if (!(flags & OPT_UNSET) && *arg_end)
229 p->opt = arg_end + 1;
230 abbrev_option = options;
231 abbrev_flags = flags;
234 /* negation allowed? */
235 if (options->flags & PARSE_OPT_NONEG)
237 /* negated and abbreviated very much? */
238 if (!prefixcmp("no-", arg)) {
243 if (strncmp(arg, "no-", 3))
246 rest = skip_prefix(arg + 3, options->long_name);
247 /* abbreviated and negated? */
248 if (!rest && !prefixcmp(options->long_name, arg + 3))
258 return get_value(p, options, flags);
261 if (ambiguous_option)
262 return error("Ambiguous option: %s "
263 "(could be --%s%s or --%s%s)",
265 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
266 ambiguous_option->long_name,
267 (abbrev_flags & OPT_UNSET) ? "no-" : "",
268 abbrev_option->long_name);
270 return get_value(p, abbrev_option, abbrev_flags);
274 static int parse_nodash_opt(struct parse_opt_ctx_t *p, const char *arg,
275 const struct option *options)
277 for (; options->type != OPTION_END; options++) {
278 if (!(options->flags & PARSE_OPT_NODASH))
280 if (options->short_name == arg[0] && arg[1] == '\0')
281 return get_value(p, options, OPT_SHORT);
286 static void check_typos(const char *arg, const struct option *options)
291 if (!prefixcmp(arg, "no-")) {
292 error ("did you mean `--%s` (with two dashes ?)", arg);
296 for (; options->type != OPTION_END; options++) {
297 if (!options->long_name)
299 if (!prefixcmp(options->long_name, arg)) {
300 error ("did you mean `--%s` (with two dashes ?)", arg);
306 static void parse_options_check(const struct option *opts)
310 for (; opts->type != OPTION_END; opts++) {
311 if ((opts->flags & PARSE_OPT_LASTARG_DEFAULT) &&
312 (opts->flags & PARSE_OPT_OPTARG))
313 err |= optbug(opts, "uses incompatible flags "
314 "LASTARG_DEFAULT and OPTARG");
315 if (opts->flags & PARSE_OPT_NODASH &&
316 ((opts->flags & PARSE_OPT_OPTARG) ||
317 !(opts->flags & PARSE_OPT_NOARG) ||
318 !(opts->flags & PARSE_OPT_NONEG) ||
320 err |= optbug(opts, "uses feature "
321 "not supported for dashless options");
322 switch (opts->type) {
329 if ((opts->flags & PARSE_OPT_OPTARG) ||
330 !(opts->flags & PARSE_OPT_NOARG))
331 err |= optbug(opts, "should not accept an argument");
333 ; /* ok. (usually accepts an argument) */
340 void parse_options_start(struct parse_opt_ctx_t *ctx,
341 int argc, const char **argv, const char *prefix,
342 const struct option *options, int flags)
344 memset(ctx, 0, sizeof(*ctx));
345 ctx->argc = argc - 1;
346 ctx->argv = argv + 1;
348 ctx->prefix = prefix;
349 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
351 if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
352 (flags & PARSE_OPT_STOP_AT_NON_OPTION))
353 die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
354 parse_options_check(options);
357 static int usage_with_options_internal(struct parse_opt_ctx_t *,
358 const char * const *,
359 const struct option *, int, int);
361 int parse_options_step(struct parse_opt_ctx_t *ctx,
362 const struct option *options,
363 const char * const usagestr[])
365 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
367 /* we must reset ->opt, unknown short option leave it dangling */
370 for (; ctx->argc; ctx->argc--, ctx->argv++) {
371 const char *arg = ctx->argv[0];
373 if (*arg != '-' || !arg[1]) {
374 if (parse_nodash_opt(ctx, arg, options) == 0)
376 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
377 return PARSE_OPT_NON_OPTION;
378 ctx->out[ctx->cpidx++] = ctx->argv[0];
384 if (internal_help && *ctx->opt == 'h')
385 return parse_options_usage(ctx, usagestr, options, 0);
386 switch (parse_short_opt(ctx, options)) {
388 return parse_options_usage(ctx, usagestr, options, 1);
393 check_typos(arg + 1, options);
395 if (internal_help && *ctx->opt == 'h')
396 return parse_options_usage(ctx, usagestr, options, 0);
397 switch (parse_short_opt(ctx, options)) {
399 return parse_options_usage(ctx, usagestr, options, 1);
401 /* fake a short option thing to hide the fact that we may have
402 * started to parse aggregated stuff
404 * This is leaky, too bad.
406 ctx->argv[0] = xstrdup(ctx->opt - 1);
407 *(char *)ctx->argv[0] = '-';
414 if (!arg[2]) { /* "--" */
415 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
422 if (internal_help && !strcmp(arg + 2, "help-all"))
423 return usage_with_options_internal(ctx, usagestr, options, 1, 0);
424 if (internal_help && !strcmp(arg + 2, "help"))
425 return parse_options_usage(ctx, usagestr, options, 0);
426 switch (parse_long_opt(ctx, arg + 2, options)) {
428 return parse_options_usage(ctx, usagestr, options, 1);
434 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
435 return PARSE_OPT_UNKNOWN;
436 ctx->out[ctx->cpidx++] = ctx->argv[0];
439 return PARSE_OPT_DONE;
442 int parse_options_end(struct parse_opt_ctx_t *ctx)
444 memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out));
445 ctx->out[ctx->cpidx + ctx->argc] = NULL;
446 return ctx->cpidx + ctx->argc;
449 int parse_options(int argc, const char **argv, const char *prefix,
450 const struct option *options, const char * const usagestr[],
453 struct parse_opt_ctx_t ctx;
455 parse_options_start(&ctx, argc, argv, prefix, options, flags);
456 switch (parse_options_step(&ctx, options, usagestr)) {
459 case PARSE_OPT_NON_OPTION:
462 default: /* PARSE_OPT_UNKNOWN */
463 if (ctx.argv[0][1] == '-') {
464 error("unknown option `%s'", ctx.argv[0] + 2);
466 error("unknown switch `%c'", *ctx.opt);
468 usage_with_options(usagestr, options);
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(outfile, "usage: %s\n", *usagestr++);
504 while (*usagestr && **usagestr)
505 fprintf(outfile, " or: %s\n", *usagestr++);
507 fprintf(outfile, "%s%s\n",
508 **usagestr ? " " : "",
513 if (opts->type != OPTION_GROUP)
514 fputc('\n', outfile);
516 for (; opts->type != OPTION_END; opts++) {
520 if (opts->type == OPTION_GROUP) {
521 fputc('\n', outfile);
523 fprintf(outfile, "%s\n", opts->help);
526 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
529 pos = fprintf(outfile, " ");
530 if (opts->short_name && !(opts->flags & PARSE_OPT_NEGHELP)) {
531 if (opts->flags & PARSE_OPT_NODASH)
532 pos += fprintf(outfile, "%c", opts->short_name);
534 pos += fprintf(outfile, "-%c", opts->short_name);
536 if (opts->long_name && opts->short_name)
537 pos += fprintf(outfile, ", ");
539 pos += fprintf(outfile, "--%s%s",
540 (opts->flags & PARSE_OPT_NEGHELP) ? "no-" : "",
542 if (opts->type == OPTION_NUMBER)
543 pos += fprintf(outfile, "-NUM");
545 if ((opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
546 !(opts->flags & PARSE_OPT_NOARG))
547 pos += usage_argh(opts, outfile);
549 if (pos <= USAGE_OPTS_WIDTH)
550 pad = USAGE_OPTS_WIDTH - pos;
552 fputc('\n', outfile);
553 pad = USAGE_OPTS_WIDTH;
555 fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", opts->help);
557 fputc('\n', outfile);
559 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
560 fputs("EOF\n", outfile);
562 return PARSE_OPT_HELP;
565 void NORETURN usage_with_options(const char * const *usagestr,
566 const struct option *opts)
568 usage_with_options_internal(NULL, usagestr, opts, 0, 1);
572 void NORETURN usage_msg_opt(const char *msg,
573 const char * const *usagestr,
574 const struct option *options)
576 fprintf(stderr, "%s\n\n", msg);
577 usage_with_options(usagestr, options);
580 static int parse_options_usage(struct parse_opt_ctx_t *ctx,
581 const char * const *usagestr,
582 const struct option *opts, int err)
584 return usage_with_options_internal(ctx, usagestr, opts, 0, err);
588 /*----- some often used options -----*/
591 int parse_opt_abbrev_cb(const struct option *opt, const char *arg, int unset)
596 v = unset ? 0 : DEFAULT_ABBREV;
598 v = strtol(arg, (char **)&arg, 10);
600 return opterror(opt, "expects a numerical value", 0);
601 if (v && v < MINIMUM_ABBREV)
606 *(int *)(opt->value) = v;
610 int parse_opt_approxidate_cb(const struct option *opt, const char *arg,
613 *(unsigned long *)(opt->value) = approxidate(arg);
617 int parse_opt_color_flag_cb(const struct option *opt, const char *arg,
623 arg = unset ? "never" : (const char *)opt->defval;
624 value = git_config_colorbool(NULL, arg, -1);
627 "expects \"always\", \"auto\", or \"never\"", 0);
628 *(int *)opt->value = value;
632 int parse_opt_verbosity_cb(const struct option *opt, const char *arg,
635 int *target = opt->value;
638 /* --no-quiet, --no-verbose */
640 else if (opt->short_name == 'v') {
654 int parse_opt_with_commit(const struct option *opt, const char *arg, int unset)
656 unsigned char sha1[20];
657 struct commit *commit;
661 if (get_sha1(arg, sha1))
662 return error("malformed object name %s", arg);
663 commit = lookup_commit_reference(sha1);
665 return error("no such commit %s", arg);
666 commit_list_insert(commit, opt->value);
670 int parse_opt_tertiary(const struct option *opt, const char *arg, int unset)
672 int *target = opt->value;
673 *target = unset ? 2 : 1;
677 int parse_options_concat(struct option *dst, size_t dst_size, struct option *src)
681 for (i = 0; i < dst_size; i++)
682 if (dst[i].type == OPTION_END)
684 for (j = 0; i < dst_size; i++, j++) {
686 if (src[j].type == OPTION_END)
692 int parse_opt_string_list(const struct option *opt, const char *arg, int unset)
694 struct string_list *v = opt->value;
697 string_list_clear(v, 0);
704 string_list_append(v, xstrdup(arg));