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 void fix_filename(const char *prefix
, char **file
)
68 *file
= 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
:
134 *(const char **)opt
->value
= NULL
;
135 else if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
)
136 *(const char **)opt
->value
= (const char *)opt
->defval
;
138 err
= get_arg(p
, opt
, flags
, (const char **)opt
->value
);
141 fix_filename(p
->prefix
, (char **)opt
->value
);
144 case OPTION_CALLBACK
:
146 const char *p_arg
= NULL
;
151 else if (opt
->flags
& PARSE_OPT_NOARG
)
153 else if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
)
155 else if (get_arg(p
, opt
, flags
, &arg
))
161 if (opt
->flags
& PARSE_OPT_CMDMODE
)
164 return (*opt
->callback
)(opt
, p_arg
, p_unset
) ? (-1) : 0;
166 return (*opt
->ll_callback
)(p
, opt
, p_arg
, p_unset
);
170 *(int *)opt
->value
= 0;
173 if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
) {
174 *(int *)opt
->value
= opt
->defval
;
177 if (get_arg(p
, opt
, flags
, &arg
))
180 return error(_("%s expects a numerical value"),
181 optname(opt
, flags
));
182 *(int *)opt
->value
= strtol(arg
, (char **)&s
, 10);
184 return error(_("%s expects a numerical value"),
185 optname(opt
, flags
));
188 case OPTION_MAGNITUDE
:
190 *(unsigned long *)opt
->value
= 0;
193 if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
) {
194 *(unsigned long *)opt
->value
= opt
->defval
;
197 if (get_arg(p
, opt
, flags
, &arg
))
199 if (!git_parse_ulong(arg
, opt
->value
))
200 return error(_("%s expects a non-negative integer value"
201 " with an optional k/m/g suffix"),
202 optname(opt
, flags
));
206 BUG("opt->type %d should not happen", opt
->type
);
210 struct parse_opt_cmdmode_list
{
211 int value
, *value_ptr
;
212 const struct option
*opt
;
214 enum opt_parsed flags
;
215 struct parse_opt_cmdmode_list
*next
;
218 static void build_cmdmode_list(struct parse_opt_ctx_t
*ctx
,
219 const struct option
*opts
)
221 ctx
->cmdmode_list
= NULL
;
223 for (; opts
->type
!= OPTION_END
; opts
++) {
224 struct parse_opt_cmdmode_list
*elem
= ctx
->cmdmode_list
;
225 int *value_ptr
= opts
->value
;
227 if (!(opts
->flags
& PARSE_OPT_CMDMODE
) || !value_ptr
)
230 while (elem
&& elem
->value_ptr
!= value_ptr
)
235 CALLOC_ARRAY(elem
, 1);
236 elem
->value_ptr
= value_ptr
;
237 elem
->value
= *value_ptr
;
238 elem
->next
= ctx
->cmdmode_list
;
239 ctx
->cmdmode_list
= elem
;
243 static char *optnamearg(const struct option
*opt
, const char *arg
,
244 enum opt_parsed flags
)
246 if (flags
& OPT_SHORT
)
247 return xstrfmt("-%c%s", opt
->short_name
, arg
? arg
: "");
248 return xstrfmt("--%s%s%s%s", flags
& OPT_UNSET
? "no-" : "",
249 opt
->long_name
, arg
? "=" : "", arg
? arg
: "");
252 static enum parse_opt_result
get_value(struct parse_opt_ctx_t
*p
,
253 const struct option
*opt
,
254 enum opt_parsed flags
)
256 const char *arg
= NULL
;
257 enum parse_opt_result result
= do_get_value(p
, opt
, flags
, &arg
);
258 struct parse_opt_cmdmode_list
*elem
= p
->cmdmode_list
;
259 char *opt_name
, *other_opt_name
;
261 for (; elem
; elem
= elem
->next
) {
262 if (*elem
->value_ptr
== elem
->value
)
266 (elem
->opt
->flags
| opt
->flags
) & PARSE_OPT_CMDMODE
)
272 elem
->value
= *elem
->value_ptr
;
278 opt_name
= optnamearg(opt
, arg
, flags
);
279 other_opt_name
= optnamearg(elem
->opt
, elem
->arg
, elem
->flags
);
280 error(_("options '%s' and '%s' cannot be used together"),
281 opt_name
, other_opt_name
);
283 free(other_opt_name
);
287 static enum parse_opt_result
parse_short_opt(struct parse_opt_ctx_t
*p
,
288 const struct option
*options
)
290 const struct option
*numopt
= NULL
;
292 for (; options
->type
!= OPTION_END
; options
++) {
293 if (options
->short_name
== *p
->opt
) {
294 p
->opt
= p
->opt
[1] ? p
->opt
+ 1 : NULL
;
295 return get_value(p
, options
, OPT_SHORT
);
299 * Handle the numerical option later, explicit one-digit
300 * options take precedence over it.
302 if (options
->type
== OPTION_NUMBER
)
305 if (numopt
&& isdigit(*p
->opt
)) {
310 while (isdigit(p
->opt
[len
]))
312 arg
= xmemdupz(p
->opt
, len
);
313 p
->opt
= p
->opt
[len
] ? p
->opt
+ len
: NULL
;
314 if (numopt
->callback
)
315 rc
= (*numopt
->callback
)(numopt
, arg
, 0) ? (-1) : 0;
317 rc
= (*numopt
->ll_callback
)(p
, numopt
, arg
, 0);
321 return PARSE_OPT_UNKNOWN
;
324 static int has_string(const char *it
, const char **array
)
327 if (!strcmp(it
, *(array
++)))
332 static int is_alias(struct parse_opt_ctx_t
*ctx
,
333 const struct option
*one_opt
,
334 const struct option
*another_opt
)
338 if (!ctx
->alias_groups
)
341 if (!one_opt
->long_name
|| !another_opt
->long_name
)
344 for (group
= ctx
->alias_groups
; *group
; group
+= 3) {
345 /* it and other are from the same family? */
346 if (has_string(one_opt
->long_name
, group
) &&
347 has_string(another_opt
->long_name
, group
))
353 struct parsed_option
{
354 const struct option
*option
;
355 enum opt_parsed flags
;
358 static void register_abbrev(struct parse_opt_ctx_t
*p
,
359 const struct option
*option
, enum opt_parsed flags
,
360 struct parsed_option
*abbrev
,
361 struct parsed_option
*ambiguous
)
363 if (p
->flags
& PARSE_OPT_KEEP_UNKNOWN_OPT
)
365 if (abbrev
->option
&&
366 !(abbrev
->flags
== flags
&& is_alias(p
, abbrev
->option
, option
))) {
368 * If this is abbreviated, it is
369 * ambiguous. So when there is no
370 * exact match later, we need to
373 ambiguous
->option
= abbrev
->option
;
374 ambiguous
->flags
= abbrev
->flags
;
376 abbrev
->option
= option
;
377 abbrev
->flags
= flags
;
380 static enum parse_opt_result
parse_long_opt(
381 struct parse_opt_ctx_t
*p
, const char *arg
,
382 const struct option
*options
)
384 const char *arg_end
= strchrnul(arg
, '=');
385 const char *arg_start
= arg
;
386 enum opt_parsed flags
= OPT_LONG
;
387 int arg_starts_with_no_no
= 0;
388 struct parsed_option abbrev
= { .option
= NULL
, .flags
= OPT_LONG
};
389 struct parsed_option ambiguous
= { .option
= NULL
, .flags
= OPT_LONG
};
391 if (skip_prefix(arg_start
, "no-", &arg_start
)) {
392 if (skip_prefix(arg_start
, "no-", &arg_start
))
393 arg_starts_with_no_no
= 1;
398 for (; options
->type
!= OPTION_END
; options
++) {
399 const char *rest
, *long_name
= options
->long_name
;
400 enum opt_parsed opt_flags
= OPT_LONG
;
401 int allow_unset
= !(options
->flags
& PARSE_OPT_NONEG
);
403 if (options
->type
== OPTION_SUBCOMMAND
)
408 if (skip_prefix(long_name
, "no-", &long_name
))
409 opt_flags
|= OPT_UNSET
;
410 else if (arg_starts_with_no_no
)
413 if (((flags
^ opt_flags
) & OPT_UNSET
) && !allow_unset
)
416 if (skip_prefix(arg_start
, long_name
, &rest
)) {
421 return get_value(p
, options
, flags
^ opt_flags
);
425 if (!strncmp(long_name
, arg_start
, arg_end
- arg_start
))
426 register_abbrev(p
, options
, flags
^ opt_flags
,
427 &abbrev
, &ambiguous
);
429 /* negated and abbreviated very much? */
430 if (allow_unset
&& starts_with("no-", arg
))
431 register_abbrev(p
, options
, OPT_UNSET
^ opt_flags
,
432 &abbrev
, &ambiguous
);
435 if (disallow_abbreviated_options
&& (ambiguous
.option
|| abbrev
.option
))
436 die("disallowed abbreviated or ambiguous option '%.*s'",
437 (int)(arg_end
- arg
), arg
);
439 if (ambiguous
.option
) {
440 error(_("ambiguous option: %s "
441 "(could be --%s%s or --%s%s)"),
443 (ambiguous
.flags
& OPT_UNSET
) ? "no-" : "",
444 ambiguous
.option
->long_name
,
445 (abbrev
.flags
& OPT_UNSET
) ? "no-" : "",
446 abbrev
.option
->long_name
);
447 return PARSE_OPT_HELP
;
451 p
->opt
= arg_end
+ 1;
452 return get_value(p
, abbrev
.option
, abbrev
.flags
);
454 return PARSE_OPT_UNKNOWN
;
457 static enum parse_opt_result
parse_nodash_opt(struct parse_opt_ctx_t
*p
,
459 const struct option
*options
)
461 for (; options
->type
!= OPTION_END
; options
++) {
462 if (!(options
->flags
& PARSE_OPT_NODASH
))
464 if (options
->short_name
== arg
[0] && arg
[1] == '\0')
465 return get_value(p
, options
, OPT_SHORT
);
467 return PARSE_OPT_ERROR
;
470 static enum parse_opt_result
parse_subcommand(const char *arg
,
471 const struct option
*options
)
473 for (; options
->type
!= OPTION_END
; options
++)
474 if (options
->type
== OPTION_SUBCOMMAND
&&
475 !strcmp(options
->long_name
, arg
)) {
476 *(parse_opt_subcommand_fn
**)options
->value
= options
->subcommand_fn
;
477 return PARSE_OPT_SUBCOMMAND
;
480 return PARSE_OPT_UNKNOWN
;
483 static void check_typos(const char *arg
, const struct option
*options
)
488 if (starts_with(arg
, "no-")) {
489 error(_("did you mean `--%s` (with two dashes)?"), arg
);
493 for (; options
->type
!= OPTION_END
; options
++) {
494 if (!options
->long_name
)
496 if (starts_with(options
->long_name
, arg
)) {
497 error(_("did you mean `--%s` (with two dashes)?"), arg
);
503 static void parse_options_check(const struct option
*opts
)
505 char short_opts
[128];
506 void *subcommand_value
= NULL
;
508 memset(short_opts
, '\0', sizeof(short_opts
));
509 for (; opts
->type
!= OPTION_END
; opts
++) {
510 if ((opts
->flags
& PARSE_OPT_LASTARG_DEFAULT
) &&
511 (opts
->flags
& PARSE_OPT_OPTARG
))
512 optbug(opts
, "uses incompatible flags "
513 "LASTARG_DEFAULT and OPTARG");
514 if (opts
->short_name
) {
515 if (0x7F <= opts
->short_name
)
516 optbug(opts
, "invalid short name");
517 else if (short_opts
[opts
->short_name
]++)
518 optbug(opts
, "short name already used");
520 if (opts
->flags
& PARSE_OPT_NODASH
&&
521 ((opts
->flags
& PARSE_OPT_OPTARG
) ||
522 !(opts
->flags
& PARSE_OPT_NOARG
) ||
523 !(opts
->flags
& PARSE_OPT_NONEG
) ||
525 optbug(opts
, "uses feature "
526 "not supported for dashless options");
527 if (opts
->type
== OPTION_SET_INT
&& !opts
->defval
&&
528 opts
->long_name
&& !(opts
->flags
& PARSE_OPT_NONEG
))
529 optbug(opts
, "OPTION_SET_INT 0 should not be negatable");
530 switch (opts
->type
) {
536 if ((opts
->flags
& PARSE_OPT_OPTARG
) ||
537 !(opts
->flags
& PARSE_OPT_NOARG
))
538 optbug(opts
, "should not accept an argument");
540 case OPTION_CALLBACK
:
541 if (!opts
->callback
&& !opts
->ll_callback
)
542 optbug(opts
, "OPTION_CALLBACK needs one callback");
543 else if (opts
->callback
&& opts
->ll_callback
)
544 optbug(opts
, "OPTION_CALLBACK can't have two callbacks");
546 case OPTION_LOWLEVEL_CALLBACK
:
547 if (!opts
->ll_callback
)
548 optbug(opts
, "OPTION_LOWLEVEL_CALLBACK needs a callback");
550 optbug(opts
, "OPTION_LOWLEVEL_CALLBACK needs no high level callback");
553 optbug(opts
, "OPT_ALIAS() should not remain at this point. "
554 "Are you using parse_options_step() directly?\n"
555 "That case is not supported yet.");
557 case OPTION_SUBCOMMAND
:
558 if (!opts
->value
|| !opts
->subcommand_fn
)
559 optbug(opts
, "OPTION_SUBCOMMAND needs a value and a subcommand function");
560 if (!subcommand_value
)
561 subcommand_value
= opts
->value
;
562 else if (subcommand_value
!= opts
->value
)
563 optbug(opts
, "all OPTION_SUBCOMMANDs need the same value");
566 ; /* ok. (usually accepts an argument) */
569 strcspn(opts
->argh
, " _") != strlen(opts
->argh
))
570 optbug(opts
, "multi-word argh should use dash to separate words");
572 BUG_if_bug("invalid 'struct option'");
575 static int has_subcommands(const struct option
*options
)
577 for (; options
->type
!= OPTION_END
; options
++)
578 if (options
->type
== OPTION_SUBCOMMAND
)
583 static void parse_options_start_1(struct parse_opt_ctx_t
*ctx
,
584 int argc
, const char **argv
, const char *prefix
,
585 const struct option
*options
,
586 enum parse_opt_flags flags
)
590 if (!(flags
& PARSE_OPT_ONE_SHOT
)) {
594 ctx
->total
= ctx
->argc
;
596 ctx
->prefix
= prefix
;
597 ctx
->cpidx
= ((flags
& PARSE_OPT_KEEP_ARGV0
) != 0);
599 ctx
->has_subcommands
= has_subcommands(options
);
600 if (!ctx
->has_subcommands
&& (flags
& PARSE_OPT_SUBCOMMAND_OPTIONAL
))
601 BUG("Using PARSE_OPT_SUBCOMMAND_OPTIONAL without subcommands");
602 if (ctx
->has_subcommands
) {
603 if (flags
& PARSE_OPT_STOP_AT_NON_OPTION
)
604 BUG("subcommands are incompatible with PARSE_OPT_STOP_AT_NON_OPTION");
605 if (!(flags
& PARSE_OPT_SUBCOMMAND_OPTIONAL
)) {
606 if (flags
& PARSE_OPT_KEEP_UNKNOWN_OPT
)
607 BUG("subcommands are incompatible with PARSE_OPT_KEEP_UNKNOWN_OPT unless in combination with PARSE_OPT_SUBCOMMAND_OPTIONAL");
608 if (flags
& PARSE_OPT_KEEP_DASHDASH
)
609 BUG("subcommands are incompatible with PARSE_OPT_KEEP_DASHDASH unless in combination with PARSE_OPT_SUBCOMMAND_OPTIONAL");
612 if ((flags
& PARSE_OPT_KEEP_UNKNOWN_OPT
) &&
613 (flags
& PARSE_OPT_STOP_AT_NON_OPTION
) &&
614 !(flags
& PARSE_OPT_ONE_SHOT
))
615 BUG("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
616 if ((flags
& PARSE_OPT_ONE_SHOT
) &&
617 (flags
& PARSE_OPT_KEEP_ARGV0
))
618 BUG("Can't keep argv0 if you don't have it");
619 parse_options_check(options
);
620 build_cmdmode_list(ctx
, options
);
623 void parse_options_start(struct parse_opt_ctx_t
*ctx
,
624 int argc
, const char **argv
, const char *prefix
,
625 const struct option
*options
,
626 enum parse_opt_flags flags
)
628 memset(ctx
, 0, sizeof(*ctx
));
629 parse_options_start_1(ctx
, argc
, argv
, prefix
, options
, flags
);
632 static void show_negated_gitcomp(const struct option
*opts
, int show_all
,
635 int printed_dashdash
= 0;
637 for (; opts
->type
!= OPTION_END
; opts
++) {
638 int has_unset_form
= 0;
641 if (!opts
->long_name
)
644 (opts
->flags
& (PARSE_OPT_HIDDEN
| PARSE_OPT_NOCOMPLETE
)))
646 if (opts
->flags
& PARSE_OPT_NONEG
)
649 switch (opts
->type
) {
651 case OPTION_FILENAME
:
653 case OPTION_MAGNITUDE
:
654 case OPTION_CALLBACK
:
667 if (skip_prefix(opts
->long_name
, "no-", &name
)) {
669 printf(" --%s", name
);
670 } else if (nr_noopts
>= 0) {
671 if (nr_noopts
&& !printed_dashdash
) {
673 printed_dashdash
= 1;
675 printf(" --no-%s", opts
->long_name
);
681 static int show_gitcomp(const struct option
*opts
, int show_all
)
683 const struct option
*original_opts
= opts
;
686 for (; opts
->type
!= OPTION_END
; opts
++) {
687 const char *prefix
= "--";
688 const char *suffix
= "";
690 if (!opts
->long_name
)
693 (opts
->flags
& (PARSE_OPT_HIDDEN
| PARSE_OPT_NOCOMPLETE
| PARSE_OPT_FROM_ALIAS
)))
696 switch (opts
->type
) {
697 case OPTION_SUBCOMMAND
:
703 case OPTION_FILENAME
:
705 case OPTION_MAGNITUDE
:
706 case OPTION_CALLBACK
:
707 if (opts
->flags
& PARSE_OPT_NOARG
)
709 if (opts
->flags
& PARSE_OPT_OPTARG
)
711 if (opts
->flags
& PARSE_OPT_LASTARG_DEFAULT
)
718 if (opts
->flags
& PARSE_OPT_COMP_ARG
)
720 if (starts_with(opts
->long_name
, "no-"))
722 printf("%s%s%s%s", opts
== original_opts
? "" : " ",
723 prefix
, opts
->long_name
, suffix
);
725 show_negated_gitcomp(original_opts
, show_all
, -1);
726 show_negated_gitcomp(original_opts
, show_all
, nr_noopts
);
728 return PARSE_OPT_COMPLETE
;
732 * Scan and may produce a new option[] array, which should be used
733 * instead of the original 'options'.
735 * Right now this is only used to preprocess and substitute
738 * The returned options should be freed using free_preprocessed_options.
740 static struct option
*preprocess_options(struct parse_opt_ctx_t
*ctx
,
741 const struct option
*options
)
743 struct option
*newopt
;
747 for (nr
= 0; options
[nr
].type
!= OPTION_END
; nr
++) {
748 if (options
[nr
].type
== OPTION_ALIAS
)
755 DUP_ARRAY(newopt
, options
, nr
+ 1);
757 /* each alias has two string pointers and NULL */
758 CALLOC_ARRAY(ctx
->alias_groups
, 3 * (nr_aliases
+ 1));
760 for (alias
= 0, i
= 0; i
< nr
; i
++) {
762 const char *long_name
;
764 struct strbuf help
= STRBUF_INIT
;
767 if (newopt
[i
].type
!= OPTION_ALIAS
)
770 short_name
= newopt
[i
].short_name
;
771 long_name
= newopt
[i
].long_name
;
772 source
= newopt
[i
].value
;
775 BUG("An alias must have long option name");
776 strbuf_addf(&help
, _("alias of --%s"), source
);
778 for (j
= 0; j
< nr
; j
++) {
779 const char *name
= options
[j
].long_name
;
781 if (!name
|| strcmp(name
, source
))
784 if (options
[j
].type
== OPTION_ALIAS
)
785 BUG("No please. Nested aliases are not supported.");
787 memcpy(newopt
+ i
, options
+ j
, sizeof(*newopt
));
788 newopt
[i
].short_name
= short_name
;
789 newopt
[i
].long_name
= long_name
;
790 newopt
[i
].help
= strbuf_detach(&help
, NULL
);
791 newopt
[i
].flags
|= PARSE_OPT_FROM_ALIAS
;
796 BUG("could not find source option '%s' of alias '%s'",
797 source
, newopt
[i
].long_name
);
798 ctx
->alias_groups
[alias
* 3 + 0] = newopt
[i
].long_name
;
799 ctx
->alias_groups
[alias
* 3 + 1] = options
[j
].long_name
;
800 ctx
->alias_groups
[alias
* 3 + 2] = NULL
;
807 static void free_preprocessed_options(struct option
*options
)
814 for (i
= 0; options
[i
].type
!= OPTION_END
; i
++) {
815 if (options
[i
].flags
& PARSE_OPT_FROM_ALIAS
)
816 free((void *)options
[i
].help
);
821 static enum parse_opt_result
usage_with_options_internal(struct parse_opt_ctx_t
*,
822 const char * const *,
823 const struct option
*,
826 enum parse_opt_result
parse_options_step(struct parse_opt_ctx_t
*ctx
,
827 const struct option
*options
,
828 const char * const usagestr
[])
830 int internal_help
= !(ctx
->flags
& PARSE_OPT_NO_INTERNAL_HELP
);
832 /* we must reset ->opt, unknown short option leave it dangling */
835 for (; ctx
->argc
; ctx
->argc
--, ctx
->argv
++) {
836 const char *arg
= ctx
->argv
[0];
838 if (ctx
->flags
& PARSE_OPT_ONE_SHOT
&&
839 ctx
->argc
!= ctx
->total
)
842 if (*arg
!= '-' || !arg
[1]) {
843 if (parse_nodash_opt(ctx
, arg
, options
) == 0)
845 if (!ctx
->has_subcommands
) {
846 if (ctx
->flags
& PARSE_OPT_STOP_AT_NON_OPTION
)
847 return PARSE_OPT_NON_OPTION
;
848 ctx
->out
[ctx
->cpidx
++] = ctx
->argv
[0];
851 switch (parse_subcommand(arg
, options
)) {
852 case PARSE_OPT_SUBCOMMAND
:
853 return PARSE_OPT_SUBCOMMAND
;
854 case PARSE_OPT_UNKNOWN
:
855 if (ctx
->flags
& PARSE_OPT_SUBCOMMAND_OPTIONAL
)
857 * arg is neither a short or long
858 * option nor a subcommand. Since
859 * this command has a default
860 * operation mode, we have to treat
861 * this arg and all remaining args
862 * as args meant to that default
864 * So we are done parsing.
866 return PARSE_OPT_DONE
;
867 error(_("unknown subcommand: `%s'"), arg
);
868 usage_with_options(usagestr
, options
);
869 case PARSE_OPT_COMPLETE
:
871 case PARSE_OPT_ERROR
:
873 case PARSE_OPT_NON_OPTION
:
875 BUG("parse_subcommand() cannot return these");
879 /* lone -h asks for help */
880 if (internal_help
&& ctx
->total
== 1 && !strcmp(arg
+ 1, "h"))
884 * lone --git-completion-helper and --git-completion-helper-all
885 * are asked by git-completion.bash
887 if (ctx
->total
== 1 && !strcmp(arg
, "--git-completion-helper"))
888 return show_gitcomp(options
, 0);
889 if (ctx
->total
== 1 && !strcmp(arg
, "--git-completion-helper-all"))
890 return show_gitcomp(options
, 1);
894 switch (parse_short_opt(ctx
, options
)) {
895 case PARSE_OPT_ERROR
:
896 return PARSE_OPT_ERROR
;
897 case PARSE_OPT_UNKNOWN
:
899 check_typos(arg
+ 1, options
);
900 if (internal_help
&& *ctx
->opt
== 'h')
903 case PARSE_OPT_NON_OPTION
:
904 case PARSE_OPT_SUBCOMMAND
:
906 case PARSE_OPT_COMPLETE
:
907 BUG("parse_short_opt() cannot return these");
912 check_typos(arg
+ 1, options
);
914 switch (parse_short_opt(ctx
, options
)) {
915 case PARSE_OPT_ERROR
:
916 return PARSE_OPT_ERROR
;
917 case PARSE_OPT_UNKNOWN
:
918 if (internal_help
&& *ctx
->opt
== 'h')
921 /* fake a short option thing to hide the fact that we may have
922 * started to parse aggregated stuff
924 * This is leaky, too bad.
926 ctx
->argv
[0] = xstrdup(ctx
->opt
- 1);
927 *(char *)ctx
->argv
[0] = '-';
929 case PARSE_OPT_NON_OPTION
:
930 case PARSE_OPT_SUBCOMMAND
:
931 case PARSE_OPT_COMPLETE
:
933 BUG("parse_short_opt() cannot return these");
941 if (!arg
[2] /* "--" */) {
942 if (!(ctx
->flags
& PARSE_OPT_KEEP_DASHDASH
)) {
947 } else if (!strcmp(arg
+ 2, "end-of-options")) {
948 if (!(ctx
->flags
& PARSE_OPT_KEEP_UNKNOWN_OPT
)) {
955 if (internal_help
&& !strcmp(arg
+ 2, "help-all"))
956 return usage_with_options_internal(ctx
, usagestr
, options
, 1, 0);
957 if (internal_help
&& !strcmp(arg
+ 2, "help"))
959 switch (parse_long_opt(ctx
, arg
+ 2, options
)) {
960 case PARSE_OPT_ERROR
:
961 return PARSE_OPT_ERROR
;
962 case PARSE_OPT_UNKNOWN
:
966 case PARSE_OPT_NON_OPTION
:
967 case PARSE_OPT_SUBCOMMAND
:
968 case PARSE_OPT_COMPLETE
:
969 BUG("parse_long_opt() cannot return these");
975 if (ctx
->flags
& PARSE_OPT_ONE_SHOT
)
977 if (ctx
->has_subcommands
&&
978 (ctx
->flags
& PARSE_OPT_SUBCOMMAND_OPTIONAL
) &&
979 (ctx
->flags
& PARSE_OPT_KEEP_UNKNOWN_OPT
)) {
981 * Found an unknown option given to a command with
982 * subcommands that has a default operation mode:
983 * we treat this option and all remaining args as
984 * arguments meant to that default operation mode.
985 * So we are done parsing.
987 return PARSE_OPT_DONE
;
989 if (!(ctx
->flags
& PARSE_OPT_KEEP_UNKNOWN_OPT
))
990 return PARSE_OPT_UNKNOWN
;
991 ctx
->out
[ctx
->cpidx
++] = ctx
->argv
[0];
994 return PARSE_OPT_DONE
;
997 return usage_with_options_internal(ctx
, usagestr
, options
, 0, 0);
1000 int parse_options_end(struct parse_opt_ctx_t
*ctx
)
1002 if (ctx
->flags
& PARSE_OPT_ONE_SHOT
)
1003 return ctx
->total
- ctx
->argc
;
1005 MOVE_ARRAY(ctx
->out
+ ctx
->cpidx
, ctx
->argv
, ctx
->argc
);
1006 ctx
->out
[ctx
->cpidx
+ ctx
->argc
] = NULL
;
1007 return ctx
->cpidx
+ ctx
->argc
;
1010 int parse_options(int argc
, const char **argv
,
1012 const struct option
*options
,
1013 const char * const usagestr
[],
1014 enum parse_opt_flags flags
)
1016 struct parse_opt_ctx_t ctx
;
1017 struct option
*real_options
;
1019 disallow_abbreviated_options
=
1020 git_env_bool("GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS", 0);
1022 memset(&ctx
, 0, sizeof(ctx
));
1023 real_options
= preprocess_options(&ctx
, options
);
1025 options
= real_options
;
1026 parse_options_start_1(&ctx
, argc
, argv
, prefix
, options
, flags
);
1027 switch (parse_options_step(&ctx
, options
, usagestr
)) {
1028 case PARSE_OPT_HELP
:
1029 case PARSE_OPT_ERROR
:
1031 case PARSE_OPT_COMPLETE
:
1033 case PARSE_OPT_NON_OPTION
:
1034 case PARSE_OPT_SUBCOMMAND
:
1036 case PARSE_OPT_DONE
:
1037 if (ctx
.has_subcommands
&&
1038 !(flags
& PARSE_OPT_SUBCOMMAND_OPTIONAL
)) {
1039 error(_("need a subcommand"));
1040 usage_with_options(usagestr
, options
);
1043 case PARSE_OPT_UNKNOWN
:
1044 if (ctx
.argv
[0][1] == '-') {
1045 error(_("unknown option `%s'"), ctx
.argv
[0] + 2);
1046 } else if (isascii(*ctx
.opt
)) {
1047 error(_("unknown switch `%c'"), *ctx
.opt
);
1049 error(_("unknown non-ascii option in string: `%s'"),
1052 usage_with_options(usagestr
, options
);
1055 precompose_argv_prefix(argc
, argv
, NULL
);
1056 free_preprocessed_options(real_options
);
1057 free(ctx
.alias_groups
);
1058 for (struct parse_opt_cmdmode_list
*elem
= ctx
.cmdmode_list
; elem
;) {
1059 struct parse_opt_cmdmode_list
*next
= elem
->next
;
1063 return parse_options_end(&ctx
);
1066 static int usage_argh(const struct option
*opts
, FILE *outfile
)
1069 int literal
= (opts
->flags
& PARSE_OPT_LITERAL_ARGHELP
) ||
1070 !opts
->argh
|| !!strpbrk(opts
->argh
, "()<>[]|");
1071 if (opts
->flags
& PARSE_OPT_OPTARG
)
1072 if (opts
->long_name
)
1073 s
= literal
? "[=%s]" : "[=<%s>]";
1075 s
= literal
? "[%s]" : "[<%s>]";
1077 s
= literal
? " %s" : " <%s>";
1078 return utf8_fprintf(outfile
, s
, opts
->argh
? _(opts
->argh
) : _("..."));
1081 static int usage_indent(FILE *outfile
)
1083 return fprintf(outfile
, " ");
1086 #define USAGE_OPTS_WIDTH 26
1088 static void usage_padding(FILE *outfile
, size_t pos
)
1090 if (pos
< USAGE_OPTS_WIDTH
)
1091 fprintf(outfile
, "%*s", USAGE_OPTS_WIDTH
- (int)pos
, "");
1093 fprintf(outfile
, "\n%*s", USAGE_OPTS_WIDTH
, "");
1096 static const struct option
*find_option_by_long_name(const struct option
*opts
,
1097 const char *long_name
)
1099 for (; opts
->type
!= OPTION_END
; opts
++) {
1100 if (opts
->long_name
&& !strcmp(opts
->long_name
, long_name
))
1106 static enum parse_opt_result
usage_with_options_internal(struct parse_opt_ctx_t
*ctx
,
1107 const char * const *usagestr
,
1108 const struct option
*opts
,
1111 const struct option
*all_opts
= opts
;
1112 FILE *outfile
= err
? stderr
: stdout
;
1115 const char *usage_prefix
= _("usage: %s");
1117 * The translation could be anything, but we can count on
1118 * msgfmt(1)'s --check option to have asserted that "%s" is in
1119 * the translation. So compute the length of the "usage: "
1120 * part. We are assuming that the translator wasn't overly
1121 * clever and used e.g. "%1$s" instead of "%s", there's only
1122 * one "%s" in "usage_prefix" above, so there's no reason to
1123 * do so even with a RTL language.
1125 size_t usage_len
= strlen(usage_prefix
) - strlen("%s");
1127 * TRANSLATORS: the colon here should align with the
1128 * one in "usage: %s" translation.
1130 const char *or_prefix
= _(" or: %s");
1132 * TRANSLATORS: You should only need to translate this format
1133 * string if your language is a RTL language (e.g. Arabic,
1134 * Hebrew etc.), not if it's a LTR language (e.g. German,
1135 * Russian, Chinese etc.).
1137 * When a translated usage string has an embedded "\n" it's
1138 * because options have wrapped to the next line. The line
1139 * after the "\n" will then be padded to align with the
1140 * command name, such as N_("git cmd [opt]\n<8
1141 * spaces>[opt2]"), where the 8 spaces are the same length as
1144 * This format string prints out that already-translated
1145 * line. The "%*s" is whitespace padding to account for the
1146 * padding at the start of the line that we add in this
1147 * function. The "%s" is a line in the (hopefully already
1148 * translated) N_() usage string, which contained embedded
1149 * newlines before we split it up.
1151 const char *usage_continued
= _("%*s%s");
1152 const char *prefix
= usage_prefix
;
1153 int saw_empty_line
= 0;
1156 return PARSE_OPT_HELP
;
1158 if (!err
&& ctx
&& ctx
->flags
& PARSE_OPT_SHELL_EVAL
)
1159 fprintf(outfile
, "cat <<\\EOF\n");
1162 const char *str
= _(*usagestr
++);
1163 struct string_list list
= STRING_LIST_INIT_DUP
;
1166 if (!saw_empty_line
&& !*str
)
1169 string_list_split(&list
, str
, '\n', -1);
1170 for (j
= 0; j
< list
.nr
; j
++) {
1171 const char *line
= list
.items
[j
].string
;
1173 if (saw_empty_line
&& *line
)
1174 fprintf_ln(outfile
, _(" %s"), line
);
1175 else if (saw_empty_line
)
1176 fputc('\n', outfile
);
1178 fprintf_ln(outfile
, prefix
, line
);
1180 fprintf_ln(outfile
, usage_continued
,
1181 (int)usage_len
, "", line
);
1183 string_list_clear(&list
, 0);
1190 for (; opts
->type
!= OPTION_END
; opts
++) {
1192 const char *cp
, *np
;
1193 const char *positive_name
= NULL
;
1195 if (opts
->type
== OPTION_SUBCOMMAND
)
1197 if (opts
->type
== OPTION_GROUP
) {
1198 fputc('\n', outfile
);
1201 fprintf(outfile
, "%s\n", _(opts
->help
));
1204 if (!full
&& (opts
->flags
& PARSE_OPT_HIDDEN
))
1208 fputc('\n', outfile
);
1212 pos
= usage_indent(outfile
);
1213 if (opts
->short_name
) {
1214 if (opts
->flags
& PARSE_OPT_NODASH
)
1215 pos
+= fprintf(outfile
, "%c", opts
->short_name
);
1217 pos
+= fprintf(outfile
, "-%c", opts
->short_name
);
1219 if (opts
->long_name
&& opts
->short_name
)
1220 pos
+= fprintf(outfile
, ", ");
1221 if (opts
->long_name
) {
1222 const char *long_name
= opts
->long_name
;
1223 if ((opts
->flags
& PARSE_OPT_NONEG
) ||
1224 skip_prefix(long_name
, "no-", &positive_name
))
1225 pos
+= fprintf(outfile
, "--%s", long_name
);
1227 pos
+= fprintf(outfile
, "--[no-]%s", long_name
);
1230 if (opts
->type
== OPTION_NUMBER
)
1231 pos
+= utf8_fprintf(outfile
, _("-NUM"));
1233 if ((opts
->flags
& PARSE_OPT_LITERAL_ARGHELP
) ||
1234 !(opts
->flags
& PARSE_OPT_NOARG
))
1235 pos
+= usage_argh(opts
, outfile
);
1237 if (opts
->type
== OPTION_ALIAS
) {
1238 usage_padding(outfile
, pos
);
1239 fprintf_ln(outfile
, _("alias of --%s"),
1240 (const char *)opts
->value
);
1244 for (cp
= opts
->help
? _(opts
->help
) : ""; *cp
; cp
= np
) {
1245 np
= strchrnul(cp
, '\n');
1248 usage_padding(outfile
, pos
);
1249 fwrite(cp
, 1, np
- cp
, outfile
);
1252 fputc('\n', outfile
);
1254 if (positive_name
) {
1255 if (find_option_by_long_name(all_opts
, positive_name
))
1257 pos
= usage_indent(outfile
);
1258 pos
+= fprintf(outfile
, "--%s", positive_name
);
1259 usage_padding(outfile
, pos
);
1260 fprintf_ln(outfile
, _("opposite of --no-%s"),
1264 fputc('\n', outfile
);
1266 if (!err
&& ctx
&& ctx
->flags
& PARSE_OPT_SHELL_EVAL
)
1267 fputs("EOF\n", outfile
);
1269 return PARSE_OPT_HELP
;
1272 void NORETURN
usage_with_options(const char * const *usagestr
,
1273 const struct option
*opts
)
1275 usage_with_options_internal(NULL
, usagestr
, opts
, 0, 1);
1279 void NORETURN
usage_msg_opt(const char *msg
,
1280 const char * const *usagestr
,
1281 const struct option
*options
)
1283 die_message("%s\n", msg
); /* The extra \n is intentional */
1284 usage_with_options(usagestr
, options
);
1287 void NORETURN
usage_msg_optf(const char * const fmt
,
1288 const char * const *usagestr
,
1289 const struct option
*options
, ...)
1291 struct strbuf msg
= STRBUF_INIT
;
1293 va_start(ap
, options
);
1294 strbuf_vaddf(&msg
, fmt
, ap
);
1297 usage_msg_opt(msg
.buf
, usagestr
, options
);
1300 void die_for_incompatible_opt4(int opt1
, const char *opt1_name
,
1301 int opt2
, const char *opt2_name
,
1302 int opt3
, const char *opt3_name
,
1303 int opt4
, const char *opt4_name
)
1306 const char *options
[4];
1309 options
[count
++] = opt1_name
;
1311 options
[count
++] = opt2_name
;
1313 options
[count
++] = opt3_name
;
1315 options
[count
++] = opt4_name
;
1318 die(_("options '%s', '%s', '%s', and '%s' cannot be used together"),
1319 opt1_name
, opt2_name
, opt3_name
, opt4_name
);
1322 die(_("options '%s', '%s', and '%s' cannot be used together"),
1323 options
[0], options
[1], options
[2]);
1326 die(_("options '%s' and '%s' cannot be used together"),
1327 options
[0], options
[1]);