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);
66 if (!(flags & OPT_SHORT) && p->opt) {
69 if (!(opt->flags & PARSE_OPT_NOARG))
77 return opterror(opt, "takes no value", flags);
86 *(int *)opt->value &= ~opt->defval;
88 *(int *)opt->value |= opt->defval;
93 *(int *)opt->value |= opt->defval;
95 *(int *)opt->value &= ~opt->defval;
99 *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
103 *(int *)opt->value = unset ? 0 : opt->defval;
107 *(void **)opt->value = unset ? NULL : (void *)opt->defval;
112 *(const char **)opt->value = NULL;
113 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
114 *(const char **)opt->value = (const char *)opt->defval;
116 return get_arg(p, opt, flags, (const char **)opt->value);
119 case OPTION_FILENAME:
122 *(const char **)opt->value = NULL;
123 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
124 *(const char **)opt->value = (const char *)opt->defval;
126 err = get_arg(p, opt, flags, (const char **)opt->value);
129 fix_filename(p->prefix, (const char **)opt->value);
132 case OPTION_CALLBACK:
134 return (*opt->callback)(opt, NULL, 1) ? (-1) : 0;
135 if (opt->flags & PARSE_OPT_NOARG)
136 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
137 if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
138 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
139 if (get_arg(p, opt, flags, &arg))
141 return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
145 *(int *)opt->value = 0;
148 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
149 *(int *)opt->value = opt->defval;
152 if (get_arg(p, opt, flags, &arg))
154 *(int *)opt->value = strtol(arg, (char **)&s, 10);
156 return opterror(opt, "expects a numerical value", flags);
160 die("should not happen, someone must be hit on the forehead");
164 static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)
166 const struct option *numopt = NULL;
168 for (; options->type != OPTION_END; options++) {
169 if (options->short_name == *p->opt) {
170 p->opt = p->opt[1] ? p->opt + 1 : NULL;
171 return get_value(p, options, OPT_SHORT);
175 * Handle the numerical option later, explicit one-digit
176 * options take precedence over it.
178 if (options->type == OPTION_NUMBER)
181 if (numopt && isdigit(*p->opt)) {
186 while (isdigit(p->opt[len]))
188 arg = xmemdupz(p->opt, len);
189 p->opt = p->opt[len] ? p->opt + len : NULL;
190 rc = (*numopt->callback)(numopt, arg, 0) ? (-1) : 0;
197 static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
198 const struct option *options)
200 const char *arg_end = strchr(arg, '=');
201 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
202 int abbrev_flags = 0, ambiguous_flags = 0;
205 arg_end = arg + strlen(arg);
207 for (; options->type != OPTION_END; options++) {
211 if (!options->long_name)
214 rest = skip_prefix(arg, options->long_name);
215 if (options->type == OPTION_ARGUMENT) {
219 return opterror(options, "takes no value", flags);
222 p->out[p->cpidx++] = arg - 2;
227 if (!strncmp(options->long_name, arg, arg_end - arg)) {
231 * If this is abbreviated, it is
232 * ambiguous. So when there is no
233 * exact match later, we need to
236 ambiguous_option = abbrev_option;
237 ambiguous_flags = abbrev_flags;
239 if (!(flags & OPT_UNSET) && *arg_end)
240 p->opt = arg_end + 1;
241 abbrev_option = options;
242 abbrev_flags = flags;
245 /* negation allowed? */
246 if (options->flags & PARSE_OPT_NONEG)
248 /* negated and abbreviated very much? */
249 if (!prefixcmp("no-", arg)) {
254 if (strncmp(arg, "no-", 3))
257 rest = skip_prefix(arg + 3, options->long_name);
258 /* abbreviated and negated? */
259 if (!rest && !prefixcmp(options->long_name, arg + 3))
269 return get_value(p, options, flags);
272 if (ambiguous_option)
273 return error("Ambiguous option: %s "
274 "(could be --%s%s or --%s%s)",
276 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
277 ambiguous_option->long_name,
278 (abbrev_flags & OPT_UNSET) ? "no-" : "",
279 abbrev_option->long_name);
281 return get_value(p, abbrev_option, abbrev_flags);
285 static int parse_nodash_opt(struct parse_opt_ctx_t *p, const char *arg,
286 const struct option *options)
288 for (; options->type != OPTION_END; options++) {
289 if (!(options->flags & PARSE_OPT_NODASH))
291 if (options->short_name == arg[0] && arg[1] == '\0')
292 return get_value(p, options, OPT_SHORT);
297 static void check_typos(const char *arg, const struct option *options)
302 if (!prefixcmp(arg, "no-")) {
303 error ("did you mean `--%s` (with two dashes ?)", arg);
307 for (; options->type != OPTION_END; options++) {
308 if (!options->long_name)
310 if (!prefixcmp(options->long_name, arg)) {
311 error ("did you mean `--%s` (with two dashes ?)", arg);
317 static void parse_options_check(const struct option *opts)
321 for (; opts->type != OPTION_END; opts++) {
322 if ((opts->flags & PARSE_OPT_LASTARG_DEFAULT) &&
323 (opts->flags & PARSE_OPT_OPTARG))
324 err |= optbug(opts, "uses incompatible flags "
325 "LASTARG_DEFAULT and OPTARG");
326 if (opts->flags & PARSE_OPT_NODASH &&
327 ((opts->flags & PARSE_OPT_OPTARG) ||
328 !(opts->flags & PARSE_OPT_NOARG) ||
329 !(opts->flags & PARSE_OPT_NONEG) ||
331 err |= optbug(opts, "uses feature "
332 "not supported for dashless options");
333 switch (opts->type) {
340 if ((opts->flags & PARSE_OPT_OPTARG) ||
341 !(opts->flags & PARSE_OPT_NOARG))
342 err |= optbug(opts, "should not accept an argument");
344 ; /* ok. (usually accepts an argument) */
351 void parse_options_start(struct parse_opt_ctx_t *ctx,
352 int argc, const char **argv, const char *prefix,
353 const struct option *options, int flags)
355 memset(ctx, 0, sizeof(*ctx));
356 ctx->argc = argc - 1;
357 ctx->argv = argv + 1;
359 ctx->prefix = prefix;
360 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
362 if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
363 (flags & PARSE_OPT_STOP_AT_NON_OPTION))
364 die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
365 parse_options_check(options);
368 static int usage_with_options_internal(struct parse_opt_ctx_t *,
369 const char * const *,
370 const struct option *, int, int);
372 int parse_options_step(struct parse_opt_ctx_t *ctx,
373 const struct option *options,
374 const char * const usagestr[])
376 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
378 /* we must reset ->opt, unknown short option leave it dangling */
381 for (; ctx->argc; ctx->argc--, ctx->argv++) {
382 const char *arg = ctx->argv[0];
384 if (*arg != '-' || !arg[1]) {
385 if (parse_nodash_opt(ctx, arg, options) == 0)
387 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
389 ctx->out[ctx->cpidx++] = ctx->argv[0];
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);
404 check_typos(arg + 1, options);
406 if (internal_help && *ctx->opt == 'h')
407 return parse_options_usage(ctx, usagestr, options, 0);
408 switch (parse_short_opt(ctx, options)) {
410 return parse_options_usage(ctx, usagestr, options, 1);
412 /* fake a short option thing to hide the fact that we may have
413 * started to parse aggregated stuff
415 * This is leaky, too bad.
417 ctx->argv[0] = xstrdup(ctx->opt - 1);
418 *(char *)ctx->argv[0] = '-';
425 if (!arg[2]) { /* "--" */
426 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
433 if (internal_help && !strcmp(arg + 2, "help-all"))
434 return usage_with_options_internal(ctx, usagestr, options, 1, 0);
435 if (internal_help && !strcmp(arg + 2, "help"))
436 return parse_options_usage(ctx, usagestr, options, 0);
437 switch (parse_long_opt(ctx, arg + 2, options)) {
439 return parse_options_usage(ctx, usagestr, options, 1);
445 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
446 return PARSE_OPT_UNKNOWN;
447 ctx->out[ctx->cpidx++] = ctx->argv[0];
450 return PARSE_OPT_DONE;
453 int parse_options_end(struct parse_opt_ctx_t *ctx)
455 memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out));
456 ctx->out[ctx->cpidx + ctx->argc] = NULL;
457 return ctx->cpidx + ctx->argc;
460 int parse_options(int argc, const char **argv, const char *prefix,
461 const struct option *options, const char * const usagestr[],
464 struct parse_opt_ctx_t ctx;
466 parse_options_start(&ctx, argc, argv, prefix, options, flags);
467 switch (parse_options_step(&ctx, options, usagestr)) {
472 default: /* PARSE_OPT_UNKNOWN */
473 if (ctx.argv[0][1] == '-') {
474 error("unknown option `%s'", ctx.argv[0] + 2);
476 error("unknown switch `%c'", *ctx.opt);
478 usage_with_options(usagestr, options);
481 return parse_options_end(&ctx);
484 static int usage_argh(const struct option *opts, FILE *outfile)
487 int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) || !opts->argh;
488 if (opts->flags & PARSE_OPT_OPTARG)
490 s = literal ? "[=%s]" : "[=<%s>]";
492 s = literal ? "[%s]" : "[<%s>]";
494 s = literal ? " %s" : " <%s>";
495 return fprintf(outfile, s, opts->argh ? opts->argh : "...");
498 #define USAGE_OPTS_WIDTH 24
501 static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
502 const char * const *usagestr,
503 const struct option *opts, int full, int err)
505 FILE *outfile = err ? stderr : stdout;
508 return PARSE_OPT_HELP;
510 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
511 fprintf(outfile, "cat <<\\EOF\n");
513 fprintf(outfile, "usage: %s\n", *usagestr++);
514 while (*usagestr && **usagestr)
515 fprintf(outfile, " or: %s\n", *usagestr++);
517 fprintf(outfile, "%s%s\n",
518 **usagestr ? " " : "",
523 if (opts->type != OPTION_GROUP)
524 fputc('\n', outfile);
526 for (; opts->type != OPTION_END; opts++) {
530 if (opts->type == OPTION_GROUP) {
531 fputc('\n', outfile);
533 fprintf(outfile, "%s\n", opts->help);
536 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
539 pos = fprintf(outfile, " ");
540 if (opts->short_name && !(opts->flags & PARSE_OPT_NEGHELP)) {
541 if (opts->flags & PARSE_OPT_NODASH)
542 pos += fprintf(outfile, "%c", opts->short_name);
544 pos += fprintf(outfile, "-%c", opts->short_name);
546 if (opts->long_name && opts->short_name)
547 pos += fprintf(outfile, ", ");
549 pos += fprintf(outfile, "--%s%s",
550 (opts->flags & PARSE_OPT_NEGHELP) ? "no-" : "",
552 if (opts->type == OPTION_NUMBER)
553 pos += fprintf(outfile, "-NUM");
555 if (!(opts->flags & PARSE_OPT_NOARG))
556 pos += usage_argh(opts, outfile);
558 if (pos <= USAGE_OPTS_WIDTH)
559 pad = USAGE_OPTS_WIDTH - pos;
561 fputc('\n', outfile);
562 pad = USAGE_OPTS_WIDTH;
564 fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", opts->help);
566 fputc('\n', outfile);
568 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
569 fputs("EOF\n", outfile);
571 return PARSE_OPT_HELP;
574 void usage_with_options(const char * const *usagestr,
575 const struct option *opts)
577 usage_with_options_internal(NULL, usagestr, opts, 0, 1);
581 void usage_msg_opt(const char *msg,
582 const char * const *usagestr,
583 const struct option *options)
585 fprintf(stderr, "%s\n\n", msg);
586 usage_with_options(usagestr, options);
589 static int parse_options_usage(struct parse_opt_ctx_t *ctx,
590 const char * const *usagestr,
591 const struct option *opts, int err)
593 return usage_with_options_internal(ctx, usagestr, opts, 0, err);
597 /*----- some often used options -----*/
600 int parse_opt_abbrev_cb(const struct option *opt, const char *arg, int unset)
605 v = unset ? 0 : DEFAULT_ABBREV;
607 v = strtol(arg, (char **)&arg, 10);
609 return opterror(opt, "expects a numerical value", 0);
610 if (v && v < MINIMUM_ABBREV)
615 *(int *)(opt->value) = v;
619 int parse_opt_approxidate_cb(const struct option *opt, const char *arg,
622 *(unsigned long *)(opt->value) = approxidate(arg);
626 int parse_opt_color_flag_cb(const struct option *opt, const char *arg,
632 arg = unset ? "never" : (const char *)opt->defval;
633 value = git_config_colorbool(NULL, arg, -1);
636 "expects \"always\", \"auto\", or \"never\"", 0);
637 *(int *)opt->value = value;
641 int parse_opt_verbosity_cb(const struct option *opt, const char *arg,
644 int *target = opt->value;
647 /* --no-quiet, --no-verbose */
649 else if (opt->short_name == 'v') {
663 int parse_opt_with_commit(const struct option *opt, const char *arg, int unset)
665 unsigned char sha1[20];
666 struct commit *commit;
670 if (get_sha1(arg, sha1))
671 return error("malformed object name %s", arg);
672 commit = lookup_commit_reference(sha1);
674 return error("no such commit %s", arg);
675 commit_list_insert(commit, opt->value);
679 int parse_opt_tertiary(const struct option *opt, const char *arg, int unset)
681 int *target = opt->value;
682 *target = unset ? 2 : 1;
686 int parse_options_concat(struct option *dst, size_t dst_size, struct option *src)
690 for (i = 0; i < dst_size; i++)
691 if (dst[i].type == OPTION_END)
693 for (j = 0; i < dst_size; i++, j++) {
695 if (src[j].type == OPTION_END)