The sixteenth batch
[git/gitster.git] / parse-options.c
blob5224203ffe7bf8a70b4f58060bed20a5dad1afae
1 #include "git-compat-util.h"
2 #include "parse-options.h"
3 #include "abspath.h"
4 #include "parse.h"
5 #include "gettext.h"
6 #include "strbuf.h"
7 #include "string-list.h"
8 #include "utf8.h"
10 static int disallow_abbreviated_options;
12 enum opt_parsed {
13 OPT_LONG = 0,
14 OPT_SHORT = 1<<0,
15 OPT_UNSET = 1<<1,
18 static void optbug(const struct option *opt, const char *reason)
20 if (opt->long_name && opt->short_name)
21 bug("switch '%c' (--%s) %s", opt->short_name,
22 opt->long_name, reason);
23 else if (opt->long_name)
24 bug("option '%s' %s", opt->long_name, reason);
25 else
26 bug("switch '%c' %s", opt->short_name, reason);
29 static const char *optname(const struct option *opt, enum opt_parsed flags)
31 static struct strbuf sb = STRBUF_INIT;
33 strbuf_reset(&sb);
34 if (flags & OPT_SHORT)
35 strbuf_addf(&sb, "switch `%c'", opt->short_name);
36 else if (flags & OPT_UNSET)
37 strbuf_addf(&sb, "option `no-%s'", opt->long_name);
38 else if (flags == OPT_LONG)
39 strbuf_addf(&sb, "option `%s'", opt->long_name);
40 else
41 BUG("optname() got unknown flags %d", flags);
43 return sb.buf;
46 static enum parse_opt_result get_arg(struct parse_opt_ctx_t *p,
47 const struct option *opt,
48 enum opt_parsed flags, const char **arg)
50 if (p->opt) {
51 *arg = p->opt;
52 p->opt = NULL;
53 } else if (p->argc == 1 && (opt->flags & PARSE_OPT_LASTARG_DEFAULT)) {
54 *arg = (const char *)opt->defval;
55 } else if (p->argc > 1) {
56 p->argc--;
57 *arg = *++p->argv;
58 } else
59 return error(_("%s requires a value"), optname(opt, flags));
60 return 0;
63 static char *fix_filename(const char *prefix, const char *file)
65 if (!file || !*file)
66 return NULL;
67 else
68 return prefix_filename_except_for_dash(prefix, file);
71 static int do_get_int_value(const void *value, size_t precision, intmax_t *ret)
73 switch (precision) {
74 case sizeof(int8_t):
75 *ret = *(int8_t *)value;
76 return 0;
77 case sizeof(int16_t):
78 *ret = *(int16_t *)value;
79 return 0;
80 case sizeof(int32_t):
81 *ret = *(int32_t *)value;
82 return 0;
83 case sizeof(int64_t):
84 *ret = *(int64_t *)value;
85 return 0;
86 default:
87 return -1;
91 static intmax_t get_int_value(const struct option *opt, enum opt_parsed flags)
93 intmax_t ret;
94 if (do_get_int_value(opt->value, opt->precision, &ret))
95 BUG("invalid precision for option %s", optname(opt, flags));
96 return ret;
99 static enum parse_opt_result set_int_value(const struct option *opt,
100 enum opt_parsed flags,
101 intmax_t value)
103 switch (opt->precision) {
104 case sizeof(int8_t):
105 *(int8_t *)opt->value = value;
106 return 0;
107 case sizeof(int16_t):
108 *(int16_t *)opt->value = value;
109 return 0;
110 case sizeof(int32_t):
111 *(int32_t *)opt->value = value;
112 return 0;
113 case sizeof(int64_t):
114 *(int64_t *)opt->value = value;
115 return 0;
116 default:
117 BUG("invalid precision for option %s", optname(opt, flags));
121 static int signed_int_fits(intmax_t value, size_t precision)
123 size_t bits = precision * CHAR_BIT;
124 intmax_t upper_bound = INTMAX_MAX >> (bitsizeof(intmax_t) - bits);
125 intmax_t lower_bound = -upper_bound - 1;
126 return lower_bound <= value && value <= upper_bound;
129 static enum parse_opt_result do_get_value(struct parse_opt_ctx_t *p,
130 const struct option *opt,
131 enum opt_parsed flags,
132 const char **argp)
134 const char *arg;
135 const int unset = flags & OPT_UNSET;
136 int err;
138 if (unset && p->opt)
139 return error(_("%s takes no value"), optname(opt, flags));
140 if (unset && (opt->flags & PARSE_OPT_NONEG))
141 return error(_("%s isn't available"), optname(opt, flags));
142 if (!(flags & OPT_SHORT) && p->opt && (opt->flags & PARSE_OPT_NOARG))
143 return error(_("%s takes no value"), optname(opt, flags));
145 switch (opt->type) {
146 case OPTION_LOWLEVEL_CALLBACK:
147 return opt->ll_callback(p, opt, NULL, unset);
149 case OPTION_BIT:
151 intmax_t value = get_int_value(opt, flags);
152 if (unset)
153 value &= ~opt->defval;
154 else
155 value |= opt->defval;
156 return set_int_value(opt, flags, value);
159 case OPTION_NEGBIT:
161 intmax_t value = get_int_value(opt, flags);
162 if (unset)
163 value |= opt->defval;
164 else
165 value &= ~opt->defval;
166 return set_int_value(opt, flags, value);
169 case OPTION_BITOP:
171 intmax_t value = get_int_value(opt, flags);
172 if (unset)
173 BUG("BITOP can't have unset form");
174 value &= ~opt->extra;
175 value |= opt->defval;
176 return set_int_value(opt, flags, value);
179 case OPTION_COUNTUP:
181 size_t bits = CHAR_BIT * opt->precision;
182 intmax_t upper_bound = INTMAX_MAX >> (bitsizeof(intmax_t) - bits);
183 intmax_t value = get_int_value(opt, flags);
185 if (value < 0)
186 value = 0;
187 if (unset)
188 value = 0;
189 else if (value < upper_bound)
190 value++;
191 else
192 return error(_("value for %s exceeds %"PRIdMAX),
193 optname(opt, flags), upper_bound);
194 return set_int_value(opt, flags, value);
197 case OPTION_SET_INT:
198 return set_int_value(opt, flags, unset ? 0 : opt->defval);
200 case OPTION_STRING:
201 if (unset)
202 *(const char **)opt->value = NULL;
203 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
204 *(const char **)opt->value = (const char *)opt->defval;
205 else
206 return get_arg(p, opt, flags, (const char **)opt->value);
207 return 0;
209 case OPTION_FILENAME:
211 const char *value;
213 FREE_AND_NULL(*(char **)opt->value);
215 err = 0;
217 if (unset)
218 value = NULL;
219 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
220 value = (const char *) opt->defval;
221 else
222 err = get_arg(p, opt, flags, &value);
224 if (!err)
225 *(char **)opt->value = fix_filename(p->prefix, value);
226 return err;
228 case OPTION_CALLBACK:
230 const char *p_arg = NULL;
231 int p_unset;
233 if (unset)
234 p_unset = 1;
235 else if (opt->flags & PARSE_OPT_NOARG)
236 p_unset = 0;
237 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
238 p_unset = 0;
239 else if (get_arg(p, opt, flags, &arg))
240 return -1;
241 else {
242 p_unset = 0;
243 p_arg = arg;
245 if (opt->flags & PARSE_OPT_CMDMODE)
246 *argp = p_arg;
247 if (opt->callback)
248 return (*opt->callback)(opt, p_arg, p_unset) ? (-1) : 0;
249 else
250 return (*opt->ll_callback)(p, opt, p_arg, p_unset);
252 case OPTION_INTEGER:
254 intmax_t upper_bound = INTMAX_MAX >> (bitsizeof(intmax_t) - CHAR_BIT * opt->precision);
255 intmax_t lower_bound = -upper_bound - 1;
256 intmax_t value;
258 if (unset) {
259 value = 0;
260 } else if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
261 value = opt->defval;
262 } else if (get_arg(p, opt, flags, &arg)) {
263 return -1;
264 } else if (!*arg) {
265 return error(_("%s expects a numerical value"),
266 optname(opt, flags));
267 } else if (!git_parse_signed(arg, &value, upper_bound)) {
268 if (errno == ERANGE)
269 return error(_("value %s for %s not in range [%"PRIdMAX",%"PRIdMAX"]"),
270 arg, optname(opt, flags), lower_bound, upper_bound);
272 return error(_("%s expects an integer value with an optional k/m/g suffix"),
273 optname(opt, flags));
276 if (value < lower_bound)
277 return error(_("value %s for %s not in range [%"PRIdMAX",%"PRIdMAX"]"),
278 arg, optname(opt, flags), (intmax_t)lower_bound, (intmax_t)upper_bound);
280 return set_int_value(opt, flags, value);
282 case OPTION_UNSIGNED:
284 uintmax_t upper_bound = UINTMAX_MAX >> (bitsizeof(uintmax_t) - CHAR_BIT * opt->precision);
285 uintmax_t value;
287 if (unset) {
288 value = 0;
289 } else if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
290 value = opt->defval;
291 } else if (get_arg(p, opt, flags, &arg)) {
292 return -1;
293 } else if (!*arg) {
294 return error(_("%s expects a numerical value"),
295 optname(opt, flags));
296 } else if (!git_parse_unsigned(arg, &value, upper_bound)) {
297 if (errno == ERANGE)
298 return error(_("value %s for %s not in range [%"PRIdMAX",%"PRIdMAX"]"),
299 arg, optname(opt, flags), (uintmax_t) 0, upper_bound);
301 return error(_("%s expects a non-negative integer value"
302 " with an optional k/m/g suffix"),
303 optname(opt, flags));
306 switch (opt->precision) {
307 case 1:
308 *(uint8_t *)opt->value = value;
309 return 0;
310 case 2:
311 *(uint16_t *)opt->value = value;
312 return 0;
313 case 4:
314 *(uint32_t *)opt->value = value;
315 return 0;
316 case 8:
317 *(uint64_t *)opt->value = value;
318 return 0;
319 default:
320 BUG("invalid precision for option %s",
321 optname(opt, flags));
325 default:
326 BUG("opt->type %d should not happen", opt->type);
330 struct parse_opt_cmdmode_list {
331 intmax_t value;
332 void *value_ptr;
333 size_t precision;
334 const struct option *opt;
335 const char *arg;
336 enum opt_parsed flags;
337 struct parse_opt_cmdmode_list *next;
340 static void build_cmdmode_list(struct parse_opt_ctx_t *ctx,
341 const struct option *opts)
343 ctx->cmdmode_list = NULL;
345 for (; opts->type != OPTION_END; opts++) {
346 struct parse_opt_cmdmode_list *elem = ctx->cmdmode_list;
347 void *value_ptr = opts->value;
349 if (!(opts->flags & PARSE_OPT_CMDMODE) || !value_ptr)
350 continue;
352 while (elem && elem->value_ptr != value_ptr)
353 elem = elem->next;
354 if (elem)
355 continue;
357 CALLOC_ARRAY(elem, 1);
358 elem->value_ptr = value_ptr;
359 elem->precision = opts->precision;
360 if (do_get_int_value(value_ptr, opts->precision, &elem->value))
361 optbug(opts, "has invalid precision");
362 elem->next = ctx->cmdmode_list;
363 ctx->cmdmode_list = elem;
365 BUG_if_bug("invalid 'struct option'");
368 static char *optnamearg(const struct option *opt, const char *arg,
369 enum opt_parsed flags)
371 if (flags & OPT_SHORT)
372 return xstrfmt("-%c%s", opt->short_name, arg ? arg : "");
373 return xstrfmt("--%s%s%s%s", flags & OPT_UNSET ? "no-" : "",
374 opt->long_name, arg ? "=" : "", arg ? arg : "");
377 static enum parse_opt_result get_value(struct parse_opt_ctx_t *p,
378 const struct option *opt,
379 enum opt_parsed flags)
381 const char *arg = NULL;
382 enum parse_opt_result result = do_get_value(p, opt, flags, &arg);
383 struct parse_opt_cmdmode_list *elem = p->cmdmode_list;
384 char *opt_name, *other_opt_name;
386 for (; elem; elem = elem->next) {
387 intmax_t new_value;
389 if (do_get_int_value(elem->value_ptr, elem->precision,
390 &new_value))
391 BUG("impossible: invalid precision");
393 if (new_value == elem->value)
394 continue;
396 if (elem->opt &&
397 (elem->opt->flags | opt->flags) & PARSE_OPT_CMDMODE)
398 break;
400 elem->opt = opt;
401 elem->arg = arg;
402 elem->flags = flags;
403 elem->value = new_value;
406 if (result || !elem)
407 return result;
409 opt_name = optnamearg(opt, arg, flags);
410 other_opt_name = optnamearg(elem->opt, elem->arg, elem->flags);
411 error(_("options '%s' and '%s' cannot be used together"),
412 opt_name, other_opt_name);
413 free(opt_name);
414 free(other_opt_name);
415 return -1;
418 static enum parse_opt_result parse_short_opt(struct parse_opt_ctx_t *p,
419 const struct option *options)
421 const struct option *numopt = NULL;
423 for (; options->type != OPTION_END; options++) {
424 if (options->short_name == *p->opt) {
425 p->opt = p->opt[1] ? p->opt + 1 : NULL;
426 return get_value(p, options, OPT_SHORT);
430 * Handle the numerical option later, explicit one-digit
431 * options take precedence over it.
433 if (options->type == OPTION_NUMBER)
434 numopt = options;
436 if (numopt && isdigit(*p->opt)) {
437 size_t len = 1;
438 char *arg;
439 int rc;
441 while (isdigit(p->opt[len]))
442 len++;
443 arg = xmemdupz(p->opt, len);
444 p->opt = p->opt[len] ? p->opt + len : NULL;
445 if (numopt->callback)
446 rc = (*numopt->callback)(numopt, arg, 0) ? (-1) : 0;
447 else
448 rc = (*numopt->ll_callback)(p, numopt, arg, 0);
449 free(arg);
450 return rc;
452 return PARSE_OPT_UNKNOWN;
455 static int has_string(const char *it, const char **array)
457 while (*array)
458 if (!strcmp(it, *(array++)))
459 return 1;
460 return 0;
463 static int is_alias(struct parse_opt_ctx_t *ctx,
464 const struct option *one_opt,
465 const struct option *another_opt)
467 const char **group;
469 if (!ctx->alias_groups)
470 return 0;
472 if (!one_opt->long_name || !another_opt->long_name)
473 return 0;
475 for (group = ctx->alias_groups; *group; group += 3) {
476 /* it and other are from the same family? */
477 if (has_string(one_opt->long_name, group) &&
478 has_string(another_opt->long_name, group))
479 return 1;
481 return 0;
484 struct parsed_option {
485 const struct option *option;
486 enum opt_parsed flags;
489 static void register_abbrev(struct parse_opt_ctx_t *p,
490 const struct option *option, enum opt_parsed flags,
491 struct parsed_option *abbrev,
492 struct parsed_option *ambiguous)
494 if (p->flags & PARSE_OPT_KEEP_UNKNOWN_OPT)
495 return;
496 if (abbrev->option &&
497 !(abbrev->flags == flags && is_alias(p, abbrev->option, option))) {
499 * If this is abbreviated, it is
500 * ambiguous. So when there is no
501 * exact match later, we need to
502 * error out.
504 ambiguous->option = abbrev->option;
505 ambiguous->flags = abbrev->flags;
507 abbrev->option = option;
508 abbrev->flags = flags;
511 static enum parse_opt_result parse_long_opt(
512 struct parse_opt_ctx_t *p, const char *arg,
513 const struct option *options)
515 const char *arg_end = strchrnul(arg, '=');
516 const char *arg_start = arg;
517 enum opt_parsed flags = OPT_LONG;
518 int arg_starts_with_no_no = 0;
519 struct parsed_option abbrev = { .option = NULL, .flags = OPT_LONG };
520 struct parsed_option ambiguous = { .option = NULL, .flags = OPT_LONG };
522 if (skip_prefix(arg_start, "no-", &arg_start)) {
523 if (skip_prefix(arg_start, "no-", &arg_start))
524 arg_starts_with_no_no = 1;
525 else
526 flags |= OPT_UNSET;
529 for (; options->type != OPTION_END; options++) {
530 const char *rest, *long_name = options->long_name;
531 enum opt_parsed opt_flags = OPT_LONG;
532 int allow_unset = !(options->flags & PARSE_OPT_NONEG);
534 if (options->type == OPTION_SUBCOMMAND)
535 continue;
536 if (!long_name)
537 continue;
539 if (skip_prefix(long_name, "no-", &long_name))
540 opt_flags |= OPT_UNSET;
541 else if (arg_starts_with_no_no)
542 continue;
544 if (((flags ^ opt_flags) & OPT_UNSET) && !allow_unset)
545 continue;
547 if (skip_prefix(arg_start, long_name, &rest)) {
548 if (*rest == '=')
549 p->opt = rest + 1;
550 else if (*rest)
551 continue;
552 return get_value(p, options, flags ^ opt_flags);
555 /* abbreviated? */
556 if (!strncmp(long_name, arg_start, arg_end - arg_start))
557 register_abbrev(p, options, flags ^ opt_flags,
558 &abbrev, &ambiguous);
560 /* negated and abbreviated very much? */
561 if (allow_unset && starts_with("no-", arg))
562 register_abbrev(p, options, OPT_UNSET ^ opt_flags,
563 &abbrev, &ambiguous);
566 if (disallow_abbreviated_options && (ambiguous.option || abbrev.option))
567 die("disallowed abbreviated or ambiguous option '%.*s'",
568 (int)(arg_end - arg), arg);
570 if (ambiguous.option) {
571 error(_("ambiguous option: %s "
572 "(could be --%s%s or --%s%s)"),
573 arg,
574 (ambiguous.flags & OPT_UNSET) ? "no-" : "",
575 ambiguous.option->long_name,
576 (abbrev.flags & OPT_UNSET) ? "no-" : "",
577 abbrev.option->long_name);
578 return PARSE_OPT_HELP;
580 if (abbrev.option) {
581 if (*arg_end)
582 p->opt = arg_end + 1;
583 return get_value(p, abbrev.option, abbrev.flags);
585 return PARSE_OPT_UNKNOWN;
588 static enum parse_opt_result parse_nodash_opt(struct parse_opt_ctx_t *p,
589 const char *arg,
590 const struct option *options)
592 for (; options->type != OPTION_END; options++) {
593 if (!(options->flags & PARSE_OPT_NODASH))
594 continue;
595 if (options->short_name == arg[0] && arg[1] == '\0')
596 return get_value(p, options, OPT_SHORT);
598 return PARSE_OPT_ERROR;
601 static enum parse_opt_result parse_subcommand(const char *arg,
602 const struct option *options)
604 for (; options->type != OPTION_END; options++)
605 if (options->type == OPTION_SUBCOMMAND &&
606 !strcmp(options->long_name, arg)) {
607 *(parse_opt_subcommand_fn **)options->value = options->subcommand_fn;
608 return PARSE_OPT_SUBCOMMAND;
611 return PARSE_OPT_UNKNOWN;
614 static void check_typos(const char *arg, const struct option *options)
616 if (strlen(arg) < 3)
617 return;
619 if (starts_with(arg, "no-")) {
620 error(_("did you mean `--%s` (with two dashes)?"), arg);
621 exit(129);
624 for (; options->type != OPTION_END; options++) {
625 if (!options->long_name)
626 continue;
627 if (starts_with(options->long_name, arg)) {
628 error(_("did you mean `--%s` (with two dashes)?"), arg);
629 exit(129);
634 static void parse_options_check(const struct option *opts)
636 char short_opts[128];
637 void *subcommand_value = NULL;
639 memset(short_opts, '\0', sizeof(short_opts));
640 for (; opts->type != OPTION_END; opts++) {
641 if ((opts->flags & PARSE_OPT_LASTARG_DEFAULT) &&
642 (opts->flags & PARSE_OPT_OPTARG))
643 optbug(opts, "uses incompatible flags "
644 "LASTARG_DEFAULT and OPTARG");
645 if (opts->short_name) {
646 if (0x7F <= opts->short_name)
647 optbug(opts, "invalid short name");
648 else if (short_opts[opts->short_name]++)
649 optbug(opts, "short name already used");
651 if (opts->flags & PARSE_OPT_NODASH &&
652 ((opts->flags & PARSE_OPT_OPTARG) ||
653 !(opts->flags & PARSE_OPT_NOARG) ||
654 !(opts->flags & PARSE_OPT_NONEG) ||
655 opts->long_name))
656 optbug(opts, "uses feature "
657 "not supported for dashless options");
658 if (opts->type == OPTION_SET_INT && !opts->defval &&
659 opts->long_name && !(opts->flags & PARSE_OPT_NONEG))
660 optbug(opts, "OPTION_SET_INT 0 should not be negatable");
661 switch (opts->type) {
662 case OPTION_SET_INT:
663 case OPTION_BIT:
664 case OPTION_NEGBIT:
665 case OPTION_BITOP:
666 case OPTION_COUNTUP:
667 if (!signed_int_fits(opts->defval, opts->precision))
668 optbug(opts, "has invalid defval");
669 /* fallthru */
670 case OPTION_NUMBER:
671 if ((opts->flags & PARSE_OPT_OPTARG) ||
672 !(opts->flags & PARSE_OPT_NOARG))
673 optbug(opts, "should not accept an argument");
674 break;
675 case OPTION_CALLBACK:
676 if (!opts->callback && !opts->ll_callback)
677 optbug(opts, "OPTION_CALLBACK needs one callback");
678 else if (opts->callback && opts->ll_callback)
679 optbug(opts, "OPTION_CALLBACK can't have two callbacks");
680 break;
681 case OPTION_LOWLEVEL_CALLBACK:
682 if (!opts->ll_callback)
683 optbug(opts, "OPTION_LOWLEVEL_CALLBACK needs a callback");
684 if (opts->callback)
685 optbug(opts, "OPTION_LOWLEVEL_CALLBACK needs no high level callback");
686 break;
687 case OPTION_ALIAS:
688 optbug(opts, "OPT_ALIAS() should not remain at this point. "
689 "Are you using parse_options_step() directly?\n"
690 "That case is not supported yet.");
691 break;
692 case OPTION_SUBCOMMAND:
693 if (!opts->value || !opts->subcommand_fn)
694 optbug(opts, "OPTION_SUBCOMMAND needs a value and a subcommand function");
695 if (!subcommand_value)
696 subcommand_value = opts->value;
697 else if (subcommand_value != opts->value)
698 optbug(opts, "all OPTION_SUBCOMMANDs need the same value");
699 break;
700 default:
701 ; /* ok. (usually accepts an argument) */
703 if (opts->argh &&
704 strcspn(opts->argh, " _") != strlen(opts->argh))
705 optbug(opts, "multi-word argh should use dash to separate words");
707 BUG_if_bug("invalid 'struct option'");
710 static int has_subcommands(const struct option *options)
712 for (; options->type != OPTION_END; options++)
713 if (options->type == OPTION_SUBCOMMAND)
714 return 1;
715 return 0;
718 static void parse_options_start_1(struct parse_opt_ctx_t *ctx,
719 int argc, const char **argv, const char *prefix,
720 const struct option *options,
721 enum parse_opt_flags flags)
723 ctx->argc = argc;
724 ctx->argv = argv;
725 if (!(flags & PARSE_OPT_ONE_SHOT)) {
726 ctx->argc--;
727 ctx->argv++;
729 ctx->total = ctx->argc;
730 ctx->out = argv;
731 ctx->prefix = prefix;
732 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
733 ctx->flags = flags;
734 ctx->has_subcommands = has_subcommands(options);
735 if (!ctx->has_subcommands && (flags & PARSE_OPT_SUBCOMMAND_OPTIONAL))
736 BUG("Using PARSE_OPT_SUBCOMMAND_OPTIONAL without subcommands");
737 if (ctx->has_subcommands) {
738 if (flags & PARSE_OPT_STOP_AT_NON_OPTION)
739 BUG("subcommands are incompatible with PARSE_OPT_STOP_AT_NON_OPTION");
740 if (!(flags & PARSE_OPT_SUBCOMMAND_OPTIONAL)) {
741 if (flags & PARSE_OPT_KEEP_UNKNOWN_OPT)
742 BUG("subcommands are incompatible with PARSE_OPT_KEEP_UNKNOWN_OPT unless in combination with PARSE_OPT_SUBCOMMAND_OPTIONAL");
743 if (flags & PARSE_OPT_KEEP_DASHDASH)
744 BUG("subcommands are incompatible with PARSE_OPT_KEEP_DASHDASH unless in combination with PARSE_OPT_SUBCOMMAND_OPTIONAL");
747 if ((flags & PARSE_OPT_KEEP_UNKNOWN_OPT) &&
748 (flags & PARSE_OPT_STOP_AT_NON_OPTION) &&
749 !(flags & PARSE_OPT_ONE_SHOT))
750 BUG("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
751 if ((flags & PARSE_OPT_ONE_SHOT) &&
752 (flags & PARSE_OPT_KEEP_ARGV0))
753 BUG("Can't keep argv0 if you don't have it");
754 parse_options_check(options);
755 build_cmdmode_list(ctx, options);
758 void parse_options_start(struct parse_opt_ctx_t *ctx,
759 int argc, const char **argv, const char *prefix,
760 const struct option *options,
761 enum parse_opt_flags flags)
763 memset(ctx, 0, sizeof(*ctx));
764 parse_options_start_1(ctx, argc, argv, prefix, options, flags);
767 static void show_negated_gitcomp(const struct option *opts, int show_all,
768 int nr_noopts)
770 int printed_dashdash = 0;
772 for (; opts->type != OPTION_END; opts++) {
773 int has_unset_form = 0;
774 const char *name;
776 if (!opts->long_name)
777 continue;
778 if (!show_all &&
779 (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE)))
780 continue;
781 if (opts->flags & PARSE_OPT_NONEG)
782 continue;
784 switch (opts->type) {
785 case OPTION_STRING:
786 case OPTION_FILENAME:
787 case OPTION_INTEGER:
788 case OPTION_UNSIGNED:
789 case OPTION_CALLBACK:
790 case OPTION_BIT:
791 case OPTION_NEGBIT:
792 case OPTION_COUNTUP:
793 case OPTION_SET_INT:
794 has_unset_form = 1;
795 break;
796 default:
797 break;
799 if (!has_unset_form)
800 continue;
802 if (skip_prefix(opts->long_name, "no-", &name)) {
803 if (nr_noopts < 0)
804 printf(" --%s", name);
805 } else if (nr_noopts >= 0) {
806 if (nr_noopts && !printed_dashdash) {
807 printf(" --");
808 printed_dashdash = 1;
810 printf(" --no-%s", opts->long_name);
811 nr_noopts++;
816 static int show_gitcomp(const struct option *opts, int show_all)
818 const struct option *original_opts = opts;
819 int nr_noopts = 0;
821 for (; opts->type != OPTION_END; opts++) {
822 const char *prefix = "--";
823 const char *suffix = "";
825 if (!opts->long_name)
826 continue;
827 if (!show_all &&
828 (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE | PARSE_OPT_FROM_ALIAS)))
829 continue;
831 switch (opts->type) {
832 case OPTION_SUBCOMMAND:
833 prefix = "";
834 break;
835 case OPTION_GROUP:
836 continue;
837 case OPTION_STRING:
838 case OPTION_FILENAME:
839 case OPTION_INTEGER:
840 case OPTION_UNSIGNED:
841 case OPTION_CALLBACK:
842 if (opts->flags & PARSE_OPT_NOARG)
843 break;
844 if (opts->flags & PARSE_OPT_OPTARG)
845 break;
846 if (opts->flags & PARSE_OPT_LASTARG_DEFAULT)
847 break;
848 suffix = "=";
849 break;
850 default:
851 break;
853 if (opts->flags & PARSE_OPT_COMP_ARG)
854 suffix = "=";
855 if (starts_with(opts->long_name, "no-"))
856 nr_noopts++;
857 printf("%s%s%s%s", opts == original_opts ? "" : " ",
858 prefix, opts->long_name, suffix);
860 show_negated_gitcomp(original_opts, show_all, -1);
861 show_negated_gitcomp(original_opts, show_all, nr_noopts);
862 fputc('\n', stdout);
863 return PARSE_OPT_COMPLETE;
867 * Scan and may produce a new option[] array, which should be used
868 * instead of the original 'options'.
870 * Right now this is only used to preprocess and substitute
871 * OPTION_ALIAS.
873 * The returned options should be freed using free_preprocessed_options.
875 static struct option *preprocess_options(struct parse_opt_ctx_t *ctx,
876 const struct option *options)
878 struct option *newopt;
879 int i, nr, alias;
880 int nr_aliases = 0;
882 for (nr = 0; options[nr].type != OPTION_END; nr++) {
883 if (options[nr].type == OPTION_ALIAS)
884 nr_aliases++;
887 if (!nr_aliases)
888 return NULL;
890 DUP_ARRAY(newopt, options, nr + 1);
892 /* each alias has two string pointers and NULL */
893 CALLOC_ARRAY(ctx->alias_groups, 3 * (nr_aliases + 1));
895 for (alias = 0, i = 0; i < nr; i++) {
896 int short_name;
897 const char *long_name;
898 const char *source;
899 struct strbuf help = STRBUF_INIT;
900 int j;
902 if (newopt[i].type != OPTION_ALIAS)
903 continue;
905 short_name = newopt[i].short_name;
906 long_name = newopt[i].long_name;
907 source = newopt[i].value;
909 if (!long_name)
910 BUG("An alias must have long option name");
911 strbuf_addf(&help, _("alias of --%s"), source);
913 for (j = 0; j < nr; j++) {
914 const char *name = options[j].long_name;
916 if (!name || strcmp(name, source))
917 continue;
919 if (options[j].type == OPTION_ALIAS)
920 BUG("No please. Nested aliases are not supported.");
922 memcpy(newopt + i, options + j, sizeof(*newopt));
923 newopt[i].short_name = short_name;
924 newopt[i].long_name = long_name;
925 newopt[i].help = strbuf_detach(&help, NULL);
926 newopt[i].flags |= PARSE_OPT_FROM_ALIAS;
927 break;
930 if (j == nr)
931 BUG("could not find source option '%s' of alias '%s'",
932 source, newopt[i].long_name);
933 ctx->alias_groups[alias * 3 + 0] = newopt[i].long_name;
934 ctx->alias_groups[alias * 3 + 1] = options[j].long_name;
935 ctx->alias_groups[alias * 3 + 2] = NULL;
936 alias++;
939 return newopt;
942 static void free_preprocessed_options(struct option *options)
944 int i;
946 if (!options)
947 return;
949 for (i = 0; options[i].type != OPTION_END; i++) {
950 if (options[i].flags & PARSE_OPT_FROM_ALIAS)
951 free((void *)options[i].help);
953 free(options);
956 static enum parse_opt_result usage_with_options_internal(struct parse_opt_ctx_t *,
957 const char * const *,
958 const struct option *,
959 int, int);
961 enum parse_opt_result parse_options_step(struct parse_opt_ctx_t *ctx,
962 const struct option *options,
963 const char * const usagestr[])
965 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
967 /* we must reset ->opt, unknown short option leave it dangling */
968 ctx->opt = NULL;
970 for (; ctx->argc; ctx->argc--, ctx->argv++) {
971 const char *arg = ctx->argv[0];
973 if (ctx->flags & PARSE_OPT_ONE_SHOT &&
974 ctx->argc != ctx->total)
975 break;
977 if (*arg != '-' || !arg[1]) {
978 if (parse_nodash_opt(ctx, arg, options) == 0)
979 continue;
980 if (!ctx->has_subcommands) {
981 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
982 return PARSE_OPT_NON_OPTION;
983 ctx->out[ctx->cpidx++] = ctx->argv[0];
984 continue;
986 switch (parse_subcommand(arg, options)) {
987 case PARSE_OPT_SUBCOMMAND:
988 return PARSE_OPT_SUBCOMMAND;
989 case PARSE_OPT_UNKNOWN:
990 if (ctx->flags & PARSE_OPT_SUBCOMMAND_OPTIONAL)
992 * arg is neither a short or long
993 * option nor a subcommand. Since
994 * this command has a default
995 * operation mode, we have to treat
996 * this arg and all remaining args
997 * as args meant to that default
998 * operation mode.
999 * So we are done parsing.
1001 return PARSE_OPT_DONE;
1002 error(_("unknown subcommand: `%s'"), arg);
1003 usage_with_options(usagestr, options);
1004 case PARSE_OPT_COMPLETE:
1005 case PARSE_OPT_HELP:
1006 case PARSE_OPT_ERROR:
1007 case PARSE_OPT_DONE:
1008 case PARSE_OPT_NON_OPTION:
1009 /* Impossible. */
1010 BUG("parse_subcommand() cannot return these");
1014 /* lone -h asks for help */
1015 if (internal_help && ctx->total == 1 && !strcmp(arg + 1, "h"))
1016 goto show_usage;
1019 * lone --git-completion-helper and --git-completion-helper-all
1020 * are asked by git-completion.bash
1022 if (ctx->total == 1 && !strcmp(arg, "--git-completion-helper"))
1023 return show_gitcomp(options, 0);
1024 if (ctx->total == 1 && !strcmp(arg, "--git-completion-helper-all"))
1025 return show_gitcomp(options, 1);
1027 if (arg[1] != '-') {
1028 ctx->opt = arg + 1;
1029 switch (parse_short_opt(ctx, options)) {
1030 case PARSE_OPT_ERROR:
1031 return PARSE_OPT_ERROR;
1032 case PARSE_OPT_UNKNOWN:
1033 if (ctx->opt)
1034 check_typos(arg + 1, options);
1035 if (internal_help && *ctx->opt == 'h')
1036 goto show_usage;
1037 goto unknown;
1038 case PARSE_OPT_NON_OPTION:
1039 case PARSE_OPT_SUBCOMMAND:
1040 case PARSE_OPT_HELP:
1041 case PARSE_OPT_COMPLETE:
1042 BUG("parse_short_opt() cannot return these");
1043 case PARSE_OPT_DONE:
1044 break;
1046 if (ctx->opt)
1047 check_typos(arg + 1, options);
1048 while (ctx->opt) {
1049 switch (parse_short_opt(ctx, options)) {
1050 case PARSE_OPT_ERROR:
1051 return PARSE_OPT_ERROR;
1052 case PARSE_OPT_UNKNOWN:
1053 if (internal_help && *ctx->opt == 'h')
1054 goto show_usage;
1056 /* fake a short option thing to hide the fact that we may have
1057 * started to parse aggregated stuff
1059 * This is leaky, too bad.
1061 ctx->argv[0] = xstrdup(ctx->opt - 1);
1062 *(char *)ctx->argv[0] = '-';
1063 goto unknown;
1064 case PARSE_OPT_NON_OPTION:
1065 case PARSE_OPT_SUBCOMMAND:
1066 case PARSE_OPT_COMPLETE:
1067 case PARSE_OPT_HELP:
1068 BUG("parse_short_opt() cannot return these");
1069 case PARSE_OPT_DONE:
1070 break;
1073 continue;
1076 if (!arg[2] /* "--" */) {
1077 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
1078 ctx->argc--;
1079 ctx->argv++;
1081 break;
1082 } else if (!strcmp(arg + 2, "end-of-options")) {
1083 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN_OPT)) {
1084 ctx->argc--;
1085 ctx->argv++;
1087 break;
1090 if (internal_help && !strcmp(arg + 2, "help-all"))
1091 return usage_with_options_internal(ctx, usagestr, options, 1, 0);
1092 if (internal_help && !strcmp(arg + 2, "help"))
1093 goto show_usage;
1094 switch (parse_long_opt(ctx, arg + 2, options)) {
1095 case PARSE_OPT_ERROR:
1096 return PARSE_OPT_ERROR;
1097 case PARSE_OPT_UNKNOWN:
1098 goto unknown;
1099 case PARSE_OPT_HELP:
1100 goto show_usage;
1101 case PARSE_OPT_NON_OPTION:
1102 case PARSE_OPT_SUBCOMMAND:
1103 case PARSE_OPT_COMPLETE:
1104 BUG("parse_long_opt() cannot return these");
1105 case PARSE_OPT_DONE:
1106 break;
1108 continue;
1109 unknown:
1110 if (ctx->flags & PARSE_OPT_ONE_SHOT)
1111 break;
1112 if (ctx->has_subcommands &&
1113 (ctx->flags & PARSE_OPT_SUBCOMMAND_OPTIONAL) &&
1114 (ctx->flags & PARSE_OPT_KEEP_UNKNOWN_OPT)) {
1116 * Found an unknown option given to a command with
1117 * subcommands that has a default operation mode:
1118 * we treat this option and all remaining args as
1119 * arguments meant to that default operation mode.
1120 * So we are done parsing.
1122 return PARSE_OPT_DONE;
1124 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN_OPT))
1125 return PARSE_OPT_UNKNOWN;
1126 ctx->out[ctx->cpidx++] = ctx->argv[0];
1127 ctx->opt = NULL;
1129 return PARSE_OPT_DONE;
1131 show_usage:
1132 return usage_with_options_internal(ctx, usagestr, options, 0, 0);
1135 int parse_options_end(struct parse_opt_ctx_t *ctx)
1137 if (ctx->flags & PARSE_OPT_ONE_SHOT)
1138 return ctx->total - ctx->argc;
1140 MOVE_ARRAY(ctx->out + ctx->cpidx, ctx->argv, ctx->argc);
1141 ctx->out[ctx->cpidx + ctx->argc] = NULL;
1142 return ctx->cpidx + ctx->argc;
1145 int parse_options(int argc, const char **argv,
1146 const char *prefix,
1147 const struct option *options,
1148 const char * const usagestr[],
1149 enum parse_opt_flags flags)
1151 struct parse_opt_ctx_t ctx;
1152 struct option *real_options;
1154 disallow_abbreviated_options =
1155 git_env_bool("GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS", 0);
1157 memset(&ctx, 0, sizeof(ctx));
1158 real_options = preprocess_options(&ctx, options);
1159 if (real_options)
1160 options = real_options;
1161 parse_options_start_1(&ctx, argc, argv, prefix, options, flags);
1162 switch (parse_options_step(&ctx, options, usagestr)) {
1163 case PARSE_OPT_HELP:
1164 case PARSE_OPT_ERROR:
1165 exit(129);
1166 case PARSE_OPT_COMPLETE:
1167 exit(0);
1168 case PARSE_OPT_NON_OPTION:
1169 case PARSE_OPT_SUBCOMMAND:
1170 break;
1171 case PARSE_OPT_DONE:
1172 if (ctx.has_subcommands &&
1173 !(flags & PARSE_OPT_SUBCOMMAND_OPTIONAL)) {
1174 error(_("need a subcommand"));
1175 usage_with_options(usagestr, options);
1177 break;
1178 case PARSE_OPT_UNKNOWN:
1179 if (ctx.argv[0][1] == '-') {
1180 error(_("unknown option `%s'"), ctx.argv[0] + 2);
1181 } else if (isascii(*ctx.opt)) {
1182 error(_("unknown switch `%c'"), *ctx.opt);
1183 } else {
1184 error(_("unknown non-ascii option in string: `%s'"),
1185 ctx.argv[0]);
1187 usage_with_options(usagestr, options);
1190 precompose_argv_prefix(argc, argv, NULL);
1191 free_preprocessed_options(real_options);
1192 free(ctx.alias_groups);
1193 for (struct parse_opt_cmdmode_list *elem = ctx.cmdmode_list; elem;) {
1194 struct parse_opt_cmdmode_list *next = elem->next;
1195 free(elem);
1196 elem = next;
1198 return parse_options_end(&ctx);
1201 static int usage_argh(const struct option *opts, FILE *outfile)
1203 const char *s;
1204 int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
1205 !opts->argh || !!strpbrk(opts->argh, "()<>[]|");
1206 if (opts->flags & PARSE_OPT_OPTARG)
1207 if (opts->long_name)
1209 * TRANSLATORS: The "<%s>" part of this string
1210 * stands for an optional value given to a command
1211 * line option in the long form, and "<>" is there
1212 * as a convention to signal that it is a
1213 * placeholder (i.e. the user should substitute it
1214 * with the real value). If your language uses a
1215 * different convention, you can change "<%s>" part
1216 * to match yours, e.g. it might use "|%s|" instead,
1217 * or if the alphabet is different enough it may use
1218 * "%s" without any placeholder signal. Most
1219 * translations leave this message as is.
1221 s = literal ? "[=%s]" : _("[=<%s>]");
1222 else
1224 * TRANSLATORS: The "<%s>" part of this string
1225 * stands for an optional value given to a command
1226 * line option in the short form, and "<>" is there
1227 * as a convention to signal that it is a
1228 * placeholder (i.e. the user should substitute it
1229 * with the real value). If your language uses a
1230 * different convention, you can change "<%s>" part
1231 * to match yours, e.g. it might use "|%s|" instead,
1232 * or if the alphabet is different enough it may use
1233 * "%s" without any placeholder signal. Most
1234 * translations leave this message as is.
1236 s = literal ? "[%s]" : _("[<%s>]");
1237 else
1239 * TRANSLATORS: The "<%s>" part of this string stands for a
1240 * value given to a command line option, and "<>" is there
1241 * as a convention to signal that it is a placeholder
1242 * (i.e. the user should substitute it with the real value).
1243 * If your language uses a different convention, you can
1244 * change "<%s>" part to match yours, e.g. it might use
1245 * "|%s|" instead, or if the alphabet is different enough it
1246 * may use "%s" without any placeholder signal. Most
1247 * translations leave this message as is.
1249 s = literal ? " %s" : _(" <%s>");
1250 return utf8_fprintf(outfile, s, opts->argh ? _(opts->argh) : _("..."));
1253 static int usage_indent(FILE *outfile)
1255 return fprintf(outfile, " ");
1258 #define USAGE_OPTS_WIDTH 26
1260 static void usage_padding(FILE *outfile, size_t pos)
1262 if (pos < USAGE_OPTS_WIDTH)
1263 fprintf(outfile, "%*s", USAGE_OPTS_WIDTH - (int)pos, "");
1264 else
1265 fprintf(outfile, "\n%*s", USAGE_OPTS_WIDTH, "");
1268 static const struct option *find_option_by_long_name(const struct option *opts,
1269 const char *long_name)
1271 for (; opts->type != OPTION_END; opts++) {
1272 if (opts->long_name && !strcmp(opts->long_name, long_name))
1273 return opts;
1275 return NULL;
1278 static enum parse_opt_result usage_with_options_internal(struct parse_opt_ctx_t *ctx,
1279 const char * const *usagestr,
1280 const struct option *opts,
1281 int full, int err)
1283 const struct option *all_opts = opts;
1284 FILE *outfile = err ? stderr : stdout;
1285 int need_newline;
1287 const char *usage_prefix = _("usage: %s");
1289 * The translation could be anything, but we can count on
1290 * msgfmt(1)'s --check option to have asserted that "%s" is in
1291 * the translation. So compute the length of the "usage: "
1292 * part. We are assuming that the translator wasn't overly
1293 * clever and used e.g. "%1$s" instead of "%s", there's only
1294 * one "%s" in "usage_prefix" above, so there's no reason to
1295 * do so even with a RTL language.
1297 size_t usage_len = strlen(usage_prefix) - strlen("%s");
1299 * TRANSLATORS: the colon here should align with the
1300 * one in "usage: %s" translation.
1302 const char *or_prefix = _(" or: %s");
1304 * TRANSLATORS: You should only need to translate this format
1305 * string if your language is a RTL language (e.g. Arabic,
1306 * Hebrew etc.), not if it's a LTR language (e.g. German,
1307 * Russian, Chinese etc.).
1309 * When a translated usage string has an embedded "\n" it's
1310 * because options have wrapped to the next line. The line
1311 * after the "\n" will then be padded to align with the
1312 * command name, such as N_("git cmd [opt]\n<8
1313 * spaces>[opt2]"), where the 8 spaces are the same length as
1314 * "git cmd ".
1316 * This format string prints out that already-translated
1317 * line. The "%*s" is whitespace padding to account for the
1318 * padding at the start of the line that we add in this
1319 * function. The "%s" is a line in the (hopefully already
1320 * translated) N_() usage string, which contained embedded
1321 * newlines before we split it up.
1323 const char *usage_continued = _("%*s%s");
1324 const char *prefix = usage_prefix;
1325 int saw_empty_line = 0;
1327 if (!usagestr)
1328 return PARSE_OPT_HELP;
1330 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
1331 fprintf(outfile, "cat <<\\EOF\n");
1333 while (*usagestr) {
1334 const char *str = _(*usagestr++);
1335 struct string_list list = STRING_LIST_INIT_DUP;
1336 unsigned int j;
1338 if (!saw_empty_line && !*str)
1339 saw_empty_line = 1;
1341 string_list_split(&list, str, '\n', -1);
1342 for (j = 0; j < list.nr; j++) {
1343 const char *line = list.items[j].string;
1345 if (saw_empty_line && *line)
1346 fprintf_ln(outfile, _(" %s"), line);
1347 else if (saw_empty_line)
1348 fputc('\n', outfile);
1349 else if (!j)
1350 fprintf_ln(outfile, prefix, line);
1351 else
1352 fprintf_ln(outfile, usage_continued,
1353 (int)usage_len, "", line);
1355 string_list_clear(&list, 0);
1357 prefix = or_prefix;
1360 need_newline = 1;
1362 for (; opts->type != OPTION_END; opts++) {
1363 size_t pos;
1364 const char *cp, *np;
1365 const char *positive_name = NULL;
1367 if (opts->type == OPTION_SUBCOMMAND)
1368 continue;
1369 if (opts->type == OPTION_GROUP) {
1370 fputc('\n', outfile);
1371 need_newline = 0;
1372 if (*opts->help)
1373 fprintf(outfile, "%s\n", _(opts->help));
1374 continue;
1376 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
1377 continue;
1379 if (need_newline) {
1380 fputc('\n', outfile);
1381 need_newline = 0;
1384 pos = usage_indent(outfile);
1385 if (opts->short_name) {
1386 if (opts->flags & PARSE_OPT_NODASH)
1387 pos += fprintf(outfile, "%c", opts->short_name);
1388 else
1389 pos += fprintf(outfile, "-%c", opts->short_name);
1391 if (opts->long_name && opts->short_name)
1392 pos += fprintf(outfile, ", ");
1393 if (opts->long_name) {
1394 const char *long_name = opts->long_name;
1395 if ((opts->flags & PARSE_OPT_NONEG) ||
1396 skip_prefix(long_name, "no-", &positive_name))
1397 pos += fprintf(outfile, "--%s", long_name);
1398 else
1399 pos += fprintf(outfile, "--[no-]%s", long_name);
1402 if (opts->type == OPTION_NUMBER)
1403 pos += utf8_fprintf(outfile, _("-NUM"));
1405 if ((opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
1406 !(opts->flags & PARSE_OPT_NOARG))
1407 pos += usage_argh(opts, outfile);
1409 if (opts->type == OPTION_ALIAS) {
1410 usage_padding(outfile, pos);
1411 fprintf_ln(outfile, _("alias of --%s"),
1412 (const char *)opts->value);
1413 continue;
1416 for (cp = opts->help ? _(opts->help) : ""; *cp; cp = np) {
1417 np = strchrnul(cp, '\n');
1418 if (*np)
1419 np++;
1420 usage_padding(outfile, pos);
1421 fwrite(cp, 1, np - cp, outfile);
1422 pos = 0;
1424 fputc('\n', outfile);
1426 if (positive_name) {
1427 if (find_option_by_long_name(all_opts, positive_name))
1428 continue;
1429 pos = usage_indent(outfile);
1430 pos += fprintf(outfile, "--%s", positive_name);
1431 usage_padding(outfile, pos);
1432 fprintf_ln(outfile, _("opposite of --no-%s"),
1433 positive_name);
1436 fputc('\n', outfile);
1438 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
1439 fputs("EOF\n", outfile);
1441 return PARSE_OPT_HELP;
1444 void NORETURN usage_with_options(const char * const *usagestr,
1445 const struct option *opts)
1447 usage_with_options_internal(NULL, usagestr, opts, 0, 1);
1448 exit(129);
1451 void show_usage_with_options_if_asked(int ac, const char **av,
1452 const char * const *usagestr,
1453 const struct option *opts)
1455 if (ac == 2 && !strcmp(av[1], "-h")) {
1456 usage_with_options_internal(NULL, usagestr, opts, 0, 0);
1457 exit(129);
1461 void NORETURN usage_msg_opt(const char *msg,
1462 const char * const *usagestr,
1463 const struct option *options)
1465 die_message("%s\n", msg); /* The extra \n is intentional */
1466 usage_with_options(usagestr, options);
1469 void NORETURN usage_msg_optf(const char * const fmt,
1470 const char * const *usagestr,
1471 const struct option *options, ...)
1473 struct strbuf msg = STRBUF_INIT;
1474 va_list ap;
1475 va_start(ap, options);
1476 strbuf_vaddf(&msg, fmt, ap);
1477 va_end(ap);
1479 usage_msg_opt(msg.buf, usagestr, options);
1482 void die_for_incompatible_opt4(int opt1, const char *opt1_name,
1483 int opt2, const char *opt2_name,
1484 int opt3, const char *opt3_name,
1485 int opt4, const char *opt4_name)
1487 int count = 0;
1488 const char *options[4];
1490 if (opt1)
1491 options[count++] = opt1_name;
1492 if (opt2)
1493 options[count++] = opt2_name;
1494 if (opt3)
1495 options[count++] = opt3_name;
1496 if (opt4)
1497 options[count++] = opt4_name;
1498 switch (count) {
1499 case 4:
1500 die(_("options '%s', '%s', '%s', and '%s' cannot be used together"),
1501 opt1_name, opt2_name, opt3_name, opt4_name);
1502 break;
1503 case 3:
1504 die(_("options '%s', '%s', and '%s' cannot be used together"),
1505 options[0], options[1], options[2]);
1506 break;
1507 case 2:
1508 die(_("options '%s' and '%s' cannot be used together"),
1509 options[0], options[1]);
1510 break;
1511 default:
1512 break;