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 opterror(const struct option *opt, const char *reason, int flags)
16 if (flags & OPT_SHORT)
17 return error("switch `%c' %s", opt->short_name, reason);
18 if (flags & OPT_UNSET)
19 return error("option `no-%s' %s", opt->long_name, reason);
20 return error("option `%s' %s", opt->long_name, reason);
23 static int get_arg(struct parse_opt_ctx_t *p, const struct option *opt,
24 int flags, const char **arg)
29 } else if (p->argc == 1 && (opt->flags & PARSE_OPT_LASTARG_DEFAULT)) {
30 *arg = (const char *)opt->defval;
31 } else if (p->argc > 1) {
35 return opterror(opt, "requires a value", flags);
39 static void fix_filename(const char *prefix, const char **file)
41 if (!file || !*file || !prefix || is_absolute_path(*file)
42 || !strcmp("-", *file))
44 *file = xstrdup(prefix_filename(prefix, strlen(prefix), *file));
47 static int get_value(struct parse_opt_ctx_t *p,
48 const struct option *opt, int flags)
51 const int unset = flags & OPT_UNSET;
55 return opterror(opt, "takes no value", flags);
56 if (unset && (opt->flags & PARSE_OPT_NONEG))
57 return opterror(opt, "isn't available", flags);
59 if (!(flags & OPT_SHORT) && p->opt) {
62 if (!(opt->flags & PARSE_OPT_NOARG))
70 return opterror(opt, "takes no value", flags);
79 *(int *)opt->value &= ~opt->defval;
81 *(int *)opt->value |= opt->defval;
86 *(int *)opt->value |= opt->defval;
88 *(int *)opt->value &= ~opt->defval;
92 *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
96 *(int *)opt->value = unset ? 0 : opt->defval;
100 *(void **)opt->value = unset ? NULL : (void *)opt->defval;
105 *(const char **)opt->value = NULL;
106 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
107 *(const char **)opt->value = (const char *)opt->defval;
109 return get_arg(p, opt, flags, (const char **)opt->value);
112 case OPTION_FILENAME:
115 *(const char **)opt->value = NULL;
116 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
117 *(const char **)opt->value = (const char *)opt->defval;
119 err = get_arg(p, opt, flags, (const char **)opt->value);
122 fix_filename(p->prefix, (const char **)opt->value);
125 case OPTION_CALLBACK:
127 return (*opt->callback)(opt, NULL, 1) ? (-1) : 0;
128 if (opt->flags & PARSE_OPT_NOARG)
129 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
130 if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
131 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
132 if (get_arg(p, opt, flags, &arg))
134 return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
138 *(int *)opt->value = 0;
141 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
142 *(int *)opt->value = opt->defval;
145 if (get_arg(p, opt, flags, &arg))
147 *(int *)opt->value = strtol(arg, (char **)&s, 10);
149 return opterror(opt, "expects a numerical value", flags);
153 die("should not happen, someone must be hit on the forehead");
157 static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)
159 const struct option *numopt = NULL;
161 for (; options->type != OPTION_END; options++) {
162 if (options->short_name == *p->opt) {
163 p->opt = p->opt[1] ? p->opt + 1 : NULL;
164 return get_value(p, options, OPT_SHORT);
168 * Handle the numerical option later, explicit one-digit
169 * options take precedence over it.
171 if (options->type == OPTION_NUMBER)
174 if (numopt && isdigit(*p->opt)) {
179 while (isdigit(p->opt[len]))
181 arg = xmemdupz(p->opt, len);
182 p->opt = p->opt[len] ? p->opt + len : NULL;
183 rc = (*numopt->callback)(numopt, arg, 0) ? (-1) : 0;
190 static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
191 const struct option *options)
193 const char *arg_end = strchr(arg, '=');
194 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
195 int abbrev_flags = 0, ambiguous_flags = 0;
198 arg_end = arg + strlen(arg);
200 for (; options->type != OPTION_END; options++) {
204 if (!options->long_name)
207 rest = skip_prefix(arg, options->long_name);
208 if (options->type == OPTION_ARGUMENT) {
212 return opterror(options, "takes no value", flags);
215 p->out[p->cpidx++] = arg - 2;
220 if (!strncmp(options->long_name, arg, arg_end - arg)) {
224 * If this is abbreviated, it is
225 * ambiguous. So when there is no
226 * exact match later, we need to
229 ambiguous_option = abbrev_option;
230 ambiguous_flags = abbrev_flags;
232 if (!(flags & OPT_UNSET) && *arg_end)
233 p->opt = arg_end + 1;
234 abbrev_option = options;
235 abbrev_flags = flags;
238 /* negation allowed? */
239 if (options->flags & PARSE_OPT_NONEG)
241 /* negated and abbreviated very much? */
242 if (!prefixcmp("no-", arg)) {
247 if (strncmp(arg, "no-", 3))
250 rest = skip_prefix(arg + 3, options->long_name);
251 /* abbreviated and negated? */
252 if (!rest && !prefixcmp(options->long_name, arg + 3))
262 return get_value(p, options, flags);
265 if (ambiguous_option)
266 return error("Ambiguous option: %s "
267 "(could be --%s%s or --%s%s)",
269 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
270 ambiguous_option->long_name,
271 (abbrev_flags & OPT_UNSET) ? "no-" : "",
272 abbrev_option->long_name);
274 return get_value(p, abbrev_option, abbrev_flags);
278 static int parse_nodash_opt(struct parse_opt_ctx_t *p, const char *arg,
279 const struct option *options)
281 for (; options->type != OPTION_END; options++) {
282 if (!(options->flags & PARSE_OPT_NODASH))
284 if ((options->flags & PARSE_OPT_OPTARG) ||
285 !(options->flags & PARSE_OPT_NOARG))
286 die("BUG: dashless options don't support arguments");
287 if (!(options->flags & PARSE_OPT_NONEG))
288 die("BUG: dashless options don't support negation");
289 if (options->long_name)
290 die("BUG: dashless options can't be long");
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 if (opts->long_name) {
325 error("`--%s` uses incompatible flags "
326 "LASTARG_DEFAULT and OPTARG", opts->long_name);
328 error("`-%c` uses incompatible flags "
329 "LASTARG_DEFAULT and OPTARG", opts->short_name);
339 void parse_options_start(struct parse_opt_ctx_t *ctx,
340 int argc, const char **argv, const char *prefix,
343 memset(ctx, 0, sizeof(*ctx));
344 ctx->argc = argc - 1;
345 ctx->argv = argv + 1;
347 ctx->prefix = prefix;
348 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
350 if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
351 (flags & PARSE_OPT_STOP_AT_NON_OPTION))
352 die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
355 static int usage_with_options_internal(struct parse_opt_ctx_t *,
356 const char * const *,
357 const struct option *, int, int);
359 int parse_options_step(struct parse_opt_ctx_t *ctx,
360 const struct option *options,
361 const char * const usagestr[])
363 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
365 parse_options_check(options);
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)
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, flags);
456 switch (parse_options_step(&ctx, options, usagestr)) {
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 return parse_options_end(&ctx);
473 static int usage_argh(const struct option *opts, FILE *outfile)
476 int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) || !opts->argh;
477 if (opts->flags & PARSE_OPT_OPTARG)
479 s = literal ? "[=%s]" : "[=<%s>]";
481 s = literal ? "[%s]" : "[<%s>]";
483 s = literal ? " %s" : " <%s>";
484 return fprintf(outfile, s, opts->argh ? opts->argh : "...");
487 #define USAGE_OPTS_WIDTH 24
490 static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
491 const char * const *usagestr,
492 const struct option *opts, int full, int err)
494 FILE *outfile = err ? stderr : stdout;
497 return PARSE_OPT_HELP;
499 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
500 fprintf(outfile, "cat <<\\EOF\n");
502 fprintf(outfile, "usage: %s\n", *usagestr++);
503 while (*usagestr && **usagestr)
504 fprintf(outfile, " or: %s\n", *usagestr++);
506 fprintf(outfile, "%s%s\n",
507 **usagestr ? " " : "",
512 if (opts->type != OPTION_GROUP)
513 fputc('\n', outfile);
515 for (; opts->type != OPTION_END; opts++) {
519 if (opts->type == OPTION_GROUP) {
520 fputc('\n', outfile);
522 fprintf(outfile, "%s\n", opts->help);
525 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
528 pos = fprintf(outfile, " ");
529 if (opts->short_name && !(opts->flags & PARSE_OPT_NEGHELP)) {
530 if (opts->flags & PARSE_OPT_NODASH)
531 pos += fprintf(outfile, "%c", opts->short_name);
533 pos += fprintf(outfile, "-%c", opts->short_name);
535 if (opts->long_name && opts->short_name)
536 pos += fprintf(outfile, ", ");
538 pos += fprintf(outfile, "--%s%s",
539 (opts->flags & PARSE_OPT_NEGHELP) ? "no-" : "",
541 if (opts->type == OPTION_NUMBER)
542 pos += fprintf(outfile, "-NUM");
544 if (!(opts->flags & PARSE_OPT_NOARG))
545 pos += usage_argh(opts, outfile);
547 if (pos <= USAGE_OPTS_WIDTH)
548 pad = USAGE_OPTS_WIDTH - pos;
550 fputc('\n', outfile);
551 pad = USAGE_OPTS_WIDTH;
553 fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", opts->help);
555 fputc('\n', outfile);
557 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
558 fputs("EOF\n", outfile);
560 return PARSE_OPT_HELP;
563 void usage_with_options(const char * const *usagestr,
564 const struct option *opts)
566 usage_with_options_internal(NULL, usagestr, opts, 0, 1);
570 void usage_msg_opt(const char *msg,
571 const char * const *usagestr,
572 const struct option *options)
574 fprintf(stderr, "%s\n\n", msg);
575 usage_with_options(usagestr, options);
578 static int parse_options_usage(struct parse_opt_ctx_t *ctx,
579 const char * const *usagestr,
580 const struct option *opts, int err)
582 return usage_with_options_internal(ctx, usagestr, opts, 0, err);
586 /*----- some often used options -----*/
589 int parse_opt_abbrev_cb(const struct option *opt, const char *arg, int unset)
594 v = unset ? 0 : DEFAULT_ABBREV;
596 v = strtol(arg, (char **)&arg, 10);
598 return opterror(opt, "expects a numerical value", 0);
599 if (v && v < MINIMUM_ABBREV)
604 *(int *)(opt->value) = v;
608 int parse_opt_approxidate_cb(const struct option *opt, const char *arg,
611 *(unsigned long *)(opt->value) = approxidate(arg);
615 int parse_opt_color_flag_cb(const struct option *opt, const char *arg,
621 arg = unset ? "never" : (const char *)opt->defval;
622 value = git_config_colorbool(NULL, arg, -1);
625 "expects \"always\", \"auto\", or \"never\"", 0);
626 *(int *)opt->value = value;
630 int parse_opt_verbosity_cb(const struct option *opt, const char *arg,
633 int *target = opt->value;
636 /* --no-quiet, --no-verbose */
638 else if (opt->short_name == 'v') {
652 int parse_opt_with_commit(const struct option *opt, const char *arg, int unset)
654 unsigned char sha1[20];
655 struct commit *commit;
659 if (get_sha1(arg, sha1))
660 return error("malformed object name %s", arg);
661 commit = lookup_commit_reference(sha1);
663 return error("no such commit %s", arg);
664 commit_list_insert(commit, opt->value);
668 int parse_opt_tertiary(const struct option *opt, const char *arg, int unset)
670 int *target = opt->value;
671 *target = unset ? 2 : 1;
675 int parse_options_concat(struct option *dst, size_t dst_size, struct option *src)
679 for (i = 0; i < dst_size; i++)
680 if (dst[i].type == OPTION_END)
682 for (j = 0; i < dst_size; i++, j++) {
684 if (src[j].type == OPTION_END)