1 #include "git-compat-util.h"
2 #include "parse-options.h"
11 static int disallow_abbreviated_options
;
19 static void optbug(const struct option
*opt
, const char *reason
)
21 if (opt
->long_name
&& opt
->short_name
)
22 bug("switch '%c' (--%s) %s", opt
->short_name
,
23 opt
->long_name
, reason
);
24 else if (opt
->long_name
)
25 bug("option '%s' %s", opt
->long_name
, reason
);
27 bug("switch '%c' %s", opt
->short_name
, reason
);
30 static const char *optname(const struct option
*opt
, enum opt_parsed flags
)
32 static struct strbuf sb
= STRBUF_INIT
;
35 if (flags
& OPT_SHORT
)
36 strbuf_addf(&sb
, "switch `%c'", opt
->short_name
);
37 else if (flags
& OPT_UNSET
)
38 strbuf_addf(&sb
, "option `no-%s'", opt
->long_name
);
39 else if (flags
== OPT_LONG
)
40 strbuf_addf(&sb
, "option `%s'", opt
->long_name
);
42 BUG("optname() got unknown flags %d", flags
);
47 static enum parse_opt_result
get_arg(struct parse_opt_ctx_t
*p
,
48 const struct option
*opt
,
49 enum opt_parsed flags
, const char **arg
)
54 } else if (p
->argc
== 1 && (opt
->flags
& PARSE_OPT_LASTARG_DEFAULT
)) {
55 *arg
= (const char *)opt
->defval
;
56 } else if (p
->argc
> 1) {
60 return error(_("%s requires a value"), optname(opt
, flags
));
64 static void fix_filename(const char *prefix
, char **file
)
69 *file
= prefix_filename_except_for_dash(prefix
, *file
);
72 static enum parse_opt_result
opt_command_mode_error(
73 const struct option
*opt
,
74 const struct option
*all_opts
,
75 enum opt_parsed flags
)
77 const struct option
*that
;
78 struct strbuf that_name
= STRBUF_INIT
;
81 * Find the other option that was used to set the variable
82 * already, and report that this is not compatible with it.
84 for (that
= all_opts
; that
->type
!= OPTION_END
; that
++) {
86 !(that
->flags
& PARSE_OPT_CMDMODE
) ||
87 that
->value
!= opt
->value
||
88 that
->defval
!= *(int *)opt
->value
)
92 strbuf_addf(&that_name
, "--%s", that
->long_name
);
94 strbuf_addf(&that_name
, "-%c", that
->short_name
);
95 error(_("%s is incompatible with %s"),
96 optname(opt
, flags
), that_name
.buf
);
97 strbuf_release(&that_name
);
98 return PARSE_OPT_ERROR
;
100 return error(_("%s : incompatible with something else"),
101 optname(opt
, flags
));
104 static enum parse_opt_result
get_value(struct parse_opt_ctx_t
*p
,
105 const struct option
*opt
,
106 const struct option
*all_opts
,
107 enum opt_parsed flags
)
110 const int unset
= flags
& OPT_UNSET
;
114 return error(_("%s takes no value"), optname(opt
, flags
));
115 if (unset
&& (opt
->flags
& PARSE_OPT_NONEG
))
116 return error(_("%s isn't available"), optname(opt
, flags
));
117 if (!(flags
& OPT_SHORT
) && p
->opt
&& (opt
->flags
& PARSE_OPT_NOARG
))
118 return error(_("%s takes no value"), optname(opt
, flags
));
121 * Giving the same mode option twice, although unnecessary,
122 * is not a grave error, so let it pass.
124 if ((opt
->flags
& PARSE_OPT_CMDMODE
) &&
125 *(int *)opt
->value
&& *(int *)opt
->value
!= opt
->defval
)
126 return opt_command_mode_error(opt
, all_opts
, flags
);
129 case OPTION_LOWLEVEL_CALLBACK
:
130 return opt
->ll_callback(p
, opt
, NULL
, unset
);
134 *(int *)opt
->value
&= ~opt
->defval
;
136 *(int *)opt
->value
|= opt
->defval
;
141 *(int *)opt
->value
|= opt
->defval
;
143 *(int *)opt
->value
&= ~opt
->defval
;
148 BUG("BITOP can't have unset form");
149 *(int *)opt
->value
&= ~opt
->extra
;
150 *(int *)opt
->value
|= opt
->defval
;
154 if (*(int *)opt
->value
< 0)
155 *(int *)opt
->value
= 0;
156 *(int *)opt
->value
= unset
? 0 : *(int *)opt
->value
+ 1;
160 *(int *)opt
->value
= unset
? 0 : opt
->defval
;
165 *(const char **)opt
->value
= NULL
;
166 else if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
)
167 *(const char **)opt
->value
= (const char *)opt
->defval
;
169 return get_arg(p
, opt
, flags
, (const char **)opt
->value
);
172 case OPTION_FILENAME
:
175 *(const char **)opt
->value
= NULL
;
176 else if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
)
177 *(const char **)opt
->value
= (const char *)opt
->defval
;
179 err
= get_arg(p
, opt
, flags
, (const char **)opt
->value
);
182 fix_filename(p
->prefix
, (char **)opt
->value
);
185 case OPTION_CALLBACK
:
187 const char *p_arg
= NULL
;
192 else if (opt
->flags
& PARSE_OPT_NOARG
)
194 else if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
)
196 else if (get_arg(p
, opt
, flags
, &arg
))
203 return (*opt
->callback
)(opt
, p_arg
, p_unset
) ? (-1) : 0;
205 return (*opt
->ll_callback
)(p
, opt
, p_arg
, p_unset
);
209 *(int *)opt
->value
= 0;
212 if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
) {
213 *(int *)opt
->value
= opt
->defval
;
216 if (get_arg(p
, opt
, flags
, &arg
))
219 return error(_("%s expects a numerical value"),
220 optname(opt
, flags
));
221 *(int *)opt
->value
= strtol(arg
, (char **)&s
, 10);
223 return error(_("%s expects a numerical value"),
224 optname(opt
, flags
));
227 case OPTION_MAGNITUDE
:
229 *(unsigned long *)opt
->value
= 0;
232 if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
) {
233 *(unsigned long *)opt
->value
= opt
->defval
;
236 if (get_arg(p
, opt
, flags
, &arg
))
238 if (!git_parse_ulong(arg
, opt
->value
))
239 return error(_("%s expects a non-negative integer value"
240 " with an optional k/m/g suffix"),
241 optname(opt
, flags
));
245 BUG("opt->type %d should not happen", opt
->type
);
249 static enum parse_opt_result
parse_short_opt(struct parse_opt_ctx_t
*p
,
250 const struct option
*options
)
252 const struct option
*all_opts
= options
;
253 const struct option
*numopt
= NULL
;
255 for (; options
->type
!= OPTION_END
; options
++) {
256 if (options
->short_name
== *p
->opt
) {
257 p
->opt
= p
->opt
[1] ? p
->opt
+ 1 : NULL
;
258 return get_value(p
, options
, all_opts
, OPT_SHORT
);
262 * Handle the numerical option later, explicit one-digit
263 * options take precedence over it.
265 if (options
->type
== OPTION_NUMBER
)
268 if (numopt
&& isdigit(*p
->opt
)) {
273 while (isdigit(p
->opt
[len
]))
275 arg
= xmemdupz(p
->opt
, len
);
276 p
->opt
= p
->opt
[len
] ? p
->opt
+ len
: NULL
;
277 if (numopt
->callback
)
278 rc
= (*numopt
->callback
)(numopt
, arg
, 0) ? (-1) : 0;
280 rc
= (*numopt
->ll_callback
)(p
, numopt
, arg
, 0);
284 return PARSE_OPT_UNKNOWN
;
287 static int has_string(const char *it
, const char **array
)
290 if (!strcmp(it
, *(array
++)))
295 static int is_alias(struct parse_opt_ctx_t
*ctx
,
296 const struct option
*one_opt
,
297 const struct option
*another_opt
)
301 if (!ctx
->alias_groups
)
304 if (!one_opt
->long_name
|| !another_opt
->long_name
)
307 for (group
= ctx
->alias_groups
; *group
; group
+= 3) {
308 /* it and other are from the same family? */
309 if (has_string(one_opt
->long_name
, group
) &&
310 has_string(another_opt
->long_name
, group
))
316 static enum parse_opt_result
parse_long_opt(
317 struct parse_opt_ctx_t
*p
, const char *arg
,
318 const struct option
*options
)
320 const struct option
*all_opts
= options
;
321 const char *arg_end
= strchrnul(arg
, '=');
322 const struct option
*abbrev_option
= NULL
, *ambiguous_option
= NULL
;
323 enum opt_parsed abbrev_flags
= OPT_LONG
, ambiguous_flags
= OPT_LONG
;
325 for (; options
->type
!= OPTION_END
; options
++) {
326 const char *rest
, *long_name
= options
->long_name
;
327 enum opt_parsed flags
= OPT_LONG
, opt_flags
= OPT_LONG
;
329 if (options
->type
== OPTION_SUBCOMMAND
)
335 if (!skip_prefix(arg
, long_name
, &rest
))
339 if (!(p
->flags
& PARSE_OPT_KEEP_UNKNOWN_OPT
) &&
340 !strncmp(long_name
, arg
, arg_end
- arg
)) {
343 !is_alias(p
, abbrev_option
, options
)) {
345 * If this is abbreviated, it is
346 * ambiguous. So when there is no
347 * exact match later, we need to
350 ambiguous_option
= abbrev_option
;
351 ambiguous_flags
= abbrev_flags
;
353 if (!(flags
& OPT_UNSET
) && *arg_end
)
354 p
->opt
= arg_end
+ 1;
355 abbrev_option
= options
;
356 abbrev_flags
= flags
^ opt_flags
;
359 /* negation allowed? */
360 if (options
->flags
& PARSE_OPT_NONEG
)
362 /* negated and abbreviated very much? */
363 if (starts_with("no-", arg
)) {
368 if (!starts_with(arg
, "no-")) {
369 if (skip_prefix(long_name
, "no-", &long_name
)) {
370 opt_flags
|= OPT_UNSET
;
376 if (!skip_prefix(arg
+ 3, long_name
, &rest
)) {
377 /* abbreviated and negated? */
378 if (starts_with(long_name
, arg
+ 3))
389 return get_value(p
, options
, all_opts
, flags
^ opt_flags
);
392 if (disallow_abbreviated_options
&& (ambiguous_option
|| abbrev_option
))
393 die("disallowed abbreviated or ambiguous option '%.*s'",
394 (int)(arg_end
- arg
), arg
);
396 if (ambiguous_option
) {
397 error(_("ambiguous option: %s "
398 "(could be --%s%s or --%s%s)"),
400 (ambiguous_flags
& OPT_UNSET
) ? "no-" : "",
401 ambiguous_option
->long_name
,
402 (abbrev_flags
& OPT_UNSET
) ? "no-" : "",
403 abbrev_option
->long_name
);
404 return PARSE_OPT_HELP
;
407 return get_value(p
, abbrev_option
, all_opts
, abbrev_flags
);
408 return PARSE_OPT_UNKNOWN
;
411 static enum parse_opt_result
parse_nodash_opt(struct parse_opt_ctx_t
*p
,
413 const struct option
*options
)
415 const struct option
*all_opts
= options
;
417 for (; options
->type
!= OPTION_END
; options
++) {
418 if (!(options
->flags
& PARSE_OPT_NODASH
))
420 if (options
->short_name
== arg
[0] && arg
[1] == '\0')
421 return get_value(p
, options
, all_opts
, OPT_SHORT
);
423 return PARSE_OPT_ERROR
;
426 static enum parse_opt_result
parse_subcommand(const char *arg
,
427 const struct option
*options
)
429 for (; options
->type
!= OPTION_END
; options
++)
430 if (options
->type
== OPTION_SUBCOMMAND
&&
431 !strcmp(options
->long_name
, arg
)) {
432 *(parse_opt_subcommand_fn
**)options
->value
= options
->subcommand_fn
;
433 return PARSE_OPT_SUBCOMMAND
;
436 return PARSE_OPT_UNKNOWN
;
439 static void check_typos(const char *arg
, const struct option
*options
)
444 if (starts_with(arg
, "no-")) {
445 error(_("did you mean `--%s` (with two dashes)?"), arg
);
449 for (; options
->type
!= OPTION_END
; options
++) {
450 if (!options
->long_name
)
452 if (starts_with(options
->long_name
, arg
)) {
453 error(_("did you mean `--%s` (with two dashes)?"), arg
);
459 static void parse_options_check(const struct option
*opts
)
461 char short_opts
[128];
462 void *subcommand_value
= NULL
;
464 memset(short_opts
, '\0', sizeof(short_opts
));
465 for (; opts
->type
!= OPTION_END
; opts
++) {
466 if ((opts
->flags
& PARSE_OPT_LASTARG_DEFAULT
) &&
467 (opts
->flags
& PARSE_OPT_OPTARG
))
468 optbug(opts
, "uses incompatible flags "
469 "LASTARG_DEFAULT and OPTARG");
470 if (opts
->short_name
) {
471 if (0x7F <= opts
->short_name
)
472 optbug(opts
, "invalid short name");
473 else if (short_opts
[opts
->short_name
]++)
474 optbug(opts
, "short name already used");
476 if (opts
->flags
& PARSE_OPT_NODASH
&&
477 ((opts
->flags
& PARSE_OPT_OPTARG
) ||
478 !(opts
->flags
& PARSE_OPT_NOARG
) ||
479 !(opts
->flags
& PARSE_OPT_NONEG
) ||
481 optbug(opts
, "uses feature "
482 "not supported for dashless options");
483 switch (opts
->type
) {
489 if ((opts
->flags
& PARSE_OPT_OPTARG
) ||
490 !(opts
->flags
& PARSE_OPT_NOARG
))
491 optbug(opts
, "should not accept an argument");
493 case OPTION_CALLBACK
:
494 if (!opts
->callback
&& !opts
->ll_callback
)
495 optbug(opts
, "OPTION_CALLBACK needs one callback");
496 else if (opts
->callback
&& opts
->ll_callback
)
497 optbug(opts
, "OPTION_CALLBACK can't have two callbacks");
499 case OPTION_LOWLEVEL_CALLBACK
:
500 if (!opts
->ll_callback
)
501 optbug(opts
, "OPTION_LOWLEVEL_CALLBACK needs a callback");
503 optbug(opts
, "OPTION_LOWLEVEL_CALLBACK needs no high level callback");
506 optbug(opts
, "OPT_ALIAS() should not remain at this point. "
507 "Are you using parse_options_step() directly?\n"
508 "That case is not supported yet.");
510 case OPTION_SUBCOMMAND
:
511 if (!opts
->value
|| !opts
->subcommand_fn
)
512 optbug(opts
, "OPTION_SUBCOMMAND needs a value and a subcommand function");
513 if (!subcommand_value
)
514 subcommand_value
= opts
->value
;
515 else if (subcommand_value
!= opts
->value
)
516 optbug(opts
, "all OPTION_SUBCOMMANDs need the same value");
519 ; /* ok. (usually accepts an argument) */
522 strcspn(opts
->argh
, " _") != strlen(opts
->argh
))
523 optbug(opts
, "multi-word argh should use dash to separate words");
525 BUG_if_bug("invalid 'struct option'");
528 static int has_subcommands(const struct option
*options
)
530 for (; options
->type
!= OPTION_END
; options
++)
531 if (options
->type
== OPTION_SUBCOMMAND
)
536 static void parse_options_start_1(struct parse_opt_ctx_t
*ctx
,
537 int argc
, const char **argv
, const char *prefix
,
538 const struct option
*options
,
539 enum parse_opt_flags flags
)
543 if (!(flags
& PARSE_OPT_ONE_SHOT
)) {
547 ctx
->total
= ctx
->argc
;
549 ctx
->prefix
= prefix
;
550 ctx
->cpidx
= ((flags
& PARSE_OPT_KEEP_ARGV0
) != 0);
552 ctx
->has_subcommands
= has_subcommands(options
);
553 if (!ctx
->has_subcommands
&& (flags
& PARSE_OPT_SUBCOMMAND_OPTIONAL
))
554 BUG("Using PARSE_OPT_SUBCOMMAND_OPTIONAL without subcommands");
555 if (ctx
->has_subcommands
) {
556 if (flags
& PARSE_OPT_STOP_AT_NON_OPTION
)
557 BUG("subcommands are incompatible with PARSE_OPT_STOP_AT_NON_OPTION");
558 if (!(flags
& PARSE_OPT_SUBCOMMAND_OPTIONAL
)) {
559 if (flags
& PARSE_OPT_KEEP_UNKNOWN_OPT
)
560 BUG("subcommands are incompatible with PARSE_OPT_KEEP_UNKNOWN_OPT unless in combination with PARSE_OPT_SUBCOMMAND_OPTIONAL");
561 if (flags
& PARSE_OPT_KEEP_DASHDASH
)
562 BUG("subcommands are incompatible with PARSE_OPT_KEEP_DASHDASH unless in combination with PARSE_OPT_SUBCOMMAND_OPTIONAL");
565 if ((flags
& PARSE_OPT_KEEP_UNKNOWN_OPT
) &&
566 (flags
& PARSE_OPT_STOP_AT_NON_OPTION
) &&
567 !(flags
& PARSE_OPT_ONE_SHOT
))
568 BUG("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
569 if ((flags
& PARSE_OPT_ONE_SHOT
) &&
570 (flags
& PARSE_OPT_KEEP_ARGV0
))
571 BUG("Can't keep argv0 if you don't have it");
572 parse_options_check(options
);
575 void parse_options_start(struct parse_opt_ctx_t
*ctx
,
576 int argc
, const char **argv
, const char *prefix
,
577 const struct option
*options
,
578 enum parse_opt_flags flags
)
580 memset(ctx
, 0, sizeof(*ctx
));
581 parse_options_start_1(ctx
, argc
, argv
, prefix
, options
, flags
);
584 static void show_negated_gitcomp(const struct option
*opts
, int show_all
,
587 int printed_dashdash
= 0;
589 for (; opts
->type
!= OPTION_END
; opts
++) {
590 int has_unset_form
= 0;
593 if (!opts
->long_name
)
596 (opts
->flags
& (PARSE_OPT_HIDDEN
| PARSE_OPT_NOCOMPLETE
)))
598 if (opts
->flags
& PARSE_OPT_NONEG
)
601 switch (opts
->type
) {
603 case OPTION_FILENAME
:
605 case OPTION_MAGNITUDE
:
606 case OPTION_CALLBACK
:
619 if (skip_prefix(opts
->long_name
, "no-", &name
)) {
621 printf(" --%s", name
);
622 } else if (nr_noopts
>= 0) {
623 if (nr_noopts
&& !printed_dashdash
) {
625 printed_dashdash
= 1;
627 printf(" --no-%s", opts
->long_name
);
633 static int show_gitcomp(const struct option
*opts
, int show_all
)
635 const struct option
*original_opts
= opts
;
638 for (; opts
->type
!= OPTION_END
; opts
++) {
639 const char *prefix
= "--";
640 const char *suffix
= "";
642 if (!opts
->long_name
)
645 (opts
->flags
& (PARSE_OPT_HIDDEN
| PARSE_OPT_NOCOMPLETE
| PARSE_OPT_FROM_ALIAS
)))
648 switch (opts
->type
) {
649 case OPTION_SUBCOMMAND
:
655 case OPTION_FILENAME
:
657 case OPTION_MAGNITUDE
:
658 case OPTION_CALLBACK
:
659 if (opts
->flags
& PARSE_OPT_NOARG
)
661 if (opts
->flags
& PARSE_OPT_OPTARG
)
663 if (opts
->flags
& PARSE_OPT_LASTARG_DEFAULT
)
670 if (opts
->flags
& PARSE_OPT_COMP_ARG
)
672 if (starts_with(opts
->long_name
, "no-"))
674 printf("%s%s%s%s", opts
== original_opts
? "" : " ",
675 prefix
, opts
->long_name
, suffix
);
677 show_negated_gitcomp(original_opts
, show_all
, -1);
678 show_negated_gitcomp(original_opts
, show_all
, nr_noopts
);
680 return PARSE_OPT_COMPLETE
;
684 * Scan and may produce a new option[] array, which should be used
685 * instead of the original 'options'.
687 * Right now this is only used to preprocess and substitute
690 * The returned options should be freed using free_preprocessed_options.
692 static struct option
*preprocess_options(struct parse_opt_ctx_t
*ctx
,
693 const struct option
*options
)
695 struct option
*newopt
;
699 for (nr
= 0; options
[nr
].type
!= OPTION_END
; nr
++) {
700 if (options
[nr
].type
== OPTION_ALIAS
)
707 DUP_ARRAY(newopt
, options
, nr
+ 1);
709 /* each alias has two string pointers and NULL */
710 CALLOC_ARRAY(ctx
->alias_groups
, 3 * (nr_aliases
+ 1));
712 for (alias
= 0, i
= 0; i
< nr
; i
++) {
714 const char *long_name
;
716 struct strbuf help
= STRBUF_INIT
;
719 if (newopt
[i
].type
!= OPTION_ALIAS
)
722 short_name
= newopt
[i
].short_name
;
723 long_name
= newopt
[i
].long_name
;
724 source
= newopt
[i
].value
;
727 BUG("An alias must have long option name");
728 strbuf_addf(&help
, _("alias of --%s"), source
);
730 for (j
= 0; j
< nr
; j
++) {
731 const char *name
= options
[j
].long_name
;
733 if (!name
|| strcmp(name
, source
))
736 if (options
[j
].type
== OPTION_ALIAS
)
737 BUG("No please. Nested aliases are not supported.");
739 memcpy(newopt
+ i
, options
+ j
, sizeof(*newopt
));
740 newopt
[i
].short_name
= short_name
;
741 newopt
[i
].long_name
= long_name
;
742 newopt
[i
].help
= strbuf_detach(&help
, NULL
);
743 newopt
[i
].flags
|= PARSE_OPT_FROM_ALIAS
;
748 BUG("could not find source option '%s' of alias '%s'",
749 source
, newopt
[i
].long_name
);
750 ctx
->alias_groups
[alias
* 3 + 0] = newopt
[i
].long_name
;
751 ctx
->alias_groups
[alias
* 3 + 1] = options
[j
].long_name
;
752 ctx
->alias_groups
[alias
* 3 + 2] = NULL
;
759 static void free_preprocessed_options(struct option
*options
)
766 for (i
= 0; options
[i
].type
!= OPTION_END
; i
++) {
767 if (options
[i
].flags
& PARSE_OPT_FROM_ALIAS
)
768 free((void *)options
[i
].help
);
773 static enum parse_opt_result
usage_with_options_internal(struct parse_opt_ctx_t
*,
774 const char * const *,
775 const struct option
*,
778 enum parse_opt_result
parse_options_step(struct parse_opt_ctx_t
*ctx
,
779 const struct option
*options
,
780 const char * const usagestr
[])
782 int internal_help
= !(ctx
->flags
& PARSE_OPT_NO_INTERNAL_HELP
);
784 /* we must reset ->opt, unknown short option leave it dangling */
787 for (; ctx
->argc
; ctx
->argc
--, ctx
->argv
++) {
788 const char *arg
= ctx
->argv
[0];
790 if (ctx
->flags
& PARSE_OPT_ONE_SHOT
&&
791 ctx
->argc
!= ctx
->total
)
794 if (*arg
!= '-' || !arg
[1]) {
795 if (parse_nodash_opt(ctx
, arg
, options
) == 0)
797 if (!ctx
->has_subcommands
) {
798 if (ctx
->flags
& PARSE_OPT_STOP_AT_NON_OPTION
)
799 return PARSE_OPT_NON_OPTION
;
800 ctx
->out
[ctx
->cpidx
++] = ctx
->argv
[0];
803 switch (parse_subcommand(arg
, options
)) {
804 case PARSE_OPT_SUBCOMMAND
:
805 return PARSE_OPT_SUBCOMMAND
;
806 case PARSE_OPT_UNKNOWN
:
807 if (ctx
->flags
& PARSE_OPT_SUBCOMMAND_OPTIONAL
)
809 * arg is neither a short or long
810 * option nor a subcommand. Since
811 * this command has a default
812 * operation mode, we have to treat
813 * this arg and all remaining args
814 * as args meant to that default
816 * So we are done parsing.
818 return PARSE_OPT_DONE
;
819 error(_("unknown subcommand: `%s'"), arg
);
820 usage_with_options(usagestr
, options
);
821 case PARSE_OPT_COMPLETE
:
823 case PARSE_OPT_ERROR
:
825 case PARSE_OPT_NON_OPTION
:
827 BUG("parse_subcommand() cannot return these");
831 /* lone -h asks for help */
832 if (internal_help
&& ctx
->total
== 1 && !strcmp(arg
+ 1, "h"))
836 * lone --git-completion-helper and --git-completion-helper-all
837 * are asked by git-completion.bash
839 if (ctx
->total
== 1 && !strcmp(arg
, "--git-completion-helper"))
840 return show_gitcomp(options
, 0);
841 if (ctx
->total
== 1 && !strcmp(arg
, "--git-completion-helper-all"))
842 return show_gitcomp(options
, 1);
846 switch (parse_short_opt(ctx
, options
)) {
847 case PARSE_OPT_ERROR
:
848 return PARSE_OPT_ERROR
;
849 case PARSE_OPT_UNKNOWN
:
851 check_typos(arg
+ 1, options
);
852 if (internal_help
&& *ctx
->opt
== 'h')
855 case PARSE_OPT_NON_OPTION
:
856 case PARSE_OPT_SUBCOMMAND
:
858 case PARSE_OPT_COMPLETE
:
859 BUG("parse_short_opt() cannot return these");
864 check_typos(arg
+ 1, options
);
866 switch (parse_short_opt(ctx
, options
)) {
867 case PARSE_OPT_ERROR
:
868 return PARSE_OPT_ERROR
;
869 case PARSE_OPT_UNKNOWN
:
870 if (internal_help
&& *ctx
->opt
== 'h')
873 /* fake a short option thing to hide the fact that we may have
874 * started to parse aggregated stuff
876 * This is leaky, too bad.
878 ctx
->argv
[0] = xstrdup(ctx
->opt
- 1);
879 *(char *)ctx
->argv
[0] = '-';
881 case PARSE_OPT_NON_OPTION
:
882 case PARSE_OPT_SUBCOMMAND
:
883 case PARSE_OPT_COMPLETE
:
885 BUG("parse_short_opt() cannot return these");
893 if (!arg
[2] /* "--" */ ||
894 !strcmp(arg
+ 2, "end-of-options")) {
895 if (!(ctx
->flags
& PARSE_OPT_KEEP_DASHDASH
)) {
902 if (internal_help
&& !strcmp(arg
+ 2, "help-all"))
903 return usage_with_options_internal(ctx
, usagestr
, options
, 1, 0);
904 if (internal_help
&& !strcmp(arg
+ 2, "help"))
906 switch (parse_long_opt(ctx
, arg
+ 2, options
)) {
907 case PARSE_OPT_ERROR
:
908 return PARSE_OPT_ERROR
;
909 case PARSE_OPT_UNKNOWN
:
913 case PARSE_OPT_NON_OPTION
:
914 case PARSE_OPT_SUBCOMMAND
:
915 case PARSE_OPT_COMPLETE
:
916 BUG("parse_long_opt() cannot return these");
922 if (ctx
->flags
& PARSE_OPT_ONE_SHOT
)
924 if (ctx
->has_subcommands
&&
925 (ctx
->flags
& PARSE_OPT_SUBCOMMAND_OPTIONAL
) &&
926 (ctx
->flags
& PARSE_OPT_KEEP_UNKNOWN_OPT
)) {
928 * Found an unknown option given to a command with
929 * subcommands that has a default operation mode:
930 * we treat this option and all remaining args as
931 * arguments meant to that default operation mode.
932 * So we are done parsing.
934 return PARSE_OPT_DONE
;
936 if (!(ctx
->flags
& PARSE_OPT_KEEP_UNKNOWN_OPT
))
937 return PARSE_OPT_UNKNOWN
;
938 ctx
->out
[ctx
->cpidx
++] = ctx
->argv
[0];
941 return PARSE_OPT_DONE
;
944 return usage_with_options_internal(ctx
, usagestr
, options
, 0, 0);
947 int parse_options_end(struct parse_opt_ctx_t
*ctx
)
949 if (ctx
->flags
& PARSE_OPT_ONE_SHOT
)
950 return ctx
->total
- ctx
->argc
;
952 MOVE_ARRAY(ctx
->out
+ ctx
->cpidx
, ctx
->argv
, ctx
->argc
);
953 ctx
->out
[ctx
->cpidx
+ ctx
->argc
] = NULL
;
954 return ctx
->cpidx
+ ctx
->argc
;
957 int parse_options(int argc
, const char **argv
,
959 const struct option
*options
,
960 const char * const usagestr
[],
961 enum parse_opt_flags flags
)
963 struct parse_opt_ctx_t ctx
;
964 struct option
*real_options
;
966 disallow_abbreviated_options
=
967 git_env_bool("GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS", 0);
969 memset(&ctx
, 0, sizeof(ctx
));
970 real_options
= preprocess_options(&ctx
, options
);
972 options
= real_options
;
973 parse_options_start_1(&ctx
, argc
, argv
, prefix
, options
, flags
);
974 switch (parse_options_step(&ctx
, options
, usagestr
)) {
976 case PARSE_OPT_ERROR
:
978 case PARSE_OPT_COMPLETE
:
980 case PARSE_OPT_NON_OPTION
:
981 case PARSE_OPT_SUBCOMMAND
:
984 if (ctx
.has_subcommands
&&
985 !(flags
& PARSE_OPT_SUBCOMMAND_OPTIONAL
)) {
986 error(_("need a subcommand"));
987 usage_with_options(usagestr
, options
);
990 case PARSE_OPT_UNKNOWN
:
991 if (ctx
.argv
[0][1] == '-') {
992 error(_("unknown option `%s'"), ctx
.argv
[0] + 2);
993 } else if (isascii(*ctx
.opt
)) {
994 error(_("unknown switch `%c'"), *ctx
.opt
);
996 error(_("unknown non-ascii option in string: `%s'"),
999 usage_with_options(usagestr
, options
);
1002 precompose_argv_prefix(argc
, argv
, NULL
);
1003 free_preprocessed_options(real_options
);
1004 free(ctx
.alias_groups
);
1005 return parse_options_end(&ctx
);
1008 static int usage_argh(const struct option
*opts
, FILE *outfile
)
1011 int literal
= (opts
->flags
& PARSE_OPT_LITERAL_ARGHELP
) ||
1012 !opts
->argh
|| !!strpbrk(opts
->argh
, "()<>[]|");
1013 if (opts
->flags
& PARSE_OPT_OPTARG
)
1014 if (opts
->long_name
)
1015 s
= literal
? "[=%s]" : "[=<%s>]";
1017 s
= literal
? "[%s]" : "[<%s>]";
1019 s
= literal
? " %s" : " <%s>";
1020 return utf8_fprintf(outfile
, s
, opts
->argh
? _(opts
->argh
) : _("..."));
1023 #define USAGE_OPTS_WIDTH 24
1026 static enum parse_opt_result
usage_with_options_internal(struct parse_opt_ctx_t
*ctx
,
1027 const char * const *usagestr
,
1028 const struct option
*opts
,
1031 FILE *outfile
= err
? stderr
: stdout
;
1034 const char *usage_prefix
= _("usage: %s");
1036 * The translation could be anything, but we can count on
1037 * msgfmt(1)'s --check option to have asserted that "%s" is in
1038 * the translation. So compute the length of the "usage: "
1039 * part. We are assuming that the translator wasn't overly
1040 * clever and used e.g. "%1$s" instead of "%s", there's only
1041 * one "%s" in "usage_prefix" above, so there's no reason to
1042 * do so even with a RTL language.
1044 size_t usage_len
= strlen(usage_prefix
) - strlen("%s");
1046 * TRANSLATORS: the colon here should align with the
1047 * one in "usage: %s" translation.
1049 const char *or_prefix
= _(" or: %s");
1051 * TRANSLATORS: You should only need to translate this format
1052 * string if your language is a RTL language (e.g. Arabic,
1053 * Hebrew etc.), not if it's a LTR language (e.g. German,
1054 * Russian, Chinese etc.).
1056 * When a translated usage string has an embedded "\n" it's
1057 * because options have wrapped to the next line. The line
1058 * after the "\n" will then be padded to align with the
1059 * command name, such as N_("git cmd [opt]\n<8
1060 * spaces>[opt2]"), where the 8 spaces are the same length as
1063 * This format string prints out that already-translated
1064 * line. The "%*s" is whitespace padding to account for the
1065 * padding at the start of the line that we add in this
1066 * function. The "%s" is a line in the (hopefully already
1067 * translated) N_() usage string, which contained embedded
1068 * newlines before we split it up.
1070 const char *usage_continued
= _("%*s%s");
1071 const char *prefix
= usage_prefix
;
1072 int saw_empty_line
= 0;
1075 return PARSE_OPT_HELP
;
1077 if (!err
&& ctx
&& ctx
->flags
& PARSE_OPT_SHELL_EVAL
)
1078 fprintf(outfile
, "cat <<\\EOF\n");
1081 const char *str
= _(*usagestr
++);
1082 struct string_list list
= STRING_LIST_INIT_DUP
;
1085 if (!saw_empty_line
&& !*str
)
1088 string_list_split(&list
, str
, '\n', -1);
1089 for (j
= 0; j
< list
.nr
; j
++) {
1090 const char *line
= list
.items
[j
].string
;
1092 if (saw_empty_line
&& *line
)
1093 fprintf_ln(outfile
, _(" %s"), line
);
1094 else if (saw_empty_line
)
1095 fputc('\n', outfile
);
1097 fprintf_ln(outfile
, prefix
, line
);
1099 fprintf_ln(outfile
, usage_continued
,
1100 (int)usage_len
, "", line
);
1102 string_list_clear(&list
, 0);
1109 for (; opts
->type
!= OPTION_END
; opts
++) {
1112 const char *cp
, *np
;
1114 if (opts
->type
== OPTION_SUBCOMMAND
)
1116 if (opts
->type
== OPTION_GROUP
) {
1117 fputc('\n', outfile
);
1120 fprintf(outfile
, "%s\n", _(opts
->help
));
1123 if (!full
&& (opts
->flags
& PARSE_OPT_HIDDEN
))
1127 fputc('\n', outfile
);
1131 pos
= fprintf(outfile
, " ");
1132 if (opts
->short_name
) {
1133 if (opts
->flags
& PARSE_OPT_NODASH
)
1134 pos
+= fprintf(outfile
, "%c", opts
->short_name
);
1136 pos
+= fprintf(outfile
, "-%c", opts
->short_name
);
1138 if (opts
->long_name
&& opts
->short_name
)
1139 pos
+= fprintf(outfile
, ", ");
1140 if (opts
->long_name
)
1141 pos
+= fprintf(outfile
, "--%s", opts
->long_name
);
1142 if (opts
->type
== OPTION_NUMBER
)
1143 pos
+= utf8_fprintf(outfile
, _("-NUM"));
1145 if ((opts
->flags
& PARSE_OPT_LITERAL_ARGHELP
) ||
1146 !(opts
->flags
& PARSE_OPT_NOARG
))
1147 pos
+= usage_argh(opts
, outfile
);
1149 if (pos
== USAGE_OPTS_WIDTH
+ 1)
1151 else if (pos
<= USAGE_OPTS_WIDTH
)
1152 pad
= USAGE_OPTS_WIDTH
- pos
;
1154 fputc('\n', outfile
);
1155 pad
= USAGE_OPTS_WIDTH
;
1157 if (opts
->type
== OPTION_ALIAS
) {
1158 fprintf(outfile
, "%*s", pad
+ USAGE_GAP
, "");
1159 fprintf_ln(outfile
, _("alias of --%s"),
1160 (const char *)opts
->value
);
1164 for (cp
= _(opts
->help
); *cp
; cp
= np
) {
1165 np
= strchrnul(cp
, '\n');
1167 "%*s%.*s\n", pad
+ USAGE_GAP
, "",
1168 (int)(np
- cp
), cp
);
1171 pad
= USAGE_OPTS_WIDTH
;
1174 fputc('\n', outfile
);
1176 if (!err
&& ctx
&& ctx
->flags
& PARSE_OPT_SHELL_EVAL
)
1177 fputs("EOF\n", outfile
);
1179 return PARSE_OPT_HELP
;
1182 void NORETURN
usage_with_options(const char * const *usagestr
,
1183 const struct option
*opts
)
1185 usage_with_options_internal(NULL
, usagestr
, opts
, 0, 1);
1189 void NORETURN
usage_msg_opt(const char *msg
,
1190 const char * const *usagestr
,
1191 const struct option
*options
)
1193 die_message("%s\n", msg
); /* The extra \n is intentional */
1194 usage_with_options(usagestr
, options
);
1197 void NORETURN
usage_msg_optf(const char * const fmt
,
1198 const char * const *usagestr
,
1199 const struct option
*options
, ...)
1201 struct strbuf msg
= STRBUF_INIT
;
1203 va_start(ap
, options
);
1204 strbuf_vaddf(&msg
, fmt
, ap
);
1207 usage_msg_opt(msg
.buf
, usagestr
, options
);
1210 void die_for_incompatible_opt4(int opt1
, const char *opt1_name
,
1211 int opt2
, const char *opt2_name
,
1212 int opt3
, const char *opt3_name
,
1213 int opt4
, const char *opt4_name
)
1216 const char *options
[4];
1219 options
[count
++] = opt1_name
;
1221 options
[count
++] = opt2_name
;
1223 options
[count
++] = opt3_name
;
1225 options
[count
++] = opt4_name
;
1228 die(_("options '%s', '%s', '%s', and '%s' cannot be used together"),
1229 opt1_name
, opt2_name
, opt3_name
, opt4_name
);
1232 die(_("options '%s', '%s', and '%s' cannot be used together"),
1233 options
[0], options
[1], options
[2]);
1236 die(_("options '%s' and '%s' cannot be used together"),
1237 options
[0], options
[1]);