1 #include "git-compat-util.h"
2 #include "parse-options.h"
9 #include "string-list.h"
12 static int disallow_abbreviated_options
;
20 static void optbug(const struct option
*opt
, const char *reason
)
22 if (opt
->long_name
&& opt
->short_name
)
23 bug("switch '%c' (--%s) %s", opt
->short_name
,
24 opt
->long_name
, reason
);
25 else if (opt
->long_name
)
26 bug("option '%s' %s", opt
->long_name
, reason
);
28 bug("switch '%c' %s", opt
->short_name
, reason
);
31 static const char *optname(const struct option
*opt
, enum opt_parsed flags
)
33 static struct strbuf sb
= STRBUF_INIT
;
36 if (flags
& OPT_SHORT
)
37 strbuf_addf(&sb
, "switch `%c'", opt
->short_name
);
38 else if (flags
& OPT_UNSET
)
39 strbuf_addf(&sb
, "option `no-%s'", opt
->long_name
);
40 else if (flags
== OPT_LONG
)
41 strbuf_addf(&sb
, "option `%s'", opt
->long_name
);
43 BUG("optname() got unknown flags %d", flags
);
48 static enum parse_opt_result
get_arg(struct parse_opt_ctx_t
*p
,
49 const struct option
*opt
,
50 enum opt_parsed flags
, const char **arg
)
55 } else if (p
->argc
== 1 && (opt
->flags
& PARSE_OPT_LASTARG_DEFAULT
)) {
56 *arg
= (const char *)opt
->defval
;
57 } else if (p
->argc
> 1) {
61 return error(_("%s requires a value"), optname(opt
, flags
));
65 static void fix_filename(const char *prefix
, char **file
)
70 *file
= prefix_filename_except_for_dash(prefix
, *file
);
73 static enum parse_opt_result
opt_command_mode_error(
74 const struct option
*opt
,
75 const struct option
*all_opts
,
76 enum opt_parsed flags
)
78 const struct option
*that
;
79 struct strbuf that_name
= STRBUF_INIT
;
82 * Find the other option that was used to set the variable
83 * already, and report that this is not compatible with it.
85 for (that
= all_opts
; that
->type
!= OPTION_END
; that
++) {
87 !(that
->flags
& PARSE_OPT_CMDMODE
) ||
88 that
->value
!= opt
->value
||
89 that
->defval
!= *(int *)opt
->value
)
93 strbuf_addf(&that_name
, "--%s", that
->long_name
);
95 strbuf_addf(&that_name
, "-%c", that
->short_name
);
96 error(_("%s is incompatible with %s"),
97 optname(opt
, flags
), that_name
.buf
);
98 strbuf_release(&that_name
);
99 return PARSE_OPT_ERROR
;
101 return error(_("%s : incompatible with something else"),
102 optname(opt
, flags
));
105 static enum parse_opt_result
get_value(struct parse_opt_ctx_t
*p
,
106 const struct option
*opt
,
107 const struct option
*all_opts
,
108 enum opt_parsed flags
)
111 const int unset
= flags
& OPT_UNSET
;
115 return error(_("%s takes no value"), optname(opt
, flags
));
116 if (unset
&& (opt
->flags
& PARSE_OPT_NONEG
))
117 return error(_("%s isn't available"), optname(opt
, flags
));
118 if (!(flags
& OPT_SHORT
) && p
->opt
&& (opt
->flags
& PARSE_OPT_NOARG
))
119 return error(_("%s takes no value"), optname(opt
, flags
));
122 * Giving the same mode option twice, although unnecessary,
123 * is not a grave error, so let it pass.
125 if ((opt
->flags
& PARSE_OPT_CMDMODE
) &&
126 *(int *)opt
->value
&& *(int *)opt
->value
!= opt
->defval
)
127 return opt_command_mode_error(opt
, all_opts
, flags
);
130 case OPTION_LOWLEVEL_CALLBACK
:
131 return opt
->ll_callback(p
, opt
, NULL
, unset
);
135 *(int *)opt
->value
&= ~opt
->defval
;
137 *(int *)opt
->value
|= opt
->defval
;
142 *(int *)opt
->value
|= opt
->defval
;
144 *(int *)opt
->value
&= ~opt
->defval
;
149 BUG("BITOP can't have unset form");
150 *(int *)opt
->value
&= ~opt
->extra
;
151 *(int *)opt
->value
|= opt
->defval
;
155 if (*(int *)opt
->value
< 0)
156 *(int *)opt
->value
= 0;
157 *(int *)opt
->value
= unset
? 0 : *(int *)opt
->value
+ 1;
161 *(int *)opt
->value
= unset
? 0 : opt
->defval
;
166 *(const char **)opt
->value
= NULL
;
167 else if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
)
168 *(const char **)opt
->value
= (const char *)opt
->defval
;
170 return get_arg(p
, opt
, flags
, (const char **)opt
->value
);
173 case OPTION_FILENAME
:
176 *(const char **)opt
->value
= NULL
;
177 else if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
)
178 *(const char **)opt
->value
= (const char *)opt
->defval
;
180 err
= get_arg(p
, opt
, flags
, (const char **)opt
->value
);
183 fix_filename(p
->prefix
, (char **)opt
->value
);
186 case OPTION_CALLBACK
:
188 const char *p_arg
= NULL
;
193 else if (opt
->flags
& PARSE_OPT_NOARG
)
195 else if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
)
197 else if (get_arg(p
, opt
, flags
, &arg
))
204 return (*opt
->callback
)(opt
, p_arg
, p_unset
) ? (-1) : 0;
206 return (*opt
->ll_callback
)(p
, opt
, p_arg
, p_unset
);
210 *(int *)opt
->value
= 0;
213 if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
) {
214 *(int *)opt
->value
= opt
->defval
;
217 if (get_arg(p
, opt
, flags
, &arg
))
220 return error(_("%s expects a numerical value"),
221 optname(opt
, flags
));
222 *(int *)opt
->value
= strtol(arg
, (char **)&s
, 10);
224 return error(_("%s expects a numerical value"),
225 optname(opt
, flags
));
228 case OPTION_MAGNITUDE
:
230 *(unsigned long *)opt
->value
= 0;
233 if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
) {
234 *(unsigned long *)opt
->value
= opt
->defval
;
237 if (get_arg(p
, opt
, flags
, &arg
))
239 if (!git_parse_ulong(arg
, opt
->value
))
240 return error(_("%s expects a non-negative integer value"
241 " with an optional k/m/g suffix"),
242 optname(opt
, flags
));
246 BUG("opt->type %d should not happen", opt
->type
);
250 static enum parse_opt_result
parse_short_opt(struct parse_opt_ctx_t
*p
,
251 const struct option
*options
)
253 const struct option
*all_opts
= options
;
254 const struct option
*numopt
= NULL
;
256 for (; options
->type
!= OPTION_END
; options
++) {
257 if (options
->short_name
== *p
->opt
) {
258 p
->opt
= p
->opt
[1] ? p
->opt
+ 1 : NULL
;
259 return get_value(p
, options
, all_opts
, OPT_SHORT
);
263 * Handle the numerical option later, explicit one-digit
264 * options take precedence over it.
266 if (options
->type
== OPTION_NUMBER
)
269 if (numopt
&& isdigit(*p
->opt
)) {
274 while (isdigit(p
->opt
[len
]))
276 arg
= xmemdupz(p
->opt
, len
);
277 p
->opt
= p
->opt
[len
] ? p
->opt
+ len
: NULL
;
278 if (numopt
->callback
)
279 rc
= (*numopt
->callback
)(numopt
, arg
, 0) ? (-1) : 0;
281 rc
= (*numopt
->ll_callback
)(p
, numopt
, arg
, 0);
285 return PARSE_OPT_UNKNOWN
;
288 static int has_string(const char *it
, const char **array
)
291 if (!strcmp(it
, *(array
++)))
296 static int is_alias(struct parse_opt_ctx_t
*ctx
,
297 const struct option
*one_opt
,
298 const struct option
*another_opt
)
302 if (!ctx
->alias_groups
)
305 if (!one_opt
->long_name
|| !another_opt
->long_name
)
308 for (group
= ctx
->alias_groups
; *group
; group
+= 3) {
309 /* it and other are from the same family? */
310 if (has_string(one_opt
->long_name
, group
) &&
311 has_string(another_opt
->long_name
, group
))
317 static enum parse_opt_result
parse_long_opt(
318 struct parse_opt_ctx_t
*p
, const char *arg
,
319 const struct option
*options
)
321 const struct option
*all_opts
= options
;
322 const char *arg_end
= strchrnul(arg
, '=');
323 const struct option
*abbrev_option
= NULL
, *ambiguous_option
= NULL
;
324 enum opt_parsed abbrev_flags
= OPT_LONG
, ambiguous_flags
= OPT_LONG
;
326 for (; options
->type
!= OPTION_END
; options
++) {
327 const char *rest
, *long_name
= options
->long_name
;
328 enum opt_parsed flags
= OPT_LONG
, opt_flags
= OPT_LONG
;
330 if (options
->type
== OPTION_SUBCOMMAND
)
336 if (!skip_prefix(arg
, long_name
, &rest
))
340 if (!(p
->flags
& PARSE_OPT_KEEP_UNKNOWN_OPT
) &&
341 !strncmp(long_name
, arg
, arg_end
- arg
)) {
344 !is_alias(p
, abbrev_option
, options
)) {
346 * If this is abbreviated, it is
347 * ambiguous. So when there is no
348 * exact match later, we need to
351 ambiguous_option
= abbrev_option
;
352 ambiguous_flags
= abbrev_flags
;
354 if (!(flags
& OPT_UNSET
) && *arg_end
)
355 p
->opt
= arg_end
+ 1;
356 abbrev_option
= options
;
357 abbrev_flags
= flags
^ opt_flags
;
360 /* negation allowed? */
361 if (options
->flags
& PARSE_OPT_NONEG
)
363 /* negated and abbreviated very much? */
364 if (starts_with("no-", arg
)) {
369 if (!starts_with(arg
, "no-")) {
370 if (skip_prefix(long_name
, "no-", &long_name
)) {
371 opt_flags
|= OPT_UNSET
;
377 if (!skip_prefix(arg
+ 3, long_name
, &rest
)) {
378 /* abbreviated and negated? */
379 if (starts_with(long_name
, arg
+ 3))
390 return get_value(p
, options
, all_opts
, flags
^ opt_flags
);
393 if (disallow_abbreviated_options
&& (ambiguous_option
|| abbrev_option
))
394 die("disallowed abbreviated or ambiguous option '%.*s'",
395 (int)(arg_end
- arg
), arg
);
397 if (ambiguous_option
) {
398 error(_("ambiguous option: %s "
399 "(could be --%s%s or --%s%s)"),
401 (ambiguous_flags
& OPT_UNSET
) ? "no-" : "",
402 ambiguous_option
->long_name
,
403 (abbrev_flags
& OPT_UNSET
) ? "no-" : "",
404 abbrev_option
->long_name
);
405 return PARSE_OPT_HELP
;
408 return get_value(p
, abbrev_option
, all_opts
, abbrev_flags
);
409 return PARSE_OPT_UNKNOWN
;
412 static enum parse_opt_result
parse_nodash_opt(struct parse_opt_ctx_t
*p
,
414 const struct option
*options
)
416 const struct option
*all_opts
= options
;
418 for (; options
->type
!= OPTION_END
; options
++) {
419 if (!(options
->flags
& PARSE_OPT_NODASH
))
421 if (options
->short_name
== arg
[0] && arg
[1] == '\0')
422 return get_value(p
, options
, all_opts
, OPT_SHORT
);
424 return PARSE_OPT_ERROR
;
427 static enum parse_opt_result
parse_subcommand(const char *arg
,
428 const struct option
*options
)
430 for (; options
->type
!= OPTION_END
; options
++)
431 if (options
->type
== OPTION_SUBCOMMAND
&&
432 !strcmp(options
->long_name
, arg
)) {
433 *(parse_opt_subcommand_fn
**)options
->value
= options
->subcommand_fn
;
434 return PARSE_OPT_SUBCOMMAND
;
437 return PARSE_OPT_UNKNOWN
;
440 static void check_typos(const char *arg
, const struct option
*options
)
445 if (starts_with(arg
, "no-")) {
446 error(_("did you mean `--%s` (with two dashes)?"), arg
);
450 for (; options
->type
!= OPTION_END
; options
++) {
451 if (!options
->long_name
)
453 if (starts_with(options
->long_name
, arg
)) {
454 error(_("did you mean `--%s` (with two dashes)?"), arg
);
460 static void parse_options_check(const struct option
*opts
)
462 char short_opts
[128];
463 void *subcommand_value
= NULL
;
465 memset(short_opts
, '\0', sizeof(short_opts
));
466 for (; opts
->type
!= OPTION_END
; opts
++) {
467 if ((opts
->flags
& PARSE_OPT_LASTARG_DEFAULT
) &&
468 (opts
->flags
& PARSE_OPT_OPTARG
))
469 optbug(opts
, "uses incompatible flags "
470 "LASTARG_DEFAULT and OPTARG");
471 if (opts
->short_name
) {
472 if (0x7F <= opts
->short_name
)
473 optbug(opts
, "invalid short name");
474 else if (short_opts
[opts
->short_name
]++)
475 optbug(opts
, "short name already used");
477 if (opts
->flags
& PARSE_OPT_NODASH
&&
478 ((opts
->flags
& PARSE_OPT_OPTARG
) ||
479 !(opts
->flags
& PARSE_OPT_NOARG
) ||
480 !(opts
->flags
& PARSE_OPT_NONEG
) ||
482 optbug(opts
, "uses feature "
483 "not supported for dashless options");
484 if (opts
->type
== OPTION_SET_INT
&& !opts
->defval
&&
485 opts
->long_name
&& !(opts
->flags
& PARSE_OPT_NONEG
))
486 optbug(opts
, "OPTION_SET_INT 0 should not be negatable");
487 switch (opts
->type
) {
493 if ((opts
->flags
& PARSE_OPT_OPTARG
) ||
494 !(opts
->flags
& PARSE_OPT_NOARG
))
495 optbug(opts
, "should not accept an argument");
497 case OPTION_CALLBACK
:
498 if (!opts
->callback
&& !opts
->ll_callback
)
499 optbug(opts
, "OPTION_CALLBACK needs one callback");
500 else if (opts
->callback
&& opts
->ll_callback
)
501 optbug(opts
, "OPTION_CALLBACK can't have two callbacks");
503 case OPTION_LOWLEVEL_CALLBACK
:
504 if (!opts
->ll_callback
)
505 optbug(opts
, "OPTION_LOWLEVEL_CALLBACK needs a callback");
507 optbug(opts
, "OPTION_LOWLEVEL_CALLBACK needs no high level callback");
510 optbug(opts
, "OPT_ALIAS() should not remain at this point. "
511 "Are you using parse_options_step() directly?\n"
512 "That case is not supported yet.");
514 case OPTION_SUBCOMMAND
:
515 if (!opts
->value
|| !opts
->subcommand_fn
)
516 optbug(opts
, "OPTION_SUBCOMMAND needs a value and a subcommand function");
517 if (!subcommand_value
)
518 subcommand_value
= opts
->value
;
519 else if (subcommand_value
!= opts
->value
)
520 optbug(opts
, "all OPTION_SUBCOMMANDs need the same value");
523 ; /* ok. (usually accepts an argument) */
526 strcspn(opts
->argh
, " _") != strlen(opts
->argh
))
527 optbug(opts
, "multi-word argh should use dash to separate words");
529 BUG_if_bug("invalid 'struct option'");
532 static int has_subcommands(const struct option
*options
)
534 for (; options
->type
!= OPTION_END
; options
++)
535 if (options
->type
== OPTION_SUBCOMMAND
)
540 static void parse_options_start_1(struct parse_opt_ctx_t
*ctx
,
541 int argc
, const char **argv
, const char *prefix
,
542 const struct option
*options
,
543 enum parse_opt_flags flags
)
547 if (!(flags
& PARSE_OPT_ONE_SHOT
)) {
551 ctx
->total
= ctx
->argc
;
553 ctx
->prefix
= prefix
;
554 ctx
->cpidx
= ((flags
& PARSE_OPT_KEEP_ARGV0
) != 0);
556 ctx
->has_subcommands
= has_subcommands(options
);
557 if (!ctx
->has_subcommands
&& (flags
& PARSE_OPT_SUBCOMMAND_OPTIONAL
))
558 BUG("Using PARSE_OPT_SUBCOMMAND_OPTIONAL without subcommands");
559 if (ctx
->has_subcommands
) {
560 if (flags
& PARSE_OPT_STOP_AT_NON_OPTION
)
561 BUG("subcommands are incompatible with PARSE_OPT_STOP_AT_NON_OPTION");
562 if (!(flags
& PARSE_OPT_SUBCOMMAND_OPTIONAL
)) {
563 if (flags
& PARSE_OPT_KEEP_UNKNOWN_OPT
)
564 BUG("subcommands are incompatible with PARSE_OPT_KEEP_UNKNOWN_OPT unless in combination with PARSE_OPT_SUBCOMMAND_OPTIONAL");
565 if (flags
& PARSE_OPT_KEEP_DASHDASH
)
566 BUG("subcommands are incompatible with PARSE_OPT_KEEP_DASHDASH unless in combination with PARSE_OPT_SUBCOMMAND_OPTIONAL");
569 if ((flags
& PARSE_OPT_KEEP_UNKNOWN_OPT
) &&
570 (flags
& PARSE_OPT_STOP_AT_NON_OPTION
) &&
571 !(flags
& PARSE_OPT_ONE_SHOT
))
572 BUG("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
573 if ((flags
& PARSE_OPT_ONE_SHOT
) &&
574 (flags
& PARSE_OPT_KEEP_ARGV0
))
575 BUG("Can't keep argv0 if you don't have it");
576 parse_options_check(options
);
579 void parse_options_start(struct parse_opt_ctx_t
*ctx
,
580 int argc
, const char **argv
, const char *prefix
,
581 const struct option
*options
,
582 enum parse_opt_flags flags
)
584 memset(ctx
, 0, sizeof(*ctx
));
585 parse_options_start_1(ctx
, argc
, argv
, prefix
, options
, flags
);
588 static void show_negated_gitcomp(const struct option
*opts
, int show_all
,
591 int printed_dashdash
= 0;
593 for (; opts
->type
!= OPTION_END
; opts
++) {
594 int has_unset_form
= 0;
597 if (!opts
->long_name
)
600 (opts
->flags
& (PARSE_OPT_HIDDEN
| PARSE_OPT_NOCOMPLETE
)))
602 if (opts
->flags
& PARSE_OPT_NONEG
)
605 switch (opts
->type
) {
607 case OPTION_FILENAME
:
609 case OPTION_MAGNITUDE
:
610 case OPTION_CALLBACK
:
623 if (skip_prefix(opts
->long_name
, "no-", &name
)) {
625 printf(" --%s", name
);
626 } else if (nr_noopts
>= 0) {
627 if (nr_noopts
&& !printed_dashdash
) {
629 printed_dashdash
= 1;
631 printf(" --no-%s", opts
->long_name
);
637 static int show_gitcomp(const struct option
*opts
, int show_all
)
639 const struct option
*original_opts
= opts
;
642 for (; opts
->type
!= OPTION_END
; opts
++) {
643 const char *prefix
= "--";
644 const char *suffix
= "";
646 if (!opts
->long_name
)
649 (opts
->flags
& (PARSE_OPT_HIDDEN
| PARSE_OPT_NOCOMPLETE
| PARSE_OPT_FROM_ALIAS
)))
652 switch (opts
->type
) {
653 case OPTION_SUBCOMMAND
:
659 case OPTION_FILENAME
:
661 case OPTION_MAGNITUDE
:
662 case OPTION_CALLBACK
:
663 if (opts
->flags
& PARSE_OPT_NOARG
)
665 if (opts
->flags
& PARSE_OPT_OPTARG
)
667 if (opts
->flags
& PARSE_OPT_LASTARG_DEFAULT
)
674 if (opts
->flags
& PARSE_OPT_COMP_ARG
)
676 if (starts_with(opts
->long_name
, "no-"))
678 printf("%s%s%s%s", opts
== original_opts
? "" : " ",
679 prefix
, opts
->long_name
, suffix
);
681 show_negated_gitcomp(original_opts
, show_all
, -1);
682 show_negated_gitcomp(original_opts
, show_all
, nr_noopts
);
684 return PARSE_OPT_COMPLETE
;
688 * Scan and may produce a new option[] array, which should be used
689 * instead of the original 'options'.
691 * Right now this is only used to preprocess and substitute
694 * The returned options should be freed using free_preprocessed_options.
696 static struct option
*preprocess_options(struct parse_opt_ctx_t
*ctx
,
697 const struct option
*options
)
699 struct option
*newopt
;
703 for (nr
= 0; options
[nr
].type
!= OPTION_END
; nr
++) {
704 if (options
[nr
].type
== OPTION_ALIAS
)
711 DUP_ARRAY(newopt
, options
, nr
+ 1);
713 /* each alias has two string pointers and NULL */
714 CALLOC_ARRAY(ctx
->alias_groups
, 3 * (nr_aliases
+ 1));
716 for (alias
= 0, i
= 0; i
< nr
; i
++) {
718 const char *long_name
;
720 struct strbuf help
= STRBUF_INIT
;
723 if (newopt
[i
].type
!= OPTION_ALIAS
)
726 short_name
= newopt
[i
].short_name
;
727 long_name
= newopt
[i
].long_name
;
728 source
= newopt
[i
].value
;
731 BUG("An alias must have long option name");
732 strbuf_addf(&help
, _("alias of --%s"), source
);
734 for (j
= 0; j
< nr
; j
++) {
735 const char *name
= options
[j
].long_name
;
737 if (!name
|| strcmp(name
, source
))
740 if (options
[j
].type
== OPTION_ALIAS
)
741 BUG("No please. Nested aliases are not supported.");
743 memcpy(newopt
+ i
, options
+ j
, sizeof(*newopt
));
744 newopt
[i
].short_name
= short_name
;
745 newopt
[i
].long_name
= long_name
;
746 newopt
[i
].help
= strbuf_detach(&help
, NULL
);
747 newopt
[i
].flags
|= PARSE_OPT_FROM_ALIAS
;
752 BUG("could not find source option '%s' of alias '%s'",
753 source
, newopt
[i
].long_name
);
754 ctx
->alias_groups
[alias
* 3 + 0] = newopt
[i
].long_name
;
755 ctx
->alias_groups
[alias
* 3 + 1] = options
[j
].long_name
;
756 ctx
->alias_groups
[alias
* 3 + 2] = NULL
;
763 static void free_preprocessed_options(struct option
*options
)
770 for (i
= 0; options
[i
].type
!= OPTION_END
; i
++) {
771 if (options
[i
].flags
& PARSE_OPT_FROM_ALIAS
)
772 free((void *)options
[i
].help
);
777 static enum parse_opt_result
usage_with_options_internal(struct parse_opt_ctx_t
*,
778 const char * const *,
779 const struct option
*,
782 enum parse_opt_result
parse_options_step(struct parse_opt_ctx_t
*ctx
,
783 const struct option
*options
,
784 const char * const usagestr
[])
786 int internal_help
= !(ctx
->flags
& PARSE_OPT_NO_INTERNAL_HELP
);
788 /* we must reset ->opt, unknown short option leave it dangling */
791 for (; ctx
->argc
; ctx
->argc
--, ctx
->argv
++) {
792 const char *arg
= ctx
->argv
[0];
794 if (ctx
->flags
& PARSE_OPT_ONE_SHOT
&&
795 ctx
->argc
!= ctx
->total
)
798 if (*arg
!= '-' || !arg
[1]) {
799 if (parse_nodash_opt(ctx
, arg
, options
) == 0)
801 if (!ctx
->has_subcommands
) {
802 if (ctx
->flags
& PARSE_OPT_STOP_AT_NON_OPTION
)
803 return PARSE_OPT_NON_OPTION
;
804 ctx
->out
[ctx
->cpidx
++] = ctx
->argv
[0];
807 switch (parse_subcommand(arg
, options
)) {
808 case PARSE_OPT_SUBCOMMAND
:
809 return PARSE_OPT_SUBCOMMAND
;
810 case PARSE_OPT_UNKNOWN
:
811 if (ctx
->flags
& PARSE_OPT_SUBCOMMAND_OPTIONAL
)
813 * arg is neither a short or long
814 * option nor a subcommand. Since
815 * this command has a default
816 * operation mode, we have to treat
817 * this arg and all remaining args
818 * as args meant to that default
820 * So we are done parsing.
822 return PARSE_OPT_DONE
;
823 error(_("unknown subcommand: `%s'"), arg
);
824 usage_with_options(usagestr
, options
);
825 case PARSE_OPT_COMPLETE
:
827 case PARSE_OPT_ERROR
:
829 case PARSE_OPT_NON_OPTION
:
831 BUG("parse_subcommand() cannot return these");
835 /* lone -h asks for help */
836 if (internal_help
&& ctx
->total
== 1 && !strcmp(arg
+ 1, "h"))
840 * lone --git-completion-helper and --git-completion-helper-all
841 * are asked by git-completion.bash
843 if (ctx
->total
== 1 && !strcmp(arg
, "--git-completion-helper"))
844 return show_gitcomp(options
, 0);
845 if (ctx
->total
== 1 && !strcmp(arg
, "--git-completion-helper-all"))
846 return show_gitcomp(options
, 1);
850 switch (parse_short_opt(ctx
, options
)) {
851 case PARSE_OPT_ERROR
:
852 return PARSE_OPT_ERROR
;
853 case PARSE_OPT_UNKNOWN
:
855 check_typos(arg
+ 1, options
);
856 if (internal_help
&& *ctx
->opt
== 'h')
859 case PARSE_OPT_NON_OPTION
:
860 case PARSE_OPT_SUBCOMMAND
:
862 case PARSE_OPT_COMPLETE
:
863 BUG("parse_short_opt() cannot return these");
868 check_typos(arg
+ 1, options
);
870 switch (parse_short_opt(ctx
, options
)) {
871 case PARSE_OPT_ERROR
:
872 return PARSE_OPT_ERROR
;
873 case PARSE_OPT_UNKNOWN
:
874 if (internal_help
&& *ctx
->opt
== 'h')
877 /* fake a short option thing to hide the fact that we may have
878 * started to parse aggregated stuff
880 * This is leaky, too bad.
882 ctx
->argv
[0] = xstrdup(ctx
->opt
- 1);
883 *(char *)ctx
->argv
[0] = '-';
885 case PARSE_OPT_NON_OPTION
:
886 case PARSE_OPT_SUBCOMMAND
:
887 case PARSE_OPT_COMPLETE
:
889 BUG("parse_short_opt() cannot return these");
897 if (!arg
[2] /* "--" */ ||
898 !strcmp(arg
+ 2, "end-of-options")) {
899 if (!(ctx
->flags
& PARSE_OPT_KEEP_DASHDASH
)) {
906 if (internal_help
&& !strcmp(arg
+ 2, "help-all"))
907 return usage_with_options_internal(ctx
, usagestr
, options
, 1, 0);
908 if (internal_help
&& !strcmp(arg
+ 2, "help"))
910 switch (parse_long_opt(ctx
, arg
+ 2, options
)) {
911 case PARSE_OPT_ERROR
:
912 return PARSE_OPT_ERROR
;
913 case PARSE_OPT_UNKNOWN
:
917 case PARSE_OPT_NON_OPTION
:
918 case PARSE_OPT_SUBCOMMAND
:
919 case PARSE_OPT_COMPLETE
:
920 BUG("parse_long_opt() cannot return these");
926 if (ctx
->flags
& PARSE_OPT_ONE_SHOT
)
928 if (ctx
->has_subcommands
&&
929 (ctx
->flags
& PARSE_OPT_SUBCOMMAND_OPTIONAL
) &&
930 (ctx
->flags
& PARSE_OPT_KEEP_UNKNOWN_OPT
)) {
932 * Found an unknown option given to a command with
933 * subcommands that has a default operation mode:
934 * we treat this option and all remaining args as
935 * arguments meant to that default operation mode.
936 * So we are done parsing.
938 return PARSE_OPT_DONE
;
940 if (!(ctx
->flags
& PARSE_OPT_KEEP_UNKNOWN_OPT
))
941 return PARSE_OPT_UNKNOWN
;
942 ctx
->out
[ctx
->cpidx
++] = ctx
->argv
[0];
945 return PARSE_OPT_DONE
;
948 return usage_with_options_internal(ctx
, usagestr
, options
, 0, 0);
951 int parse_options_end(struct parse_opt_ctx_t
*ctx
)
953 if (ctx
->flags
& PARSE_OPT_ONE_SHOT
)
954 return ctx
->total
- ctx
->argc
;
956 MOVE_ARRAY(ctx
->out
+ ctx
->cpidx
, ctx
->argv
, ctx
->argc
);
957 ctx
->out
[ctx
->cpidx
+ ctx
->argc
] = NULL
;
958 return ctx
->cpidx
+ ctx
->argc
;
961 int parse_options(int argc
, const char **argv
,
963 const struct option
*options
,
964 const char * const usagestr
[],
965 enum parse_opt_flags flags
)
967 struct parse_opt_ctx_t ctx
;
968 struct option
*real_options
;
970 disallow_abbreviated_options
=
971 git_env_bool("GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS", 0);
973 memset(&ctx
, 0, sizeof(ctx
));
974 real_options
= preprocess_options(&ctx
, options
);
976 options
= real_options
;
977 parse_options_start_1(&ctx
, argc
, argv
, prefix
, options
, flags
);
978 switch (parse_options_step(&ctx
, options
, usagestr
)) {
980 case PARSE_OPT_ERROR
:
982 case PARSE_OPT_COMPLETE
:
984 case PARSE_OPT_NON_OPTION
:
985 case PARSE_OPT_SUBCOMMAND
:
988 if (ctx
.has_subcommands
&&
989 !(flags
& PARSE_OPT_SUBCOMMAND_OPTIONAL
)) {
990 error(_("need a subcommand"));
991 usage_with_options(usagestr
, options
);
994 case PARSE_OPT_UNKNOWN
:
995 if (ctx
.argv
[0][1] == '-') {
996 error(_("unknown option `%s'"), ctx
.argv
[0] + 2);
997 } else if (isascii(*ctx
.opt
)) {
998 error(_("unknown switch `%c'"), *ctx
.opt
);
1000 error(_("unknown non-ascii option in string: `%s'"),
1003 usage_with_options(usagestr
, options
);
1006 precompose_argv_prefix(argc
, argv
, NULL
);
1007 free_preprocessed_options(real_options
);
1008 free(ctx
.alias_groups
);
1009 return parse_options_end(&ctx
);
1012 static int usage_argh(const struct option
*opts
, FILE *outfile
)
1015 int literal
= (opts
->flags
& PARSE_OPT_LITERAL_ARGHELP
) ||
1016 !opts
->argh
|| !!strpbrk(opts
->argh
, "()<>[]|");
1017 if (opts
->flags
& PARSE_OPT_OPTARG
)
1018 if (opts
->long_name
)
1019 s
= literal
? "[=%s]" : "[=<%s>]";
1021 s
= literal
? "[%s]" : "[<%s>]";
1023 s
= literal
? " %s" : " <%s>";
1024 return utf8_fprintf(outfile
, s
, opts
->argh
? _(opts
->argh
) : _("..."));
1027 static int usage_indent(FILE *outfile
)
1029 return fprintf(outfile
, " ");
1032 #define USAGE_OPTS_WIDTH 26
1034 static void usage_padding(FILE *outfile
, size_t pos
)
1036 if (pos
< USAGE_OPTS_WIDTH
)
1037 fprintf(outfile
, "%*s", USAGE_OPTS_WIDTH
- (int)pos
, "");
1039 fprintf(outfile
, "\n%*s", USAGE_OPTS_WIDTH
, "");
1042 static const struct option
*find_option_by_long_name(const struct option
*opts
,
1043 const char *long_name
)
1045 for (; opts
->type
!= OPTION_END
; opts
++) {
1046 if (opts
->long_name
&& !strcmp(opts
->long_name
, long_name
))
1052 static enum parse_opt_result
usage_with_options_internal(struct parse_opt_ctx_t
*ctx
,
1053 const char * const *usagestr
,
1054 const struct option
*opts
,
1057 const struct option
*all_opts
= opts
;
1058 FILE *outfile
= err
? stderr
: stdout
;
1061 const char *usage_prefix
= _("usage: %s");
1063 * The translation could be anything, but we can count on
1064 * msgfmt(1)'s --check option to have asserted that "%s" is in
1065 * the translation. So compute the length of the "usage: "
1066 * part. We are assuming that the translator wasn't overly
1067 * clever and used e.g. "%1$s" instead of "%s", there's only
1068 * one "%s" in "usage_prefix" above, so there's no reason to
1069 * do so even with a RTL language.
1071 size_t usage_len
= strlen(usage_prefix
) - strlen("%s");
1073 * TRANSLATORS: the colon here should align with the
1074 * one in "usage: %s" translation.
1076 const char *or_prefix
= _(" or: %s");
1078 * TRANSLATORS: You should only need to translate this format
1079 * string if your language is a RTL language (e.g. Arabic,
1080 * Hebrew etc.), not if it's a LTR language (e.g. German,
1081 * Russian, Chinese etc.).
1083 * When a translated usage string has an embedded "\n" it's
1084 * because options have wrapped to the next line. The line
1085 * after the "\n" will then be padded to align with the
1086 * command name, such as N_("git cmd [opt]\n<8
1087 * spaces>[opt2]"), where the 8 spaces are the same length as
1090 * This format string prints out that already-translated
1091 * line. The "%*s" is whitespace padding to account for the
1092 * padding at the start of the line that we add in this
1093 * function. The "%s" is a line in the (hopefully already
1094 * translated) N_() usage string, which contained embedded
1095 * newlines before we split it up.
1097 const char *usage_continued
= _("%*s%s");
1098 const char *prefix
= usage_prefix
;
1099 int saw_empty_line
= 0;
1102 return PARSE_OPT_HELP
;
1104 if (!err
&& ctx
&& ctx
->flags
& PARSE_OPT_SHELL_EVAL
)
1105 fprintf(outfile
, "cat <<\\EOF\n");
1108 const char *str
= _(*usagestr
++);
1109 struct string_list list
= STRING_LIST_INIT_DUP
;
1112 if (!saw_empty_line
&& !*str
)
1115 string_list_split(&list
, str
, '\n', -1);
1116 for (j
= 0; j
< list
.nr
; j
++) {
1117 const char *line
= list
.items
[j
].string
;
1119 if (saw_empty_line
&& *line
)
1120 fprintf_ln(outfile
, _(" %s"), line
);
1121 else if (saw_empty_line
)
1122 fputc('\n', outfile
);
1124 fprintf_ln(outfile
, prefix
, line
);
1126 fprintf_ln(outfile
, usage_continued
,
1127 (int)usage_len
, "", line
);
1129 string_list_clear(&list
, 0);
1136 for (; opts
->type
!= OPTION_END
; opts
++) {
1138 const char *cp
, *np
;
1139 const char *positive_name
= NULL
;
1141 if (opts
->type
== OPTION_SUBCOMMAND
)
1143 if (opts
->type
== OPTION_GROUP
) {
1144 fputc('\n', outfile
);
1147 fprintf(outfile
, "%s\n", _(opts
->help
));
1150 if (!full
&& (opts
->flags
& PARSE_OPT_HIDDEN
))
1154 fputc('\n', outfile
);
1158 pos
= usage_indent(outfile
);
1159 if (opts
->short_name
) {
1160 if (opts
->flags
& PARSE_OPT_NODASH
)
1161 pos
+= fprintf(outfile
, "%c", opts
->short_name
);
1163 pos
+= fprintf(outfile
, "-%c", opts
->short_name
);
1165 if (opts
->long_name
&& opts
->short_name
)
1166 pos
+= fprintf(outfile
, ", ");
1167 if (opts
->long_name
) {
1168 const char *long_name
= opts
->long_name
;
1169 if ((opts
->flags
& PARSE_OPT_NONEG
) ||
1170 skip_prefix(long_name
, "no-", &positive_name
))
1171 pos
+= fprintf(outfile
, "--%s", long_name
);
1173 pos
+= fprintf(outfile
, "--[no-]%s", long_name
);
1176 if (opts
->type
== OPTION_NUMBER
)
1177 pos
+= utf8_fprintf(outfile
, _("-NUM"));
1179 if ((opts
->flags
& PARSE_OPT_LITERAL_ARGHELP
) ||
1180 !(opts
->flags
& PARSE_OPT_NOARG
))
1181 pos
+= usage_argh(opts
, outfile
);
1183 if (opts
->type
== OPTION_ALIAS
) {
1184 usage_padding(outfile
, pos
);
1185 fprintf_ln(outfile
, _("alias of --%s"),
1186 (const char *)opts
->value
);
1190 for (cp
= opts
->help
? _(opts
->help
) : ""; *cp
; cp
= np
) {
1191 np
= strchrnul(cp
, '\n');
1194 usage_padding(outfile
, pos
);
1195 fwrite(cp
, 1, np
- cp
, outfile
);
1198 fputc('\n', outfile
);
1200 if (positive_name
) {
1201 if (find_option_by_long_name(all_opts
, positive_name
))
1203 pos
= usage_indent(outfile
);
1204 pos
+= fprintf(outfile
, "--%s", positive_name
);
1205 usage_padding(outfile
, pos
);
1206 fprintf_ln(outfile
, _("opposite of --no-%s"),
1210 fputc('\n', outfile
);
1212 if (!err
&& ctx
&& ctx
->flags
& PARSE_OPT_SHELL_EVAL
)
1213 fputs("EOF\n", outfile
);
1215 return PARSE_OPT_HELP
;
1218 void NORETURN
usage_with_options(const char * const *usagestr
,
1219 const struct option
*opts
)
1221 usage_with_options_internal(NULL
, usagestr
, opts
, 0, 1);
1225 void NORETURN
usage_msg_opt(const char *msg
,
1226 const char * const *usagestr
,
1227 const struct option
*options
)
1229 die_message("%s\n", msg
); /* The extra \n is intentional */
1230 usage_with_options(usagestr
, options
);
1233 void NORETURN
usage_msg_optf(const char * const fmt
,
1234 const char * const *usagestr
,
1235 const struct option
*options
, ...)
1237 struct strbuf msg
= STRBUF_INIT
;
1239 va_start(ap
, options
);
1240 strbuf_vaddf(&msg
, fmt
, ap
);
1243 usage_msg_opt(msg
.buf
, usagestr
, options
);
1246 void die_for_incompatible_opt4(int opt1
, const char *opt1_name
,
1247 int opt2
, const char *opt2_name
,
1248 int opt3
, const char *opt3_name
,
1249 int opt4
, const char *opt4_name
)
1252 const char *options
[4];
1255 options
[count
++] = opt1_name
;
1257 options
[count
++] = opt2_name
;
1259 options
[count
++] = opt3_name
;
1261 options
[count
++] = opt4_name
;
1264 die(_("options '%s', '%s', '%s', and '%s' cannot be used together"),
1265 opt1_name
, opt2_name
, opt3_name
, opt4_name
);
1268 die(_("options '%s', '%s', and '%s' cannot be used together"),
1269 options
[0], options
[1], options
[2]);
1272 die(_("options '%s' and '%s' cannot be used together"),
1273 options
[0], options
[1]);