1 #include "git-compat-util.h"
2 #include "parse-options.h"
7 #include "string-list.h"
10 static int disallow_abbreviated_options
;
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
);
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
;
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
);
41 BUG("optname() got unknown flags %d", flags
);
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
)
53 } else if (p
->argc
== 1 && (opt
->flags
& PARSE_OPT_LASTARG_DEFAULT
)) {
54 *arg
= (const char *)opt
->defval
;
55 } else if (p
->argc
> 1) {
59 return error(_("%s requires a value"), optname(opt
, flags
));
63 static char *fix_filename(const char *prefix
, const char *file
)
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
)
75 *ret
= *(int8_t *)value
;
78 *ret
= *(int16_t *)value
;
81 *ret
= *(int32_t *)value
;
84 *ret
= *(int64_t *)value
;
91 static intmax_t get_int_value(const struct option
*opt
, enum opt_parsed flags
)
94 if (do_get_int_value(opt
->value
, opt
->precision
, &ret
))
95 BUG("invalid precision for option %s", optname(opt
, flags
));
99 static enum parse_opt_result
set_int_value(const struct option
*opt
,
100 enum opt_parsed flags
,
103 switch (opt
->precision
) {
105 *(int8_t *)opt
->value
= value
;
107 case sizeof(int16_t):
108 *(int16_t *)opt
->value
= value
;
110 case sizeof(int32_t):
111 *(int32_t *)opt
->value
= value
;
113 case sizeof(int64_t):
114 *(int64_t *)opt
->value
= value
;
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
,
135 const int unset
= flags
& OPT_UNSET
;
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
));
146 case OPTION_LOWLEVEL_CALLBACK
:
147 return opt
->ll_callback(p
, opt
, NULL
, unset
);
151 intmax_t value
= get_int_value(opt
, flags
);
153 value
&= ~opt
->defval
;
155 value
|= opt
->defval
;
156 return set_int_value(opt
, flags
, value
);
161 intmax_t value
= get_int_value(opt
, flags
);
163 value
|= opt
->defval
;
165 value
&= ~opt
->defval
;
166 return set_int_value(opt
, flags
, value
);
171 intmax_t value
= get_int_value(opt
, flags
);
173 BUG("BITOP can't have unset form");
174 value
&= ~opt
->extra
;
175 value
|= opt
->defval
;
176 return set_int_value(opt
, flags
, value
);
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
);
189 else if (value
< upper_bound
)
192 return error(_("value for %s exceeds %"PRIdMAX
),
193 optname(opt
, flags
), upper_bound
);
194 return set_int_value(opt
, flags
, value
);
198 return set_int_value(opt
, flags
, unset
? 0 : opt
->defval
);
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
;
206 return get_arg(p
, opt
, flags
, (const char **)opt
->value
);
209 case OPTION_FILENAME
:
213 FREE_AND_NULL(*(char **)opt
->value
);
219 else if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
)
220 value
= (const char *) opt
->defval
;
222 err
= get_arg(p
, opt
, flags
, &value
);
225 *(char **)opt
->value
= fix_filename(p
->prefix
, value
);
228 case OPTION_CALLBACK
:
230 const char *p_arg
= NULL
;
235 else if (opt
->flags
& PARSE_OPT_NOARG
)
237 else if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
)
239 else if (get_arg(p
, opt
, flags
, &arg
))
245 if (opt
->flags
& PARSE_OPT_CMDMODE
)
248 return (*opt
->callback
)(opt
, p_arg
, p_unset
) ? (-1) : 0;
250 return (*opt
->ll_callback
)(p
, opt
, p_arg
, p_unset
);
254 intmax_t upper_bound
= INTMAX_MAX
>> (bitsizeof(intmax_t) - CHAR_BIT
* opt
->precision
);
255 intmax_t lower_bound
= -upper_bound
- 1;
260 } else if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
) {
262 } else if (get_arg(p
, opt
, flags
, &arg
)) {
265 return error(_("%s expects a numerical value"),
266 optname(opt
, flags
));
267 } else if (!git_parse_signed(arg
, &value
, upper_bound
)) {
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
);
289 } else if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
) {
291 } else if (get_arg(p
, opt
, flags
, &arg
)) {
294 return error(_("%s expects a numerical value"),
295 optname(opt
, flags
));
296 } else if (!git_parse_unsigned(arg
, &value
, upper_bound
)) {
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
) {
308 *(uint8_t *)opt
->value
= value
;
311 *(uint16_t *)opt
->value
= value
;
314 *(uint32_t *)opt
->value
= value
;
317 *(uint64_t *)opt
->value
= value
;
320 BUG("invalid precision for option %s",
321 optname(opt
, flags
));
326 BUG("opt->type %d should not happen", opt
->type
);
330 struct parse_opt_cmdmode_list
{
334 const struct option
*opt
;
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
)
352 while (elem
&& elem
->value_ptr
!= value_ptr
)
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
) {
389 if (do_get_int_value(elem
->value_ptr
, elem
->precision
,
391 BUG("impossible: invalid precision");
393 if (new_value
== elem
->value
)
397 (elem
->opt
->flags
| opt
->flags
) & PARSE_OPT_CMDMODE
)
403 elem
->value
= new_value
;
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
);
414 free(other_opt_name
);
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
)
436 if (numopt
&& isdigit(*p
->opt
)) {
441 while (isdigit(p
->opt
[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;
448 rc
= (*numopt
->ll_callback
)(p
, numopt
, arg
, 0);
452 return PARSE_OPT_UNKNOWN
;
455 static int has_string(const char *it
, const char **array
)
458 if (!strcmp(it
, *(array
++)))
463 static int is_alias(struct parse_opt_ctx_t
*ctx
,
464 const struct option
*one_opt
,
465 const struct option
*another_opt
)
469 if (!ctx
->alias_groups
)
472 if (!one_opt
->long_name
|| !another_opt
->long_name
)
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
))
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
)
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
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;
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
)
539 if (skip_prefix(long_name
, "no-", &long_name
))
540 opt_flags
|= OPT_UNSET
;
541 else if (arg_starts_with_no_no
)
544 if (((flags
^ opt_flags
) & OPT_UNSET
) && !allow_unset
)
547 if (skip_prefix(arg_start
, long_name
, &rest
)) {
552 return get_value(p
, options
, flags
^ opt_flags
);
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)"),
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
;
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
,
590 const struct option
*options
)
592 for (; options
->type
!= OPTION_END
; options
++) {
593 if (!(options
->flags
& PARSE_OPT_NODASH
))
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
)
619 if (starts_with(arg
, "no-")) {
620 error(_("did you mean `--%s` (with two dashes)?"), arg
);
624 for (; options
->type
!= OPTION_END
; options
++) {
625 if (!options
->long_name
)
627 if (starts_with(options
->long_name
, arg
)) {
628 error(_("did you mean `--%s` (with two dashes)?"), arg
);
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
) ||
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
) {
667 if (!signed_int_fits(opts
->defval
, opts
->precision
))
668 optbug(opts
, "has invalid defval");
671 if ((opts
->flags
& PARSE_OPT_OPTARG
) ||
672 !(opts
->flags
& PARSE_OPT_NOARG
))
673 optbug(opts
, "should not accept an argument");
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");
681 case OPTION_LOWLEVEL_CALLBACK
:
682 if (!opts
->ll_callback
)
683 optbug(opts
, "OPTION_LOWLEVEL_CALLBACK needs a callback");
685 optbug(opts
, "OPTION_LOWLEVEL_CALLBACK needs no high level callback");
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.");
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");
701 ; /* ok. (usually accepts an argument) */
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
)
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
)
725 if (!(flags
& PARSE_OPT_ONE_SHOT
)) {
729 ctx
->total
= ctx
->argc
;
731 ctx
->prefix
= prefix
;
732 ctx
->cpidx
= ((flags
& PARSE_OPT_KEEP_ARGV0
) != 0);
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
,
770 int printed_dashdash
= 0;
772 for (; opts
->type
!= OPTION_END
; opts
++) {
773 int has_unset_form
= 0;
776 if (!opts
->long_name
)
779 (opts
->flags
& (PARSE_OPT_HIDDEN
| PARSE_OPT_NOCOMPLETE
)))
781 if (opts
->flags
& PARSE_OPT_NONEG
)
784 switch (opts
->type
) {
786 case OPTION_FILENAME
:
788 case OPTION_UNSIGNED
:
789 case OPTION_CALLBACK
:
802 if (skip_prefix(opts
->long_name
, "no-", &name
)) {
804 printf(" --%s", name
);
805 } else if (nr_noopts
>= 0) {
806 if (nr_noopts
&& !printed_dashdash
) {
808 printed_dashdash
= 1;
810 printf(" --no-%s", opts
->long_name
);
816 static int show_gitcomp(const struct option
*opts
, int show_all
)
818 const struct option
*original_opts
= opts
;
821 for (; opts
->type
!= OPTION_END
; opts
++) {
822 const char *prefix
= "--";
823 const char *suffix
= "";
825 if (!opts
->long_name
)
828 (opts
->flags
& (PARSE_OPT_HIDDEN
| PARSE_OPT_NOCOMPLETE
| PARSE_OPT_FROM_ALIAS
)))
831 switch (opts
->type
) {
832 case OPTION_SUBCOMMAND
:
838 case OPTION_FILENAME
:
840 case OPTION_UNSIGNED
:
841 case OPTION_CALLBACK
:
842 if (opts
->flags
& PARSE_OPT_NOARG
)
844 if (opts
->flags
& PARSE_OPT_OPTARG
)
846 if (opts
->flags
& PARSE_OPT_LASTARG_DEFAULT
)
853 if (opts
->flags
& PARSE_OPT_COMP_ARG
)
855 if (starts_with(opts
->long_name
, "no-"))
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
);
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
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
;
882 for (nr
= 0; options
[nr
].type
!= OPTION_END
; nr
++) {
883 if (options
[nr
].type
== OPTION_ALIAS
)
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
++) {
897 const char *long_name
;
899 struct strbuf help
= STRBUF_INIT
;
902 if (newopt
[i
].type
!= OPTION_ALIAS
)
905 short_name
= newopt
[i
].short_name
;
906 long_name
= newopt
[i
].long_name
;
907 source
= newopt
[i
].value
;
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
))
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
;
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
;
942 static void free_preprocessed_options(struct option
*options
)
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
);
956 static enum parse_opt_result
usage_with_options_internal(struct parse_opt_ctx_t
*,
957 const char * const *,
958 const struct option
*,
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 */
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
)
977 if (*arg
!= '-' || !arg
[1]) {
978 if (parse_nodash_opt(ctx
, arg
, options
) == 0)
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];
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
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
:
1010 BUG("parse_subcommand() cannot return these");
1014 /* lone -h asks for help */
1015 if (internal_help
&& ctx
->total
== 1 && !strcmp(arg
+ 1, "h"))
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] != '-') {
1029 switch (parse_short_opt(ctx
, options
)) {
1030 case PARSE_OPT_ERROR
:
1031 return PARSE_OPT_ERROR
;
1032 case PARSE_OPT_UNKNOWN
:
1034 check_typos(arg
+ 1, options
);
1035 if (internal_help
&& *ctx
->opt
== 'h')
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
:
1047 check_typos(arg
+ 1, options
);
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')
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] = '-';
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
:
1076 if (!arg
[2] /* "--" */) {
1077 if (!(ctx
->flags
& PARSE_OPT_KEEP_DASHDASH
)) {
1082 } else if (!strcmp(arg
+ 2, "end-of-options")) {
1083 if (!(ctx
->flags
& PARSE_OPT_KEEP_UNKNOWN_OPT
)) {
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"))
1094 switch (parse_long_opt(ctx
, arg
+ 2, options
)) {
1095 case PARSE_OPT_ERROR
:
1096 return PARSE_OPT_ERROR
;
1097 case PARSE_OPT_UNKNOWN
:
1099 case PARSE_OPT_HELP
:
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
:
1110 if (ctx
->flags
& PARSE_OPT_ONE_SHOT
)
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];
1129 return PARSE_OPT_DONE
;
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
,
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
);
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
:
1166 case PARSE_OPT_COMPLETE
:
1168 case PARSE_OPT_NON_OPTION
:
1169 case PARSE_OPT_SUBCOMMAND
:
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
);
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
);
1184 error(_("unknown non-ascii option in string: `%s'"),
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
;
1198 return parse_options_end(&ctx
);
1201 static int usage_argh(const struct option
*opts
, FILE *outfile
)
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>]");
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>]");
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
, "");
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
))
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
,
1283 const struct option
*all_opts
= opts
;
1284 FILE *outfile
= err
? stderr
: stdout
;
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
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;
1328 return PARSE_OPT_HELP
;
1330 if (!err
&& ctx
&& ctx
->flags
& PARSE_OPT_SHELL_EVAL
)
1331 fprintf(outfile
, "cat <<\\EOF\n");
1334 const char *str
= _(*usagestr
++);
1335 struct string_list list
= STRING_LIST_INIT_DUP
;
1338 if (!saw_empty_line
&& !*str
)
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
);
1350 fprintf_ln(outfile
, prefix
, line
);
1352 fprintf_ln(outfile
, usage_continued
,
1353 (int)usage_len
, "", line
);
1355 string_list_clear(&list
, 0);
1362 for (; opts
->type
!= OPTION_END
; opts
++) {
1364 const char *cp
, *np
;
1365 const char *positive_name
= NULL
;
1367 if (opts
->type
== OPTION_SUBCOMMAND
)
1369 if (opts
->type
== OPTION_GROUP
) {
1370 fputc('\n', outfile
);
1373 fprintf(outfile
, "%s\n", _(opts
->help
));
1376 if (!full
&& (opts
->flags
& PARSE_OPT_HIDDEN
))
1380 fputc('\n', outfile
);
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
);
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
);
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
);
1416 for (cp
= opts
->help
? _(opts
->help
) : ""; *cp
; cp
= np
) {
1417 np
= strchrnul(cp
, '\n');
1420 usage_padding(outfile
, pos
);
1421 fwrite(cp
, 1, np
- cp
, outfile
);
1424 fputc('\n', outfile
);
1426 if (positive_name
) {
1427 if (find_option_by_long_name(all_opts
, positive_name
))
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"),
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);
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);
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
;
1475 va_start(ap
, options
);
1476 strbuf_vaddf(&msg
, fmt
, 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
)
1488 const char *options
[4];
1491 options
[count
++] = opt1_name
;
1493 options
[count
++] = opt2_name
;
1495 options
[count
++] = opt3_name
;
1497 options
[count
++] = opt4_name
;
1500 die(_("options '%s', '%s', '%s', and '%s' cannot be used together"),
1501 opt1_name
, opt2_name
, opt3_name
, opt4_name
);
1504 die(_("options '%s', '%s', and '%s' cannot be used together"),
1505 options
[0], options
[1], options
[2]);
1508 die(_("options '%s' and '%s' cannot be used together"),
1509 options
[0], options
[1]);