1 #include "git-compat-util.h"
2 #include "parse-options.h"
9 static int disallow_abbreviated_options
;
17 static void optbug(const struct option
*opt
, const char *reason
)
19 if (opt
->long_name
&& opt
->short_name
)
20 bug("switch '%c' (--%s) %s", opt
->short_name
,
21 opt
->long_name
, reason
);
22 else if (opt
->long_name
)
23 bug("option '%s' %s", opt
->long_name
, reason
);
25 bug("switch '%c' %s", opt
->short_name
, reason
);
28 static const char *optname(const struct option
*opt
, enum opt_parsed flags
)
30 static struct strbuf sb
= STRBUF_INIT
;
33 if (flags
& OPT_SHORT
)
34 strbuf_addf(&sb
, "switch `%c'", opt
->short_name
);
35 else if (flags
& OPT_UNSET
)
36 strbuf_addf(&sb
, "option `no-%s'", opt
->long_name
);
37 else if (flags
== OPT_LONG
)
38 strbuf_addf(&sb
, "option `%s'", opt
->long_name
);
40 BUG("optname() got unknown flags %d", flags
);
45 static enum parse_opt_result
get_arg(struct parse_opt_ctx_t
*p
,
46 const struct option
*opt
,
47 enum opt_parsed flags
, const char **arg
)
52 } else if (p
->argc
== 1 && (opt
->flags
& PARSE_OPT_LASTARG_DEFAULT
)) {
53 *arg
= (const char *)opt
->defval
;
54 } else if (p
->argc
> 1) {
58 return error(_("%s requires a value"), optname(opt
, flags
));
62 static void fix_filename(const char *prefix
, const char **file
)
64 if (!file
|| !*file
|| !prefix
|| is_absolute_path(*file
)
65 || !strcmp("-", *file
))
67 *file
= prefix_filename(prefix
, *file
);
70 static enum parse_opt_result
opt_command_mode_error(
71 const struct option
*opt
,
72 const struct option
*all_opts
,
73 enum opt_parsed flags
)
75 const struct option
*that
;
76 struct strbuf that_name
= STRBUF_INIT
;
79 * Find the other option that was used to set the variable
80 * already, and report that this is not compatible with it.
82 for (that
= all_opts
; that
->type
!= OPTION_END
; that
++) {
84 !(that
->flags
& PARSE_OPT_CMDMODE
) ||
85 that
->value
!= opt
->value
||
86 that
->defval
!= *(int *)opt
->value
)
90 strbuf_addf(&that_name
, "--%s", that
->long_name
);
92 strbuf_addf(&that_name
, "-%c", that
->short_name
);
93 error(_("%s is incompatible with %s"),
94 optname(opt
, flags
), that_name
.buf
);
95 strbuf_release(&that_name
);
96 return PARSE_OPT_ERROR
;
98 return error(_("%s : incompatible with something else"),
102 static enum parse_opt_result
get_value(struct parse_opt_ctx_t
*p
,
103 const struct option
*opt
,
104 const struct option
*all_opts
,
105 enum opt_parsed flags
)
108 const int unset
= flags
& OPT_UNSET
;
112 return error(_("%s takes no value"), optname(opt
, flags
));
113 if (unset
&& (opt
->flags
& PARSE_OPT_NONEG
))
114 return error(_("%s isn't available"), optname(opt
, flags
));
115 if (!(flags
& OPT_SHORT
) && p
->opt
&& (opt
->flags
& PARSE_OPT_NOARG
))
116 return error(_("%s takes no value"), optname(opt
, flags
));
119 * Giving the same mode option twice, although unnecessary,
120 * is not a grave error, so let it pass.
122 if ((opt
->flags
& PARSE_OPT_CMDMODE
) &&
123 *(int *)opt
->value
&& *(int *)opt
->value
!= opt
->defval
)
124 return opt_command_mode_error(opt
, all_opts
, flags
);
127 case OPTION_LOWLEVEL_CALLBACK
:
128 return opt
->ll_callback(p
, opt
, NULL
, unset
);
132 *(int *)opt
->value
&= ~opt
->defval
;
134 *(int *)opt
->value
|= opt
->defval
;
139 *(int *)opt
->value
|= opt
->defval
;
141 *(int *)opt
->value
&= ~opt
->defval
;
146 BUG("BITOP can't have unset form");
147 *(int *)opt
->value
&= ~opt
->extra
;
148 *(int *)opt
->value
|= opt
->defval
;
152 if (*(int *)opt
->value
< 0)
153 *(int *)opt
->value
= 0;
154 *(int *)opt
->value
= unset
? 0 : *(int *)opt
->value
+ 1;
158 *(int *)opt
->value
= unset
? 0 : opt
->defval
;
163 *(const char **)opt
->value
= NULL
;
164 else if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
)
165 *(const char **)opt
->value
= (const char *)opt
->defval
;
167 return get_arg(p
, opt
, flags
, (const char **)opt
->value
);
170 case OPTION_FILENAME
:
173 *(const char **)opt
->value
= NULL
;
174 else if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
)
175 *(const char **)opt
->value
= (const char *)opt
->defval
;
177 err
= get_arg(p
, opt
, flags
, (const char **)opt
->value
);
180 fix_filename(p
->prefix
, (const char **)opt
->value
);
183 case OPTION_CALLBACK
:
185 const char *p_arg
= NULL
;
190 else if (opt
->flags
& PARSE_OPT_NOARG
)
192 else if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
)
194 else if (get_arg(p
, opt
, flags
, &arg
))
201 return (*opt
->callback
)(opt
, p_arg
, p_unset
) ? (-1) : 0;
203 return (*opt
->ll_callback
)(p
, opt
, p_arg
, p_unset
);
207 *(int *)opt
->value
= 0;
210 if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
) {
211 *(int *)opt
->value
= opt
->defval
;
214 if (get_arg(p
, opt
, flags
, &arg
))
217 return error(_("%s expects a numerical value"),
218 optname(opt
, flags
));
219 *(int *)opt
->value
= strtol(arg
, (char **)&s
, 10);
221 return error(_("%s expects a numerical value"),
222 optname(opt
, flags
));
225 case OPTION_MAGNITUDE
:
227 *(unsigned long *)opt
->value
= 0;
230 if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
) {
231 *(unsigned long *)opt
->value
= opt
->defval
;
234 if (get_arg(p
, opt
, flags
, &arg
))
236 if (!git_parse_ulong(arg
, opt
->value
))
237 return error(_("%s expects a non-negative integer value"
238 " with an optional k/m/g suffix"),
239 optname(opt
, flags
));
243 BUG("opt->type %d should not happen", opt
->type
);
247 static enum parse_opt_result
parse_short_opt(struct parse_opt_ctx_t
*p
,
248 const struct option
*options
)
250 const struct option
*all_opts
= options
;
251 const struct option
*numopt
= NULL
;
253 for (; options
->type
!= OPTION_END
; options
++) {
254 if (options
->short_name
== *p
->opt
) {
255 p
->opt
= p
->opt
[1] ? p
->opt
+ 1 : NULL
;
256 return get_value(p
, options
, all_opts
, OPT_SHORT
);
260 * Handle the numerical option later, explicit one-digit
261 * options take precedence over it.
263 if (options
->type
== OPTION_NUMBER
)
266 if (numopt
&& isdigit(*p
->opt
)) {
271 while (isdigit(p
->opt
[len
]))
273 arg
= xmemdupz(p
->opt
, len
);
274 p
->opt
= p
->opt
[len
] ? p
->opt
+ len
: NULL
;
275 if (numopt
->callback
)
276 rc
= (*numopt
->callback
)(numopt
, arg
, 0) ? (-1) : 0;
278 rc
= (*numopt
->ll_callback
)(p
, numopt
, arg
, 0);
282 return PARSE_OPT_UNKNOWN
;
285 static int has_string(const char *it
, const char **array
)
288 if (!strcmp(it
, *(array
++)))
293 static int is_alias(struct parse_opt_ctx_t
*ctx
,
294 const struct option
*one_opt
,
295 const struct option
*another_opt
)
299 if (!ctx
->alias_groups
)
302 if (!one_opt
->long_name
|| !another_opt
->long_name
)
305 for (group
= ctx
->alias_groups
; *group
; group
+= 3) {
306 /* it and other are from the same family? */
307 if (has_string(one_opt
->long_name
, group
) &&
308 has_string(another_opt
->long_name
, group
))
314 static enum parse_opt_result
parse_long_opt(
315 struct parse_opt_ctx_t
*p
, const char *arg
,
316 const struct option
*options
)
318 const struct option
*all_opts
= options
;
319 const char *arg_end
= strchrnul(arg
, '=');
320 const struct option
*abbrev_option
= NULL
, *ambiguous_option
= NULL
;
321 enum opt_parsed abbrev_flags
= OPT_LONG
, ambiguous_flags
= OPT_LONG
;
323 for (; options
->type
!= OPTION_END
; options
++) {
324 const char *rest
, *long_name
= options
->long_name
;
325 enum opt_parsed flags
= OPT_LONG
, opt_flags
= OPT_LONG
;
327 if (options
->type
== OPTION_SUBCOMMAND
)
333 if (!skip_prefix(arg
, long_name
, &rest
))
337 if (!(p
->flags
& PARSE_OPT_KEEP_UNKNOWN_OPT
) &&
338 !strncmp(long_name
, arg
, arg_end
- arg
)) {
341 !is_alias(p
, abbrev_option
, options
)) {
343 * If this is abbreviated, it is
344 * ambiguous. So when there is no
345 * exact match later, we need to
348 ambiguous_option
= abbrev_option
;
349 ambiguous_flags
= abbrev_flags
;
351 if (!(flags
& OPT_UNSET
) && *arg_end
)
352 p
->opt
= arg_end
+ 1;
353 abbrev_option
= options
;
354 abbrev_flags
= flags
^ opt_flags
;
357 /* negation allowed? */
358 if (options
->flags
& PARSE_OPT_NONEG
)
360 /* negated and abbreviated very much? */
361 if (starts_with("no-", arg
)) {
366 if (!starts_with(arg
, "no-")) {
367 if (skip_prefix(long_name
, "no-", &long_name
)) {
368 opt_flags
|= OPT_UNSET
;
374 if (!skip_prefix(arg
+ 3, long_name
, &rest
)) {
375 /* abbreviated and negated? */
376 if (starts_with(long_name
, arg
+ 3))
387 return get_value(p
, options
, all_opts
, flags
^ opt_flags
);
390 if (disallow_abbreviated_options
&& (ambiguous_option
|| abbrev_option
))
391 die("disallowed abbreviated or ambiguous option '%.*s'",
392 (int)(arg_end
- arg
), arg
);
394 if (ambiguous_option
) {
395 error(_("ambiguous option: %s "
396 "(could be --%s%s or --%s%s)"),
398 (ambiguous_flags
& OPT_UNSET
) ? "no-" : "",
399 ambiguous_option
->long_name
,
400 (abbrev_flags
& OPT_UNSET
) ? "no-" : "",
401 abbrev_option
->long_name
);
402 return PARSE_OPT_HELP
;
405 return get_value(p
, abbrev_option
, all_opts
, abbrev_flags
);
406 return PARSE_OPT_UNKNOWN
;
409 static enum parse_opt_result
parse_nodash_opt(struct parse_opt_ctx_t
*p
,
411 const struct option
*options
)
413 const struct option
*all_opts
= options
;
415 for (; options
->type
!= OPTION_END
; options
++) {
416 if (!(options
->flags
& PARSE_OPT_NODASH
))
418 if (options
->short_name
== arg
[0] && arg
[1] == '\0')
419 return get_value(p
, options
, all_opts
, OPT_SHORT
);
421 return PARSE_OPT_ERROR
;
424 static enum parse_opt_result
parse_subcommand(const char *arg
,
425 const struct option
*options
)
427 for (; options
->type
!= OPTION_END
; options
++)
428 if (options
->type
== OPTION_SUBCOMMAND
&&
429 !strcmp(options
->long_name
, arg
)) {
430 *(parse_opt_subcommand_fn
**)options
->value
= options
->subcommand_fn
;
431 return PARSE_OPT_SUBCOMMAND
;
434 return PARSE_OPT_UNKNOWN
;
437 static void check_typos(const char *arg
, const struct option
*options
)
442 if (starts_with(arg
, "no-")) {
443 error(_("did you mean `--%s` (with two dashes)?"), arg
);
447 for (; options
->type
!= OPTION_END
; options
++) {
448 if (!options
->long_name
)
450 if (starts_with(options
->long_name
, arg
)) {
451 error(_("did you mean `--%s` (with two dashes)?"), arg
);
457 static void parse_options_check(const struct option
*opts
)
459 char short_opts
[128];
460 void *subcommand_value
= NULL
;
462 memset(short_opts
, '\0', sizeof(short_opts
));
463 for (; opts
->type
!= OPTION_END
; opts
++) {
464 if ((opts
->flags
& PARSE_OPT_LASTARG_DEFAULT
) &&
465 (opts
->flags
& PARSE_OPT_OPTARG
))
466 optbug(opts
, "uses incompatible flags "
467 "LASTARG_DEFAULT and OPTARG");
468 if (opts
->short_name
) {
469 if (0x7F <= opts
->short_name
)
470 optbug(opts
, "invalid short name");
471 else if (short_opts
[opts
->short_name
]++)
472 optbug(opts
, "short name already used");
474 if (opts
->flags
& PARSE_OPT_NODASH
&&
475 ((opts
->flags
& PARSE_OPT_OPTARG
) ||
476 !(opts
->flags
& PARSE_OPT_NOARG
) ||
477 !(opts
->flags
& PARSE_OPT_NONEG
) ||
479 optbug(opts
, "uses feature "
480 "not supported for dashless options");
481 switch (opts
->type
) {
487 if ((opts
->flags
& PARSE_OPT_OPTARG
) ||
488 !(opts
->flags
& PARSE_OPT_NOARG
))
489 optbug(opts
, "should not accept an argument");
491 case OPTION_CALLBACK
:
492 if (!opts
->callback
&& !opts
->ll_callback
)
493 optbug(opts
, "OPTION_CALLBACK needs one callback");
494 else if (opts
->callback
&& opts
->ll_callback
)
495 optbug(opts
, "OPTION_CALLBACK can't have two callbacks");
497 case OPTION_LOWLEVEL_CALLBACK
:
498 if (!opts
->ll_callback
)
499 optbug(opts
, "OPTION_LOWLEVEL_CALLBACK needs a callback");
501 optbug(opts
, "OPTION_LOWLEVEL_CALLBACK needs no high level callback");
504 optbug(opts
, "OPT_ALIAS() should not remain at this point. "
505 "Are you using parse_options_step() directly?\n"
506 "That case is not supported yet.");
508 case OPTION_SUBCOMMAND
:
509 if (!opts
->value
|| !opts
->subcommand_fn
)
510 optbug(opts
, "OPTION_SUBCOMMAND needs a value and a subcommand function");
511 if (!subcommand_value
)
512 subcommand_value
= opts
->value
;
513 else if (subcommand_value
!= opts
->value
)
514 optbug(opts
, "all OPTION_SUBCOMMANDs need the same value");
517 ; /* ok. (usually accepts an argument) */
520 strcspn(opts
->argh
, " _") != strlen(opts
->argh
))
521 optbug(opts
, "multi-word argh should use dash to separate words");
523 BUG_if_bug("invalid 'struct option'");
526 static int has_subcommands(const struct option
*options
)
528 for (; options
->type
!= OPTION_END
; options
++)
529 if (options
->type
== OPTION_SUBCOMMAND
)
534 static void parse_options_start_1(struct parse_opt_ctx_t
*ctx
,
535 int argc
, const char **argv
, const char *prefix
,
536 const struct option
*options
,
537 enum parse_opt_flags flags
)
541 if (!(flags
& PARSE_OPT_ONE_SHOT
)) {
545 ctx
->total
= ctx
->argc
;
547 ctx
->prefix
= prefix
;
548 ctx
->cpidx
= ((flags
& PARSE_OPT_KEEP_ARGV0
) != 0);
550 ctx
->has_subcommands
= has_subcommands(options
);
551 if (!ctx
->has_subcommands
&& (flags
& PARSE_OPT_SUBCOMMAND_OPTIONAL
))
552 BUG("Using PARSE_OPT_SUBCOMMAND_OPTIONAL without subcommands");
553 if (ctx
->has_subcommands
) {
554 if (flags
& PARSE_OPT_STOP_AT_NON_OPTION
)
555 BUG("subcommands are incompatible with PARSE_OPT_STOP_AT_NON_OPTION");
556 if (!(flags
& PARSE_OPT_SUBCOMMAND_OPTIONAL
)) {
557 if (flags
& PARSE_OPT_KEEP_UNKNOWN_OPT
)
558 BUG("subcommands are incompatible with PARSE_OPT_KEEP_UNKNOWN_OPT unless in combination with PARSE_OPT_SUBCOMMAND_OPTIONAL");
559 if (flags
& PARSE_OPT_KEEP_DASHDASH
)
560 BUG("subcommands are incompatible with PARSE_OPT_KEEP_DASHDASH unless in combination with PARSE_OPT_SUBCOMMAND_OPTIONAL");
563 if ((flags
& PARSE_OPT_KEEP_UNKNOWN_OPT
) &&
564 (flags
& PARSE_OPT_STOP_AT_NON_OPTION
) &&
565 !(flags
& PARSE_OPT_ONE_SHOT
))
566 BUG("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
567 if ((flags
& PARSE_OPT_ONE_SHOT
) &&
568 (flags
& PARSE_OPT_KEEP_ARGV0
))
569 BUG("Can't keep argv0 if you don't have it");
570 parse_options_check(options
);
573 void parse_options_start(struct parse_opt_ctx_t
*ctx
,
574 int argc
, const char **argv
, const char *prefix
,
575 const struct option
*options
,
576 enum parse_opt_flags flags
)
578 memset(ctx
, 0, sizeof(*ctx
));
579 parse_options_start_1(ctx
, argc
, argv
, prefix
, options
, flags
);
582 static void show_negated_gitcomp(const struct option
*opts
, int show_all
,
585 int printed_dashdash
= 0;
587 for (; opts
->type
!= OPTION_END
; opts
++) {
588 int has_unset_form
= 0;
591 if (!opts
->long_name
)
594 (opts
->flags
& (PARSE_OPT_HIDDEN
| PARSE_OPT_NOCOMPLETE
)))
596 if (opts
->flags
& PARSE_OPT_NONEG
)
599 switch (opts
->type
) {
601 case OPTION_FILENAME
:
603 case OPTION_MAGNITUDE
:
604 case OPTION_CALLBACK
:
617 if (skip_prefix(opts
->long_name
, "no-", &name
)) {
619 printf(" --%s", name
);
620 } else if (nr_noopts
>= 0) {
621 if (nr_noopts
&& !printed_dashdash
) {
623 printed_dashdash
= 1;
625 printf(" --no-%s", opts
->long_name
);
631 static int show_gitcomp(const struct option
*opts
, int show_all
)
633 const struct option
*original_opts
= opts
;
636 for (; opts
->type
!= OPTION_END
; opts
++) {
637 const char *prefix
= "--";
638 const char *suffix
= "";
640 if (!opts
->long_name
)
643 (opts
->flags
& (PARSE_OPT_HIDDEN
| PARSE_OPT_NOCOMPLETE
| PARSE_OPT_FROM_ALIAS
)))
646 switch (opts
->type
) {
647 case OPTION_SUBCOMMAND
:
653 case OPTION_FILENAME
:
655 case OPTION_MAGNITUDE
:
656 case OPTION_CALLBACK
:
657 if (opts
->flags
& PARSE_OPT_NOARG
)
659 if (opts
->flags
& PARSE_OPT_OPTARG
)
661 if (opts
->flags
& PARSE_OPT_LASTARG_DEFAULT
)
668 if (opts
->flags
& PARSE_OPT_COMP_ARG
)
670 if (starts_with(opts
->long_name
, "no-"))
672 printf("%s%s%s%s", opts
== original_opts
? "" : " ",
673 prefix
, opts
->long_name
, suffix
);
675 show_negated_gitcomp(original_opts
, show_all
, -1);
676 show_negated_gitcomp(original_opts
, show_all
, nr_noopts
);
678 return PARSE_OPT_COMPLETE
;
682 * Scan and may produce a new option[] array, which should be used
683 * instead of the original 'options'.
685 * Right now this is only used to preprocess and substitute
688 * The returned options should be freed using free_preprocessed_options.
690 static struct option
*preprocess_options(struct parse_opt_ctx_t
*ctx
,
691 const struct option
*options
)
693 struct option
*newopt
;
697 for (nr
= 0; options
[nr
].type
!= OPTION_END
; nr
++) {
698 if (options
[nr
].type
== OPTION_ALIAS
)
705 DUP_ARRAY(newopt
, options
, nr
+ 1);
707 /* each alias has two string pointers and NULL */
708 CALLOC_ARRAY(ctx
->alias_groups
, 3 * (nr_aliases
+ 1));
710 for (alias
= 0, i
= 0; i
< nr
; i
++) {
712 const char *long_name
;
714 struct strbuf help
= STRBUF_INIT
;
717 if (newopt
[i
].type
!= OPTION_ALIAS
)
720 short_name
= newopt
[i
].short_name
;
721 long_name
= newopt
[i
].long_name
;
722 source
= newopt
[i
].value
;
725 BUG("An alias must have long option name");
726 strbuf_addf(&help
, _("alias of --%s"), source
);
728 for (j
= 0; j
< nr
; j
++) {
729 const char *name
= options
[j
].long_name
;
731 if (!name
|| strcmp(name
, source
))
734 if (options
[j
].type
== OPTION_ALIAS
)
735 BUG("No please. Nested aliases are not supported.");
737 memcpy(newopt
+ i
, options
+ j
, sizeof(*newopt
));
738 newopt
[i
].short_name
= short_name
;
739 newopt
[i
].long_name
= long_name
;
740 newopt
[i
].help
= strbuf_detach(&help
, NULL
);
741 newopt
[i
].flags
|= PARSE_OPT_FROM_ALIAS
;
746 BUG("could not find source option '%s' of alias '%s'",
747 source
, newopt
[i
].long_name
);
748 ctx
->alias_groups
[alias
* 3 + 0] = newopt
[i
].long_name
;
749 ctx
->alias_groups
[alias
* 3 + 1] = options
[j
].long_name
;
750 ctx
->alias_groups
[alias
* 3 + 2] = NULL
;
757 static void free_preprocessed_options(struct option
*options
)
764 for (i
= 0; options
[i
].type
!= OPTION_END
; i
++) {
765 if (options
[i
].flags
& PARSE_OPT_FROM_ALIAS
)
766 free((void *)options
[i
].help
);
771 static enum parse_opt_result
usage_with_options_internal(struct parse_opt_ctx_t
*,
772 const char * const *,
773 const struct option
*,
776 enum parse_opt_result
parse_options_step(struct parse_opt_ctx_t
*ctx
,
777 const struct option
*options
,
778 const char * const usagestr
[])
780 int internal_help
= !(ctx
->flags
& PARSE_OPT_NO_INTERNAL_HELP
);
782 /* we must reset ->opt, unknown short option leave it dangling */
785 for (; ctx
->argc
; ctx
->argc
--, ctx
->argv
++) {
786 const char *arg
= ctx
->argv
[0];
788 if (ctx
->flags
& PARSE_OPT_ONE_SHOT
&&
789 ctx
->argc
!= ctx
->total
)
792 if (*arg
!= '-' || !arg
[1]) {
793 if (parse_nodash_opt(ctx
, arg
, options
) == 0)
795 if (!ctx
->has_subcommands
) {
796 if (ctx
->flags
& PARSE_OPT_STOP_AT_NON_OPTION
)
797 return PARSE_OPT_NON_OPTION
;
798 ctx
->out
[ctx
->cpidx
++] = ctx
->argv
[0];
801 switch (parse_subcommand(arg
, options
)) {
802 case PARSE_OPT_SUBCOMMAND
:
803 return PARSE_OPT_SUBCOMMAND
;
804 case PARSE_OPT_UNKNOWN
:
805 if (ctx
->flags
& PARSE_OPT_SUBCOMMAND_OPTIONAL
)
807 * arg is neither a short or long
808 * option nor a subcommand. Since
809 * this command has a default
810 * operation mode, we have to treat
811 * this arg and all remaining args
812 * as args meant to that default
814 * So we are done parsing.
816 return PARSE_OPT_DONE
;
817 error(_("unknown subcommand: `%s'"), arg
);
818 usage_with_options(usagestr
, options
);
819 case PARSE_OPT_COMPLETE
:
821 case PARSE_OPT_ERROR
:
823 case PARSE_OPT_NON_OPTION
:
825 BUG("parse_subcommand() cannot return these");
829 /* lone -h asks for help */
830 if (internal_help
&& ctx
->total
== 1 && !strcmp(arg
+ 1, "h"))
834 * lone --git-completion-helper and --git-completion-helper-all
835 * are asked by git-completion.bash
837 if (ctx
->total
== 1 && !strcmp(arg
, "--git-completion-helper"))
838 return show_gitcomp(options
, 0);
839 if (ctx
->total
== 1 && !strcmp(arg
, "--git-completion-helper-all"))
840 return show_gitcomp(options
, 1);
844 switch (parse_short_opt(ctx
, options
)) {
845 case PARSE_OPT_ERROR
:
846 return PARSE_OPT_ERROR
;
847 case PARSE_OPT_UNKNOWN
:
849 check_typos(arg
+ 1, options
);
850 if (internal_help
&& *ctx
->opt
== 'h')
853 case PARSE_OPT_NON_OPTION
:
854 case PARSE_OPT_SUBCOMMAND
:
856 case PARSE_OPT_COMPLETE
:
857 BUG("parse_short_opt() cannot return these");
862 check_typos(arg
+ 1, options
);
864 switch (parse_short_opt(ctx
, options
)) {
865 case PARSE_OPT_ERROR
:
866 return PARSE_OPT_ERROR
;
867 case PARSE_OPT_UNKNOWN
:
868 if (internal_help
&& *ctx
->opt
== 'h')
871 /* fake a short option thing to hide the fact that we may have
872 * started to parse aggregated stuff
874 * This is leaky, too bad.
876 ctx
->argv
[0] = xstrdup(ctx
->opt
- 1);
877 *(char *)ctx
->argv
[0] = '-';
879 case PARSE_OPT_NON_OPTION
:
880 case PARSE_OPT_SUBCOMMAND
:
881 case PARSE_OPT_COMPLETE
:
883 BUG("parse_short_opt() cannot return these");
891 if (!arg
[2] /* "--" */ ||
892 !strcmp(arg
+ 2, "end-of-options")) {
893 if (!(ctx
->flags
& PARSE_OPT_KEEP_DASHDASH
)) {
900 if (internal_help
&& !strcmp(arg
+ 2, "help-all"))
901 return usage_with_options_internal(ctx
, usagestr
, options
, 1, 0);
902 if (internal_help
&& !strcmp(arg
+ 2, "help"))
904 switch (parse_long_opt(ctx
, arg
+ 2, options
)) {
905 case PARSE_OPT_ERROR
:
906 return PARSE_OPT_ERROR
;
907 case PARSE_OPT_UNKNOWN
:
911 case PARSE_OPT_NON_OPTION
:
912 case PARSE_OPT_SUBCOMMAND
:
913 case PARSE_OPT_COMPLETE
:
914 BUG("parse_long_opt() cannot return these");
920 if (ctx
->flags
& PARSE_OPT_ONE_SHOT
)
922 if (ctx
->has_subcommands
&&
923 (ctx
->flags
& PARSE_OPT_SUBCOMMAND_OPTIONAL
) &&
924 (ctx
->flags
& PARSE_OPT_KEEP_UNKNOWN_OPT
)) {
926 * Found an unknown option given to a command with
927 * subcommands that has a default operation mode:
928 * we treat this option and all remaining args as
929 * arguments meant to that default operation mode.
930 * So we are done parsing.
932 return PARSE_OPT_DONE
;
934 if (!(ctx
->flags
& PARSE_OPT_KEEP_UNKNOWN_OPT
))
935 return PARSE_OPT_UNKNOWN
;
936 ctx
->out
[ctx
->cpidx
++] = ctx
->argv
[0];
939 return PARSE_OPT_DONE
;
942 return usage_with_options_internal(ctx
, usagestr
, options
, 0, 0);
945 int parse_options_end(struct parse_opt_ctx_t
*ctx
)
947 if (ctx
->flags
& PARSE_OPT_ONE_SHOT
)
948 return ctx
->total
- ctx
->argc
;
950 MOVE_ARRAY(ctx
->out
+ ctx
->cpidx
, ctx
->argv
, ctx
->argc
);
951 ctx
->out
[ctx
->cpidx
+ ctx
->argc
] = NULL
;
952 return ctx
->cpidx
+ ctx
->argc
;
955 int parse_options(int argc
, const char **argv
,
957 const struct option
*options
,
958 const char * const usagestr
[],
959 enum parse_opt_flags flags
)
961 struct parse_opt_ctx_t ctx
;
962 struct option
*real_options
;
964 disallow_abbreviated_options
=
965 git_env_bool("GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS", 0);
967 memset(&ctx
, 0, sizeof(ctx
));
968 real_options
= preprocess_options(&ctx
, options
);
970 options
= real_options
;
971 parse_options_start_1(&ctx
, argc
, argv
, prefix
, options
, flags
);
972 switch (parse_options_step(&ctx
, options
, usagestr
)) {
974 case PARSE_OPT_ERROR
:
976 case PARSE_OPT_COMPLETE
:
978 case PARSE_OPT_NON_OPTION
:
979 case PARSE_OPT_SUBCOMMAND
:
982 if (ctx
.has_subcommands
&&
983 !(flags
& PARSE_OPT_SUBCOMMAND_OPTIONAL
)) {
984 error(_("need a subcommand"));
985 usage_with_options(usagestr
, options
);
988 case PARSE_OPT_UNKNOWN
:
989 if (ctx
.argv
[0][1] == '-') {
990 error(_("unknown option `%s'"), ctx
.argv
[0] + 2);
991 } else if (isascii(*ctx
.opt
)) {
992 error(_("unknown switch `%c'"), *ctx
.opt
);
994 error(_("unknown non-ascii option in string: `%s'"),
997 usage_with_options(usagestr
, options
);
1000 precompose_argv_prefix(argc
, argv
, NULL
);
1001 free_preprocessed_options(real_options
);
1002 free(ctx
.alias_groups
);
1003 return parse_options_end(&ctx
);
1006 static int usage_argh(const struct option
*opts
, FILE *outfile
)
1009 int literal
= (opts
->flags
& PARSE_OPT_LITERAL_ARGHELP
) ||
1010 !opts
->argh
|| !!strpbrk(opts
->argh
, "()<>[]|");
1011 if (opts
->flags
& PARSE_OPT_OPTARG
)
1012 if (opts
->long_name
)
1013 s
= literal
? "[=%s]" : "[=<%s>]";
1015 s
= literal
? "[%s]" : "[<%s>]";
1017 s
= literal
? " %s" : " <%s>";
1018 return utf8_fprintf(outfile
, s
, opts
->argh
? _(opts
->argh
) : _("..."));
1021 #define USAGE_OPTS_WIDTH 24
1024 static enum parse_opt_result
usage_with_options_internal(struct parse_opt_ctx_t
*ctx
,
1025 const char * const *usagestr
,
1026 const struct option
*opts
,
1029 FILE *outfile
= err
? stderr
: stdout
;
1032 const char *usage_prefix
= _("usage: %s");
1034 * The translation could be anything, but we can count on
1035 * msgfmt(1)'s --check option to have asserted that "%s" is in
1036 * the translation. So compute the length of the "usage: "
1037 * part. We are assuming that the translator wasn't overly
1038 * clever and used e.g. "%1$s" instead of "%s", there's only
1039 * one "%s" in "usage_prefix" above, so there's no reason to
1040 * do so even with a RTL language.
1042 size_t usage_len
= strlen(usage_prefix
) - strlen("%s");
1044 * TRANSLATORS: the colon here should align with the
1045 * one in "usage: %s" translation.
1047 const char *or_prefix
= _(" or: %s");
1049 * TRANSLATORS: You should only need to translate this format
1050 * string if your language is a RTL language (e.g. Arabic,
1051 * Hebrew etc.), not if it's a LTR language (e.g. German,
1052 * Russian, Chinese etc.).
1054 * When a translated usage string has an embedded "\n" it's
1055 * because options have wrapped to the next line. The line
1056 * after the "\n" will then be padded to align with the
1057 * command name, such as N_("git cmd [opt]\n<8
1058 * spaces>[opt2]"), where the 8 spaces are the same length as
1061 * This format string prints out that already-translated
1062 * line. The "%*s" is whitespace padding to account for the
1063 * padding at the start of the line that we add in this
1064 * function. The "%s" is a line in the (hopefully already
1065 * translated) N_() usage string, which contained embedded
1066 * newlines before we split it up.
1068 const char *usage_continued
= _("%*s%s");
1069 const char *prefix
= usage_prefix
;
1070 int saw_empty_line
= 0;
1073 return PARSE_OPT_HELP
;
1075 if (!err
&& ctx
&& ctx
->flags
& PARSE_OPT_SHELL_EVAL
)
1076 fprintf(outfile
, "cat <<\\EOF\n");
1079 const char *str
= _(*usagestr
++);
1080 struct string_list list
= STRING_LIST_INIT_DUP
;
1083 if (!saw_empty_line
&& !*str
)
1086 string_list_split(&list
, str
, '\n', -1);
1087 for (j
= 0; j
< list
.nr
; j
++) {
1088 const char *line
= list
.items
[j
].string
;
1090 if (saw_empty_line
&& *line
)
1091 fprintf_ln(outfile
, _(" %s"), line
);
1092 else if (saw_empty_line
)
1093 fputc('\n', outfile
);
1095 fprintf_ln(outfile
, prefix
, line
);
1097 fprintf_ln(outfile
, usage_continued
,
1098 (int)usage_len
, "", line
);
1100 string_list_clear(&list
, 0);
1107 for (; opts
->type
!= OPTION_END
; opts
++) {
1111 if (opts
->type
== OPTION_SUBCOMMAND
)
1113 if (opts
->type
== OPTION_GROUP
) {
1114 fputc('\n', outfile
);
1117 fprintf(outfile
, "%s\n", _(opts
->help
));
1120 if (!full
&& (opts
->flags
& PARSE_OPT_HIDDEN
))
1124 fputc('\n', outfile
);
1128 pos
= fprintf(outfile
, " ");
1129 if (opts
->short_name
) {
1130 if (opts
->flags
& PARSE_OPT_NODASH
)
1131 pos
+= fprintf(outfile
, "%c", opts
->short_name
);
1133 pos
+= fprintf(outfile
, "-%c", opts
->short_name
);
1135 if (opts
->long_name
&& opts
->short_name
)
1136 pos
+= fprintf(outfile
, ", ");
1137 if (opts
->long_name
)
1138 pos
+= fprintf(outfile
, "--%s", opts
->long_name
);
1139 if (opts
->type
== OPTION_NUMBER
)
1140 pos
+= utf8_fprintf(outfile
, _("-NUM"));
1142 if ((opts
->flags
& PARSE_OPT_LITERAL_ARGHELP
) ||
1143 !(opts
->flags
& PARSE_OPT_NOARG
))
1144 pos
+= usage_argh(opts
, outfile
);
1146 if (pos
<= USAGE_OPTS_WIDTH
)
1147 pad
= USAGE_OPTS_WIDTH
- pos
;
1149 fputc('\n', outfile
);
1150 pad
= USAGE_OPTS_WIDTH
;
1152 if (opts
->type
== OPTION_ALIAS
) {
1153 fprintf(outfile
, "%*s", pad
+ USAGE_GAP
, "");
1154 fprintf_ln(outfile
, _("alias of --%s"),
1155 (const char *)opts
->value
);
1158 fprintf(outfile
, "%*s%s\n", pad
+ USAGE_GAP
, "", _(opts
->help
));
1160 fputc('\n', outfile
);
1162 if (!err
&& ctx
&& ctx
->flags
& PARSE_OPT_SHELL_EVAL
)
1163 fputs("EOF\n", outfile
);
1165 return PARSE_OPT_HELP
;
1168 void NORETURN
usage_with_options(const char * const *usagestr
,
1169 const struct option
*opts
)
1171 usage_with_options_internal(NULL
, usagestr
, opts
, 0, 1);
1175 void NORETURN
usage_msg_opt(const char *msg
,
1176 const char * const *usagestr
,
1177 const struct option
*options
)
1179 die_message("%s\n", msg
); /* The extra \n is intentional */
1180 usage_with_options(usagestr
, options
);
1183 void NORETURN
usage_msg_optf(const char * const fmt
,
1184 const char * const *usagestr
,
1185 const struct option
*options
, ...)
1187 struct strbuf msg
= STRBUF_INIT
;
1189 va_start(ap
, options
);
1190 strbuf_vaddf(&msg
, fmt
, ap
);
1193 usage_msg_opt(msg
.buf
, usagestr
, options
);
1196 void die_for_incompatible_opt4(int opt1
, const char *opt1_name
,
1197 int opt2
, const char *opt2_name
,
1198 int opt3
, const char *opt3_name
,
1199 int opt4
, const char *opt4_name
)
1202 const char *options
[4];
1205 options
[count
++] = opt1_name
;
1207 options
[count
++] = opt2_name
;
1209 options
[count
++] = opt3_name
;
1211 options
[count
++] = opt4_name
;
1214 die(_("options '%s', '%s', '%s', and '%s' cannot be used together"),
1215 opt1_name
, opt2_name
, opt3_name
, opt4_name
);
1218 die(_("options '%s', '%s', and '%s' cannot be used together"),
1219 options
[0], options
[1], options
[2]);
1222 die(_("options '%s' and '%s' cannot be used together"),
1223 options
[0], options
[1]);