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))
199 return error(_("%s expects a numerical value"),
200 optname(opt, flags));
201 *(int *)opt->value = strtol(arg, (char **)&s, 10);
203 return error(_("%s expects a numerical value"),
204 optname(opt, flags));
207 case OPTION_MAGNITUDE:
209 *(unsigned long *)opt->value = 0;
212 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
213 *(unsigned long *)opt->value = opt->defval;
216 if (get_arg(p, opt, flags, &arg))
218 if (!git_parse_ulong(arg, opt->value))
219 return error(_("%s expects a non-negative integer value"
220 " with an optional k/m/g suffix"),
221 optname(opt, flags));
225 BUG("opt->type %d should not happen", opt->type);
229 static enum parse_opt_result parse_short_opt(struct parse_opt_ctx_t *p,
230 const struct option *options)
232 const struct option *all_opts = options;
233 const struct option *numopt = NULL;
235 for (; options->type != OPTION_END; options++) {
236 if (options->short_name == *p->opt) {
237 p->opt = p->opt[1] ? p->opt + 1 : NULL;
238 return get_value(p, options, all_opts, OPT_SHORT);
242 * Handle the numerical option later, explicit one-digit
243 * options take precedence over it.
245 if (options->type == OPTION_NUMBER)
248 if (numopt && isdigit(*p->opt)) {
253 while (isdigit(p->opt[len]))
255 arg = xmemdupz(p->opt, len);
256 p->opt = p->opt[len] ? p->opt + len : NULL;
257 if (numopt->callback)
258 rc = (*numopt->callback)(numopt, arg, 0) ? (-1) : 0;
260 rc = (*numopt->ll_callback)(p, numopt, arg, 0);
264 return PARSE_OPT_UNKNOWN;
267 static int has_string(const char *it, const char **array)
270 if (!strcmp(it, *(array++)))
275 static int is_alias(struct parse_opt_ctx_t *ctx,
276 const struct option *one_opt,
277 const struct option *another_opt)
281 if (!ctx->alias_groups)
284 if (!one_opt->long_name || !another_opt->long_name)
287 for (group = ctx->alias_groups; *group; group += 3) {
288 /* it and other are from the same family? */
289 if (has_string(one_opt->long_name, group) &&
290 has_string(another_opt->long_name, group))
296 static enum parse_opt_result parse_long_opt(
297 struct parse_opt_ctx_t *p, const char *arg,
298 const struct option *options)
300 const struct option *all_opts = options;
301 const char *arg_end = strchrnul(arg, '=');
302 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
303 int abbrev_flags = 0, ambiguous_flags = 0;
305 for (; options->type != OPTION_END; options++) {
306 const char *rest, *long_name = options->long_name;
307 int flags = 0, opt_flags = 0;
313 if (!skip_prefix(arg, long_name, &rest))
315 if (options->type == OPTION_ARGUMENT) {
319 return error(_("%s takes no value"),
320 optname(options, flags));
324 *(int *)options->value = options->defval;
325 p->out[p->cpidx++] = arg - 2;
326 return PARSE_OPT_DONE;
330 if (!(p->flags & PARSE_OPT_KEEP_UNKNOWN) &&
331 !strncmp(long_name, arg, arg_end - arg)) {
334 !is_alias(p, abbrev_option, options)) {
336 * If this is abbreviated, it is
337 * ambiguous. So when there is no
338 * exact match later, we need to
341 ambiguous_option = abbrev_option;
342 ambiguous_flags = abbrev_flags;
344 if (!(flags & OPT_UNSET) && *arg_end)
345 p->opt = arg_end + 1;
346 abbrev_option = options;
347 abbrev_flags = flags ^ opt_flags;
350 /* negation allowed? */
351 if (options->flags & PARSE_OPT_NONEG)
353 /* negated and abbreviated very much? */
354 if (starts_with("no-", arg)) {
359 if (!starts_with(arg, "no-")) {
360 if (skip_prefix(long_name, "no-", &long_name)) {
361 opt_flags |= OPT_UNSET;
367 if (!skip_prefix(arg + 3, long_name, &rest)) {
368 /* abbreviated and negated? */
369 if (starts_with(long_name, arg + 3))
380 return get_value(p, options, all_opts, flags ^ opt_flags);
383 if (disallow_abbreviated_options && (ambiguous_option || abbrev_option))
384 die("disallowed abbreviated or ambiguous option '%.*s'",
385 (int)(arg_end - arg), arg);
387 if (ambiguous_option) {
388 error(_("ambiguous option: %s "
389 "(could be --%s%s or --%s%s)"),
391 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
392 ambiguous_option->long_name,
393 (abbrev_flags & OPT_UNSET) ? "no-" : "",
394 abbrev_option->long_name);
395 return PARSE_OPT_HELP;
398 return get_value(p, abbrev_option, all_opts, abbrev_flags);
399 return PARSE_OPT_UNKNOWN;
402 static int parse_nodash_opt(struct parse_opt_ctx_t *p, const char *arg,
403 const struct option *options)
405 const struct option *all_opts = options;
407 for (; options->type != OPTION_END; options++) {
408 if (!(options->flags & PARSE_OPT_NODASH))
410 if (options->short_name == arg[0] && arg[1] == '\0')
411 return get_value(p, options, all_opts, OPT_SHORT);
416 static void check_typos(const char *arg, const struct option *options)
421 if (starts_with(arg, "no-")) {
422 error(_("did you mean `--%s` (with two dashes)?"), arg);
426 for (; options->type != OPTION_END; options++) {
427 if (!options->long_name)
429 if (starts_with(options->long_name, arg)) {
430 error(_("did you mean `--%s` (with two dashes)?"), arg);
436 static void parse_options_check(const struct option *opts)
439 char short_opts[128];
441 memset(short_opts, '\0', sizeof(short_opts));
442 for (; opts->type != OPTION_END; opts++) {
443 if ((opts->flags & PARSE_OPT_LASTARG_DEFAULT) &&
444 (opts->flags & PARSE_OPT_OPTARG))
445 err |= optbug(opts, "uses incompatible flags "
446 "LASTARG_DEFAULT and OPTARG");
447 if (opts->short_name) {
448 if (0x7F <= opts->short_name)
449 err |= optbug(opts, "invalid short name");
450 else if (short_opts[opts->short_name]++)
451 err |= optbug(opts, "short name already used");
453 if (opts->flags & PARSE_OPT_NODASH &&
454 ((opts->flags & PARSE_OPT_OPTARG) ||
455 !(opts->flags & PARSE_OPT_NOARG) ||
456 !(opts->flags & PARSE_OPT_NONEG) ||
458 err |= optbug(opts, "uses feature "
459 "not supported for dashless options");
460 switch (opts->type) {
466 if ((opts->flags & PARSE_OPT_OPTARG) ||
467 !(opts->flags & PARSE_OPT_NOARG))
468 err |= optbug(opts, "should not accept an argument");
470 case OPTION_CALLBACK:
471 if (!opts->callback && !opts->ll_callback)
472 BUG("OPTION_CALLBACK needs one callback");
473 if (opts->callback && opts->ll_callback)
474 BUG("OPTION_CALLBACK can't have two callbacks");
476 case OPTION_LOWLEVEL_CALLBACK:
477 if (!opts->ll_callback)
478 BUG("OPTION_LOWLEVEL_CALLBACK needs a callback");
480 BUG("OPTION_LOWLEVEL_CALLBACK needs no high level callback");
483 BUG("OPT_ALIAS() should not remain at this point. "
484 "Are you using parse_options_step() directly?\n"
485 "That case is not supported yet.");
487 ; /* ok. (usually accepts an argument) */
490 strcspn(opts->argh, " _") != strlen(opts->argh))
491 err |= optbug(opts, "multi-word argh should use dash to separate words");
497 static void parse_options_start_1(struct parse_opt_ctx_t *ctx,
498 int argc, const char **argv, const char *prefix,
499 const struct option *options, int flags)
503 if (!(flags & PARSE_OPT_ONE_SHOT)) {
507 ctx->total = ctx->argc;
509 ctx->prefix = prefix;
510 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
512 if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
513 (flags & PARSE_OPT_STOP_AT_NON_OPTION) &&
514 !(flags & PARSE_OPT_ONE_SHOT))
515 BUG("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
516 if ((flags & PARSE_OPT_ONE_SHOT) &&
517 (flags & PARSE_OPT_KEEP_ARGV0))
518 BUG("Can't keep argv0 if you don't have it");
519 parse_options_check(options);
522 void parse_options_start(struct parse_opt_ctx_t *ctx,
523 int argc, const char **argv, const char *prefix,
524 const struct option *options, int flags)
526 memset(ctx, 0, sizeof(*ctx));
527 parse_options_start_1(ctx, argc, argv, prefix, options, flags);
530 static void show_negated_gitcomp(const struct option *opts, int nr_noopts)
532 int printed_dashdash = 0;
534 for (; opts->type != OPTION_END; opts++) {
535 int has_unset_form = 0;
538 if (!opts->long_name)
540 if (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE))
542 if (opts->flags & PARSE_OPT_NONEG)
545 switch (opts->type) {
547 case OPTION_FILENAME:
549 case OPTION_MAGNITUDE:
550 case OPTION_CALLBACK:
563 if (skip_prefix(opts->long_name, "no-", &name)) {
565 printf(" --%s", name);
566 } else if (nr_noopts >= 0) {
567 if (nr_noopts && !printed_dashdash) {
569 printed_dashdash = 1;
571 printf(" --no-%s", opts->long_name);
577 static int show_gitcomp(const struct option *opts)
579 const struct option *original_opts = opts;
582 for (; opts->type != OPTION_END; opts++) {
583 const char *suffix = "";
585 if (!opts->long_name)
587 if (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE))
590 switch (opts->type) {
594 case OPTION_FILENAME:
596 case OPTION_MAGNITUDE:
597 case OPTION_CALLBACK:
598 if (opts->flags & PARSE_OPT_NOARG)
600 if (opts->flags & PARSE_OPT_OPTARG)
602 if (opts->flags & PARSE_OPT_LASTARG_DEFAULT)
609 if (opts->flags & PARSE_OPT_COMP_ARG)
611 if (starts_with(opts->long_name, "no-"))
613 printf(" --%s%s", opts->long_name, suffix);
615 show_negated_gitcomp(original_opts, -1);
616 show_negated_gitcomp(original_opts, nr_noopts);
618 return PARSE_OPT_COMPLETE;
622 * Scan and may produce a new option[] array, which should be used
623 * instead of the original 'options'.
625 * Right now this is only used to preprocess and substitute
628 static struct option *preprocess_options(struct parse_opt_ctx_t *ctx,
629 const struct option *options)
631 struct option *newopt;
635 for (nr = 0; options[nr].type != OPTION_END; nr++) {
636 if (options[nr].type == OPTION_ALIAS)
643 ALLOC_ARRAY(newopt, nr + 1);
644 COPY_ARRAY(newopt, options, nr + 1);
646 /* each alias has two string pointers and NULL */
647 CALLOC_ARRAY(ctx->alias_groups, 3 * (nr_aliases + 1));
649 for (alias = 0, i = 0; i < nr; i++) {
651 const char *long_name;
655 if (newopt[i].type != OPTION_ALIAS)
658 short_name = newopt[i].short_name;
659 long_name = newopt[i].long_name;
660 source = newopt[i].value;
663 BUG("An alias must have long option name");
665 for (j = 0; j < nr; j++) {
666 const char *name = options[j].long_name;
668 if (!name || strcmp(name, source))
671 if (options[j].type == OPTION_ALIAS)
672 BUG("No please. Nested aliases are not supported.");
675 * NEEDSWORK: this is a bit inconsistent because
676 * usage_with_options() on the original options[] will print
677 * help string as "alias of %s" but "git cmd -h" will
678 * print the original help string.
680 memcpy(newopt + i, options + j, sizeof(*newopt));
681 newopt[i].short_name = short_name;
682 newopt[i].long_name = long_name;
687 BUG("could not find source option '%s' of alias '%s'",
688 source, newopt[i].long_name);
689 ctx->alias_groups[alias * 3 + 0] = newopt[i].long_name;
690 ctx->alias_groups[alias * 3 + 1] = options[j].long_name;
691 ctx->alias_groups[alias * 3 + 2] = NULL;
698 static int usage_with_options_internal(struct parse_opt_ctx_t *,
699 const char * const *,
700 const struct option *, int, int);
702 int parse_options_step(struct parse_opt_ctx_t *ctx,
703 const struct option *options,
704 const char * const usagestr[])
706 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
708 /* we must reset ->opt, unknown short option leave it dangling */
711 for (; ctx->argc; ctx->argc--, ctx->argv++) {
712 const char *arg = ctx->argv[0];
714 if (ctx->flags & PARSE_OPT_ONE_SHOT &&
715 ctx->argc != ctx->total)
718 if (*arg != '-' || !arg[1]) {
719 if (parse_nodash_opt(ctx, arg, options) == 0)
721 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
722 return PARSE_OPT_NON_OPTION;
723 ctx->out[ctx->cpidx++] = ctx->argv[0];
727 /* lone -h asks for help */
728 if (internal_help && ctx->total == 1 && !strcmp(arg + 1, "h"))
731 /* lone --git-completion-helper is asked by git-completion.bash */
732 if (ctx->total == 1 && !strcmp(arg + 1, "-git-completion-helper"))
733 return show_gitcomp(options);
737 switch (parse_short_opt(ctx, options)) {
738 case PARSE_OPT_ERROR:
739 return PARSE_OPT_ERROR;
740 case PARSE_OPT_UNKNOWN:
742 check_typos(arg + 1, options);
743 if (internal_help && *ctx->opt == 'h')
746 case PARSE_OPT_NON_OPTION:
748 case PARSE_OPT_COMPLETE:
749 BUG("parse_short_opt() cannot return these");
754 check_typos(arg + 1, options);
756 switch (parse_short_opt(ctx, options)) {
757 case PARSE_OPT_ERROR:
758 return PARSE_OPT_ERROR;
759 case PARSE_OPT_UNKNOWN:
760 if (internal_help && *ctx->opt == 'h')
763 /* fake a short option thing to hide the fact that we may have
764 * started to parse aggregated stuff
766 * This is leaky, too bad.
768 ctx->argv[0] = xstrdup(ctx->opt - 1);
769 *(char *)ctx->argv[0] = '-';
771 case PARSE_OPT_NON_OPTION:
772 case PARSE_OPT_COMPLETE:
774 BUG("parse_short_opt() cannot return these");
782 if (!arg[2] /* "--" */ ||
783 !strcmp(arg + 2, "end-of-options")) {
784 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
791 if (internal_help && !strcmp(arg + 2, "help-all"))
792 return usage_with_options_internal(ctx, usagestr, options, 1, 0);
793 if (internal_help && !strcmp(arg + 2, "help"))
795 switch (parse_long_opt(ctx, arg + 2, options)) {
796 case PARSE_OPT_ERROR:
797 return PARSE_OPT_ERROR;
798 case PARSE_OPT_UNKNOWN:
802 case PARSE_OPT_NON_OPTION:
803 case PARSE_OPT_COMPLETE:
804 BUG("parse_long_opt() cannot return these");
810 if (ctx->flags & PARSE_OPT_ONE_SHOT)
812 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
813 return PARSE_OPT_UNKNOWN;
814 ctx->out[ctx->cpidx++] = ctx->argv[0];
817 return PARSE_OPT_DONE;
820 return usage_with_options_internal(ctx, usagestr, options, 0, 0);
823 int parse_options_end(struct parse_opt_ctx_t *ctx)
825 if (ctx->flags & PARSE_OPT_ONE_SHOT)
826 return ctx->total - ctx->argc;
828 MOVE_ARRAY(ctx->out + ctx->cpidx, ctx->argv, ctx->argc);
829 ctx->out[ctx->cpidx + ctx->argc] = NULL;
830 return ctx->cpidx + ctx->argc;
833 int parse_options(int argc, const char **argv, const char *prefix,
834 const struct option *options, const char * const usagestr[],
837 struct parse_opt_ctx_t ctx;
838 struct option *real_options;
840 disallow_abbreviated_options =
841 git_env_bool("GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS", 0);
843 memset(&ctx, 0, sizeof(ctx));
844 real_options = preprocess_options(&ctx, options);
846 options = real_options;
847 parse_options_start_1(&ctx, argc, argv, prefix, options, flags);
848 switch (parse_options_step(&ctx, options, usagestr)) {
850 case PARSE_OPT_ERROR:
852 case PARSE_OPT_COMPLETE:
854 case PARSE_OPT_NON_OPTION:
857 default: /* PARSE_OPT_UNKNOWN */
858 if (ctx.argv[0][1] == '-') {
859 error(_("unknown option `%s'"), ctx.argv[0] + 2);
860 } else if (isascii(*ctx.opt)) {
861 error(_("unknown switch `%c'"), *ctx.opt);
863 error(_("unknown non-ascii option in string: `%s'"),
866 usage_with_options(usagestr, options);
869 precompose_argv(argc, argv);
871 free(ctx.alias_groups);
872 return parse_options_end(&ctx);
875 static int usage_argh(const struct option *opts, FILE *outfile)
878 int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
879 !opts->argh || !!strpbrk(opts->argh, "()<>[]|");
880 if (opts->flags & PARSE_OPT_OPTARG)
882 s = literal ? "[=%s]" : "[=<%s>]";
884 s = literal ? "[%s]" : "[<%s>]";
886 s = literal ? " %s" : " <%s>";
887 return utf8_fprintf(outfile, s, opts->argh ? _(opts->argh) : _("..."));
890 #define USAGE_OPTS_WIDTH 24
893 static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
894 const char * const *usagestr,
895 const struct option *opts, int full, int err)
897 FILE *outfile = err ? stderr : stdout;
901 return PARSE_OPT_HELP;
903 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
904 fprintf(outfile, "cat <<\\EOF\n");
906 fprintf_ln(outfile, _("usage: %s"), _(*usagestr++));
907 while (*usagestr && **usagestr)
909 * TRANSLATORS: the colon here should align with the
910 * one in "usage: %s" translation.
912 fprintf_ln(outfile, _(" or: %s"), _(*usagestr++));
915 fprintf_ln(outfile, _(" %s"), _(*usagestr));
917 fputc('\n', outfile);
923 for (; opts->type != OPTION_END; opts++) {
927 if (opts->type == OPTION_GROUP) {
928 fputc('\n', outfile);
931 fprintf(outfile, "%s\n", _(opts->help));
934 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
938 fputc('\n', outfile);
942 pos = fprintf(outfile, " ");
943 if (opts->short_name) {
944 if (opts->flags & PARSE_OPT_NODASH)
945 pos += fprintf(outfile, "%c", opts->short_name);
947 pos += fprintf(outfile, "-%c", opts->short_name);
949 if (opts->long_name && opts->short_name)
950 pos += fprintf(outfile, ", ");
952 pos += fprintf(outfile, "--%s", opts->long_name);
953 if (opts->type == OPTION_NUMBER)
954 pos += utf8_fprintf(outfile, _("-NUM"));
956 if ((opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
957 !(opts->flags & PARSE_OPT_NOARG))
958 pos += usage_argh(opts, outfile);
960 if (pos <= USAGE_OPTS_WIDTH)
961 pad = USAGE_OPTS_WIDTH - pos;
963 fputc('\n', outfile);
964 pad = USAGE_OPTS_WIDTH;
966 if (opts->type == OPTION_ALIAS) {
967 fprintf(outfile, "%*s", pad + USAGE_GAP, "");
968 fprintf_ln(outfile, _("alias of --%s"),
969 (const char *)opts->value);
972 fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", _(opts->help));
974 fputc('\n', outfile);
976 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
977 fputs("EOF\n", outfile);
979 return PARSE_OPT_HELP;
982 void NORETURN usage_with_options(const char * const *usagestr,
983 const struct option *opts)
985 usage_with_options_internal(NULL, usagestr, opts, 0, 1);
989 void NORETURN usage_msg_opt(const char *msg,
990 const char * const *usagestr,
991 const struct option *options)
993 fprintf(stderr, "fatal: %s\n\n", msg);
994 usage_with_options(usagestr, options);
997 const char *optname(const struct option *opt, int flags)
999 static struct strbuf sb = STRBUF_INIT;
1002 if (flags & OPT_SHORT)
1003 strbuf_addf(&sb, "switch `%c'", opt->short_name);
1004 else if (flags & OPT_UNSET)
1005 strbuf_addf(&sb, "option `no-%s'", opt->long_name);
1007 strbuf_addf(&sb, "option `%s'", opt->long_name);