1 #include "git-compat-util.h"
2 #include "parse-options.h"
6 static int parse_options_usage(const char * const *usagestr,
7 const struct option *opts);
12 static int opterror(const struct option *opt, const char *reason, int flags)
14 if (flags & OPT_SHORT)
15 return error("switch `%c' %s", opt->short_name, reason);
16 if (flags & OPT_UNSET)
17 return error("option `no-%s' %s", opt->long_name, reason);
18 return error("option `%s' %s", opt->long_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);
57 if (!(flags & OPT_SHORT) && p->opt) {
60 if (!(opt->flags & PARSE_OPT_NOARG))
68 return opterror(opt, "takes no value", flags);
77 *(int *)opt->value &= ~opt->defval;
79 *(int *)opt->value |= opt->defval;
84 *(int *)opt->value |= opt->defval;
86 *(int *)opt->value &= ~opt->defval;
90 *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
94 *(int *)opt->value = unset ? 0 : opt->defval;
98 *(void **)opt->value = unset ? NULL : (void *)opt->defval;
103 *(const char **)opt->value = NULL;
104 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
105 *(const char **)opt->value = (const char *)opt->defval;
107 return get_arg(p, opt, flags, (const char **)opt->value);
110 case OPTION_FILENAME:
113 *(const char **)opt->value = NULL;
114 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
115 *(const char **)opt->value = (const char *)opt->defval;
117 err = get_arg(p, opt, flags, (const char **)opt->value);
120 fix_filename(p->prefix, (const char **)opt->value);
123 case OPTION_CALLBACK:
125 return (*opt->callback)(opt, NULL, 1) ? (-1) : 0;
126 if (opt->flags & PARSE_OPT_NOARG)
127 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
128 if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
129 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
130 if (get_arg(p, opt, flags, &arg))
132 return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
136 *(int *)opt->value = 0;
139 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
140 *(int *)opt->value = opt->defval;
143 if (get_arg(p, opt, flags, &arg))
145 *(int *)opt->value = strtol(arg, (char **)&s, 10);
147 return opterror(opt, "expects a numerical value", flags);
151 die("should not happen, someone must be hit on the forehead");
155 static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)
157 const struct option *numopt = NULL;
159 for (; options->type != OPTION_END; options++) {
160 if (options->short_name == *p->opt) {
161 p->opt = p->opt[1] ? p->opt + 1 : NULL;
162 return get_value(p, options, OPT_SHORT);
166 * Handle the numerical option later, explicit one-digit
167 * options take precedence over it.
169 if (options->type == OPTION_NUMBER)
172 if (numopt && isdigit(*p->opt)) {
177 while (isdigit(p->opt[len]))
179 arg = xmemdupz(p->opt, len);
180 p->opt = p->opt[len] ? p->opt + len : NULL;
181 rc = (*numopt->callback)(numopt, arg, 0) ? (-1) : 0;
188 static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
189 const struct option *options)
191 const char *arg_end = strchr(arg, '=');
192 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
193 int abbrev_flags = 0, ambiguous_flags = 0;
196 arg_end = arg + strlen(arg);
198 for (; options->type != OPTION_END; options++) {
202 if (!options->long_name)
205 rest = skip_prefix(arg, options->long_name);
206 if (options->type == OPTION_ARGUMENT) {
210 return opterror(options, "takes no value", flags);
213 p->out[p->cpidx++] = arg - 2;
218 if (!strncmp(options->long_name, arg, arg_end - arg)) {
222 * If this is abbreviated, it is
223 * ambiguous. So when there is no
224 * exact match later, we need to
227 ambiguous_option = abbrev_option;
228 ambiguous_flags = abbrev_flags;
230 if (!(flags & OPT_UNSET) && *arg_end)
231 p->opt = arg_end + 1;
232 abbrev_option = options;
233 abbrev_flags = flags;
236 /* negation allowed? */
237 if (options->flags & PARSE_OPT_NONEG)
239 /* negated and abbreviated very much? */
240 if (!prefixcmp("no-", arg)) {
245 if (strncmp(arg, "no-", 3))
248 rest = skip_prefix(arg + 3, options->long_name);
249 /* abbreviated and negated? */
250 if (!rest && !prefixcmp(options->long_name, arg + 3))
260 return get_value(p, options, flags);
263 if (ambiguous_option)
264 return error("Ambiguous option: %s "
265 "(could be --%s%s or --%s%s)",
267 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
268 ambiguous_option->long_name,
269 (abbrev_flags & OPT_UNSET) ? "no-" : "",
270 abbrev_option->long_name);
272 return get_value(p, abbrev_option, abbrev_flags);
276 static int parse_nodash_opt(struct parse_opt_ctx_t *p, const char *arg,
277 const struct option *options)
279 for (; options->type != OPTION_END; options++) {
280 if (!(options->flags & PARSE_OPT_NODASH))
282 if ((options->flags & PARSE_OPT_OPTARG) ||
283 !(options->flags & PARSE_OPT_NOARG))
284 die("BUG: dashless options don't support arguments");
285 if (!(options->flags & PARSE_OPT_NONEG))
286 die("BUG: dashless options don't support negation");
287 if (options->long_name)
288 die("BUG: dashless options can't be long");
289 if (options->short_name == arg[0] && arg[1] == '\0')
290 return get_value(p, options, OPT_SHORT);
295 static void check_typos(const char *arg, const struct option *options)
300 if (!prefixcmp(arg, "no-")) {
301 error ("did you mean `--%s` (with two dashes ?)", arg);
305 for (; options->type != OPTION_END; options++) {
306 if (!options->long_name)
308 if (!prefixcmp(options->long_name, arg)) {
309 error ("did you mean `--%s` (with two dashes ?)", arg);
315 static void parse_options_check(const struct option *opts)
319 for (; opts->type != OPTION_END; opts++) {
320 if ((opts->flags & PARSE_OPT_LASTARG_DEFAULT) &&
321 (opts->flags & PARSE_OPT_OPTARG)) {
322 if (opts->long_name) {
323 error("`--%s` uses incompatible flags "
324 "LASTARG_DEFAULT and OPTARG", opts->long_name);
326 error("`-%c` uses incompatible flags "
327 "LASTARG_DEFAULT and OPTARG", opts->short_name);
337 void parse_options_start(struct parse_opt_ctx_t *ctx,
338 int argc, const char **argv, const char *prefix,
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");
353 static int usage_with_options_internal(const char * const *,
354 const struct option *, int);
356 int parse_options_step(struct parse_opt_ctx_t *ctx,
357 const struct option *options,
358 const char * const usagestr[])
360 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
362 parse_options_check(options);
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)
375 ctx->out[ctx->cpidx++] = ctx->argv[0];
381 if (internal_help && *ctx->opt == 'h')
382 return parse_options_usage(usagestr, options);
383 switch (parse_short_opt(ctx, options)) {
385 return parse_options_usage(usagestr, options);
390 check_typos(arg + 1, options);
392 if (internal_help && *ctx->opt == 'h')
393 return parse_options_usage(usagestr, options);
394 switch (parse_short_opt(ctx, options)) {
396 return parse_options_usage(usagestr, options);
398 /* fake a short option thing to hide the fact that we may have
399 * started to parse aggregated stuff
401 * This is leaky, too bad.
403 ctx->argv[0] = xstrdup(ctx->opt - 1);
404 *(char *)ctx->argv[0] = '-';
411 if (!arg[2]) { /* "--" */
412 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
419 if (internal_help && !strcmp(arg + 2, "help-all"))
420 return usage_with_options_internal(usagestr, options, 1);
421 if (internal_help && !strcmp(arg + 2, "help"))
422 return parse_options_usage(usagestr, options);
423 switch (parse_long_opt(ctx, arg + 2, options)) {
425 return parse_options_usage(usagestr, options);
431 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
432 return PARSE_OPT_UNKNOWN;
433 ctx->out[ctx->cpidx++] = ctx->argv[0];
436 return PARSE_OPT_DONE;
439 int parse_options_end(struct parse_opt_ctx_t *ctx)
441 memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out));
442 ctx->out[ctx->cpidx + ctx->argc] = NULL;
443 return ctx->cpidx + ctx->argc;
446 int parse_options(int argc, const char **argv, const char *prefix,
447 const struct option *options, const char * const usagestr[],
450 struct parse_opt_ctx_t ctx;
452 parse_options_start(&ctx, argc, argv, prefix, flags);
453 switch (parse_options_step(&ctx, options, usagestr)) {
458 default: /* PARSE_OPT_UNKNOWN */
459 if (ctx.argv[0][1] == '-') {
460 error("unknown option `%s'", ctx.argv[0] + 2);
462 error("unknown switch `%c'", *ctx.opt);
464 usage_with_options(usagestr, options);
467 return parse_options_end(&ctx);
470 static int usage_argh(const struct option *opts)
473 int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) || !opts->argh;
474 if (opts->flags & PARSE_OPT_OPTARG)
476 s = literal ? "[=%s]" : "[=<%s>]";
478 s = literal ? "[%s]" : "[<%s>]";
480 s = literal ? " %s" : " <%s>";
481 return fprintf(stderr, s, opts->argh ? opts->argh : "...");
484 #define USAGE_OPTS_WIDTH 24
487 static int usage_with_options_internal(const char * const *usagestr,
488 const struct option *opts, int full)
491 return PARSE_OPT_HELP;
493 fprintf(stderr, "usage: %s\n", *usagestr++);
494 while (*usagestr && **usagestr)
495 fprintf(stderr, " or: %s\n", *usagestr++);
497 fprintf(stderr, "%s%s\n",
498 **usagestr ? " " : "",
503 if (opts->type != OPTION_GROUP)
506 for (; opts->type != OPTION_END; opts++) {
510 if (opts->type == OPTION_GROUP) {
513 fprintf(stderr, "%s\n", opts->help);
516 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
519 pos = fprintf(stderr, " ");
520 if (opts->short_name && !(opts->flags & PARSE_OPT_NEGHELP)) {
521 if (opts->flags & PARSE_OPT_NODASH)
522 pos += fprintf(stderr, "%c", opts->short_name);
524 pos += fprintf(stderr, "-%c", opts->short_name);
526 if (opts->long_name && opts->short_name)
527 pos += fprintf(stderr, ", ");
529 pos += fprintf(stderr, "--%s%s",
530 (opts->flags & PARSE_OPT_NEGHELP) ? "no-" : "",
532 if (opts->type == OPTION_NUMBER)
533 pos += fprintf(stderr, "-NUM");
535 if (!(opts->flags & PARSE_OPT_NOARG))
536 pos += usage_argh(opts);
538 if (pos <= USAGE_OPTS_WIDTH)
539 pad = USAGE_OPTS_WIDTH - pos;
542 pad = USAGE_OPTS_WIDTH;
544 fprintf(stderr, "%*s%s\n", pad + USAGE_GAP, "", opts->help);
548 return PARSE_OPT_HELP;
551 void usage_with_options(const char * const *usagestr,
552 const struct option *opts)
554 usage_with_options_internal(usagestr, opts, 0);
558 void usage_msg_opt(const char *msg,
559 const char * const *usagestr,
560 const struct option *options)
562 fprintf(stderr, "%s\n\n", msg);
563 usage_with_options(usagestr, options);
566 static int parse_options_usage(const char * const *usagestr,
567 const struct option *opts)
569 return usage_with_options_internal(usagestr, opts, 0);
573 /*----- some often used options -----*/
576 int parse_opt_abbrev_cb(const struct option *opt, const char *arg, int unset)
581 v = unset ? 0 : DEFAULT_ABBREV;
583 v = strtol(arg, (char **)&arg, 10);
585 return opterror(opt, "expects a numerical value", 0);
586 if (v && v < MINIMUM_ABBREV)
591 *(int *)(opt->value) = v;
595 int parse_opt_approxidate_cb(const struct option *opt, const char *arg,
598 *(unsigned long *)(opt->value) = approxidate(arg);
602 int parse_opt_verbosity_cb(const struct option *opt, const char *arg,
605 int *target = opt->value;
608 /* --no-quiet, --no-verbose */
610 else if (opt->short_name == 'v') {
624 int parse_opt_with_commit(const struct option *opt, const char *arg, int unset)
626 unsigned char sha1[20];
627 struct commit *commit;
631 if (get_sha1(arg, sha1))
632 return error("malformed object name %s", arg);
633 commit = lookup_commit_reference(sha1);
635 return error("no such commit %s", arg);
636 commit_list_insert(commit, opt->value);
640 int parse_opt_tertiary(const struct option *opt, const char *arg, int unset)
642 int *target = opt->value;
643 *target = unset ? 2 : 1;