1 #include "git-compat-util.h"
2 #include "parse-options.h"
9 static int disallow_abbreviated_options
;
14 int optbug(const struct option
*opt
, const char *reason
)
18 return error("BUG: switch '%c' (--%s) %s",
19 opt
->short_name
, opt
->long_name
, reason
);
20 return error("BUG: option '%s' %s", opt
->long_name
, reason
);
22 return error("BUG: switch '%c' %s", opt
->short_name
, reason
);
25 static enum parse_opt_result
get_arg(struct parse_opt_ctx_t
*p
,
26 const struct option
*opt
,
27 int flags
, const char **arg
)
32 } else if (p
->argc
== 1 && (opt
->flags
& PARSE_OPT_LASTARG_DEFAULT
)) {
33 *arg
= (const char *)opt
->defval
;
34 } else if (p
->argc
> 1) {
38 return error(_("%s requires a value"), optname(opt
, flags
));
42 static void fix_filename(const char *prefix
, const char **file
)
44 if (!file
|| !*file
|| !prefix
|| is_absolute_path(*file
)
45 || !strcmp("-", *file
))
47 *file
= prefix_filename(prefix
, *file
);
50 static enum parse_opt_result
opt_command_mode_error(
51 const struct option
*opt
,
52 const struct option
*all_opts
,
55 const struct option
*that
;
56 struct strbuf that_name
= STRBUF_INIT
;
59 * Find the other option that was used to set the variable
60 * already, and report that this is not compatible with it.
62 for (that
= all_opts
; that
->type
!= OPTION_END
; that
++) {
64 !(that
->flags
& PARSE_OPT_CMDMODE
) ||
65 that
->value
!= opt
->value
||
66 that
->defval
!= *(int *)opt
->value
)
70 strbuf_addf(&that_name
, "--%s", that
->long_name
);
72 strbuf_addf(&that_name
, "-%c", that
->short_name
);
73 error(_("%s is incompatible with %s"),
74 optname(opt
, flags
), that_name
.buf
);
75 strbuf_release(&that_name
);
76 return PARSE_OPT_ERROR
;
78 return error(_("%s : incompatible with something else"),
82 static enum parse_opt_result
get_value(struct parse_opt_ctx_t
*p
,
83 const struct option
*opt
,
84 const struct option
*all_opts
,
88 const int unset
= flags
& OPT_UNSET
;
92 return error(_("%s takes no value"), optname(opt
, flags
));
93 if (unset
&& (opt
->flags
& PARSE_OPT_NONEG
))
94 return error(_("%s isn't available"), optname(opt
, flags
));
95 if (!(flags
& OPT_SHORT
) && p
->opt
&& (opt
->flags
& PARSE_OPT_NOARG
))
96 return error(_("%s takes no value"), optname(opt
, flags
));
99 * Giving the same mode option twice, although unnecessary,
100 * is not a grave error, so let it pass.
102 if ((opt
->flags
& PARSE_OPT_CMDMODE
) &&
103 *(int *)opt
->value
&& *(int *)opt
->value
!= opt
->defval
)
104 return opt_command_mode_error(opt
, all_opts
, flags
);
107 case OPTION_LOWLEVEL_CALLBACK
:
108 return opt
->ll_callback(p
, opt
, NULL
, unset
);
112 *(int *)opt
->value
&= ~opt
->defval
;
114 *(int *)opt
->value
|= opt
->defval
;
119 *(int *)opt
->value
|= opt
->defval
;
121 *(int *)opt
->value
&= ~opt
->defval
;
126 BUG("BITOP can't have unset form");
127 *(int *)opt
->value
&= ~opt
->extra
;
128 *(int *)opt
->value
|= opt
->defval
;
132 if (*(int *)opt
->value
< 0)
133 *(int *)opt
->value
= 0;
134 *(int *)opt
->value
= unset
? 0 : *(int *)opt
->value
+ 1;
138 *(int *)opt
->value
= unset
? 0 : opt
->defval
;
143 *(const char **)opt
->value
= NULL
;
144 else if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
)
145 *(const char **)opt
->value
= (const char *)opt
->defval
;
147 return get_arg(p
, opt
, flags
, (const char **)opt
->value
);
150 case OPTION_FILENAME
:
153 *(const char **)opt
->value
= NULL
;
154 else if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
)
155 *(const char **)opt
->value
= (const char *)opt
->defval
;
157 err
= get_arg(p
, opt
, flags
, (const char **)opt
->value
);
160 fix_filename(p
->prefix
, (const char **)opt
->value
);
163 case OPTION_CALLBACK
:
165 const char *p_arg
= NULL
;
170 else if (opt
->flags
& PARSE_OPT_NOARG
)
172 else if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
)
174 else if (get_arg(p
, opt
, flags
, &arg
))
181 return (*opt
->callback
)(opt
, p_arg
, p_unset
) ? (-1) : 0;
183 return (*opt
->ll_callback
)(p
, opt
, p_arg
, p_unset
);
187 *(int *)opt
->value
= 0;
190 if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
) {
191 *(int *)opt
->value
= opt
->defval
;
194 if (get_arg(p
, opt
, flags
, &arg
))
197 return error(_("%s expects a numerical value"),
198 optname(opt
, flags
));
199 *(int *)opt
->value
= strtol(arg
, (char **)&s
, 10);
201 return error(_("%s expects a numerical value"),
202 optname(opt
, flags
));
205 case OPTION_MAGNITUDE
:
207 *(unsigned long *)opt
->value
= 0;
210 if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
) {
211 *(unsigned long *)opt
->value
= opt
->defval
;
214 if (get_arg(p
, opt
, flags
, &arg
))
216 if (!git_parse_ulong(arg
, opt
->value
))
217 return error(_("%s expects a non-negative integer value"
218 " with an optional k/m/g suffix"),
219 optname(opt
, flags
));
223 BUG("opt->type %d should not happen", opt
->type
);
227 static enum parse_opt_result
parse_short_opt(struct parse_opt_ctx_t
*p
,
228 const struct option
*options
)
230 const struct option
*all_opts
= options
;
231 const struct option
*numopt
= NULL
;
233 for (; options
->type
!= OPTION_END
; options
++) {
234 if (options
->short_name
== *p
->opt
) {
235 p
->opt
= p
->opt
[1] ? p
->opt
+ 1 : NULL
;
236 return get_value(p
, options
, all_opts
, OPT_SHORT
);
240 * Handle the numerical option later, explicit one-digit
241 * options take precedence over it.
243 if (options
->type
== OPTION_NUMBER
)
246 if (numopt
&& isdigit(*p
->opt
)) {
251 while (isdigit(p
->opt
[len
]))
253 arg
= xmemdupz(p
->opt
, len
);
254 p
->opt
= p
->opt
[len
] ? p
->opt
+ len
: NULL
;
255 if (numopt
->callback
)
256 rc
= (*numopt
->callback
)(numopt
, arg
, 0) ? (-1) : 0;
258 rc
= (*numopt
->ll_callback
)(p
, numopt
, arg
, 0);
262 return PARSE_OPT_UNKNOWN
;
265 static int has_string(const char *it
, const char **array
)
268 if (!strcmp(it
, *(array
++)))
273 static int is_alias(struct parse_opt_ctx_t
*ctx
,
274 const struct option
*one_opt
,
275 const struct option
*another_opt
)
279 if (!ctx
->alias_groups
)
282 if (!one_opt
->long_name
|| !another_opt
->long_name
)
285 for (group
= ctx
->alias_groups
; *group
; group
+= 3) {
286 /* it and other are from the same family? */
287 if (has_string(one_opt
->long_name
, group
) &&
288 has_string(another_opt
->long_name
, group
))
294 static enum parse_opt_result
parse_long_opt(
295 struct parse_opt_ctx_t
*p
, const char *arg
,
296 const struct option
*options
)
298 const struct option
*all_opts
= options
;
299 const char *arg_end
= strchrnul(arg
, '=');
300 const struct option
*abbrev_option
= NULL
, *ambiguous_option
= NULL
;
301 int abbrev_flags
= 0, ambiguous_flags
= 0;
303 for (; options
->type
!= OPTION_END
; options
++) {
304 const char *rest
, *long_name
= options
->long_name
;
305 int flags
= 0, opt_flags
= 0;
311 if (!skip_prefix(arg
, long_name
, &rest
))
315 if (!(p
->flags
& PARSE_OPT_KEEP_UNKNOWN
) &&
316 !strncmp(long_name
, arg
, arg_end
- arg
)) {
319 !is_alias(p
, abbrev_option
, options
)) {
321 * If this is abbreviated, it is
322 * ambiguous. So when there is no
323 * exact match later, we need to
326 ambiguous_option
= abbrev_option
;
327 ambiguous_flags
= abbrev_flags
;
329 if (!(flags
& OPT_UNSET
) && *arg_end
)
330 p
->opt
= arg_end
+ 1;
331 abbrev_option
= options
;
332 abbrev_flags
= flags
^ opt_flags
;
335 /* negation allowed? */
336 if (options
->flags
& PARSE_OPT_NONEG
)
338 /* negated and abbreviated very much? */
339 if (starts_with("no-", arg
)) {
344 if (!starts_with(arg
, "no-")) {
345 if (skip_prefix(long_name
, "no-", &long_name
)) {
346 opt_flags
|= OPT_UNSET
;
352 if (!skip_prefix(arg
+ 3, long_name
, &rest
)) {
353 /* abbreviated and negated? */
354 if (starts_with(long_name
, arg
+ 3))
365 return get_value(p
, options
, all_opts
, flags
^ opt_flags
);
368 if (disallow_abbreviated_options
&& (ambiguous_option
|| abbrev_option
))
369 die("disallowed abbreviated or ambiguous option '%.*s'",
370 (int)(arg_end
- arg
), arg
);
372 if (ambiguous_option
) {
373 error(_("ambiguous option: %s "
374 "(could be --%s%s or --%s%s)"),
376 (ambiguous_flags
& OPT_UNSET
) ? "no-" : "",
377 ambiguous_option
->long_name
,
378 (abbrev_flags
& OPT_UNSET
) ? "no-" : "",
379 abbrev_option
->long_name
);
380 return PARSE_OPT_HELP
;
383 return get_value(p
, abbrev_option
, all_opts
, abbrev_flags
);
384 return PARSE_OPT_UNKNOWN
;
387 static int parse_nodash_opt(struct parse_opt_ctx_t
*p
, const char *arg
,
388 const struct option
*options
)
390 const struct option
*all_opts
= options
;
392 for (; options
->type
!= OPTION_END
; options
++) {
393 if (!(options
->flags
& PARSE_OPT_NODASH
))
395 if (options
->short_name
== arg
[0] && arg
[1] == '\0')
396 return get_value(p
, options
, all_opts
, OPT_SHORT
);
401 static void check_typos(const char *arg
, const struct option
*options
)
406 if (starts_with(arg
, "no-")) {
407 error(_("did you mean `--%s` (with two dashes)?"), arg
);
411 for (; options
->type
!= OPTION_END
; options
++) {
412 if (!options
->long_name
)
414 if (starts_with(options
->long_name
, arg
)) {
415 error(_("did you mean `--%s` (with two dashes)?"), arg
);
421 static void parse_options_check(const struct option
*opts
)
424 char short_opts
[128];
426 memset(short_opts
, '\0', sizeof(short_opts
));
427 for (; opts
->type
!= OPTION_END
; opts
++) {
428 if ((opts
->flags
& PARSE_OPT_LASTARG_DEFAULT
) &&
429 (opts
->flags
& PARSE_OPT_OPTARG
))
430 err
|= optbug(opts
, "uses incompatible flags "
431 "LASTARG_DEFAULT and OPTARG");
432 if (opts
->short_name
) {
433 if (0x7F <= opts
->short_name
)
434 err
|= optbug(opts
, "invalid short name");
435 else if (short_opts
[opts
->short_name
]++)
436 err
|= optbug(opts
, "short name already used");
438 if (opts
->flags
& PARSE_OPT_NODASH
&&
439 ((opts
->flags
& PARSE_OPT_OPTARG
) ||
440 !(opts
->flags
& PARSE_OPT_NOARG
) ||
441 !(opts
->flags
& PARSE_OPT_NONEG
) ||
443 err
|= optbug(opts
, "uses feature "
444 "not supported for dashless options");
445 switch (opts
->type
) {
451 if ((opts
->flags
& PARSE_OPT_OPTARG
) ||
452 !(opts
->flags
& PARSE_OPT_NOARG
))
453 err
|= optbug(opts
, "should not accept an argument");
455 case OPTION_CALLBACK
:
456 if (!opts
->callback
&& !opts
->ll_callback
)
457 BUG("OPTION_CALLBACK needs one callback");
458 if (opts
->callback
&& opts
->ll_callback
)
459 BUG("OPTION_CALLBACK can't have two callbacks");
461 case OPTION_LOWLEVEL_CALLBACK
:
462 if (!opts
->ll_callback
)
463 BUG("OPTION_LOWLEVEL_CALLBACK needs a callback");
465 BUG("OPTION_LOWLEVEL_CALLBACK needs no high level callback");
468 BUG("OPT_ALIAS() should not remain at this point. "
469 "Are you using parse_options_step() directly?\n"
470 "That case is not supported yet.");
472 ; /* ok. (usually accepts an argument) */
475 strcspn(opts
->argh
, " _") != strlen(opts
->argh
))
476 err
|= optbug(opts
, "multi-word argh should use dash to separate words");
482 static void parse_options_start_1(struct parse_opt_ctx_t
*ctx
,
483 int argc
, const char **argv
, const char *prefix
,
484 const struct option
*options
, int flags
)
488 if (!(flags
& PARSE_OPT_ONE_SHOT
)) {
492 ctx
->total
= ctx
->argc
;
494 ctx
->prefix
= prefix
;
495 ctx
->cpidx
= ((flags
& PARSE_OPT_KEEP_ARGV0
) != 0);
497 if ((flags
& PARSE_OPT_KEEP_UNKNOWN
) &&
498 (flags
& PARSE_OPT_STOP_AT_NON_OPTION
) &&
499 !(flags
& PARSE_OPT_ONE_SHOT
))
500 BUG("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
501 if ((flags
& PARSE_OPT_ONE_SHOT
) &&
502 (flags
& PARSE_OPT_KEEP_ARGV0
))
503 BUG("Can't keep argv0 if you don't have it");
504 parse_options_check(options
);
507 void parse_options_start(struct parse_opt_ctx_t
*ctx
,
508 int argc
, const char **argv
, const char *prefix
,
509 const struct option
*options
, int flags
)
511 memset(ctx
, 0, sizeof(*ctx
));
512 parse_options_start_1(ctx
, argc
, argv
, prefix
, options
, flags
);
515 static void show_negated_gitcomp(const struct option
*opts
, int show_all
,
518 int printed_dashdash
= 0;
520 for (; opts
->type
!= OPTION_END
; opts
++) {
521 int has_unset_form
= 0;
524 if (!opts
->long_name
)
527 (opts
->flags
& (PARSE_OPT_HIDDEN
| PARSE_OPT_NOCOMPLETE
)))
529 if (opts
->flags
& PARSE_OPT_NONEG
)
532 switch (opts
->type
) {
534 case OPTION_FILENAME
:
536 case OPTION_MAGNITUDE
:
537 case OPTION_CALLBACK
:
550 if (skip_prefix(opts
->long_name
, "no-", &name
)) {
552 printf(" --%s", name
);
553 } else if (nr_noopts
>= 0) {
554 if (nr_noopts
&& !printed_dashdash
) {
556 printed_dashdash
= 1;
558 printf(" --no-%s", opts
->long_name
);
564 static int show_gitcomp(const struct option
*opts
, int show_all
)
566 const struct option
*original_opts
= opts
;
569 for (; opts
->type
!= OPTION_END
; opts
++) {
570 const char *suffix
= "";
572 if (!opts
->long_name
)
575 (opts
->flags
& (PARSE_OPT_HIDDEN
| PARSE_OPT_NOCOMPLETE
| PARSE_OPT_FROM_ALIAS
)))
578 switch (opts
->type
) {
582 case OPTION_FILENAME
:
584 case OPTION_MAGNITUDE
:
585 case OPTION_CALLBACK
:
586 if (opts
->flags
& PARSE_OPT_NOARG
)
588 if (opts
->flags
& PARSE_OPT_OPTARG
)
590 if (opts
->flags
& PARSE_OPT_LASTARG_DEFAULT
)
597 if (opts
->flags
& PARSE_OPT_COMP_ARG
)
599 if (starts_with(opts
->long_name
, "no-"))
601 printf(" --%s%s", opts
->long_name
, suffix
);
603 show_negated_gitcomp(original_opts
, show_all
, -1);
604 show_negated_gitcomp(original_opts
, show_all
, nr_noopts
);
606 return PARSE_OPT_COMPLETE
;
610 * Scan and may produce a new option[] array, which should be used
611 * instead of the original 'options'.
613 * Right now this is only used to preprocess and substitute
616 * The returned options should be freed using free_preprocessed_options.
618 static struct option
*preprocess_options(struct parse_opt_ctx_t
*ctx
,
619 const struct option
*options
)
621 struct option
*newopt
;
625 for (nr
= 0; options
[nr
].type
!= OPTION_END
; nr
++) {
626 if (options
[nr
].type
== OPTION_ALIAS
)
633 ALLOC_ARRAY(newopt
, nr
+ 1);
634 COPY_ARRAY(newopt
, options
, nr
+ 1);
636 /* each alias has two string pointers and NULL */
637 CALLOC_ARRAY(ctx
->alias_groups
, 3 * (nr_aliases
+ 1));
639 for (alias
= 0, i
= 0; i
< nr
; i
++) {
641 const char *long_name
;
643 struct strbuf help
= STRBUF_INIT
;
646 if (newopt
[i
].type
!= OPTION_ALIAS
)
649 short_name
= newopt
[i
].short_name
;
650 long_name
= newopt
[i
].long_name
;
651 source
= newopt
[i
].value
;
654 BUG("An alias must have long option name");
655 strbuf_addf(&help
, _("alias of --%s"), source
);
657 for (j
= 0; j
< nr
; j
++) {
658 const char *name
= options
[j
].long_name
;
660 if (!name
|| strcmp(name
, source
))
663 if (options
[j
].type
== OPTION_ALIAS
)
664 BUG("No please. Nested aliases are not supported.");
666 memcpy(newopt
+ i
, options
+ j
, sizeof(*newopt
));
667 newopt
[i
].short_name
= short_name
;
668 newopt
[i
].long_name
= long_name
;
669 newopt
[i
].help
= strbuf_detach(&help
, NULL
);
670 newopt
[i
].flags
|= PARSE_OPT_FROM_ALIAS
;
675 BUG("could not find source option '%s' of alias '%s'",
676 source
, newopt
[i
].long_name
);
677 ctx
->alias_groups
[alias
* 3 + 0] = newopt
[i
].long_name
;
678 ctx
->alias_groups
[alias
* 3 + 1] = options
[j
].long_name
;
679 ctx
->alias_groups
[alias
* 3 + 2] = NULL
;
686 static void free_preprocessed_options(struct option
*options
)
693 for (i
= 0; options
[i
].type
!= OPTION_END
; i
++) {
694 if (options
[i
].flags
& PARSE_OPT_FROM_ALIAS
)
695 free((void *)options
[i
].help
);
700 static int usage_with_options_internal(struct parse_opt_ctx_t
*,
701 const char * const *,
702 const struct option
*, int, int);
704 int parse_options_step(struct parse_opt_ctx_t
*ctx
,
705 const struct option
*options
,
706 const char * const usagestr
[])
708 int internal_help
= !(ctx
->flags
& PARSE_OPT_NO_INTERNAL_HELP
);
710 /* we must reset ->opt, unknown short option leave it dangling */
713 for (; ctx
->argc
; ctx
->argc
--, ctx
->argv
++) {
714 const char *arg
= ctx
->argv
[0];
716 if (ctx
->flags
& PARSE_OPT_ONE_SHOT
&&
717 ctx
->argc
!= ctx
->total
)
720 if (*arg
!= '-' || !arg
[1]) {
721 if (parse_nodash_opt(ctx
, arg
, options
) == 0)
723 if (ctx
->flags
& PARSE_OPT_STOP_AT_NON_OPTION
)
724 return PARSE_OPT_NON_OPTION
;
725 ctx
->out
[ctx
->cpidx
++] = ctx
->argv
[0];
729 /* lone -h asks for help */
730 if (internal_help
&& ctx
->total
== 1 && !strcmp(arg
+ 1, "h"))
734 * lone --git-completion-helper and --git-completion-helper-all
735 * are asked by git-completion.bash
737 if (ctx
->total
== 1 && !strcmp(arg
, "--git-completion-helper"))
738 return show_gitcomp(options
, 0);
739 if (ctx
->total
== 1 && !strcmp(arg
, "--git-completion-helper-all"))
740 return show_gitcomp(options
, 1);
744 switch (parse_short_opt(ctx
, options
)) {
745 case PARSE_OPT_ERROR
:
746 return PARSE_OPT_ERROR
;
747 case PARSE_OPT_UNKNOWN
:
749 check_typos(arg
+ 1, options
);
750 if (internal_help
&& *ctx
->opt
== 'h')
753 case PARSE_OPT_NON_OPTION
:
755 case PARSE_OPT_COMPLETE
:
756 BUG("parse_short_opt() cannot return these");
761 check_typos(arg
+ 1, options
);
763 switch (parse_short_opt(ctx
, options
)) {
764 case PARSE_OPT_ERROR
:
765 return PARSE_OPT_ERROR
;
766 case PARSE_OPT_UNKNOWN
:
767 if (internal_help
&& *ctx
->opt
== 'h')
770 /* fake a short option thing to hide the fact that we may have
771 * started to parse aggregated stuff
773 * This is leaky, too bad.
775 ctx
->argv
[0] = xstrdup(ctx
->opt
- 1);
776 *(char *)ctx
->argv
[0] = '-';
778 case PARSE_OPT_NON_OPTION
:
779 case PARSE_OPT_COMPLETE
:
781 BUG("parse_short_opt() cannot return these");
789 if (!arg
[2] /* "--" */ ||
790 !strcmp(arg
+ 2, "end-of-options")) {
791 if (!(ctx
->flags
& PARSE_OPT_KEEP_DASHDASH
)) {
798 if (internal_help
&& !strcmp(arg
+ 2, "help-all"))
799 return usage_with_options_internal(ctx
, usagestr
, options
, 1, 0);
800 if (internal_help
&& !strcmp(arg
+ 2, "help"))
802 switch (parse_long_opt(ctx
, arg
+ 2, options
)) {
803 case PARSE_OPT_ERROR
:
804 return PARSE_OPT_ERROR
;
805 case PARSE_OPT_UNKNOWN
:
809 case PARSE_OPT_NON_OPTION
:
810 case PARSE_OPT_COMPLETE
:
811 BUG("parse_long_opt() cannot return these");
817 if (ctx
->flags
& PARSE_OPT_ONE_SHOT
)
819 if (!(ctx
->flags
& PARSE_OPT_KEEP_UNKNOWN
))
820 return PARSE_OPT_UNKNOWN
;
821 ctx
->out
[ctx
->cpidx
++] = ctx
->argv
[0];
824 return PARSE_OPT_DONE
;
827 return usage_with_options_internal(ctx
, usagestr
, options
, 0, 0);
830 int parse_options_end(struct parse_opt_ctx_t
*ctx
)
832 if (ctx
->flags
& PARSE_OPT_ONE_SHOT
)
833 return ctx
->total
- ctx
->argc
;
835 MOVE_ARRAY(ctx
->out
+ ctx
->cpidx
, ctx
->argv
, ctx
->argc
);
836 ctx
->out
[ctx
->cpidx
+ ctx
->argc
] = NULL
;
837 return ctx
->cpidx
+ ctx
->argc
;
840 int parse_options(int argc
, const char **argv
, const char *prefix
,
841 const struct option
*options
, const char * const usagestr
[],
844 struct parse_opt_ctx_t ctx
;
845 struct option
*real_options
;
847 disallow_abbreviated_options
=
848 git_env_bool("GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS", 0);
850 memset(&ctx
, 0, sizeof(ctx
));
851 real_options
= preprocess_options(&ctx
, options
);
853 options
= real_options
;
854 parse_options_start_1(&ctx
, argc
, argv
, prefix
, options
, flags
);
855 switch (parse_options_step(&ctx
, options
, usagestr
)) {
857 case PARSE_OPT_ERROR
:
859 case PARSE_OPT_COMPLETE
:
861 case PARSE_OPT_NON_OPTION
:
864 default: /* PARSE_OPT_UNKNOWN */
865 if (ctx
.argv
[0][1] == '-') {
866 error(_("unknown option `%s'"), ctx
.argv
[0] + 2);
867 } else if (isascii(*ctx
.opt
)) {
868 error(_("unknown switch `%c'"), *ctx
.opt
);
870 error(_("unknown non-ascii option in string: `%s'"),
873 usage_with_options(usagestr
, options
);
876 precompose_argv_prefix(argc
, argv
, NULL
);
877 free_preprocessed_options(real_options
);
878 free(ctx
.alias_groups
);
879 return parse_options_end(&ctx
);
882 static int usage_argh(const struct option
*opts
, FILE *outfile
)
885 int literal
= (opts
->flags
& PARSE_OPT_LITERAL_ARGHELP
) ||
886 !opts
->argh
|| !!strpbrk(opts
->argh
, "()<>[]|");
887 if (opts
->flags
& PARSE_OPT_OPTARG
)
889 s
= literal
? "[=%s]" : "[=<%s>]";
891 s
= literal
? "[%s]" : "[<%s>]";
893 s
= literal
? " %s" : " <%s>";
894 return utf8_fprintf(outfile
, s
, opts
->argh
? _(opts
->argh
) : _("..."));
897 #define USAGE_OPTS_WIDTH 24
900 static int usage_with_options_internal(struct parse_opt_ctx_t
*ctx
,
901 const char * const *usagestr
,
902 const struct option
*opts
, int full
, int err
)
904 FILE *outfile
= err
? stderr
: stdout
;
908 return PARSE_OPT_HELP
;
910 if (!err
&& ctx
&& ctx
->flags
& PARSE_OPT_SHELL_EVAL
)
911 fprintf(outfile
, "cat <<\\EOF\n");
913 fprintf_ln(outfile
, _("usage: %s"), _(*usagestr
++));
914 while (*usagestr
&& **usagestr
)
916 * TRANSLATORS: the colon here should align with the
917 * one in "usage: %s" translation.
919 fprintf_ln(outfile
, _(" or: %s"), _(*usagestr
++));
922 fprintf_ln(outfile
, _(" %s"), _(*usagestr
));
924 fputc('\n', outfile
);
930 for (; opts
->type
!= OPTION_END
; opts
++) {
934 if (opts
->type
== OPTION_GROUP
) {
935 fputc('\n', outfile
);
938 fprintf(outfile
, "%s\n", _(opts
->help
));
941 if (!full
&& (opts
->flags
& PARSE_OPT_HIDDEN
))
945 fputc('\n', outfile
);
949 pos
= fprintf(outfile
, " ");
950 if (opts
->short_name
) {
951 if (opts
->flags
& PARSE_OPT_NODASH
)
952 pos
+= fprintf(outfile
, "%c", opts
->short_name
);
954 pos
+= fprintf(outfile
, "-%c", opts
->short_name
);
956 if (opts
->long_name
&& opts
->short_name
)
957 pos
+= fprintf(outfile
, ", ");
959 pos
+= fprintf(outfile
, "--%s", opts
->long_name
);
960 if (opts
->type
== OPTION_NUMBER
)
961 pos
+= utf8_fprintf(outfile
, _("-NUM"));
963 if ((opts
->flags
& PARSE_OPT_LITERAL_ARGHELP
) ||
964 !(opts
->flags
& PARSE_OPT_NOARG
))
965 pos
+= usage_argh(opts
, outfile
);
967 if (pos
<= USAGE_OPTS_WIDTH
)
968 pad
= USAGE_OPTS_WIDTH
- pos
;
970 fputc('\n', outfile
);
971 pad
= USAGE_OPTS_WIDTH
;
973 if (opts
->type
== OPTION_ALIAS
) {
974 fprintf(outfile
, "%*s", pad
+ USAGE_GAP
, "");
975 fprintf_ln(outfile
, _("alias of --%s"),
976 (const char *)opts
->value
);
979 fprintf(outfile
, "%*s%s\n", pad
+ USAGE_GAP
, "", _(opts
->help
));
981 fputc('\n', outfile
);
983 if (!err
&& ctx
&& ctx
->flags
& PARSE_OPT_SHELL_EVAL
)
984 fputs("EOF\n", outfile
);
986 return PARSE_OPT_HELP
;
989 void NORETURN
usage_with_options(const char * const *usagestr
,
990 const struct option
*opts
)
992 usage_with_options_internal(NULL
, usagestr
, opts
, 0, 1);
996 void NORETURN
usage_msg_opt(const char *msg
,
997 const char * const *usagestr
,
998 const struct option
*options
)
1000 fprintf(stderr
, "fatal: %s\n\n", msg
);
1001 usage_with_options(usagestr
, options
);
1004 const char *optname(const struct option
*opt
, int flags
)
1006 static struct strbuf sb
= STRBUF_INIT
;
1009 if (flags
& OPT_SHORT
)
1010 strbuf_addf(&sb
, "switch `%c'", opt
->short_name
);
1011 else if (flags
& OPT_UNSET
)
1012 strbuf_addf(&sb
, "option `no-%s'", opt
->long_name
);
1014 strbuf_addf(&sb
, "option `%s'", opt
->long_name
);