test-gitmw-lib.sh: use the $( ... ) construct for command substitution
[git.git] / parse-options.c
bloba5fa0b8938cb2820635bbc620b1b26cc903a6c97
1 #include "git-compat-util.h"
2 #include "parse-options.h"
3 #include "cache.h"
4 #include "commit.h"
5 #include "color.h"
6 #include "utf8.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);
12 #define OPT_SHORT 1
13 #define OPT_UNSET 2
15 int optbug(const struct option *opt, const char *reason)
17 if (opt->long_name)
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 get_arg(struct parse_opt_ctx_t *p, const struct option *opt,
23 int flags, const char **arg)
25 if (p->opt) {
26 *arg = p->opt;
27 p->opt = NULL;
28 } else if (p->argc == 1 && (opt->flags & PARSE_OPT_LASTARG_DEFAULT)) {
29 *arg = (const char *)opt->defval;
30 } else if (p->argc > 1) {
31 p->argc--;
32 *arg = *++p->argv;
33 } else
34 return opterror(opt, "requires a value", flags);
35 return 0;
38 static void fix_filename(const char *prefix, const char **file)
40 if (!file || !*file || !prefix || is_absolute_path(*file)
41 || !strcmp("-", *file))
42 return;
43 *file = xstrdup(prefix_filename(prefix, strlen(prefix), *file));
46 static int opt_command_mode_error(const struct option *opt,
47 const struct option *all_opts,
48 int flags)
50 const struct option *that;
51 struct strbuf message = STRBUF_INIT;
52 struct strbuf that_name = STRBUF_INIT;
55 * Find the other option that was used to set the variable
56 * already, and report that this is not compatible with it.
58 for (that = all_opts; that->type != OPTION_END; that++) {
59 if (that == opt ||
60 that->type != OPTION_CMDMODE ||
61 that->value != opt->value ||
62 that->defval != *(int *)opt->value)
63 continue;
65 if (that->long_name)
66 strbuf_addf(&that_name, "--%s", that->long_name);
67 else
68 strbuf_addf(&that_name, "-%c", that->short_name);
69 strbuf_addf(&message, ": incompatible with %s", that_name.buf);
70 strbuf_release(&that_name);
71 opterror(opt, message.buf, flags);
72 strbuf_release(&message);
73 return -1;
75 return opterror(opt, ": incompatible with something else", flags);
78 static int get_value(struct parse_opt_ctx_t *p,
79 const struct option *opt,
80 const struct option *all_opts,
81 int flags)
83 const char *s, *arg;
84 const int unset = flags & OPT_UNSET;
85 int err;
87 if (unset && p->opt)
88 return opterror(opt, "takes no value", flags);
89 if (unset && (opt->flags & PARSE_OPT_NONEG))
90 return opterror(opt, "isn't available", flags);
91 if (!(flags & OPT_SHORT) && p->opt && (opt->flags & PARSE_OPT_NOARG))
92 return opterror(opt, "takes no value", flags);
94 switch (opt->type) {
95 case OPTION_LOWLEVEL_CALLBACK:
96 return (*(parse_opt_ll_cb *)opt->callback)(p, opt, unset);
98 case OPTION_BIT:
99 if (unset)
100 *(int *)opt->value &= ~opt->defval;
101 else
102 *(int *)opt->value |= opt->defval;
103 return 0;
105 case OPTION_NEGBIT:
106 if (unset)
107 *(int *)opt->value |= opt->defval;
108 else
109 *(int *)opt->value &= ~opt->defval;
110 return 0;
112 case OPTION_COUNTUP:
113 *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
114 return 0;
116 case OPTION_SET_INT:
117 *(int *)opt->value = unset ? 0 : opt->defval;
118 return 0;
120 case OPTION_CMDMODE:
122 * Giving the same mode option twice, although is unnecessary,
123 * is not a grave error, so let it pass.
125 if (*(int *)opt->value && *(int *)opt->value != opt->defval)
126 return opt_command_mode_error(opt, all_opts, flags);
127 *(int *)opt->value = opt->defval;
128 return 0;
130 case OPTION_SET_PTR:
131 *(void **)opt->value = unset ? NULL : (void *)opt->defval;
132 return 0;
134 case OPTION_STRING:
135 if (unset)
136 *(const char **)opt->value = NULL;
137 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
138 *(const char **)opt->value = (const char *)opt->defval;
139 else
140 return get_arg(p, opt, flags, (const char **)opt->value);
141 return 0;
143 case OPTION_FILENAME:
144 err = 0;
145 if (unset)
146 *(const char **)opt->value = NULL;
147 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
148 *(const char **)opt->value = (const char *)opt->defval;
149 else
150 err = get_arg(p, opt, flags, (const char **)opt->value);
152 if (!err)
153 fix_filename(p->prefix, (const char **)opt->value);
154 return err;
156 case OPTION_CALLBACK:
157 if (unset)
158 return (*opt->callback)(opt, NULL, 1) ? (-1) : 0;
159 if (opt->flags & PARSE_OPT_NOARG)
160 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
161 if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
162 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
163 if (get_arg(p, opt, flags, &arg))
164 return -1;
165 return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
167 case OPTION_INTEGER:
168 if (unset) {
169 *(int *)opt->value = 0;
170 return 0;
172 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
173 *(int *)opt->value = opt->defval;
174 return 0;
176 if (get_arg(p, opt, flags, &arg))
177 return -1;
178 *(int *)opt->value = strtol(arg, (char **)&s, 10);
179 if (*s)
180 return opterror(opt, "expects a numerical value", flags);
181 return 0;
183 default:
184 die("should not happen, someone must be hit on the forehead");
188 static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)
190 const struct option *all_opts = options;
191 const struct option *numopt = NULL;
193 for (; options->type != OPTION_END; options++) {
194 if (options->short_name == *p->opt) {
195 p->opt = p->opt[1] ? p->opt + 1 : NULL;
196 return get_value(p, options, all_opts, OPT_SHORT);
200 * Handle the numerical option later, explicit one-digit
201 * options take precedence over it.
203 if (options->type == OPTION_NUMBER)
204 numopt = options;
206 if (numopt && isdigit(*p->opt)) {
207 size_t len = 1;
208 char *arg;
209 int rc;
211 while (isdigit(p->opt[len]))
212 len++;
213 arg = xmemdupz(p->opt, len);
214 p->opt = p->opt[len] ? p->opt + len : NULL;
215 rc = (*numopt->callback)(numopt, arg, 0) ? (-1) : 0;
216 free(arg);
217 return rc;
219 return -2;
222 static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
223 const struct option *options)
225 const struct option *all_opts = options;
226 const char *arg_end = strchrnul(arg, '=');
227 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
228 int abbrev_flags = 0, ambiguous_flags = 0;
230 for (; options->type != OPTION_END; options++) {
231 const char *rest, *long_name = options->long_name;
232 int flags = 0, opt_flags = 0;
234 if (!long_name)
235 continue;
237 again:
238 rest = skip_prefix(arg, long_name);
239 if (options->type == OPTION_ARGUMENT) {
240 if (!rest)
241 continue;
242 if (*rest == '=')
243 return opterror(options, "takes no value", flags);
244 if (*rest)
245 continue;
246 p->out[p->cpidx++] = arg - 2;
247 return 0;
249 if (!rest) {
250 /* abbreviated? */
251 if (!strncmp(long_name, arg, arg_end - arg)) {
252 is_abbreviated:
253 if (abbrev_option) {
255 * If this is abbreviated, it is
256 * ambiguous. So when there is no
257 * exact match later, we need to
258 * error out.
260 ambiguous_option = abbrev_option;
261 ambiguous_flags = abbrev_flags;
263 if (!(flags & OPT_UNSET) && *arg_end)
264 p->opt = arg_end + 1;
265 abbrev_option = options;
266 abbrev_flags = flags ^ opt_flags;
267 continue;
269 /* negation allowed? */
270 if (options->flags & PARSE_OPT_NONEG)
271 continue;
272 /* negated and abbreviated very much? */
273 if (starts_with("no-", arg)) {
274 flags |= OPT_UNSET;
275 goto is_abbreviated;
277 /* negated? */
278 if (!starts_with(arg, "no-")) {
279 if (starts_with(long_name, "no-")) {
280 long_name += 3;
281 opt_flags |= OPT_UNSET;
282 goto again;
284 continue;
286 flags |= OPT_UNSET;
287 rest = skip_prefix(arg + 3, long_name);
288 /* abbreviated and negated? */
289 if (!rest && starts_with(long_name, arg + 3))
290 goto is_abbreviated;
291 if (!rest)
292 continue;
294 if (*rest) {
295 if (*rest != '=')
296 continue;
297 p->opt = rest + 1;
299 return get_value(p, options, all_opts, flags ^ opt_flags);
302 if (ambiguous_option)
303 return error("Ambiguous option: %s "
304 "(could be --%s%s or --%s%s)",
305 arg,
306 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
307 ambiguous_option->long_name,
308 (abbrev_flags & OPT_UNSET) ? "no-" : "",
309 abbrev_option->long_name);
310 if (abbrev_option)
311 return get_value(p, abbrev_option, all_opts, abbrev_flags);
312 return -2;
315 static int parse_nodash_opt(struct parse_opt_ctx_t *p, const char *arg,
316 const struct option *options)
318 const struct option *all_opts = options;
320 for (; options->type != OPTION_END; options++) {
321 if (!(options->flags & PARSE_OPT_NODASH))
322 continue;
323 if (options->short_name == arg[0] && arg[1] == '\0')
324 return get_value(p, options, all_opts, OPT_SHORT);
326 return -2;
329 static void check_typos(const char *arg, const struct option *options)
331 if (strlen(arg) < 3)
332 return;
334 if (starts_with(arg, "no-")) {
335 error ("did you mean `--%s` (with two dashes ?)", arg);
336 exit(129);
339 for (; options->type != OPTION_END; options++) {
340 if (!options->long_name)
341 continue;
342 if (starts_with(options->long_name, arg)) {
343 error ("did you mean `--%s` (with two dashes ?)", arg);
344 exit(129);
349 static void parse_options_check(const struct option *opts)
351 int err = 0;
353 for (; opts->type != OPTION_END; opts++) {
354 if ((opts->flags & PARSE_OPT_LASTARG_DEFAULT) &&
355 (opts->flags & PARSE_OPT_OPTARG))
356 err |= optbug(opts, "uses incompatible flags "
357 "LASTARG_DEFAULT and OPTARG");
358 if (opts->flags & PARSE_OPT_NODASH &&
359 ((opts->flags & PARSE_OPT_OPTARG) ||
360 !(opts->flags & PARSE_OPT_NOARG) ||
361 !(opts->flags & PARSE_OPT_NONEG) ||
362 opts->long_name))
363 err |= optbug(opts, "uses feature "
364 "not supported for dashless options");
365 switch (opts->type) {
366 case OPTION_COUNTUP:
367 case OPTION_BIT:
368 case OPTION_NEGBIT:
369 case OPTION_SET_INT:
370 case OPTION_SET_PTR:
371 case OPTION_NUMBER:
372 if ((opts->flags & PARSE_OPT_OPTARG) ||
373 !(opts->flags & PARSE_OPT_NOARG))
374 err |= optbug(opts, "should not accept an argument");
375 default:
376 ; /* ok. (usually accepts an argument) */
379 if (err)
380 exit(128);
383 void parse_options_start(struct parse_opt_ctx_t *ctx,
384 int argc, const char **argv, const char *prefix,
385 const struct option *options, int flags)
387 memset(ctx, 0, sizeof(*ctx));
388 ctx->argc = argc - 1;
389 ctx->argv = argv + 1;
390 ctx->out = argv;
391 ctx->prefix = prefix;
392 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
393 ctx->flags = flags;
394 if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
395 (flags & PARSE_OPT_STOP_AT_NON_OPTION))
396 die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
397 parse_options_check(options);
400 static int usage_with_options_internal(struct parse_opt_ctx_t *,
401 const char * const *,
402 const struct option *, int, int);
404 int parse_options_step(struct parse_opt_ctx_t *ctx,
405 const struct option *options,
406 const char * const usagestr[])
408 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
410 /* we must reset ->opt, unknown short option leave it dangling */
411 ctx->opt = NULL;
413 for (; ctx->argc; ctx->argc--, ctx->argv++) {
414 const char *arg = ctx->argv[0];
416 if (*arg != '-' || !arg[1]) {
417 if (parse_nodash_opt(ctx, arg, options) == 0)
418 continue;
419 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
420 return PARSE_OPT_NON_OPTION;
421 ctx->out[ctx->cpidx++] = ctx->argv[0];
422 continue;
425 if (arg[1] != '-') {
426 ctx->opt = arg + 1;
427 if (internal_help && *ctx->opt == 'h')
428 return parse_options_usage(ctx, usagestr, options, 0);
429 switch (parse_short_opt(ctx, options)) {
430 case -1:
431 return parse_options_usage(ctx, usagestr, options, 1);
432 case -2:
433 if (ctx->opt)
434 check_typos(arg + 1, options);
435 goto unknown;
437 if (ctx->opt)
438 check_typos(arg + 1, options);
439 while (ctx->opt) {
440 if (internal_help && *ctx->opt == 'h')
441 return parse_options_usage(ctx, usagestr, options, 0);
442 switch (parse_short_opt(ctx, options)) {
443 case -1:
444 return parse_options_usage(ctx, usagestr, options, 1);
445 case -2:
446 /* fake a short option thing to hide the fact that we may have
447 * started to parse aggregated stuff
449 * This is leaky, too bad.
451 ctx->argv[0] = xstrdup(ctx->opt - 1);
452 *(char *)ctx->argv[0] = '-';
453 goto unknown;
456 continue;
459 if (!arg[2]) { /* "--" */
460 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
461 ctx->argc--;
462 ctx->argv++;
464 break;
467 if (internal_help && !strcmp(arg + 2, "help-all"))
468 return usage_with_options_internal(ctx, usagestr, options, 1, 0);
469 if (internal_help && !strcmp(arg + 2, "help"))
470 return parse_options_usage(ctx, usagestr, options, 0);
471 switch (parse_long_opt(ctx, arg + 2, options)) {
472 case -1:
473 return parse_options_usage(ctx, usagestr, options, 1);
474 case -2:
475 goto unknown;
477 continue;
478 unknown:
479 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
480 return PARSE_OPT_UNKNOWN;
481 ctx->out[ctx->cpidx++] = ctx->argv[0];
482 ctx->opt = NULL;
484 return PARSE_OPT_DONE;
487 int parse_options_end(struct parse_opt_ctx_t *ctx)
489 memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out));
490 ctx->out[ctx->cpidx + ctx->argc] = NULL;
491 return ctx->cpidx + ctx->argc;
494 int parse_options(int argc, const char **argv, const char *prefix,
495 const struct option *options, const char * const usagestr[],
496 int flags)
498 struct parse_opt_ctx_t ctx;
500 parse_options_start(&ctx, argc, argv, prefix, options, flags);
501 switch (parse_options_step(&ctx, options, usagestr)) {
502 case PARSE_OPT_HELP:
503 exit(129);
504 case PARSE_OPT_NON_OPTION:
505 case PARSE_OPT_DONE:
506 break;
507 default: /* PARSE_OPT_UNKNOWN */
508 if (ctx.argv[0][1] == '-') {
509 error("unknown option `%s'", ctx.argv[0] + 2);
510 } else if (isascii(*ctx.opt)) {
511 error("unknown switch `%c'", *ctx.opt);
512 } else {
513 error("unknown non-ascii option in string: `%s'",
514 ctx.argv[0]);
516 usage_with_options(usagestr, options);
519 precompose_argv(argc, argv);
520 return parse_options_end(&ctx);
523 static int usage_argh(const struct option *opts, FILE *outfile)
525 const char *s;
526 int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) || !opts->argh;
527 if (opts->flags & PARSE_OPT_OPTARG)
528 if (opts->long_name)
529 s = literal ? "[=%s]" : "[=<%s>]";
530 else
531 s = literal ? "[%s]" : "[<%s>]";
532 else
533 s = literal ? " %s" : " <%s>";
534 return utf8_fprintf(outfile, s, opts->argh ? _(opts->argh) : _("..."));
537 #define USAGE_OPTS_WIDTH 24
538 #define USAGE_GAP 2
540 static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
541 const char * const *usagestr,
542 const struct option *opts, int full, int err)
544 FILE *outfile = err ? stderr : stdout;
546 if (!usagestr)
547 return PARSE_OPT_HELP;
549 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
550 fprintf(outfile, "cat <<\\EOF\n");
552 fprintf_ln(outfile, _("usage: %s"), _(*usagestr++));
553 while (*usagestr && **usagestr)
554 /* TRANSLATORS: the colon here should align with the
555 one in "usage: %s" translation */
556 fprintf_ln(outfile, _(" or: %s"), _(*usagestr++));
557 while (*usagestr) {
558 if (**usagestr)
559 fprintf_ln(outfile, _(" %s"), _(*usagestr));
560 else
561 putchar('\n');
562 usagestr++;
565 if (opts->type != OPTION_GROUP)
566 fputc('\n', outfile);
568 for (; opts->type != OPTION_END; opts++) {
569 size_t pos;
570 int pad;
572 if (opts->type == OPTION_GROUP) {
573 fputc('\n', outfile);
574 if (*opts->help)
575 fprintf(outfile, "%s\n", _(opts->help));
576 continue;
578 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
579 continue;
581 pos = fprintf(outfile, " ");
582 if (opts->short_name) {
583 if (opts->flags & PARSE_OPT_NODASH)
584 pos += fprintf(outfile, "%c", opts->short_name);
585 else
586 pos += fprintf(outfile, "-%c", opts->short_name);
588 if (opts->long_name && opts->short_name)
589 pos += fprintf(outfile, ", ");
590 if (opts->long_name)
591 pos += fprintf(outfile, "--%s", opts->long_name);
592 if (opts->type == OPTION_NUMBER)
593 pos += utf8_fprintf(outfile, _("-NUM"));
595 if ((opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
596 !(opts->flags & PARSE_OPT_NOARG))
597 pos += usage_argh(opts, outfile);
599 if (pos <= USAGE_OPTS_WIDTH)
600 pad = USAGE_OPTS_WIDTH - pos;
601 else {
602 fputc('\n', outfile);
603 pad = USAGE_OPTS_WIDTH;
605 fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", _(opts->help));
607 fputc('\n', outfile);
609 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
610 fputs("EOF\n", outfile);
612 return PARSE_OPT_HELP;
615 void NORETURN usage_with_options(const char * const *usagestr,
616 const struct option *opts)
618 usage_with_options_internal(NULL, usagestr, opts, 0, 1);
619 exit(129);
622 void NORETURN usage_msg_opt(const char *msg,
623 const char * const *usagestr,
624 const struct option *options)
626 fprintf(stderr, "%s\n\n", msg);
627 usage_with_options(usagestr, options);
630 static int parse_options_usage(struct parse_opt_ctx_t *ctx,
631 const char * const *usagestr,
632 const struct option *opts, int err)
634 return usage_with_options_internal(ctx, usagestr, opts, 0, err);
637 #undef opterror
638 int opterror(const struct option *opt, const char *reason, int flags)
640 if (flags & OPT_SHORT)
641 return error("switch `%c' %s", opt->short_name, reason);
642 if (flags & OPT_UNSET)
643 return error("option `no-%s' %s", opt->long_name, reason);
644 return error("option `%s' %s", opt->long_name, reason);