1 #include "git-compat-util.h"
2 #include "parse-options.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 void fix_filename(const char *prefix
, char **file
)
68 *file
= prefix_filename_except_for_dash(prefix
, *file
);
71 static enum parse_opt_result
opt_command_mode_error(
72 const struct option
*opt
,
73 const struct option
*all_opts
,
74 enum opt_parsed flags
)
76 const struct option
*that
;
77 struct strbuf that_name
= STRBUF_INIT
;
80 * Find the other option that was used to set the variable
81 * already, and report that this is not compatible with it.
83 for (that
= all_opts
; that
->type
!= OPTION_END
; that
++) {
85 !(that
->flags
& PARSE_OPT_CMDMODE
) ||
86 that
->value
!= opt
->value
||
87 that
->defval
!= *(int *)opt
->value
)
91 strbuf_addf(&that_name
, "--%s", that
->long_name
);
93 strbuf_addf(&that_name
, "-%c", that
->short_name
);
94 error(_("%s is incompatible with %s"),
95 optname(opt
, flags
), that_name
.buf
);
96 strbuf_release(&that_name
);
97 return PARSE_OPT_ERROR
;
99 return error(_("%s : incompatible with something else"),
100 optname(opt
, flags
));
103 static enum parse_opt_result
get_value(struct parse_opt_ctx_t
*p
,
104 const struct option
*opt
,
105 const struct option
*all_opts
,
106 enum opt_parsed flags
)
109 const int unset
= flags
& OPT_UNSET
;
113 return error(_("%s takes no value"), optname(opt
, flags
));
114 if (unset
&& (opt
->flags
& PARSE_OPT_NONEG
))
115 return error(_("%s isn't available"), optname(opt
, flags
));
116 if (!(flags
& OPT_SHORT
) && p
->opt
&& (opt
->flags
& PARSE_OPT_NOARG
))
117 return error(_("%s takes no value"), optname(opt
, flags
));
120 * Giving the same mode option twice, although unnecessary,
121 * is not a grave error, so let it pass.
123 if ((opt
->flags
& PARSE_OPT_CMDMODE
) &&
124 *(int *)opt
->value
&& *(int *)opt
->value
!= opt
->defval
)
125 return opt_command_mode_error(opt
, all_opts
, flags
);
128 case OPTION_LOWLEVEL_CALLBACK
:
129 return opt
->ll_callback(p
, opt
, NULL
, unset
);
133 *(int *)opt
->value
&= ~opt
->defval
;
135 *(int *)opt
->value
|= opt
->defval
;
140 *(int *)opt
->value
|= opt
->defval
;
142 *(int *)opt
->value
&= ~opt
->defval
;
147 BUG("BITOP can't have unset form");
148 *(int *)opt
->value
&= ~opt
->extra
;
149 *(int *)opt
->value
|= opt
->defval
;
153 if (*(int *)opt
->value
< 0)
154 *(int *)opt
->value
= 0;
155 *(int *)opt
->value
= unset
? 0 : *(int *)opt
->value
+ 1;
159 *(int *)opt
->value
= unset
? 0 : opt
->defval
;
164 *(const char **)opt
->value
= NULL
;
165 else if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
)
166 *(const char **)opt
->value
= (const char *)opt
->defval
;
168 return get_arg(p
, opt
, flags
, (const char **)opt
->value
);
171 case OPTION_FILENAME
:
174 *(const char **)opt
->value
= NULL
;
175 else if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
)
176 *(const char **)opt
->value
= (const char *)opt
->defval
;
178 err
= get_arg(p
, opt
, flags
, (const char **)opt
->value
);
181 fix_filename(p
->prefix
, (char **)opt
->value
);
184 case OPTION_CALLBACK
:
186 const char *p_arg
= NULL
;
191 else if (opt
->flags
& PARSE_OPT_NOARG
)
193 else if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
)
195 else if (get_arg(p
, opt
, flags
, &arg
))
202 return (*opt
->callback
)(opt
, p_arg
, p_unset
) ? (-1) : 0;
204 return (*opt
->ll_callback
)(p
, opt
, p_arg
, p_unset
);
208 *(int *)opt
->value
= 0;
211 if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
) {
212 *(int *)opt
->value
= opt
->defval
;
215 if (get_arg(p
, opt
, flags
, &arg
))
218 return error(_("%s expects a numerical value"),
219 optname(opt
, flags
));
220 *(int *)opt
->value
= strtol(arg
, (char **)&s
, 10);
222 return error(_("%s expects a numerical value"),
223 optname(opt
, flags
));
226 case OPTION_MAGNITUDE
:
228 *(unsigned long *)opt
->value
= 0;
231 if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
) {
232 *(unsigned long *)opt
->value
= opt
->defval
;
235 if (get_arg(p
, opt
, flags
, &arg
))
237 if (!git_parse_ulong(arg
, opt
->value
))
238 return error(_("%s expects a non-negative integer value"
239 " with an optional k/m/g suffix"),
240 optname(opt
, flags
));
244 BUG("opt->type %d should not happen", opt
->type
);
248 static enum parse_opt_result
parse_short_opt(struct parse_opt_ctx_t
*p
,
249 const struct option
*options
)
251 const struct option
*all_opts
= options
;
252 const struct option
*numopt
= NULL
;
254 for (; options
->type
!= OPTION_END
; options
++) {
255 if (options
->short_name
== *p
->opt
) {
256 p
->opt
= p
->opt
[1] ? p
->opt
+ 1 : NULL
;
257 return get_value(p
, options
, all_opts
, OPT_SHORT
);
261 * Handle the numerical option later, explicit one-digit
262 * options take precedence over it.
264 if (options
->type
== OPTION_NUMBER
)
267 if (numopt
&& isdigit(*p
->opt
)) {
272 while (isdigit(p
->opt
[len
]))
274 arg
= xmemdupz(p
->opt
, len
);
275 p
->opt
= p
->opt
[len
] ? p
->opt
+ len
: NULL
;
276 if (numopt
->callback
)
277 rc
= (*numopt
->callback
)(numopt
, arg
, 0) ? (-1) : 0;
279 rc
= (*numopt
->ll_callback
)(p
, numopt
, arg
, 0);
283 return PARSE_OPT_UNKNOWN
;
286 static int has_string(const char *it
, const char **array
)
289 if (!strcmp(it
, *(array
++)))
294 static int is_alias(struct parse_opt_ctx_t
*ctx
,
295 const struct option
*one_opt
,
296 const struct option
*another_opt
)
300 if (!ctx
->alias_groups
)
303 if (!one_opt
->long_name
|| !another_opt
->long_name
)
306 for (group
= ctx
->alias_groups
; *group
; group
+= 3) {
307 /* it and other are from the same family? */
308 if (has_string(one_opt
->long_name
, group
) &&
309 has_string(another_opt
->long_name
, group
))
315 static enum parse_opt_result
parse_long_opt(
316 struct parse_opt_ctx_t
*p
, const char *arg
,
317 const struct option
*options
)
319 const struct option
*all_opts
= options
;
320 const char *arg_end
= strchrnul(arg
, '=');
321 const struct option
*abbrev_option
= NULL
, *ambiguous_option
= NULL
;
322 enum opt_parsed abbrev_flags
= OPT_LONG
, ambiguous_flags
= OPT_LONG
;
324 for (; options
->type
!= OPTION_END
; options
++) {
325 const char *rest
, *long_name
= options
->long_name
;
326 enum opt_parsed flags
= OPT_LONG
, opt_flags
= OPT_LONG
;
328 if (options
->type
== OPTION_SUBCOMMAND
)
334 if (!skip_prefix(arg
, long_name
, &rest
))
338 if (!(p
->flags
& PARSE_OPT_KEEP_UNKNOWN_OPT
) &&
339 !strncmp(long_name
, arg
, arg_end
- arg
)) {
342 !is_alias(p
, abbrev_option
, options
)) {
344 * If this is abbreviated, it is
345 * ambiguous. So when there is no
346 * exact match later, we need to
349 ambiguous_option
= abbrev_option
;
350 ambiguous_flags
= abbrev_flags
;
352 if (!(flags
& OPT_UNSET
) && *arg_end
)
353 p
->opt
= arg_end
+ 1;
354 abbrev_option
= options
;
355 abbrev_flags
= flags
^ opt_flags
;
358 /* negation allowed? */
359 if (options
->flags
& PARSE_OPT_NONEG
)
361 /* negated and abbreviated very much? */
362 if (starts_with("no-", arg
)) {
367 if (!starts_with(arg
, "no-")) {
368 if (skip_prefix(long_name
, "no-", &long_name
)) {
369 opt_flags
|= OPT_UNSET
;
375 if (!skip_prefix(arg
+ 3, long_name
, &rest
)) {
376 /* abbreviated and negated? */
377 if (starts_with(long_name
, arg
+ 3))
388 return get_value(p
, options
, all_opts
, flags
^ opt_flags
);
391 if (disallow_abbreviated_options
&& (ambiguous_option
|| abbrev_option
))
392 die("disallowed abbreviated or ambiguous option '%.*s'",
393 (int)(arg_end
- arg
), arg
);
395 if (ambiguous_option
) {
396 error(_("ambiguous option: %s "
397 "(could be --%s%s or --%s%s)"),
399 (ambiguous_flags
& OPT_UNSET
) ? "no-" : "",
400 ambiguous_option
->long_name
,
401 (abbrev_flags
& OPT_UNSET
) ? "no-" : "",
402 abbrev_option
->long_name
);
403 return PARSE_OPT_HELP
;
406 return get_value(p
, abbrev_option
, all_opts
, abbrev_flags
);
407 return PARSE_OPT_UNKNOWN
;
410 static enum parse_opt_result
parse_nodash_opt(struct parse_opt_ctx_t
*p
,
412 const struct option
*options
)
414 const struct option
*all_opts
= options
;
416 for (; options
->type
!= OPTION_END
; options
++) {
417 if (!(options
->flags
& PARSE_OPT_NODASH
))
419 if (options
->short_name
== arg
[0] && arg
[1] == '\0')
420 return get_value(p
, options
, all_opts
, OPT_SHORT
);
422 return PARSE_OPT_ERROR
;
425 static enum parse_opt_result
parse_subcommand(const char *arg
,
426 const struct option
*options
)
428 for (; options
->type
!= OPTION_END
; options
++)
429 if (options
->type
== OPTION_SUBCOMMAND
&&
430 !strcmp(options
->long_name
, arg
)) {
431 *(parse_opt_subcommand_fn
**)options
->value
= options
->subcommand_fn
;
432 return PARSE_OPT_SUBCOMMAND
;
435 return PARSE_OPT_UNKNOWN
;
438 static void check_typos(const char *arg
, const struct option
*options
)
443 if (starts_with(arg
, "no-")) {
444 error(_("did you mean `--%s` (with two dashes)?"), arg
);
448 for (; options
->type
!= OPTION_END
; options
++) {
449 if (!options
->long_name
)
451 if (starts_with(options
->long_name
, arg
)) {
452 error(_("did you mean `--%s` (with two dashes)?"), arg
);
458 static void parse_options_check(const struct option
*opts
)
460 char short_opts
[128];
461 void *subcommand_value
= NULL
;
463 memset(short_opts
, '\0', sizeof(short_opts
));
464 for (; opts
->type
!= OPTION_END
; opts
++) {
465 if ((opts
->flags
& PARSE_OPT_LASTARG_DEFAULT
) &&
466 (opts
->flags
& PARSE_OPT_OPTARG
))
467 optbug(opts
, "uses incompatible flags "
468 "LASTARG_DEFAULT and OPTARG");
469 if (opts
->short_name
) {
470 if (0x7F <= opts
->short_name
)
471 optbug(opts
, "invalid short name");
472 else if (short_opts
[opts
->short_name
]++)
473 optbug(opts
, "short name already used");
475 if (opts
->flags
& PARSE_OPT_NODASH
&&
476 ((opts
->flags
& PARSE_OPT_OPTARG
) ||
477 !(opts
->flags
& PARSE_OPT_NOARG
) ||
478 !(opts
->flags
& PARSE_OPT_NONEG
) ||
480 optbug(opts
, "uses feature "
481 "not supported for dashless options");
482 switch (opts
->type
) {
488 if ((opts
->flags
& PARSE_OPT_OPTARG
) ||
489 !(opts
->flags
& PARSE_OPT_NOARG
))
490 optbug(opts
, "should not accept an argument");
492 case OPTION_CALLBACK
:
493 if (!opts
->callback
&& !opts
->ll_callback
)
494 optbug(opts
, "OPTION_CALLBACK needs one callback");
495 else if (opts
->callback
&& opts
->ll_callback
)
496 optbug(opts
, "OPTION_CALLBACK can't have two callbacks");
498 case OPTION_LOWLEVEL_CALLBACK
:
499 if (!opts
->ll_callback
)
500 optbug(opts
, "OPTION_LOWLEVEL_CALLBACK needs a callback");
502 optbug(opts
, "OPTION_LOWLEVEL_CALLBACK needs no high level callback");
505 optbug(opts
, "OPT_ALIAS() should not remain at this point. "
506 "Are you using parse_options_step() directly?\n"
507 "That case is not supported yet.");
509 case OPTION_SUBCOMMAND
:
510 if (!opts
->value
|| !opts
->subcommand_fn
)
511 optbug(opts
, "OPTION_SUBCOMMAND needs a value and a subcommand function");
512 if (!subcommand_value
)
513 subcommand_value
= opts
->value
;
514 else if (subcommand_value
!= opts
->value
)
515 optbug(opts
, "all OPTION_SUBCOMMANDs need the same value");
518 ; /* ok. (usually accepts an argument) */
521 strcspn(opts
->argh
, " _") != strlen(opts
->argh
))
522 optbug(opts
, "multi-word argh should use dash to separate words");
524 BUG_if_bug("invalid 'struct option'");
527 static int has_subcommands(const struct option
*options
)
529 for (; options
->type
!= OPTION_END
; options
++)
530 if (options
->type
== OPTION_SUBCOMMAND
)
535 static void parse_options_start_1(struct parse_opt_ctx_t
*ctx
,
536 int argc
, const char **argv
, const char *prefix
,
537 const struct option
*options
,
538 enum parse_opt_flags flags
)
542 if (!(flags
& PARSE_OPT_ONE_SHOT
)) {
546 ctx
->total
= ctx
->argc
;
548 ctx
->prefix
= prefix
;
549 ctx
->cpidx
= ((flags
& PARSE_OPT_KEEP_ARGV0
) != 0);
551 ctx
->has_subcommands
= has_subcommands(options
);
552 if (!ctx
->has_subcommands
&& (flags
& PARSE_OPT_SUBCOMMAND_OPTIONAL
))
553 BUG("Using PARSE_OPT_SUBCOMMAND_OPTIONAL without subcommands");
554 if (ctx
->has_subcommands
) {
555 if (flags
& PARSE_OPT_STOP_AT_NON_OPTION
)
556 BUG("subcommands are incompatible with PARSE_OPT_STOP_AT_NON_OPTION");
557 if (!(flags
& PARSE_OPT_SUBCOMMAND_OPTIONAL
)) {
558 if (flags
& PARSE_OPT_KEEP_UNKNOWN_OPT
)
559 BUG("subcommands are incompatible with PARSE_OPT_KEEP_UNKNOWN_OPT unless in combination with PARSE_OPT_SUBCOMMAND_OPTIONAL");
560 if (flags
& PARSE_OPT_KEEP_DASHDASH
)
561 BUG("subcommands are incompatible with PARSE_OPT_KEEP_DASHDASH unless in combination with PARSE_OPT_SUBCOMMAND_OPTIONAL");
564 if ((flags
& PARSE_OPT_KEEP_UNKNOWN_OPT
) &&
565 (flags
& PARSE_OPT_STOP_AT_NON_OPTION
) &&
566 !(flags
& PARSE_OPT_ONE_SHOT
))
567 BUG("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
568 if ((flags
& PARSE_OPT_ONE_SHOT
) &&
569 (flags
& PARSE_OPT_KEEP_ARGV0
))
570 BUG("Can't keep argv0 if you don't have it");
571 parse_options_check(options
);
574 void parse_options_start(struct parse_opt_ctx_t
*ctx
,
575 int argc
, const char **argv
, const char *prefix
,
576 const struct option
*options
,
577 enum parse_opt_flags flags
)
579 memset(ctx
, 0, sizeof(*ctx
));
580 parse_options_start_1(ctx
, argc
, argv
, prefix
, options
, flags
);
583 static void show_negated_gitcomp(const struct option
*opts
, int show_all
,
586 int printed_dashdash
= 0;
588 for (; opts
->type
!= OPTION_END
; opts
++) {
589 int has_unset_form
= 0;
592 if (!opts
->long_name
)
595 (opts
->flags
& (PARSE_OPT_HIDDEN
| PARSE_OPT_NOCOMPLETE
)))
597 if (opts
->flags
& PARSE_OPT_NONEG
)
600 switch (opts
->type
) {
602 case OPTION_FILENAME
:
604 case OPTION_MAGNITUDE
:
605 case OPTION_CALLBACK
:
618 if (skip_prefix(opts
->long_name
, "no-", &name
)) {
620 printf(" --%s", name
);
621 } else if (nr_noopts
>= 0) {
622 if (nr_noopts
&& !printed_dashdash
) {
624 printed_dashdash
= 1;
626 printf(" --no-%s", opts
->long_name
);
632 static int show_gitcomp(const struct option
*opts
, int show_all
)
634 const struct option
*original_opts
= opts
;
637 for (; opts
->type
!= OPTION_END
; opts
++) {
638 const char *prefix
= "--";
639 const char *suffix
= "";
641 if (!opts
->long_name
)
644 (opts
->flags
& (PARSE_OPT_HIDDEN
| PARSE_OPT_NOCOMPLETE
| PARSE_OPT_FROM_ALIAS
)))
647 switch (opts
->type
) {
648 case OPTION_SUBCOMMAND
:
654 case OPTION_FILENAME
:
656 case OPTION_MAGNITUDE
:
657 case OPTION_CALLBACK
:
658 if (opts
->flags
& PARSE_OPT_NOARG
)
660 if (opts
->flags
& PARSE_OPT_OPTARG
)
662 if (opts
->flags
& PARSE_OPT_LASTARG_DEFAULT
)
669 if (opts
->flags
& PARSE_OPT_COMP_ARG
)
671 if (starts_with(opts
->long_name
, "no-"))
673 printf("%s%s%s%s", opts
== original_opts
? "" : " ",
674 prefix
, opts
->long_name
, suffix
);
676 show_negated_gitcomp(original_opts
, show_all
, -1);
677 show_negated_gitcomp(original_opts
, show_all
, nr_noopts
);
679 return PARSE_OPT_COMPLETE
;
683 * Scan and may produce a new option[] array, which should be used
684 * instead of the original 'options'.
686 * Right now this is only used to preprocess and substitute
689 * The returned options should be freed using free_preprocessed_options.
691 static struct option
*preprocess_options(struct parse_opt_ctx_t
*ctx
,
692 const struct option
*options
)
694 struct option
*newopt
;
698 for (nr
= 0; options
[nr
].type
!= OPTION_END
; nr
++) {
699 if (options
[nr
].type
== OPTION_ALIAS
)
706 DUP_ARRAY(newopt
, options
, nr
+ 1);
708 /* each alias has two string pointers and NULL */
709 CALLOC_ARRAY(ctx
->alias_groups
, 3 * (nr_aliases
+ 1));
711 for (alias
= 0, i
= 0; i
< nr
; i
++) {
713 const char *long_name
;
715 struct strbuf help
= STRBUF_INIT
;
718 if (newopt
[i
].type
!= OPTION_ALIAS
)
721 short_name
= newopt
[i
].short_name
;
722 long_name
= newopt
[i
].long_name
;
723 source
= newopt
[i
].value
;
726 BUG("An alias must have long option name");
727 strbuf_addf(&help
, _("alias of --%s"), source
);
729 for (j
= 0; j
< nr
; j
++) {
730 const char *name
= options
[j
].long_name
;
732 if (!name
|| strcmp(name
, source
))
735 if (options
[j
].type
== OPTION_ALIAS
)
736 BUG("No please. Nested aliases are not supported.");
738 memcpy(newopt
+ i
, options
+ j
, sizeof(*newopt
));
739 newopt
[i
].short_name
= short_name
;
740 newopt
[i
].long_name
= long_name
;
741 newopt
[i
].help
= strbuf_detach(&help
, NULL
);
742 newopt
[i
].flags
|= PARSE_OPT_FROM_ALIAS
;
747 BUG("could not find source option '%s' of alias '%s'",
748 source
, newopt
[i
].long_name
);
749 ctx
->alias_groups
[alias
* 3 + 0] = newopt
[i
].long_name
;
750 ctx
->alias_groups
[alias
* 3 + 1] = options
[j
].long_name
;
751 ctx
->alias_groups
[alias
* 3 + 2] = NULL
;
758 static void free_preprocessed_options(struct option
*options
)
765 for (i
= 0; options
[i
].type
!= OPTION_END
; i
++) {
766 if (options
[i
].flags
& PARSE_OPT_FROM_ALIAS
)
767 free((void *)options
[i
].help
);
772 static enum parse_opt_result
usage_with_options_internal(struct parse_opt_ctx_t
*,
773 const char * const *,
774 const struct option
*,
777 enum parse_opt_result
parse_options_step(struct parse_opt_ctx_t
*ctx
,
778 const struct option
*options
,
779 const char * const usagestr
[])
781 int internal_help
= !(ctx
->flags
& PARSE_OPT_NO_INTERNAL_HELP
);
783 /* we must reset ->opt, unknown short option leave it dangling */
786 for (; ctx
->argc
; ctx
->argc
--, ctx
->argv
++) {
787 const char *arg
= ctx
->argv
[0];
789 if (ctx
->flags
& PARSE_OPT_ONE_SHOT
&&
790 ctx
->argc
!= ctx
->total
)
793 if (*arg
!= '-' || !arg
[1]) {
794 if (parse_nodash_opt(ctx
, arg
, options
) == 0)
796 if (!ctx
->has_subcommands
) {
797 if (ctx
->flags
& PARSE_OPT_STOP_AT_NON_OPTION
)
798 return PARSE_OPT_NON_OPTION
;
799 ctx
->out
[ctx
->cpidx
++] = ctx
->argv
[0];
802 switch (parse_subcommand(arg
, options
)) {
803 case PARSE_OPT_SUBCOMMAND
:
804 return PARSE_OPT_SUBCOMMAND
;
805 case PARSE_OPT_UNKNOWN
:
806 if (ctx
->flags
& PARSE_OPT_SUBCOMMAND_OPTIONAL
)
808 * arg is neither a short or long
809 * option nor a subcommand. Since
810 * this command has a default
811 * operation mode, we have to treat
812 * this arg and all remaining args
813 * as args meant to that default
815 * So we are done parsing.
817 return PARSE_OPT_DONE
;
818 error(_("unknown subcommand: `%s'"), arg
);
819 usage_with_options(usagestr
, options
);
820 case PARSE_OPT_COMPLETE
:
822 case PARSE_OPT_ERROR
:
824 case PARSE_OPT_NON_OPTION
:
826 BUG("parse_subcommand() cannot return these");
830 /* lone -h asks for help */
831 if (internal_help
&& ctx
->total
== 1 && !strcmp(arg
+ 1, "h"))
835 * lone --git-completion-helper and --git-completion-helper-all
836 * are asked by git-completion.bash
838 if (ctx
->total
== 1 && !strcmp(arg
, "--git-completion-helper"))
839 return show_gitcomp(options
, 0);
840 if (ctx
->total
== 1 && !strcmp(arg
, "--git-completion-helper-all"))
841 return show_gitcomp(options
, 1);
845 switch (parse_short_opt(ctx
, options
)) {
846 case PARSE_OPT_ERROR
:
847 return PARSE_OPT_ERROR
;
848 case PARSE_OPT_UNKNOWN
:
850 check_typos(arg
+ 1, options
);
851 if (internal_help
&& *ctx
->opt
== 'h')
854 case PARSE_OPT_NON_OPTION
:
855 case PARSE_OPT_SUBCOMMAND
:
857 case PARSE_OPT_COMPLETE
:
858 BUG("parse_short_opt() cannot return these");
863 check_typos(arg
+ 1, options
);
865 switch (parse_short_opt(ctx
, options
)) {
866 case PARSE_OPT_ERROR
:
867 return PARSE_OPT_ERROR
;
868 case PARSE_OPT_UNKNOWN
:
869 if (internal_help
&& *ctx
->opt
== 'h')
872 /* fake a short option thing to hide the fact that we may have
873 * started to parse aggregated stuff
875 * This is leaky, too bad.
877 ctx
->argv
[0] = xstrdup(ctx
->opt
- 1);
878 *(char *)ctx
->argv
[0] = '-';
880 case PARSE_OPT_NON_OPTION
:
881 case PARSE_OPT_SUBCOMMAND
:
882 case PARSE_OPT_COMPLETE
:
884 BUG("parse_short_opt() cannot return these");
892 if (!arg
[2] /* "--" */ ||
893 !strcmp(arg
+ 2, "end-of-options")) {
894 if (!(ctx
->flags
& PARSE_OPT_KEEP_DASHDASH
)) {
901 if (internal_help
&& !strcmp(arg
+ 2, "help-all"))
902 return usage_with_options_internal(ctx
, usagestr
, options
, 1, 0);
903 if (internal_help
&& !strcmp(arg
+ 2, "help"))
905 switch (parse_long_opt(ctx
, arg
+ 2, options
)) {
906 case PARSE_OPT_ERROR
:
907 return PARSE_OPT_ERROR
;
908 case PARSE_OPT_UNKNOWN
:
912 case PARSE_OPT_NON_OPTION
:
913 case PARSE_OPT_SUBCOMMAND
:
914 case PARSE_OPT_COMPLETE
:
915 BUG("parse_long_opt() cannot return these");
921 if (ctx
->flags
& PARSE_OPT_ONE_SHOT
)
923 if (ctx
->has_subcommands
&&
924 (ctx
->flags
& PARSE_OPT_SUBCOMMAND_OPTIONAL
) &&
925 (ctx
->flags
& PARSE_OPT_KEEP_UNKNOWN_OPT
)) {
927 * Found an unknown option given to a command with
928 * subcommands that has a default operation mode:
929 * we treat this option and all remaining args as
930 * arguments meant to that default operation mode.
931 * So we are done parsing.
933 return PARSE_OPT_DONE
;
935 if (!(ctx
->flags
& PARSE_OPT_KEEP_UNKNOWN_OPT
))
936 return PARSE_OPT_UNKNOWN
;
937 ctx
->out
[ctx
->cpidx
++] = ctx
->argv
[0];
940 return PARSE_OPT_DONE
;
943 return usage_with_options_internal(ctx
, usagestr
, options
, 0, 0);
946 int parse_options_end(struct parse_opt_ctx_t
*ctx
)
948 if (ctx
->flags
& PARSE_OPT_ONE_SHOT
)
949 return ctx
->total
- ctx
->argc
;
951 MOVE_ARRAY(ctx
->out
+ ctx
->cpidx
, ctx
->argv
, ctx
->argc
);
952 ctx
->out
[ctx
->cpidx
+ ctx
->argc
] = NULL
;
953 return ctx
->cpidx
+ ctx
->argc
;
956 int parse_options(int argc
, const char **argv
,
958 const struct option
*options
,
959 const char * const usagestr
[],
960 enum parse_opt_flags flags
)
962 struct parse_opt_ctx_t ctx
;
963 struct option
*real_options
;
965 disallow_abbreviated_options
=
966 git_env_bool("GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS", 0);
968 memset(&ctx
, 0, sizeof(ctx
));
969 real_options
= preprocess_options(&ctx
, options
);
971 options
= real_options
;
972 parse_options_start_1(&ctx
, argc
, argv
, prefix
, options
, flags
);
973 switch (parse_options_step(&ctx
, options
, usagestr
)) {
975 case PARSE_OPT_ERROR
:
977 case PARSE_OPT_COMPLETE
:
979 case PARSE_OPT_NON_OPTION
:
980 case PARSE_OPT_SUBCOMMAND
:
983 if (ctx
.has_subcommands
&&
984 !(flags
& PARSE_OPT_SUBCOMMAND_OPTIONAL
)) {
985 error(_("need a subcommand"));
986 usage_with_options(usagestr
, options
);
989 case PARSE_OPT_UNKNOWN
:
990 if (ctx
.argv
[0][1] == '-') {
991 error(_("unknown option `%s'"), ctx
.argv
[0] + 2);
992 } else if (isascii(*ctx
.opt
)) {
993 error(_("unknown switch `%c'"), *ctx
.opt
);
995 error(_("unknown non-ascii option in string: `%s'"),
998 usage_with_options(usagestr
, options
);
1001 precompose_argv_prefix(argc
, argv
, NULL
);
1002 free_preprocessed_options(real_options
);
1003 free(ctx
.alias_groups
);
1004 return parse_options_end(&ctx
);
1007 static int usage_argh(const struct option
*opts
, FILE *outfile
)
1010 int literal
= (opts
->flags
& PARSE_OPT_LITERAL_ARGHELP
) ||
1011 !opts
->argh
|| !!strpbrk(opts
->argh
, "()<>[]|");
1012 if (opts
->flags
& PARSE_OPT_OPTARG
)
1013 if (opts
->long_name
)
1014 s
= literal
? "[=%s]" : "[=<%s>]";
1016 s
= literal
? "[%s]" : "[<%s>]";
1018 s
= literal
? " %s" : " <%s>";
1019 return utf8_fprintf(outfile
, s
, opts
->argh
? _(opts
->argh
) : _("..."));
1022 #define USAGE_OPTS_WIDTH 24
1025 static enum parse_opt_result
usage_with_options_internal(struct parse_opt_ctx_t
*ctx
,
1026 const char * const *usagestr
,
1027 const struct option
*opts
,
1030 FILE *outfile
= err
? stderr
: stdout
;
1033 const char *usage_prefix
= _("usage: %s");
1035 * The translation could be anything, but we can count on
1036 * msgfmt(1)'s --check option to have asserted that "%s" is in
1037 * the translation. So compute the length of the "usage: "
1038 * part. We are assuming that the translator wasn't overly
1039 * clever and used e.g. "%1$s" instead of "%s", there's only
1040 * one "%s" in "usage_prefix" above, so there's no reason to
1041 * do so even with a RTL language.
1043 size_t usage_len
= strlen(usage_prefix
) - strlen("%s");
1045 * TRANSLATORS: the colon here should align with the
1046 * one in "usage: %s" translation.
1048 const char *or_prefix
= _(" or: %s");
1050 * TRANSLATORS: You should only need to translate this format
1051 * string if your language is a RTL language (e.g. Arabic,
1052 * Hebrew etc.), not if it's a LTR language (e.g. German,
1053 * Russian, Chinese etc.).
1055 * When a translated usage string has an embedded "\n" it's
1056 * because options have wrapped to the next line. The line
1057 * after the "\n" will then be padded to align with the
1058 * command name, such as N_("git cmd [opt]\n<8
1059 * spaces>[opt2]"), where the 8 spaces are the same length as
1062 * This format string prints out that already-translated
1063 * line. The "%*s" is whitespace padding to account for the
1064 * padding at the start of the line that we add in this
1065 * function. The "%s" is a line in the (hopefully already
1066 * translated) N_() usage string, which contained embedded
1067 * newlines before we split it up.
1069 const char *usage_continued
= _("%*s%s");
1070 const char *prefix
= usage_prefix
;
1071 int saw_empty_line
= 0;
1074 return PARSE_OPT_HELP
;
1076 if (!err
&& ctx
&& ctx
->flags
& PARSE_OPT_SHELL_EVAL
)
1077 fprintf(outfile
, "cat <<\\EOF\n");
1080 const char *str
= _(*usagestr
++);
1081 struct string_list list
= STRING_LIST_INIT_DUP
;
1084 if (!saw_empty_line
&& !*str
)
1087 string_list_split(&list
, str
, '\n', -1);
1088 for (j
= 0; j
< list
.nr
; j
++) {
1089 const char *line
= list
.items
[j
].string
;
1091 if (saw_empty_line
&& *line
)
1092 fprintf_ln(outfile
, _(" %s"), line
);
1093 else if (saw_empty_line
)
1094 fputc('\n', outfile
);
1096 fprintf_ln(outfile
, prefix
, line
);
1098 fprintf_ln(outfile
, usage_continued
,
1099 (int)usage_len
, "", line
);
1101 string_list_clear(&list
, 0);
1108 for (; opts
->type
!= OPTION_END
; opts
++) {
1112 if (opts
->type
== OPTION_SUBCOMMAND
)
1114 if (opts
->type
== OPTION_GROUP
) {
1115 fputc('\n', outfile
);
1118 fprintf(outfile
, "%s\n", _(opts
->help
));
1121 if (!full
&& (opts
->flags
& PARSE_OPT_HIDDEN
))
1125 fputc('\n', outfile
);
1129 pos
= fprintf(outfile
, " ");
1130 if (opts
->short_name
) {
1131 if (opts
->flags
& PARSE_OPT_NODASH
)
1132 pos
+= fprintf(outfile
, "%c", opts
->short_name
);
1134 pos
+= fprintf(outfile
, "-%c", opts
->short_name
);
1136 if (opts
->long_name
&& opts
->short_name
)
1137 pos
+= fprintf(outfile
, ", ");
1138 if (opts
->long_name
)
1139 pos
+= fprintf(outfile
, "--%s", opts
->long_name
);
1140 if (opts
->type
== OPTION_NUMBER
)
1141 pos
+= utf8_fprintf(outfile
, _("-NUM"));
1143 if ((opts
->flags
& PARSE_OPT_LITERAL_ARGHELP
) ||
1144 !(opts
->flags
& PARSE_OPT_NOARG
))
1145 pos
+= usage_argh(opts
, outfile
);
1147 if (pos
<= USAGE_OPTS_WIDTH
)
1148 pad
= USAGE_OPTS_WIDTH
- pos
;
1150 fputc('\n', outfile
);
1151 pad
= USAGE_OPTS_WIDTH
;
1153 if (opts
->type
== OPTION_ALIAS
) {
1154 fprintf(outfile
, "%*s", pad
+ USAGE_GAP
, "");
1155 fprintf_ln(outfile
, _("alias of --%s"),
1156 (const char *)opts
->value
);
1159 fprintf(outfile
, "%*s%s\n", pad
+ USAGE_GAP
, "", _(opts
->help
));
1161 fputc('\n', outfile
);
1163 if (!err
&& ctx
&& ctx
->flags
& PARSE_OPT_SHELL_EVAL
)
1164 fputs("EOF\n", outfile
);
1166 return PARSE_OPT_HELP
;
1169 void NORETURN
usage_with_options(const char * const *usagestr
,
1170 const struct option
*opts
)
1172 usage_with_options_internal(NULL
, usagestr
, opts
, 0, 1);
1176 void NORETURN
usage_msg_opt(const char *msg
,
1177 const char * const *usagestr
,
1178 const struct option
*options
)
1180 die_message("%s\n", msg
); /* The extra \n is intentional */
1181 usage_with_options(usagestr
, options
);
1184 void NORETURN
usage_msg_optf(const char * const fmt
,
1185 const char * const *usagestr
,
1186 const struct option
*options
, ...)
1188 struct strbuf msg
= STRBUF_INIT
;
1190 va_start(ap
, options
);
1191 strbuf_vaddf(&msg
, fmt
, ap
);
1194 usage_msg_opt(msg
.buf
, usagestr
, options
);
1197 void die_for_incompatible_opt4(int opt1
, const char *opt1_name
,
1198 int opt2
, const char *opt2_name
,
1199 int opt3
, const char *opt3_name
,
1200 int opt4
, const char *opt4_name
)
1203 const char *options
[4];
1206 options
[count
++] = opt1_name
;
1208 options
[count
++] = opt2_name
;
1210 options
[count
++] = opt3_name
;
1212 options
[count
++] = opt4_name
;
1215 die(_("options '%s', '%s', '%s', and '%s' cannot be used together"),
1216 opt1_name
, opt2_name
, opt3_name
, opt4_name
);
1219 die(_("options '%s', '%s', and '%s' cannot be used together"),
1220 options
[0], options
[1], options
[2]);
1223 die(_("options '%s' and '%s' cannot be used together"),
1224 options
[0], options
[1]);