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
->type
!= OPTION_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 case OPTION_LOWLEVEL_CALLBACK
:
100 return opt
->ll_callback(p
, opt
, NULL
, unset
);
104 *(int *)opt
->value
&= ~opt
->defval
;
106 *(int *)opt
->value
|= opt
->defval
;
111 *(int *)opt
->value
|= opt
->defval
;
113 *(int *)opt
->value
&= ~opt
->defval
;
118 BUG("BITOP can't have unset form");
119 *(int *)opt
->value
&= ~opt
->extra
;
120 *(int *)opt
->value
|= opt
->defval
;
124 if (*(int *)opt
->value
< 0)
125 *(int *)opt
->value
= 0;
126 *(int *)opt
->value
= unset
? 0 : *(int *)opt
->value
+ 1;
130 *(int *)opt
->value
= unset
? 0 : opt
->defval
;
135 * Giving the same mode option twice, although is unnecessary,
136 * is not a grave error, so let it pass.
138 if (*(int *)opt
->value
&& *(int *)opt
->value
!= opt
->defval
)
139 return opt_command_mode_error(opt
, all_opts
, flags
);
140 *(int *)opt
->value
= opt
->defval
;
145 *(const char **)opt
->value
= NULL
;
146 else if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
)
147 *(const char **)opt
->value
= (const char *)opt
->defval
;
149 return get_arg(p
, opt
, flags
, (const char **)opt
->value
);
152 case OPTION_FILENAME
:
155 *(const char **)opt
->value
= NULL
;
156 else if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
)
157 *(const char **)opt
->value
= (const char *)opt
->defval
;
159 err
= get_arg(p
, opt
, flags
, (const char **)opt
->value
);
162 fix_filename(p
->prefix
, (const char **)opt
->value
);
165 case OPTION_CALLBACK
:
167 const char *p_arg
= NULL
;
172 else if (opt
->flags
& PARSE_OPT_NOARG
)
174 else if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
)
176 else if (get_arg(p
, opt
, flags
, &arg
))
183 return (*opt
->callback
)(opt
, p_arg
, p_unset
) ? (-1) : 0;
185 return (*opt
->ll_callback
)(p
, opt
, p_arg
, p_unset
);
189 *(int *)opt
->value
= 0;
192 if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
) {
193 *(int *)opt
->value
= opt
->defval
;
196 if (get_arg(p
, opt
, flags
, &arg
))
198 *(int *)opt
->value
= strtol(arg
, (char **)&s
, 10);
200 return error(_("%s expects a numerical value"),
201 optname(opt
, flags
));
204 case OPTION_MAGNITUDE
:
206 *(unsigned long *)opt
->value
= 0;
209 if (opt
->flags
& PARSE_OPT_OPTARG
&& !p
->opt
) {
210 *(unsigned long *)opt
->value
= opt
->defval
;
213 if (get_arg(p
, opt
, flags
, &arg
))
215 if (!git_parse_ulong(arg
, opt
->value
))
216 return error(_("%s expects a non-negative integer value"
217 " with an optional k/m/g suffix"),
218 optname(opt
, flags
));
222 BUG("opt->type %d should not happen", opt
->type
);
226 static enum parse_opt_result
parse_short_opt(struct parse_opt_ctx_t
*p
,
227 const struct option
*options
)
229 const struct option
*all_opts
= options
;
230 const struct option
*numopt
= NULL
;
232 for (; options
->type
!= OPTION_END
; options
++) {
233 if (options
->short_name
== *p
->opt
) {
234 p
->opt
= p
->opt
[1] ? p
->opt
+ 1 : NULL
;
235 return get_value(p
, options
, all_opts
, OPT_SHORT
);
239 * Handle the numerical option later, explicit one-digit
240 * options take precedence over it.
242 if (options
->type
== OPTION_NUMBER
)
245 if (numopt
&& isdigit(*p
->opt
)) {
250 while (isdigit(p
->opt
[len
]))
252 arg
= xmemdupz(p
->opt
, len
);
253 p
->opt
= p
->opt
[len
] ? p
->opt
+ len
: NULL
;
254 if (numopt
->callback
)
255 rc
= (*numopt
->callback
)(numopt
, arg
, 0) ? (-1) : 0;
257 rc
= (*numopt
->ll_callback
)(p
, numopt
, arg
, 0);
261 return PARSE_OPT_UNKNOWN
;
264 static enum parse_opt_result
parse_long_opt(
265 struct parse_opt_ctx_t
*p
, const char *arg
,
266 const struct option
*options
)
268 const struct option
*all_opts
= options
;
269 const char *arg_end
= strchrnul(arg
, '=');
270 const struct option
*abbrev_option
= NULL
, *ambiguous_option
= NULL
;
271 int abbrev_flags
= 0, ambiguous_flags
= 0;
273 for (; options
->type
!= OPTION_END
; options
++) {
274 const char *rest
, *long_name
= options
->long_name
;
275 int flags
= 0, opt_flags
= 0;
281 if (!skip_prefix(arg
, long_name
, &rest
))
283 if (options
->type
== OPTION_ARGUMENT
) {
287 return error(_("%s takes no value"),
288 optname(options
, flags
));
292 *(int *)options
->value
= options
->defval
;
293 p
->out
[p
->cpidx
++] = arg
- 2;
294 return PARSE_OPT_DONE
;
298 if (!(p
->flags
& PARSE_OPT_KEEP_UNKNOWN
) &&
299 !strncmp(long_name
, arg
, arg_end
- arg
)) {
303 * If this is abbreviated, it is
304 * ambiguous. So when there is no
305 * exact match later, we need to
308 ambiguous_option
= abbrev_option
;
309 ambiguous_flags
= abbrev_flags
;
311 if (!(flags
& OPT_UNSET
) && *arg_end
)
312 p
->opt
= arg_end
+ 1;
313 abbrev_option
= options
;
314 abbrev_flags
= flags
^ opt_flags
;
317 /* negation allowed? */
318 if (options
->flags
& PARSE_OPT_NONEG
)
320 /* negated and abbreviated very much? */
321 if (starts_with("no-", arg
)) {
326 if (!starts_with(arg
, "no-")) {
327 if (starts_with(long_name
, "no-")) {
329 opt_flags
|= OPT_UNSET
;
335 if (!skip_prefix(arg
+ 3, long_name
, &rest
)) {
336 /* abbreviated and negated? */
337 if (starts_with(long_name
, arg
+ 3))
348 return get_value(p
, options
, all_opts
, flags
^ opt_flags
);
351 if (disallow_abbreviated_options
&& (ambiguous_option
|| abbrev_option
))
352 die("disallowed abbreviated or ambiguous option '%.*s'",
353 (int)(arg_end
- arg
), arg
);
355 if (ambiguous_option
) {
356 error(_("ambiguous option: %s "
357 "(could be --%s%s or --%s%s)"),
359 (ambiguous_flags
& OPT_UNSET
) ? "no-" : "",
360 ambiguous_option
->long_name
,
361 (abbrev_flags
& OPT_UNSET
) ? "no-" : "",
362 abbrev_option
->long_name
);
363 return PARSE_OPT_HELP
;
366 return get_value(p
, abbrev_option
, all_opts
, abbrev_flags
);
367 return PARSE_OPT_UNKNOWN
;
370 static int parse_nodash_opt(struct parse_opt_ctx_t
*p
, const char *arg
,
371 const struct option
*options
)
373 const struct option
*all_opts
= options
;
375 for (; options
->type
!= OPTION_END
; options
++) {
376 if (!(options
->flags
& PARSE_OPT_NODASH
))
378 if (options
->short_name
== arg
[0] && arg
[1] == '\0')
379 return get_value(p
, options
, all_opts
, OPT_SHORT
);
384 static void check_typos(const char *arg
, const struct option
*options
)
389 if (starts_with(arg
, "no-")) {
390 error(_("did you mean `--%s` (with two dashes ?)"), arg
);
394 for (; options
->type
!= OPTION_END
; options
++) {
395 if (!options
->long_name
)
397 if (starts_with(options
->long_name
, arg
)) {
398 error(_("did you mean `--%s` (with two dashes ?)"), arg
);
404 static void parse_options_check(const struct option
*opts
)
407 char short_opts
[128];
409 memset(short_opts
, '\0', sizeof(short_opts
));
410 for (; opts
->type
!= OPTION_END
; opts
++) {
411 if ((opts
->flags
& PARSE_OPT_LASTARG_DEFAULT
) &&
412 (opts
->flags
& PARSE_OPT_OPTARG
))
413 err
|= optbug(opts
, "uses incompatible flags "
414 "LASTARG_DEFAULT and OPTARG");
415 if (opts
->short_name
) {
416 if (0x7F <= opts
->short_name
)
417 err
|= optbug(opts
, "invalid short name");
418 else if (short_opts
[opts
->short_name
]++)
419 err
|= optbug(opts
, "short name already used");
421 if (opts
->flags
& PARSE_OPT_NODASH
&&
422 ((opts
->flags
& PARSE_OPT_OPTARG
) ||
423 !(opts
->flags
& PARSE_OPT_NOARG
) ||
424 !(opts
->flags
& PARSE_OPT_NONEG
) ||
426 err
|= optbug(opts
, "uses feature "
427 "not supported for dashless options");
428 switch (opts
->type
) {
434 if ((opts
->flags
& PARSE_OPT_OPTARG
) ||
435 !(opts
->flags
& PARSE_OPT_NOARG
))
436 err
|= optbug(opts
, "should not accept an argument");
438 case OPTION_CALLBACK
:
439 if (!opts
->callback
&& !opts
->ll_callback
)
440 BUG("OPTION_CALLBACK needs one callback");
441 if (opts
->callback
&& opts
->ll_callback
)
442 BUG("OPTION_CALLBACK can't have two callbacks");
444 case OPTION_LOWLEVEL_CALLBACK
:
445 if (!opts
->ll_callback
)
446 BUG("OPTION_LOWLEVEL_CALLBACK needs a callback");
448 BUG("OPTION_LOWLEVEL_CALLBACK needs no high level callback");
451 ; /* ok. (usually accepts an argument) */
454 strcspn(opts
->argh
, " _") != strlen(opts
->argh
))
455 err
|= optbug(opts
, "multi-word argh should use dash to separate words");
461 void parse_options_start(struct parse_opt_ctx_t
*ctx
,
462 int argc
, const char **argv
, const char *prefix
,
463 const struct option
*options
, int flags
)
465 memset(ctx
, 0, sizeof(*ctx
));
468 if (!(flags
& PARSE_OPT_ONE_SHOT
)) {
472 ctx
->total
= ctx
->argc
;
474 ctx
->prefix
= prefix
;
475 ctx
->cpidx
= ((flags
& PARSE_OPT_KEEP_ARGV0
) != 0);
477 if ((flags
& PARSE_OPT_KEEP_UNKNOWN
) &&
478 (flags
& PARSE_OPT_STOP_AT_NON_OPTION
) &&
479 !(flags
& PARSE_OPT_ONE_SHOT
))
480 BUG("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
481 if ((flags
& PARSE_OPT_ONE_SHOT
) &&
482 (flags
& PARSE_OPT_KEEP_ARGV0
))
483 BUG("Can't keep argv0 if you don't have it");
484 parse_options_check(options
);
487 static void show_negated_gitcomp(const struct option
*opts
, int nr_noopts
)
489 int printed_dashdash
= 0;
491 for (; opts
->type
!= OPTION_END
; opts
++) {
492 int has_unset_form
= 0;
495 if (!opts
->long_name
)
497 if (opts
->flags
& (PARSE_OPT_HIDDEN
| PARSE_OPT_NOCOMPLETE
))
499 if (opts
->flags
& PARSE_OPT_NONEG
)
502 switch (opts
->type
) {
504 case OPTION_FILENAME
:
506 case OPTION_MAGNITUDE
:
507 case OPTION_CALLBACK
:
520 if (skip_prefix(opts
->long_name
, "no-", &name
)) {
522 printf(" --%s", name
);
523 } else if (nr_noopts
>= 0) {
524 if (nr_noopts
&& !printed_dashdash
) {
526 printed_dashdash
= 1;
528 printf(" --no-%s", opts
->long_name
);
534 static int show_gitcomp(const struct option
*opts
)
536 const struct option
*original_opts
= opts
;
539 for (; opts
->type
!= OPTION_END
; opts
++) {
540 const char *suffix
= "";
542 if (!opts
->long_name
)
544 if (opts
->flags
& (PARSE_OPT_HIDDEN
| PARSE_OPT_NOCOMPLETE
))
547 switch (opts
->type
) {
551 case OPTION_FILENAME
:
553 case OPTION_MAGNITUDE
:
554 case OPTION_CALLBACK
:
555 if (opts
->flags
& PARSE_OPT_NOARG
)
557 if (opts
->flags
& PARSE_OPT_OPTARG
)
559 if (opts
->flags
& PARSE_OPT_LASTARG_DEFAULT
)
566 if (opts
->flags
& PARSE_OPT_COMP_ARG
)
568 if (starts_with(opts
->long_name
, "no-"))
570 printf(" --%s%s", opts
->long_name
, suffix
);
572 show_negated_gitcomp(original_opts
, -1);
573 show_negated_gitcomp(original_opts
, nr_noopts
);
575 return PARSE_OPT_COMPLETE
;
578 static int usage_with_options_internal(struct parse_opt_ctx_t
*,
579 const char * const *,
580 const struct option
*, int, int);
582 int parse_options_step(struct parse_opt_ctx_t
*ctx
,
583 const struct option
*options
,
584 const char * const usagestr
[])
586 int internal_help
= !(ctx
->flags
& PARSE_OPT_NO_INTERNAL_HELP
);
588 /* we must reset ->opt, unknown short option leave it dangling */
591 for (; ctx
->argc
; ctx
->argc
--, ctx
->argv
++) {
592 const char *arg
= ctx
->argv
[0];
594 if (ctx
->flags
& PARSE_OPT_ONE_SHOT
&&
595 ctx
->argc
!= ctx
->total
)
598 if (*arg
!= '-' || !arg
[1]) {
599 if (parse_nodash_opt(ctx
, arg
, options
) == 0)
601 if (ctx
->flags
& PARSE_OPT_STOP_AT_NON_OPTION
)
602 return PARSE_OPT_NON_OPTION
;
603 ctx
->out
[ctx
->cpidx
++] = ctx
->argv
[0];
607 /* lone -h asks for help */
608 if (internal_help
&& ctx
->total
== 1 && !strcmp(arg
+ 1, "h"))
611 /* lone --git-completion-helper is asked by git-completion.bash */
612 if (ctx
->total
== 1 && !strcmp(arg
+ 1, "-git-completion-helper"))
613 return show_gitcomp(options
);
617 switch (parse_short_opt(ctx
, options
)) {
618 case PARSE_OPT_ERROR
:
619 return PARSE_OPT_ERROR
;
620 case PARSE_OPT_UNKNOWN
:
622 check_typos(arg
+ 1, options
);
623 if (internal_help
&& *ctx
->opt
== 'h')
626 case PARSE_OPT_NON_OPTION
:
628 case PARSE_OPT_COMPLETE
:
629 BUG("parse_short_opt() cannot return these");
634 check_typos(arg
+ 1, options
);
636 switch (parse_short_opt(ctx
, options
)) {
637 case PARSE_OPT_ERROR
:
638 return PARSE_OPT_ERROR
;
639 case PARSE_OPT_UNKNOWN
:
640 if (internal_help
&& *ctx
->opt
== 'h')
643 /* fake a short option thing to hide the fact that we may have
644 * started to parse aggregated stuff
646 * This is leaky, too bad.
648 ctx
->argv
[0] = xstrdup(ctx
->opt
- 1);
649 *(char *)ctx
->argv
[0] = '-';
651 case PARSE_OPT_NON_OPTION
:
652 case PARSE_OPT_COMPLETE
:
654 BUG("parse_short_opt() cannot return these");
662 if (!arg
[2]) { /* "--" */
663 if (!(ctx
->flags
& PARSE_OPT_KEEP_DASHDASH
)) {
670 if (internal_help
&& !strcmp(arg
+ 2, "help-all"))
671 return usage_with_options_internal(ctx
, usagestr
, options
, 1, 0);
672 if (internal_help
&& !strcmp(arg
+ 2, "help"))
674 switch (parse_long_opt(ctx
, arg
+ 2, options
)) {
675 case PARSE_OPT_ERROR
:
676 return PARSE_OPT_ERROR
;
677 case PARSE_OPT_UNKNOWN
:
681 case PARSE_OPT_NON_OPTION
:
682 case PARSE_OPT_COMPLETE
:
683 BUG("parse_long_opt() cannot return these");
689 if (ctx
->flags
& PARSE_OPT_ONE_SHOT
)
691 if (!(ctx
->flags
& PARSE_OPT_KEEP_UNKNOWN
))
692 return PARSE_OPT_UNKNOWN
;
693 ctx
->out
[ctx
->cpidx
++] = ctx
->argv
[0];
696 return PARSE_OPT_DONE
;
699 return usage_with_options_internal(ctx
, usagestr
, options
, 0, 0);
702 int parse_options_end(struct parse_opt_ctx_t
*ctx
)
704 if (ctx
->flags
& PARSE_OPT_ONE_SHOT
)
705 return ctx
->total
- ctx
->argc
;
707 MOVE_ARRAY(ctx
->out
+ ctx
->cpidx
, ctx
->argv
, ctx
->argc
);
708 ctx
->out
[ctx
->cpidx
+ ctx
->argc
] = NULL
;
709 return ctx
->cpidx
+ ctx
->argc
;
712 int parse_options(int argc
, const char **argv
, const char *prefix
,
713 const struct option
*options
, const char * const usagestr
[],
716 struct parse_opt_ctx_t ctx
;
718 disallow_abbreviated_options
=
719 git_env_bool("GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS", 0);
721 parse_options_start(&ctx
, argc
, argv
, prefix
, options
, flags
);
722 switch (parse_options_step(&ctx
, options
, usagestr
)) {
724 case PARSE_OPT_ERROR
:
726 case PARSE_OPT_COMPLETE
:
728 case PARSE_OPT_NON_OPTION
:
731 default: /* PARSE_OPT_UNKNOWN */
732 if (ctx
.argv
[0][1] == '-') {
733 error(_("unknown option `%s'"), ctx
.argv
[0] + 2);
734 } else if (isascii(*ctx
.opt
)) {
735 error(_("unknown switch `%c'"), *ctx
.opt
);
737 error(_("unknown non-ascii option in string: `%s'"),
740 usage_with_options(usagestr
, options
);
743 precompose_argv(argc
, argv
);
744 return parse_options_end(&ctx
);
747 static int usage_argh(const struct option
*opts
, FILE *outfile
)
750 int literal
= (opts
->flags
& PARSE_OPT_LITERAL_ARGHELP
) ||
751 !opts
->argh
|| !!strpbrk(opts
->argh
, "()<>[]|");
752 if (opts
->flags
& PARSE_OPT_OPTARG
)
754 s
= literal
? "[=%s]" : "[=<%s>]";
756 s
= literal
? "[%s]" : "[<%s>]";
758 s
= literal
? " %s" : " <%s>";
759 return utf8_fprintf(outfile
, s
, opts
->argh
? _(opts
->argh
) : _("..."));
762 #define USAGE_OPTS_WIDTH 24
765 static int usage_with_options_internal(struct parse_opt_ctx_t
*ctx
,
766 const char * const *usagestr
,
767 const struct option
*opts
, int full
, int err
)
769 FILE *outfile
= err
? stderr
: stdout
;
773 return PARSE_OPT_HELP
;
775 if (!err
&& ctx
&& ctx
->flags
& PARSE_OPT_SHELL_EVAL
)
776 fprintf(outfile
, "cat <<\\EOF\n");
778 fprintf_ln(outfile
, _("usage: %s"), _(*usagestr
++));
779 while (*usagestr
&& **usagestr
)
781 * TRANSLATORS: the colon here should align with the
782 * one in "usage: %s" translation.
784 fprintf_ln(outfile
, _(" or: %s"), _(*usagestr
++));
787 fprintf_ln(outfile
, _(" %s"), _(*usagestr
));
789 fputc('\n', outfile
);
795 for (; opts
->type
!= OPTION_END
; opts
++) {
799 if (opts
->type
== OPTION_GROUP
) {
800 fputc('\n', outfile
);
803 fprintf(outfile
, "%s\n", _(opts
->help
));
806 if (!full
&& (opts
->flags
& PARSE_OPT_HIDDEN
))
810 fputc('\n', outfile
);
814 pos
= fprintf(outfile
, " ");
815 if (opts
->short_name
) {
816 if (opts
->flags
& PARSE_OPT_NODASH
)
817 pos
+= fprintf(outfile
, "%c", opts
->short_name
);
819 pos
+= fprintf(outfile
, "-%c", opts
->short_name
);
821 if (opts
->long_name
&& opts
->short_name
)
822 pos
+= fprintf(outfile
, ", ");
824 pos
+= fprintf(outfile
, "--%s", opts
->long_name
);
825 if (opts
->type
== OPTION_NUMBER
)
826 pos
+= utf8_fprintf(outfile
, _("-NUM"));
828 if ((opts
->flags
& PARSE_OPT_LITERAL_ARGHELP
) ||
829 !(opts
->flags
& PARSE_OPT_NOARG
))
830 pos
+= usage_argh(opts
, outfile
);
832 if (pos
<= USAGE_OPTS_WIDTH
)
833 pad
= USAGE_OPTS_WIDTH
- pos
;
835 fputc('\n', outfile
);
836 pad
= USAGE_OPTS_WIDTH
;
838 fprintf(outfile
, "%*s%s\n", pad
+ USAGE_GAP
, "", _(opts
->help
));
840 fputc('\n', outfile
);
842 if (!err
&& ctx
&& ctx
->flags
& PARSE_OPT_SHELL_EVAL
)
843 fputs("EOF\n", outfile
);
845 return PARSE_OPT_HELP
;
848 void NORETURN
usage_with_options(const char * const *usagestr
,
849 const struct option
*opts
)
851 usage_with_options_internal(NULL
, usagestr
, opts
, 0, 1);
855 void NORETURN
usage_msg_opt(const char *msg
,
856 const char * const *usagestr
,
857 const struct option
*options
)
859 fprintf(stderr
, "fatal: %s\n\n", msg
);
860 usage_with_options(usagestr
, options
);
863 const char *optname(const struct option
*opt
, int flags
)
865 static struct strbuf sb
= STRBUF_INIT
;
868 if (flags
& OPT_SHORT
)
869 strbuf_addf(&sb
, "switch `%c'", opt
->short_name
);
870 else if (flags
& OPT_UNSET
)
871 strbuf_addf(&sb
, "option `no-%s'", opt
->long_name
);
873 strbuf_addf(&sb
, "option `%s'", opt
->long_name
);