1 #include "git-compat-util.h"
2 #include "parse-options.h"
9 static int opterror(const struct option *opt, const char *reason, int flags)
11 if (flags & OPT_SHORT)
12 return error("switch `%c' %s", opt->short_name, reason);
13 if (flags & OPT_UNSET)
14 return error("option `no-%s' %s", opt->long_name, reason);
15 return error("option `%s' %s", opt->long_name, reason);
18 static int get_arg(struct parse_opt_ctx_t *p, const struct option *opt,
19 int flags, const char **arg)
24 } else if (p->argc == 1 && (opt->flags & PARSE_OPT_LASTARG_DEFAULT)) {
25 *arg = (const char *)opt->defval;
26 } else if (p->argc > 1) {
30 return opterror(opt, "requires a value", flags);
34 static int get_value(struct parse_opt_ctx_t *p,
35 const struct option *opt, int flags)
38 const int unset = flags & OPT_UNSET;
41 return opterror(opt, "takes no value", flags);
42 if (unset && (opt->flags & PARSE_OPT_NONEG))
43 return opterror(opt, "isn't available", flags);
45 if (!(flags & OPT_SHORT) && p->opt) {
48 if (!(opt->flags & PARSE_OPT_NOARG))
56 return opterror(opt, "takes no value", flags);
65 *(int *)opt->value &= ~opt->defval;
67 *(int *)opt->value |= opt->defval;
72 *(int *)opt->value |= opt->defval;
74 *(int *)opt->value &= ~opt->defval;
78 *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
82 *(int *)opt->value = unset ? 0 : opt->defval;
86 *(void **)opt->value = unset ? NULL : (void *)opt->defval;
91 *(const char **)opt->value = NULL;
92 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
93 *(const char **)opt->value = (const char *)opt->defval;
95 return get_arg(p, opt, flags, (const char **)opt->value);
100 return (*opt->callback)(opt, NULL, 1) ? (-1) : 0;
101 if (opt->flags & PARSE_OPT_NOARG)
102 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
103 if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
104 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
105 if (get_arg(p, opt, flags, &arg))
107 return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
111 *(int *)opt->value = 0;
114 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
115 *(int *)opt->value = opt->defval;
118 if (get_arg(p, opt, flags, &arg))
120 *(int *)opt->value = strtol(arg, (char **)&s, 10);
122 return opterror(opt, "expects a numerical value", flags);
126 die("should not happen, someone must be hit on the forehead");
130 static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)
132 const struct option *numopt = NULL;
134 for (; options->type != OPTION_END; options++) {
135 if (options->short_name == *p->opt) {
136 p->opt = p->opt[1] ? p->opt + 1 : NULL;
137 return get_value(p, options, OPT_SHORT);
141 * Handle the numerical option later, explicit one-digit
142 * options take precedence over it.
144 if (options->type == OPTION_NUMBER)
147 if (numopt && isdigit(*p->opt)) {
152 while (isdigit(p->opt[len]))
154 arg = xmemdupz(p->opt, len);
155 p->opt = p->opt[len] ? p->opt + len : NULL;
156 rc = (*numopt->callback)(numopt, arg, 0) ? (-1) : 0;
163 static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
164 const struct option *options)
166 const char *arg_end = strchr(arg, '=');
167 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
168 int abbrev_flags = 0, ambiguous_flags = 0;
171 arg_end = arg + strlen(arg);
173 for (; options->type != OPTION_END; options++) {
177 if (!options->long_name)
180 rest = skip_prefix(arg, options->long_name);
181 if (options->type == OPTION_ARGUMENT) {
185 return opterror(options, "takes no value", flags);
188 p->out[p->cpidx++] = arg - 2;
193 if (!strncmp(options->long_name, arg, arg_end - arg)) {
197 * If this is abbreviated, it is
198 * ambiguous. So when there is no
199 * exact match later, we need to
202 ambiguous_option = abbrev_option;
203 ambiguous_flags = abbrev_flags;
205 if (!(flags & OPT_UNSET) && *arg_end)
206 p->opt = arg_end + 1;
207 abbrev_option = options;
208 abbrev_flags = flags;
211 /* negated and abbreviated very much? */
212 if (!prefixcmp("no-", arg)) {
217 if (strncmp(arg, "no-", 3))
220 rest = skip_prefix(arg + 3, options->long_name);
221 /* abbreviated and negated? */
222 if (!rest && !prefixcmp(options->long_name, arg + 3))
232 return get_value(p, options, flags);
235 if (ambiguous_option)
236 return error("Ambiguous option: %s "
237 "(could be --%s%s or --%s%s)",
239 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
240 ambiguous_option->long_name,
241 (abbrev_flags & OPT_UNSET) ? "no-" : "",
242 abbrev_option->long_name);
244 return get_value(p, abbrev_option, abbrev_flags);
248 static int parse_nodash_opt(struct parse_opt_ctx_t *p, const char *arg,
249 const struct option *options)
251 for (; options->type != OPTION_END; options++) {
252 if (!(options->flags & PARSE_OPT_NODASH))
254 if ((options->flags & PARSE_OPT_OPTARG) ||
255 !(options->flags & PARSE_OPT_NOARG))
256 die("BUG: dashless options don't support arguments");
257 if (!(options->flags & PARSE_OPT_NONEG))
258 die("BUG: dashless options don't support negation");
259 if (options->long_name)
260 die("BUG: dashless options can't be long");
261 if (options->short_name == arg[0] && arg[1] == '\0')
262 return get_value(p, options, OPT_SHORT);
267 static void check_typos(const char *arg, const struct option *options)
272 if (!prefixcmp(arg, "no-")) {
273 error ("did you mean `--%s` (with two dashes ?)", arg);
277 for (; options->type != OPTION_END; options++) {
278 if (!options->long_name)
280 if (!prefixcmp(options->long_name, arg)) {
281 error ("did you mean `--%s` (with two dashes ?)", arg);
287 void parse_options_start(struct parse_opt_ctx_t *ctx,
288 int argc, const char **argv, const char *prefix,
291 memset(ctx, 0, sizeof(*ctx));
292 ctx->argc = argc - 1;
293 ctx->argv = argv + 1;
295 ctx->prefix = prefix;
296 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
298 if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
299 (flags & PARSE_OPT_STOP_AT_NON_OPTION))
300 die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
303 static int usage_with_options_internal(const char * const *,
304 const struct option *, int);
306 int parse_options_step(struct parse_opt_ctx_t *ctx,
307 const struct option *options,
308 const char * const usagestr[])
310 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
312 /* we must reset ->opt, unknown short option leave it dangling */
315 for (; ctx->argc; ctx->argc--, ctx->argv++) {
316 const char *arg = ctx->argv[0];
318 if (*arg != '-' || !arg[1]) {
319 if (parse_nodash_opt(ctx, arg, options) == 0)
321 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
323 ctx->out[ctx->cpidx++] = ctx->argv[0];
329 if (internal_help && *ctx->opt == 'h')
330 return parse_options_usage(usagestr, options);
331 switch (parse_short_opt(ctx, options)) {
333 return parse_options_usage(usagestr, options);
338 check_typos(arg + 1, options);
340 if (internal_help && *ctx->opt == 'h')
341 return parse_options_usage(usagestr, options);
342 switch (parse_short_opt(ctx, options)) {
344 return parse_options_usage(usagestr, options);
346 /* fake a short option thing to hide the fact that we may have
347 * started to parse aggregated stuff
349 * This is leaky, too bad.
351 ctx->argv[0] = xstrdup(ctx->opt - 1);
352 *(char *)ctx->argv[0] = '-';
359 if (!arg[2]) { /* "--" */
360 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
367 if (internal_help && !strcmp(arg + 2, "help-all"))
368 return usage_with_options_internal(usagestr, options, 1);
369 if (internal_help && !strcmp(arg + 2, "help"))
370 return parse_options_usage(usagestr, options);
371 switch (parse_long_opt(ctx, arg + 2, options)) {
373 return parse_options_usage(usagestr, options);
379 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
380 return PARSE_OPT_UNKNOWN;
381 ctx->out[ctx->cpidx++] = ctx->argv[0];
384 return PARSE_OPT_DONE;
387 int parse_options_end(struct parse_opt_ctx_t *ctx)
389 memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out));
390 ctx->out[ctx->cpidx + ctx->argc] = NULL;
391 return ctx->cpidx + ctx->argc;
394 int parse_options(int argc, const char **argv, const char *prefix,
395 const struct option *options, const char * const usagestr[],
398 struct parse_opt_ctx_t ctx;
400 parse_options_start(&ctx, argc, argv, prefix, flags);
401 switch (parse_options_step(&ctx, options, usagestr)) {
406 default: /* PARSE_OPT_UNKNOWN */
407 if (ctx.argv[0][1] == '-') {
408 error("unknown option `%s'", ctx.argv[0] + 2);
410 error("unknown switch `%c'", *ctx.opt);
412 usage_with_options(usagestr, options);
415 return parse_options_end(&ctx);
418 static int usage_argh(const struct option *opts)
421 int literal = opts->flags & PARSE_OPT_LITERAL_ARGHELP;
422 if (opts->flags & PARSE_OPT_OPTARG)
424 s = literal ? "[=%s]" : "[=<%s>]";
426 s = literal ? "[%s]" : "[<%s>]";
428 s = literal ? " %s" : " <%s>";
429 return fprintf(stderr, s, opts->argh);
432 #define USAGE_OPTS_WIDTH 24
435 int usage_with_options_internal(const char * const *usagestr,
436 const struct option *opts, int full)
439 return PARSE_OPT_HELP;
441 fprintf(stderr, "usage: %s\n", *usagestr++);
442 while (*usagestr && **usagestr)
443 fprintf(stderr, " or: %s\n", *usagestr++);
445 fprintf(stderr, "%s%s\n",
446 **usagestr ? " " : "",
451 if (opts->type != OPTION_GROUP)
454 for (; opts->type != OPTION_END; opts++) {
458 if (opts->type == OPTION_GROUP) {
461 fprintf(stderr, "%s\n", opts->help);
464 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
467 pos = fprintf(stderr, " ");
468 if (opts->short_name) {
469 if (opts->flags & PARSE_OPT_NODASH)
470 pos += fprintf(stderr, "%c", opts->short_name);
472 pos += fprintf(stderr, "-%c", opts->short_name);
474 if (opts->long_name && opts->short_name)
475 pos += fprintf(stderr, ", ");
477 pos += fprintf(stderr, "--%s", opts->long_name);
478 if (opts->type == OPTION_NUMBER)
479 pos += fprintf(stderr, "-NUM");
481 switch (opts->type) {
482 case OPTION_ARGUMENT:
485 if (opts->flags & PARSE_OPT_OPTARG)
487 pos += fprintf(stderr, "[=<n>]");
489 pos += fprintf(stderr, "[<n>]");
491 pos += fprintf(stderr, " <n>");
493 case OPTION_CALLBACK:
494 if (opts->flags & PARSE_OPT_NOARG)
499 pos += usage_argh(opts);
501 if (opts->flags & PARSE_OPT_OPTARG)
503 pos += fprintf(stderr, "[=...]");
505 pos += fprintf(stderr, "[...]");
507 pos += fprintf(stderr, " ...");
510 default: /* OPTION_{BIT,BOOLEAN,NUMBER,SET_INT,SET_PTR} */
514 if (pos <= USAGE_OPTS_WIDTH)
515 pad = USAGE_OPTS_WIDTH - pos;
518 pad = USAGE_OPTS_WIDTH;
520 fprintf(stderr, "%*s%s\n", pad + USAGE_GAP, "", opts->help);
524 return PARSE_OPT_HELP;
527 void usage_with_options(const char * const *usagestr,
528 const struct option *opts)
530 usage_with_options_internal(usagestr, opts, 0);
534 int parse_options_usage(const char * const *usagestr,
535 const struct option *opts)
537 return usage_with_options_internal(usagestr, opts, 0);
541 /*----- some often used options -----*/
544 int parse_opt_abbrev_cb(const struct option *opt, const char *arg, int unset)
549 v = unset ? 0 : DEFAULT_ABBREV;
551 v = strtol(arg, (char **)&arg, 10);
553 return opterror(opt, "expects a numerical value", 0);
554 if (v && v < MINIMUM_ABBREV)
559 *(int *)(opt->value) = v;
563 int parse_opt_approxidate_cb(const struct option *opt, const char *arg,
566 *(unsigned long *)(opt->value) = approxidate(arg);
570 int parse_opt_verbosity_cb(const struct option *opt, const char *arg,
573 int *target = opt->value;
576 /* --no-quiet, --no-verbose */
578 else if (opt->short_name == 'v') {
592 int parse_opt_with_commit(const struct option *opt, const char *arg, int unset)
594 unsigned char sha1[20];
595 struct commit *commit;
599 if (get_sha1(arg, sha1))
600 return error("malformed object name %s", arg);
601 commit = lookup_commit_reference(sha1);
603 return error("no such commit %s", arg);
604 commit_list_insert(commit, opt->value);
609 * This should really be OPTION_FILENAME type as a part of
610 * parse_options that take prefix to do this while parsing.
612 extern const char *parse_options_fix_filename(const char *prefix, const char *file)
614 if (!file || !prefix || is_absolute_path(file) || !strcmp("-", file))
616 return prefix_filename(prefix, strlen(prefix), file);