1 #include "git-compat-util.h"
2 #include "parse-options.h"
7 #include "string-list.h"
10 static int disallow_abbreviated_options
;
18 static void optbug(const struct option
*opt
, const char *reason
)
20 if (opt
->long_name
&& opt
->short_name
)
21 bug("switch '%c' (--%s) %s", opt
->short_name
,
22 opt
->long_name
, reason
);
23 else if (opt
->long_name
)
24 bug("option '%s' %s", opt
->long_name
, reason
);
26 bug("switch '%c' %s", opt
->short_name
, reason
);
29 static const char *optname(const struct option
*opt
, enum opt_parsed flags
)
31 static struct strbuf sb
= STRBUF_INIT
;
34 if (flags
& OPT_SHORT
)
35 strbuf_addf(&sb
, "switch `%c'", opt
->short_name
);
36 else if (flags
& OPT_UNSET
)
37 strbuf_addf(&sb
, "option `no-%s'", opt
->long_name
);
38 else if (flags
== OPT_LONG
)
39 strbuf_addf(&sb
, "option `%s'", opt
->long_name
);
41 BUG("optname() got unknown flags %d", flags
);
46 static enum parse_opt_result
get_arg(struct parse_opt_ctx_t
*p
,
47 const struct option
*opt
,
48 enum opt_parsed flags
, const char **arg
)
53 } else if (p
->argc
== 1 && (opt
->flags
& PARSE_OPT_LASTARG_DEFAULT
)) {
54 *arg
= (const char *)opt
->defval
;
55 } else if (p
->argc
> 1) {
59 return error(_("%s requires a value"), optname(opt
, flags
));
63 static char *fix_filename(const char *prefix
, const char *file
)
68 return prefix_filename_except_for_dash(prefix
, file
);
71 static enum parse_opt_result
do_get_value(struct parse_opt_ctx_t
*p
,
72 const struct option
*opt
,
73 enum opt_parsed flags
,
77 const int unset
= flags
& OPT_UNSET
;
81 return error(_("%s takes no value"), optname(opt
, flags
));
82 if (unset
&& (opt
->flags
& PARSE_OPT_NONEG
))
83 return error(_("%s isn't available"), optname(opt
, flags
));
84 if (!(flags
& OPT_SHORT
) && p
->opt
&& (opt
->flags
& PARSE_OPT_NOARG
))
85 return error(_("%s takes no value"), optname(opt
, flags
));
88 case OPTION_LOWLEVEL_CALLBACK
:
89 return opt
->ll_callback(p
, opt
, NULL
, unset
);
93 *(int *)opt
->value
&= ~opt
->defval
;
95 *(int *)opt
->value
|= opt
->defval
;
100 *(int *)opt
->value
|= opt
->defval
;
102 *(int *)opt
->value
&= ~opt
->defval
;
107 BUG("BITOP can't have unset form");
108 *(int *)opt
->value
&= ~opt
->extra
;
109 *(int *)opt
->value
|= opt
->defval
;
113 if (*(int *)opt
->value
< 0)
114 *(int *)opt
->value
= 0;
115 *(int *)opt
->value
= unset
? 0 : *(int *)opt
->value
+ 1;
119 *(int *)opt
->value
= unset
? 0 : opt
->defval
;
124 *(const char **)opt
->value
= NULL
;
125 else if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
)
126 *(const char **)opt
->value
= (const char *)opt
->defval
;
128 return get_arg(p
, opt
, flags
, (const char **)opt
->value
);
131 case OPTION_FILENAME
:
135 FREE_AND_NULL(*(char **)opt
->value
);
141 else if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
)
142 value
= (const char *) opt
->defval
;
144 err
= get_arg(p
, opt
, flags
, &value
);
147 *(char **)opt
->value
= fix_filename(p
->prefix
, value
);
150 case OPTION_CALLBACK
:
152 const char *p_arg
= NULL
;
157 else if (opt
->flags
& PARSE_OPT_NOARG
)
159 else if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
)
161 else if (get_arg(p
, opt
, flags
, &arg
))
167 if (opt
->flags
& PARSE_OPT_CMDMODE
)
170 return (*opt
->callback
)(opt
, p_arg
, p_unset
) ? (-1) : 0;
172 return (*opt
->ll_callback
)(p
, opt
, p_arg
, p_unset
);
176 *(int *)opt
->value
= 0;
179 if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
) {
180 *(int *)opt
->value
= opt
->defval
;
183 if (get_arg(p
, opt
, flags
, &arg
))
186 return error(_("%s expects a numerical value"),
187 optname(opt
, flags
));
188 *(int *)opt
->value
= strtol(arg
, (char **)&s
, 10);
190 return error(_("%s expects a numerical value"),
191 optname(opt
, flags
));
194 case OPTION_MAGNITUDE
:
196 *(unsigned long *)opt
->value
= 0;
199 if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
) {
200 *(unsigned long *)opt
->value
= opt
->defval
;
203 if (get_arg(p
, opt
, flags
, &arg
))
205 if (!git_parse_ulong(arg
, opt
->value
))
206 return error(_("%s expects a non-negative integer value"
207 " with an optional k/m/g suffix"),
208 optname(opt
, flags
));
212 BUG("opt->type %d should not happen", opt
->type
);
216 struct parse_opt_cmdmode_list
{
217 int value
, *value_ptr
;
218 const struct option
*opt
;
220 enum opt_parsed flags
;
221 struct parse_opt_cmdmode_list
*next
;
224 static void build_cmdmode_list(struct parse_opt_ctx_t
*ctx
,
225 const struct option
*opts
)
227 ctx
->cmdmode_list
= NULL
;
229 for (; opts
->type
!= OPTION_END
; opts
++) {
230 struct parse_opt_cmdmode_list
*elem
= ctx
->cmdmode_list
;
231 int *value_ptr
= opts
->value
;
233 if (!(opts
->flags
& PARSE_OPT_CMDMODE
) || !value_ptr
)
236 while (elem
&& elem
->value_ptr
!= value_ptr
)
241 CALLOC_ARRAY(elem
, 1);
242 elem
->value_ptr
= value_ptr
;
243 elem
->value
= *value_ptr
;
244 elem
->next
= ctx
->cmdmode_list
;
245 ctx
->cmdmode_list
= elem
;
249 static char *optnamearg(const struct option
*opt
, const char *arg
,
250 enum opt_parsed flags
)
252 if (flags
& OPT_SHORT
)
253 return xstrfmt("-%c%s", opt
->short_name
, arg
? arg
: "");
254 return xstrfmt("--%s%s%s%s", flags
& OPT_UNSET
? "no-" : "",
255 opt
->long_name
, arg
? "=" : "", arg
? arg
: "");
258 static enum parse_opt_result
get_value(struct parse_opt_ctx_t
*p
,
259 const struct option
*opt
,
260 enum opt_parsed flags
)
262 const char *arg
= NULL
;
263 enum parse_opt_result result
= do_get_value(p
, opt
, flags
, &arg
);
264 struct parse_opt_cmdmode_list
*elem
= p
->cmdmode_list
;
265 char *opt_name
, *other_opt_name
;
267 for (; elem
; elem
= elem
->next
) {
268 if (*elem
->value_ptr
== elem
->value
)
272 (elem
->opt
->flags
| opt
->flags
) & PARSE_OPT_CMDMODE
)
278 elem
->value
= *elem
->value_ptr
;
284 opt_name
= optnamearg(opt
, arg
, flags
);
285 other_opt_name
= optnamearg(elem
->opt
, elem
->arg
, elem
->flags
);
286 error(_("options '%s' and '%s' cannot be used together"),
287 opt_name
, other_opt_name
);
289 free(other_opt_name
);
293 static enum parse_opt_result
parse_short_opt(struct parse_opt_ctx_t
*p
,
294 const struct option
*options
)
296 const struct option
*numopt
= NULL
;
298 for (; options
->type
!= OPTION_END
; options
++) {
299 if (options
->short_name
== *p
->opt
) {
300 p
->opt
= p
->opt
[1] ? p
->opt
+ 1 : NULL
;
301 return get_value(p
, options
, OPT_SHORT
);
305 * Handle the numerical option later, explicit one-digit
306 * options take precedence over it.
308 if (options
->type
== OPTION_NUMBER
)
311 if (numopt
&& isdigit(*p
->opt
)) {
316 while (isdigit(p
->opt
[len
]))
318 arg
= xmemdupz(p
->opt
, len
);
319 p
->opt
= p
->opt
[len
] ? p
->opt
+ len
: NULL
;
320 if (numopt
->callback
)
321 rc
= (*numopt
->callback
)(numopt
, arg
, 0) ? (-1) : 0;
323 rc
= (*numopt
->ll_callback
)(p
, numopt
, arg
, 0);
327 return PARSE_OPT_UNKNOWN
;
330 static int has_string(const char *it
, const char **array
)
333 if (!strcmp(it
, *(array
++)))
338 static int is_alias(struct parse_opt_ctx_t
*ctx
,
339 const struct option
*one_opt
,
340 const struct option
*another_opt
)
344 if (!ctx
->alias_groups
)
347 if (!one_opt
->long_name
|| !another_opt
->long_name
)
350 for (group
= ctx
->alias_groups
; *group
; group
+= 3) {
351 /* it and other are from the same family? */
352 if (has_string(one_opt
->long_name
, group
) &&
353 has_string(another_opt
->long_name
, group
))
359 struct parsed_option
{
360 const struct option
*option
;
361 enum opt_parsed flags
;
364 static void register_abbrev(struct parse_opt_ctx_t
*p
,
365 const struct option
*option
, enum opt_parsed flags
,
366 struct parsed_option
*abbrev
,
367 struct parsed_option
*ambiguous
)
369 if (p
->flags
& PARSE_OPT_KEEP_UNKNOWN_OPT
)
371 if (abbrev
->option
&&
372 !(abbrev
->flags
== flags
&& is_alias(p
, abbrev
->option
, option
))) {
374 * If this is abbreviated, it is
375 * ambiguous. So when there is no
376 * exact match later, we need to
379 ambiguous
->option
= abbrev
->option
;
380 ambiguous
->flags
= abbrev
->flags
;
382 abbrev
->option
= option
;
383 abbrev
->flags
= flags
;
386 static enum parse_opt_result
parse_long_opt(
387 struct parse_opt_ctx_t
*p
, const char *arg
,
388 const struct option
*options
)
390 const char *arg_end
= strchrnul(arg
, '=');
391 const char *arg_start
= arg
;
392 enum opt_parsed flags
= OPT_LONG
;
393 int arg_starts_with_no_no
= 0;
394 struct parsed_option abbrev
= { .option
= NULL
, .flags
= OPT_LONG
};
395 struct parsed_option ambiguous
= { .option
= NULL
, .flags
= OPT_LONG
};
397 if (skip_prefix(arg_start
, "no-", &arg_start
)) {
398 if (skip_prefix(arg_start
, "no-", &arg_start
))
399 arg_starts_with_no_no
= 1;
404 for (; options
->type
!= OPTION_END
; options
++) {
405 const char *rest
, *long_name
= options
->long_name
;
406 enum opt_parsed opt_flags
= OPT_LONG
;
407 int allow_unset
= !(options
->flags
& PARSE_OPT_NONEG
);
409 if (options
->type
== OPTION_SUBCOMMAND
)
414 if (skip_prefix(long_name
, "no-", &long_name
))
415 opt_flags
|= OPT_UNSET
;
416 else if (arg_starts_with_no_no
)
419 if (((flags
^ opt_flags
) & OPT_UNSET
) && !allow_unset
)
422 if (skip_prefix(arg_start
, long_name
, &rest
)) {
427 return get_value(p
, options
, flags
^ opt_flags
);
431 if (!strncmp(long_name
, arg_start
, arg_end
- arg_start
))
432 register_abbrev(p
, options
, flags
^ opt_flags
,
433 &abbrev
, &ambiguous
);
435 /* negated and abbreviated very much? */
436 if (allow_unset
&& starts_with("no-", arg
))
437 register_abbrev(p
, options
, OPT_UNSET
^ opt_flags
,
438 &abbrev
, &ambiguous
);
441 if (disallow_abbreviated_options
&& (ambiguous
.option
|| abbrev
.option
))
442 die("disallowed abbreviated or ambiguous option '%.*s'",
443 (int)(arg_end
- arg
), arg
);
445 if (ambiguous
.option
) {
446 error(_("ambiguous option: %s "
447 "(could be --%s%s or --%s%s)"),
449 (ambiguous
.flags
& OPT_UNSET
) ? "no-" : "",
450 ambiguous
.option
->long_name
,
451 (abbrev
.flags
& OPT_UNSET
) ? "no-" : "",
452 abbrev
.option
->long_name
);
453 return PARSE_OPT_HELP
;
457 p
->opt
= arg_end
+ 1;
458 return get_value(p
, abbrev
.option
, abbrev
.flags
);
460 return PARSE_OPT_UNKNOWN
;
463 static enum parse_opt_result
parse_nodash_opt(struct parse_opt_ctx_t
*p
,
465 const struct option
*options
)
467 for (; options
->type
!= OPTION_END
; options
++) {
468 if (!(options
->flags
& PARSE_OPT_NODASH
))
470 if (options
->short_name
== arg
[0] && arg
[1] == '\0')
471 return get_value(p
, options
, OPT_SHORT
);
473 return PARSE_OPT_ERROR
;
476 static enum parse_opt_result
parse_subcommand(const char *arg
,
477 const struct option
*options
)
479 for (; options
->type
!= OPTION_END
; options
++)
480 if (options
->type
== OPTION_SUBCOMMAND
&&
481 !strcmp(options
->long_name
, arg
)) {
482 *(parse_opt_subcommand_fn
**)options
->value
= options
->subcommand_fn
;
483 return PARSE_OPT_SUBCOMMAND
;
486 return PARSE_OPT_UNKNOWN
;
489 static void check_typos(const char *arg
, const struct option
*options
)
494 if (starts_with(arg
, "no-")) {
495 error(_("did you mean `--%s` (with two dashes)?"), arg
);
499 for (; options
->type
!= OPTION_END
; options
++) {
500 if (!options
->long_name
)
502 if (starts_with(options
->long_name
, arg
)) {
503 error(_("did you mean `--%s` (with two dashes)?"), arg
);
509 static void parse_options_check(const struct option
*opts
)
511 char short_opts
[128];
512 void *subcommand_value
= NULL
;
514 memset(short_opts
, '\0', sizeof(short_opts
));
515 for (; opts
->type
!= OPTION_END
; opts
++) {
516 if ((opts
->flags
& PARSE_OPT_LASTARG_DEFAULT
) &&
517 (opts
->flags
& PARSE_OPT_OPTARG
))
518 optbug(opts
, "uses incompatible flags "
519 "LASTARG_DEFAULT and OPTARG");
520 if (opts
->short_name
) {
521 if (0x7F <= opts
->short_name
)
522 optbug(opts
, "invalid short name");
523 else if (short_opts
[opts
->short_name
]++)
524 optbug(opts
, "short name already used");
526 if (opts
->flags
& PARSE_OPT_NODASH
&&
527 ((opts
->flags
& PARSE_OPT_OPTARG
) ||
528 !(opts
->flags
& PARSE_OPT_NOARG
) ||
529 !(opts
->flags
& PARSE_OPT_NONEG
) ||
531 optbug(opts
, "uses feature "
532 "not supported for dashless options");
533 if (opts
->type
== OPTION_SET_INT
&& !opts
->defval
&&
534 opts
->long_name
&& !(opts
->flags
& PARSE_OPT_NONEG
))
535 optbug(opts
, "OPTION_SET_INT 0 should not be negatable");
536 switch (opts
->type
) {
542 if ((opts
->flags
& PARSE_OPT_OPTARG
) ||
543 !(opts
->flags
& PARSE_OPT_NOARG
))
544 optbug(opts
, "should not accept an argument");
546 case OPTION_CALLBACK
:
547 if (!opts
->callback
&& !opts
->ll_callback
)
548 optbug(opts
, "OPTION_CALLBACK needs one callback");
549 else if (opts
->callback
&& opts
->ll_callback
)
550 optbug(opts
, "OPTION_CALLBACK can't have two callbacks");
552 case OPTION_LOWLEVEL_CALLBACK
:
553 if (!opts
->ll_callback
)
554 optbug(opts
, "OPTION_LOWLEVEL_CALLBACK needs a callback");
556 optbug(opts
, "OPTION_LOWLEVEL_CALLBACK needs no high level callback");
559 optbug(opts
, "OPT_ALIAS() should not remain at this point. "
560 "Are you using parse_options_step() directly?\n"
561 "That case is not supported yet.");
563 case OPTION_SUBCOMMAND
:
564 if (!opts
->value
|| !opts
->subcommand_fn
)
565 optbug(opts
, "OPTION_SUBCOMMAND needs a value and a subcommand function");
566 if (!subcommand_value
)
567 subcommand_value
= opts
->value
;
568 else if (subcommand_value
!= opts
->value
)
569 optbug(opts
, "all OPTION_SUBCOMMANDs need the same value");
572 ; /* ok. (usually accepts an argument) */
575 strcspn(opts
->argh
, " _") != strlen(opts
->argh
))
576 optbug(opts
, "multi-word argh should use dash to separate words");
578 BUG_if_bug("invalid 'struct option'");
581 static int has_subcommands(const struct option
*options
)
583 for (; options
->type
!= OPTION_END
; options
++)
584 if (options
->type
== OPTION_SUBCOMMAND
)
589 static void parse_options_start_1(struct parse_opt_ctx_t
*ctx
,
590 int argc
, const char **argv
, const char *prefix
,
591 const struct option
*options
,
592 enum parse_opt_flags flags
)
596 if (!(flags
& PARSE_OPT_ONE_SHOT
)) {
600 ctx
->total
= ctx
->argc
;
602 ctx
->prefix
= prefix
;
603 ctx
->cpidx
= ((flags
& PARSE_OPT_KEEP_ARGV0
) != 0);
605 ctx
->has_subcommands
= has_subcommands(options
);
606 if (!ctx
->has_subcommands
&& (flags
& PARSE_OPT_SUBCOMMAND_OPTIONAL
))
607 BUG("Using PARSE_OPT_SUBCOMMAND_OPTIONAL without subcommands");
608 if (ctx
->has_subcommands
) {
609 if (flags
& PARSE_OPT_STOP_AT_NON_OPTION
)
610 BUG("subcommands are incompatible with PARSE_OPT_STOP_AT_NON_OPTION");
611 if (!(flags
& PARSE_OPT_SUBCOMMAND_OPTIONAL
)) {
612 if (flags
& PARSE_OPT_KEEP_UNKNOWN_OPT
)
613 BUG("subcommands are incompatible with PARSE_OPT_KEEP_UNKNOWN_OPT unless in combination with PARSE_OPT_SUBCOMMAND_OPTIONAL");
614 if (flags
& PARSE_OPT_KEEP_DASHDASH
)
615 BUG("subcommands are incompatible with PARSE_OPT_KEEP_DASHDASH unless in combination with PARSE_OPT_SUBCOMMAND_OPTIONAL");
618 if ((flags
& PARSE_OPT_KEEP_UNKNOWN_OPT
) &&
619 (flags
& PARSE_OPT_STOP_AT_NON_OPTION
) &&
620 !(flags
& PARSE_OPT_ONE_SHOT
))
621 BUG("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
622 if ((flags
& PARSE_OPT_ONE_SHOT
) &&
623 (flags
& PARSE_OPT_KEEP_ARGV0
))
624 BUG("Can't keep argv0 if you don't have it");
625 parse_options_check(options
);
626 build_cmdmode_list(ctx
, options
);
629 void parse_options_start(struct parse_opt_ctx_t
*ctx
,
630 int argc
, const char **argv
, const char *prefix
,
631 const struct option
*options
,
632 enum parse_opt_flags flags
)
634 memset(ctx
, 0, sizeof(*ctx
));
635 parse_options_start_1(ctx
, argc
, argv
, prefix
, options
, flags
);
638 static void show_negated_gitcomp(const struct option
*opts
, int show_all
,
641 int printed_dashdash
= 0;
643 for (; opts
->type
!= OPTION_END
; opts
++) {
644 int has_unset_form
= 0;
647 if (!opts
->long_name
)
650 (opts
->flags
& (PARSE_OPT_HIDDEN
| PARSE_OPT_NOCOMPLETE
)))
652 if (opts
->flags
& PARSE_OPT_NONEG
)
655 switch (opts
->type
) {
657 case OPTION_FILENAME
:
659 case OPTION_MAGNITUDE
:
660 case OPTION_CALLBACK
:
673 if (skip_prefix(opts
->long_name
, "no-", &name
)) {
675 printf(" --%s", name
);
676 } else if (nr_noopts
>= 0) {
677 if (nr_noopts
&& !printed_dashdash
) {
679 printed_dashdash
= 1;
681 printf(" --no-%s", opts
->long_name
);
687 static int show_gitcomp(const struct option
*opts
, int show_all
)
689 const struct option
*original_opts
= opts
;
692 for (; opts
->type
!= OPTION_END
; opts
++) {
693 const char *prefix
= "--";
694 const char *suffix
= "";
696 if (!opts
->long_name
)
699 (opts
->flags
& (PARSE_OPT_HIDDEN
| PARSE_OPT_NOCOMPLETE
| PARSE_OPT_FROM_ALIAS
)))
702 switch (opts
->type
) {
703 case OPTION_SUBCOMMAND
:
709 case OPTION_FILENAME
:
711 case OPTION_MAGNITUDE
:
712 case OPTION_CALLBACK
:
713 if (opts
->flags
& PARSE_OPT_NOARG
)
715 if (opts
->flags
& PARSE_OPT_OPTARG
)
717 if (opts
->flags
& PARSE_OPT_LASTARG_DEFAULT
)
724 if (opts
->flags
& PARSE_OPT_COMP_ARG
)
726 if (starts_with(opts
->long_name
, "no-"))
728 printf("%s%s%s%s", opts
== original_opts
? "" : " ",
729 prefix
, opts
->long_name
, suffix
);
731 show_negated_gitcomp(original_opts
, show_all
, -1);
732 show_negated_gitcomp(original_opts
, show_all
, nr_noopts
);
734 return PARSE_OPT_COMPLETE
;
738 * Scan and may produce a new option[] array, which should be used
739 * instead of the original 'options'.
741 * Right now this is only used to preprocess and substitute
744 * The returned options should be freed using free_preprocessed_options.
746 static struct option
*preprocess_options(struct parse_opt_ctx_t
*ctx
,
747 const struct option
*options
)
749 struct option
*newopt
;
753 for (nr
= 0; options
[nr
].type
!= OPTION_END
; nr
++) {
754 if (options
[nr
].type
== OPTION_ALIAS
)
761 DUP_ARRAY(newopt
, options
, nr
+ 1);
763 /* each alias has two string pointers and NULL */
764 CALLOC_ARRAY(ctx
->alias_groups
, 3 * (nr_aliases
+ 1));
766 for (alias
= 0, i
= 0; i
< nr
; i
++) {
768 const char *long_name
;
770 struct strbuf help
= STRBUF_INIT
;
773 if (newopt
[i
].type
!= OPTION_ALIAS
)
776 short_name
= newopt
[i
].short_name
;
777 long_name
= newopt
[i
].long_name
;
778 source
= newopt
[i
].value
;
781 BUG("An alias must have long option name");
782 strbuf_addf(&help
, _("alias of --%s"), source
);
784 for (j
= 0; j
< nr
; j
++) {
785 const char *name
= options
[j
].long_name
;
787 if (!name
|| strcmp(name
, source
))
790 if (options
[j
].type
== OPTION_ALIAS
)
791 BUG("No please. Nested aliases are not supported.");
793 memcpy(newopt
+ i
, options
+ j
, sizeof(*newopt
));
794 newopt
[i
].short_name
= short_name
;
795 newopt
[i
].long_name
= long_name
;
796 newopt
[i
].help
= strbuf_detach(&help
, NULL
);
797 newopt
[i
].flags
|= PARSE_OPT_FROM_ALIAS
;
802 BUG("could not find source option '%s' of alias '%s'",
803 source
, newopt
[i
].long_name
);
804 ctx
->alias_groups
[alias
* 3 + 0] = newopt
[i
].long_name
;
805 ctx
->alias_groups
[alias
* 3 + 1] = options
[j
].long_name
;
806 ctx
->alias_groups
[alias
* 3 + 2] = NULL
;
813 static void free_preprocessed_options(struct option
*options
)
820 for (i
= 0; options
[i
].type
!= OPTION_END
; i
++) {
821 if (options
[i
].flags
& PARSE_OPT_FROM_ALIAS
)
822 free((void *)options
[i
].help
);
827 static enum parse_opt_result
usage_with_options_internal(struct parse_opt_ctx_t
*,
828 const char * const *,
829 const struct option
*,
832 enum parse_opt_result
parse_options_step(struct parse_opt_ctx_t
*ctx
,
833 const struct option
*options
,
834 const char * const usagestr
[])
836 int internal_help
= !(ctx
->flags
& PARSE_OPT_NO_INTERNAL_HELP
);
838 /* we must reset ->opt, unknown short option leave it dangling */
841 for (; ctx
->argc
; ctx
->argc
--, ctx
->argv
++) {
842 const char *arg
= ctx
->argv
[0];
844 if (ctx
->flags
& PARSE_OPT_ONE_SHOT
&&
845 ctx
->argc
!= ctx
->total
)
848 if (*arg
!= '-' || !arg
[1]) {
849 if (parse_nodash_opt(ctx
, arg
, options
) == 0)
851 if (!ctx
->has_subcommands
) {
852 if (ctx
->flags
& PARSE_OPT_STOP_AT_NON_OPTION
)
853 return PARSE_OPT_NON_OPTION
;
854 ctx
->out
[ctx
->cpidx
++] = ctx
->argv
[0];
857 switch (parse_subcommand(arg
, options
)) {
858 case PARSE_OPT_SUBCOMMAND
:
859 return PARSE_OPT_SUBCOMMAND
;
860 case PARSE_OPT_UNKNOWN
:
861 if (ctx
->flags
& PARSE_OPT_SUBCOMMAND_OPTIONAL
)
863 * arg is neither a short or long
864 * option nor a subcommand. Since
865 * this command has a default
866 * operation mode, we have to treat
867 * this arg and all remaining args
868 * as args meant to that default
870 * So we are done parsing.
872 return PARSE_OPT_DONE
;
873 error(_("unknown subcommand: `%s'"), arg
);
874 usage_with_options(usagestr
, options
);
875 case PARSE_OPT_COMPLETE
:
877 case PARSE_OPT_ERROR
:
879 case PARSE_OPT_NON_OPTION
:
881 BUG("parse_subcommand() cannot return these");
885 /* lone -h asks for help */
886 if (internal_help
&& ctx
->total
== 1 && !strcmp(arg
+ 1, "h"))
890 * lone --git-completion-helper and --git-completion-helper-all
891 * are asked by git-completion.bash
893 if (ctx
->total
== 1 && !strcmp(arg
, "--git-completion-helper"))
894 return show_gitcomp(options
, 0);
895 if (ctx
->total
== 1 && !strcmp(arg
, "--git-completion-helper-all"))
896 return show_gitcomp(options
, 1);
900 switch (parse_short_opt(ctx
, options
)) {
901 case PARSE_OPT_ERROR
:
902 return PARSE_OPT_ERROR
;
903 case PARSE_OPT_UNKNOWN
:
905 check_typos(arg
+ 1, options
);
906 if (internal_help
&& *ctx
->opt
== 'h')
909 case PARSE_OPT_NON_OPTION
:
910 case PARSE_OPT_SUBCOMMAND
:
912 case PARSE_OPT_COMPLETE
:
913 BUG("parse_short_opt() cannot return these");
918 check_typos(arg
+ 1, options
);
920 switch (parse_short_opt(ctx
, options
)) {
921 case PARSE_OPT_ERROR
:
922 return PARSE_OPT_ERROR
;
923 case PARSE_OPT_UNKNOWN
:
924 if (internal_help
&& *ctx
->opt
== 'h')
927 /* fake a short option thing to hide the fact that we may have
928 * started to parse aggregated stuff
930 * This is leaky, too bad.
932 ctx
->argv
[0] = xstrdup(ctx
->opt
- 1);
933 *(char *)ctx
->argv
[0] = '-';
935 case PARSE_OPT_NON_OPTION
:
936 case PARSE_OPT_SUBCOMMAND
:
937 case PARSE_OPT_COMPLETE
:
939 BUG("parse_short_opt() cannot return these");
947 if (!arg
[2] /* "--" */) {
948 if (!(ctx
->flags
& PARSE_OPT_KEEP_DASHDASH
)) {
953 } else if (!strcmp(arg
+ 2, "end-of-options")) {
954 if (!(ctx
->flags
& PARSE_OPT_KEEP_UNKNOWN_OPT
)) {
961 if (internal_help
&& !strcmp(arg
+ 2, "help-all"))
962 return usage_with_options_internal(ctx
, usagestr
, options
, 1, 0);
963 if (internal_help
&& !strcmp(arg
+ 2, "help"))
965 switch (parse_long_opt(ctx
, arg
+ 2, options
)) {
966 case PARSE_OPT_ERROR
:
967 return PARSE_OPT_ERROR
;
968 case PARSE_OPT_UNKNOWN
:
972 case PARSE_OPT_NON_OPTION
:
973 case PARSE_OPT_SUBCOMMAND
:
974 case PARSE_OPT_COMPLETE
:
975 BUG("parse_long_opt() cannot return these");
981 if (ctx
->flags
& PARSE_OPT_ONE_SHOT
)
983 if (ctx
->has_subcommands
&&
984 (ctx
->flags
& PARSE_OPT_SUBCOMMAND_OPTIONAL
) &&
985 (ctx
->flags
& PARSE_OPT_KEEP_UNKNOWN_OPT
)) {
987 * Found an unknown option given to a command with
988 * subcommands that has a default operation mode:
989 * we treat this option and all remaining args as
990 * arguments meant to that default operation mode.
991 * So we are done parsing.
993 return PARSE_OPT_DONE
;
995 if (!(ctx
->flags
& PARSE_OPT_KEEP_UNKNOWN_OPT
))
996 return PARSE_OPT_UNKNOWN
;
997 ctx
->out
[ctx
->cpidx
++] = ctx
->argv
[0];
1000 return PARSE_OPT_DONE
;
1003 return usage_with_options_internal(ctx
, usagestr
, options
, 0, 0);
1006 int parse_options_end(struct parse_opt_ctx_t
*ctx
)
1008 if (ctx
->flags
& PARSE_OPT_ONE_SHOT
)
1009 return ctx
->total
- ctx
->argc
;
1011 MOVE_ARRAY(ctx
->out
+ ctx
->cpidx
, ctx
->argv
, ctx
->argc
);
1012 ctx
->out
[ctx
->cpidx
+ ctx
->argc
] = NULL
;
1013 return ctx
->cpidx
+ ctx
->argc
;
1016 int parse_options(int argc
, const char **argv
,
1018 const struct option
*options
,
1019 const char * const usagestr
[],
1020 enum parse_opt_flags flags
)
1022 struct parse_opt_ctx_t ctx
;
1023 struct option
*real_options
;
1025 disallow_abbreviated_options
=
1026 git_env_bool("GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS", 0);
1028 memset(&ctx
, 0, sizeof(ctx
));
1029 real_options
= preprocess_options(&ctx
, options
);
1031 options
= real_options
;
1032 parse_options_start_1(&ctx
, argc
, argv
, prefix
, options
, flags
);
1033 switch (parse_options_step(&ctx
, options
, usagestr
)) {
1034 case PARSE_OPT_HELP
:
1035 case PARSE_OPT_ERROR
:
1037 case PARSE_OPT_COMPLETE
:
1039 case PARSE_OPT_NON_OPTION
:
1040 case PARSE_OPT_SUBCOMMAND
:
1042 case PARSE_OPT_DONE
:
1043 if (ctx
.has_subcommands
&&
1044 !(flags
& PARSE_OPT_SUBCOMMAND_OPTIONAL
)) {
1045 error(_("need a subcommand"));
1046 usage_with_options(usagestr
, options
);
1049 case PARSE_OPT_UNKNOWN
:
1050 if (ctx
.argv
[0][1] == '-') {
1051 error(_("unknown option `%s'"), ctx
.argv
[0] + 2);
1052 } else if (isascii(*ctx
.opt
)) {
1053 error(_("unknown switch `%c'"), *ctx
.opt
);
1055 error(_("unknown non-ascii option in string: `%s'"),
1058 usage_with_options(usagestr
, options
);
1061 precompose_argv_prefix(argc
, argv
, NULL
);
1062 free_preprocessed_options(real_options
);
1063 free(ctx
.alias_groups
);
1064 for (struct parse_opt_cmdmode_list
*elem
= ctx
.cmdmode_list
; elem
;) {
1065 struct parse_opt_cmdmode_list
*next
= elem
->next
;
1069 return parse_options_end(&ctx
);
1072 static int usage_argh(const struct option
*opts
, FILE *outfile
)
1075 int literal
= (opts
->flags
& PARSE_OPT_LITERAL_ARGHELP
) ||
1076 !opts
->argh
|| !!strpbrk(opts
->argh
, "()<>[]|");
1077 if (opts
->flags
& PARSE_OPT_OPTARG
)
1078 if (opts
->long_name
)
1079 s
= literal
? "[=%s]" : "[=<%s>]";
1081 s
= literal
? "[%s]" : "[<%s>]";
1083 s
= literal
? " %s" : " <%s>";
1084 return utf8_fprintf(outfile
, s
, opts
->argh
? _(opts
->argh
) : _("..."));
1087 static int usage_indent(FILE *outfile
)
1089 return fprintf(outfile
, " ");
1092 #define USAGE_OPTS_WIDTH 26
1094 static void usage_padding(FILE *outfile
, size_t pos
)
1096 if (pos
< USAGE_OPTS_WIDTH
)
1097 fprintf(outfile
, "%*s", USAGE_OPTS_WIDTH
- (int)pos
, "");
1099 fprintf(outfile
, "\n%*s", USAGE_OPTS_WIDTH
, "");
1102 static const struct option
*find_option_by_long_name(const struct option
*opts
,
1103 const char *long_name
)
1105 for (; opts
->type
!= OPTION_END
; opts
++) {
1106 if (opts
->long_name
&& !strcmp(opts
->long_name
, long_name
))
1112 static enum parse_opt_result
usage_with_options_internal(struct parse_opt_ctx_t
*ctx
,
1113 const char * const *usagestr
,
1114 const struct option
*opts
,
1117 const struct option
*all_opts
= opts
;
1118 FILE *outfile
= err
? stderr
: stdout
;
1121 const char *usage_prefix
= _("usage: %s");
1123 * The translation could be anything, but we can count on
1124 * msgfmt(1)'s --check option to have asserted that "%s" is in
1125 * the translation. So compute the length of the "usage: "
1126 * part. We are assuming that the translator wasn't overly
1127 * clever and used e.g. "%1$s" instead of "%s", there's only
1128 * one "%s" in "usage_prefix" above, so there's no reason to
1129 * do so even with a RTL language.
1131 size_t usage_len
= strlen(usage_prefix
) - strlen("%s");
1133 * TRANSLATORS: the colon here should align with the
1134 * one in "usage: %s" translation.
1136 const char *or_prefix
= _(" or: %s");
1138 * TRANSLATORS: You should only need to translate this format
1139 * string if your language is a RTL language (e.g. Arabic,
1140 * Hebrew etc.), not if it's a LTR language (e.g. German,
1141 * Russian, Chinese etc.).
1143 * When a translated usage string has an embedded "\n" it's
1144 * because options have wrapped to the next line. The line
1145 * after the "\n" will then be padded to align with the
1146 * command name, such as N_("git cmd [opt]\n<8
1147 * spaces>[opt2]"), where the 8 spaces are the same length as
1150 * This format string prints out that already-translated
1151 * line. The "%*s" is whitespace padding to account for the
1152 * padding at the start of the line that we add in this
1153 * function. The "%s" is a line in the (hopefully already
1154 * translated) N_() usage string, which contained embedded
1155 * newlines before we split it up.
1157 const char *usage_continued
= _("%*s%s");
1158 const char *prefix
= usage_prefix
;
1159 int saw_empty_line
= 0;
1162 return PARSE_OPT_HELP
;
1164 if (!err
&& ctx
&& ctx
->flags
& PARSE_OPT_SHELL_EVAL
)
1165 fprintf(outfile
, "cat <<\\EOF\n");
1168 const char *str
= _(*usagestr
++);
1169 struct string_list list
= STRING_LIST_INIT_DUP
;
1172 if (!saw_empty_line
&& !*str
)
1175 string_list_split(&list
, str
, '\n', -1);
1176 for (j
= 0; j
< list
.nr
; j
++) {
1177 const char *line
= list
.items
[j
].string
;
1179 if (saw_empty_line
&& *line
)
1180 fprintf_ln(outfile
, _(" %s"), line
);
1181 else if (saw_empty_line
)
1182 fputc('\n', outfile
);
1184 fprintf_ln(outfile
, prefix
, line
);
1186 fprintf_ln(outfile
, usage_continued
,
1187 (int)usage_len
, "", line
);
1189 string_list_clear(&list
, 0);
1196 for (; opts
->type
!= OPTION_END
; opts
++) {
1198 const char *cp
, *np
;
1199 const char *positive_name
= NULL
;
1201 if (opts
->type
== OPTION_SUBCOMMAND
)
1203 if (opts
->type
== OPTION_GROUP
) {
1204 fputc('\n', outfile
);
1207 fprintf(outfile
, "%s\n", _(opts
->help
));
1210 if (!full
&& (opts
->flags
& PARSE_OPT_HIDDEN
))
1214 fputc('\n', outfile
);
1218 pos
= usage_indent(outfile
);
1219 if (opts
->short_name
) {
1220 if (opts
->flags
& PARSE_OPT_NODASH
)
1221 pos
+= fprintf(outfile
, "%c", opts
->short_name
);
1223 pos
+= fprintf(outfile
, "-%c", opts
->short_name
);
1225 if (opts
->long_name
&& opts
->short_name
)
1226 pos
+= fprintf(outfile
, ", ");
1227 if (opts
->long_name
) {
1228 const char *long_name
= opts
->long_name
;
1229 if ((opts
->flags
& PARSE_OPT_NONEG
) ||
1230 skip_prefix(long_name
, "no-", &positive_name
))
1231 pos
+= fprintf(outfile
, "--%s", long_name
);
1233 pos
+= fprintf(outfile
, "--[no-]%s", long_name
);
1236 if (opts
->type
== OPTION_NUMBER
)
1237 pos
+= utf8_fprintf(outfile
, _("-NUM"));
1239 if ((opts
->flags
& PARSE_OPT_LITERAL_ARGHELP
) ||
1240 !(opts
->flags
& PARSE_OPT_NOARG
))
1241 pos
+= usage_argh(opts
, outfile
);
1243 if (opts
->type
== OPTION_ALIAS
) {
1244 usage_padding(outfile
, pos
);
1245 fprintf_ln(outfile
, _("alias of --%s"),
1246 (const char *)opts
->value
);
1250 for (cp
= opts
->help
? _(opts
->help
) : ""; *cp
; cp
= np
) {
1251 np
= strchrnul(cp
, '\n');
1254 usage_padding(outfile
, pos
);
1255 fwrite(cp
, 1, np
- cp
, outfile
);
1258 fputc('\n', outfile
);
1260 if (positive_name
) {
1261 if (find_option_by_long_name(all_opts
, positive_name
))
1263 pos
= usage_indent(outfile
);
1264 pos
+= fprintf(outfile
, "--%s", positive_name
);
1265 usage_padding(outfile
, pos
);
1266 fprintf_ln(outfile
, _("opposite of --no-%s"),
1270 fputc('\n', outfile
);
1272 if (!err
&& ctx
&& ctx
->flags
& PARSE_OPT_SHELL_EVAL
)
1273 fputs("EOF\n", outfile
);
1275 return PARSE_OPT_HELP
;
1278 void NORETURN
usage_with_options(const char * const *usagestr
,
1279 const struct option
*opts
)
1281 usage_with_options_internal(NULL
, usagestr
, opts
, 0, 1);
1285 void NORETURN
usage_msg_opt(const char *msg
,
1286 const char * const *usagestr
,
1287 const struct option
*options
)
1289 die_message("%s\n", msg
); /* The extra \n is intentional */
1290 usage_with_options(usagestr
, options
);
1293 void NORETURN
usage_msg_optf(const char * const fmt
,
1294 const char * const *usagestr
,
1295 const struct option
*options
, ...)
1297 struct strbuf msg
= STRBUF_INIT
;
1299 va_start(ap
, options
);
1300 strbuf_vaddf(&msg
, fmt
, ap
);
1303 usage_msg_opt(msg
.buf
, usagestr
, options
);
1306 void die_for_incompatible_opt4(int opt1
, const char *opt1_name
,
1307 int opt2
, const char *opt2_name
,
1308 int opt3
, const char *opt3_name
,
1309 int opt4
, const char *opt4_name
)
1312 const char *options
[4];
1315 options
[count
++] = opt1_name
;
1317 options
[count
++] = opt2_name
;
1319 options
[count
++] = opt3_name
;
1321 options
[count
++] = opt4_name
;
1324 die(_("options '%s', '%s', '%s', and '%s' cannot be used together"),
1325 opt1_name
, opt2_name
, opt3_name
, opt4_name
);
1328 die(_("options '%s', '%s', and '%s' cannot be used together"),
1329 options
[0], options
[1], options
[2]);
1332 die(_("options '%s' and '%s' cannot be used together"),
1333 options
[0], options
[1]);