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))
313 if (options->type == OPTION_ARGUMENT) {
317 return error(_("%s takes no value"),
318 optname(options, flags));
322 *(int *)options->value = options->defval;
323 p->out[p->cpidx++] = arg - 2;
324 return PARSE_OPT_DONE;
328 if (!(p->flags & PARSE_OPT_KEEP_UNKNOWN) &&
329 !strncmp(long_name, arg, arg_end - arg)) {
332 !is_alias(p, abbrev_option, options)) {
334 * If this is abbreviated, it is
335 * ambiguous. So when there is no
336 * exact match later, we need to
339 ambiguous_option = abbrev_option;
340 ambiguous_flags = abbrev_flags;
342 if (!(flags & OPT_UNSET) && *arg_end)
343 p->opt = arg_end + 1;
344 abbrev_option = options;
345 abbrev_flags = flags ^ opt_flags;
348 /* negation allowed? */
349 if (options->flags & PARSE_OPT_NONEG)
351 /* negated and abbreviated very much? */
352 if (starts_with("no-", arg)) {
357 if (!starts_with(arg, "no-")) {
358 if (skip_prefix(long_name, "no-", &long_name)) {
359 opt_flags |= OPT_UNSET;
365 if (!skip_prefix(arg + 3, long_name, &rest)) {
366 /* abbreviated and negated? */
367 if (starts_with(long_name, arg + 3))
378 return get_value(p, options, all_opts, flags ^ opt_flags);
381 if (disallow_abbreviated_options && (ambiguous_option || abbrev_option))
382 die("disallowed abbreviated or ambiguous option '%.*s'",
383 (int)(arg_end - arg), arg);
385 if (ambiguous_option) {
386 error(_("ambiguous option: %s "
387 "(could be --%s%s or --%s%s)"),
389 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
390 ambiguous_option->long_name,
391 (abbrev_flags & OPT_UNSET) ? "no-" : "",
392 abbrev_option->long_name);
393 return PARSE_OPT_HELP;
396 return get_value(p, abbrev_option, all_opts, abbrev_flags);
397 return PARSE_OPT_UNKNOWN;
400 static int parse_nodash_opt(struct parse_opt_ctx_t *p, const char *arg,
401 const struct option *options)
403 const struct option *all_opts = options;
405 for (; options->type != OPTION_END; options++) {
406 if (!(options->flags & PARSE_OPT_NODASH))
408 if (options->short_name == arg[0] && arg[1] == '\0')
409 return get_value(p, options, all_opts, OPT_SHORT);
414 static void check_typos(const char *arg, const struct option *options)
419 if (starts_with(arg, "no-")) {
420 error(_("did you mean `--%s` (with two dashes)?"), arg);
424 for (; options->type != OPTION_END; options++) {
425 if (!options->long_name)
427 if (starts_with(options->long_name, arg)) {
428 error(_("did you mean `--%s` (with two dashes)?"), arg);
434 static void parse_options_check(const struct option *opts)
437 char short_opts[128];
439 memset(short_opts, '\0', sizeof(short_opts));
440 for (; opts->type != OPTION_END; opts++) {
441 if ((opts->flags & PARSE_OPT_LASTARG_DEFAULT) &&
442 (opts->flags & PARSE_OPT_OPTARG))
443 err |= optbug(opts, "uses incompatible flags "
444 "LASTARG_DEFAULT and OPTARG");
445 if (opts->short_name) {
446 if (0x7F <= opts->short_name)
447 err |= optbug(opts, "invalid short name");
448 else if (short_opts[opts->short_name]++)
449 err |= optbug(opts, "short name already used");
451 if (opts->flags & PARSE_OPT_NODASH &&
452 ((opts->flags & PARSE_OPT_OPTARG) ||
453 !(opts->flags & PARSE_OPT_NOARG) ||
454 !(opts->flags & PARSE_OPT_NONEG) ||
456 err |= optbug(opts, "uses feature "
457 "not supported for dashless options");
458 switch (opts->type) {
464 if ((opts->flags & PARSE_OPT_OPTARG) ||
465 !(opts->flags & PARSE_OPT_NOARG))
466 err |= optbug(opts, "should not accept an argument");
468 case OPTION_CALLBACK:
469 if (!opts->callback && !opts->ll_callback)
470 BUG("OPTION_CALLBACK needs one callback");
471 if (opts->callback && opts->ll_callback)
472 BUG("OPTION_CALLBACK can't have two callbacks");
474 case OPTION_LOWLEVEL_CALLBACK:
475 if (!opts->ll_callback)
476 BUG("OPTION_LOWLEVEL_CALLBACK needs a callback");
478 BUG("OPTION_LOWLEVEL_CALLBACK needs no high level callback");
481 BUG("OPT_ALIAS() should not remain at this point. "
482 "Are you using parse_options_step() directly?\n"
483 "That case is not supported yet.");
485 ; /* ok. (usually accepts an argument) */
488 strcspn(opts->argh, " _") != strlen(opts->argh))
489 err |= optbug(opts, "multi-word argh should use dash to separate words");
495 static void parse_options_start_1(struct parse_opt_ctx_t *ctx,
496 int argc, const char **argv, const char *prefix,
497 const struct option *options, int flags)
501 if (!(flags & PARSE_OPT_ONE_SHOT)) {
505 ctx->total = ctx->argc;
507 ctx->prefix = prefix;
508 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
510 if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
511 (flags & PARSE_OPT_STOP_AT_NON_OPTION) &&
512 !(flags & PARSE_OPT_ONE_SHOT))
513 BUG("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
514 if ((flags & PARSE_OPT_ONE_SHOT) &&
515 (flags & PARSE_OPT_KEEP_ARGV0))
516 BUG("Can't keep argv0 if you don't have it");
517 parse_options_check(options);
520 void parse_options_start(struct parse_opt_ctx_t *ctx,
521 int argc, const char **argv, const char *prefix,
522 const struct option *options, int flags)
524 memset(ctx, 0, sizeof(*ctx));
525 parse_options_start_1(ctx, argc, argv, prefix, options, flags);
528 static void show_negated_gitcomp(const struct option *opts, int nr_noopts)
530 int printed_dashdash = 0;
532 for (; opts->type != OPTION_END; opts++) {
533 int has_unset_form = 0;
536 if (!opts->long_name)
538 if (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE))
540 if (opts->flags & PARSE_OPT_NONEG)
543 switch (opts->type) {
545 case OPTION_FILENAME:
547 case OPTION_MAGNITUDE:
548 case OPTION_CALLBACK:
561 if (skip_prefix(opts->long_name, "no-", &name)) {
563 printf(" --%s", name);
564 } else if (nr_noopts >= 0) {
565 if (nr_noopts && !printed_dashdash) {
567 printed_dashdash = 1;
569 printf(" --no-%s", opts->long_name);
575 static int show_gitcomp(const struct option *opts)
577 const struct option *original_opts = opts;
580 for (; opts->type != OPTION_END; opts++) {
581 const char *suffix = "";
583 if (!opts->long_name)
585 if (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE))
588 switch (opts->type) {
592 case OPTION_FILENAME:
594 case OPTION_MAGNITUDE:
595 case OPTION_CALLBACK:
596 if (opts->flags & PARSE_OPT_NOARG)
598 if (opts->flags & PARSE_OPT_OPTARG)
600 if (opts->flags & PARSE_OPT_LASTARG_DEFAULT)
607 if (opts->flags & PARSE_OPT_COMP_ARG)
609 if (starts_with(opts->long_name, "no-"))
611 printf(" --%s%s", opts->long_name, suffix);
613 show_negated_gitcomp(original_opts, -1);
614 show_negated_gitcomp(original_opts, nr_noopts);
616 return PARSE_OPT_COMPLETE;
620 * Scan and may produce a new option[] array, which should be used
621 * instead of the original 'options'.
623 * Right now this is only used to preprocess and substitute
626 static struct option *preprocess_options(struct parse_opt_ctx_t *ctx,
627 const struct option *options)
629 struct option *newopt;
633 for (nr = 0; options[nr].type != OPTION_END; nr++) {
634 if (options[nr].type == OPTION_ALIAS)
641 ALLOC_ARRAY(newopt, nr + 1);
642 COPY_ARRAY(newopt, options, nr + 1);
644 /* each alias has two string pointers and NULL */
645 CALLOC_ARRAY(ctx->alias_groups, 3 * (nr_aliases + 1));
647 for (alias = 0, i = 0; i < nr; i++) {
649 const char *long_name;
651 struct strbuf help = STRBUF_INIT;
654 if (newopt[i].type != OPTION_ALIAS)
657 short_name = newopt[i].short_name;
658 long_name = newopt[i].long_name;
659 source = newopt[i].value;
662 BUG("An alias must have long option name");
663 strbuf_addf(&help, _("alias of --%s"), source);
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.");
674 memcpy(newopt + i, options + j, sizeof(*newopt));
675 newopt[i].short_name = short_name;
676 newopt[i].long_name = long_name;
677 newopt[i].help = strbuf_detach(&help, NULL);
682 BUG("could not find source option '%s' of alias '%s'",
683 source, newopt[i].long_name);
684 ctx->alias_groups[alias * 3 + 0] = newopt[i].long_name;
685 ctx->alias_groups[alias * 3 + 1] = options[j].long_name;
686 ctx->alias_groups[alias * 3 + 2] = NULL;
693 static int usage_with_options_internal(struct parse_opt_ctx_t *,
694 const char * const *,
695 const struct option *, int, int);
697 int parse_options_step(struct parse_opt_ctx_t *ctx,
698 const struct option *options,
699 const char * const usagestr[])
701 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
703 /* we must reset ->opt, unknown short option leave it dangling */
706 for (; ctx->argc; ctx->argc--, ctx->argv++) {
707 const char *arg = ctx->argv[0];
709 if (ctx->flags & PARSE_OPT_ONE_SHOT &&
710 ctx->argc != ctx->total)
713 if (*arg != '-' || !arg[1]) {
714 if (parse_nodash_opt(ctx, arg, options) == 0)
716 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
717 return PARSE_OPT_NON_OPTION;
718 ctx->out[ctx->cpidx++] = ctx->argv[0];
722 /* lone -h asks for help */
723 if (internal_help && ctx->total == 1 && !strcmp(arg + 1, "h"))
726 /* lone --git-completion-helper is asked by git-completion.bash */
727 if (ctx->total == 1 && !strcmp(arg + 1, "-git-completion-helper"))
728 return show_gitcomp(options);
732 switch (parse_short_opt(ctx, options)) {
733 case PARSE_OPT_ERROR:
734 return PARSE_OPT_ERROR;
735 case PARSE_OPT_UNKNOWN:
737 check_typos(arg + 1, options);
738 if (internal_help && *ctx->opt == 'h')
741 case PARSE_OPT_NON_OPTION:
743 case PARSE_OPT_COMPLETE:
744 BUG("parse_short_opt() cannot return these");
749 check_typos(arg + 1, options);
751 switch (parse_short_opt(ctx, options)) {
752 case PARSE_OPT_ERROR:
753 return PARSE_OPT_ERROR;
754 case PARSE_OPT_UNKNOWN:
755 if (internal_help && *ctx->opt == 'h')
758 /* fake a short option thing to hide the fact that we may have
759 * started to parse aggregated stuff
761 * This is leaky, too bad.
763 ctx->argv[0] = xstrdup(ctx->opt - 1);
764 *(char *)ctx->argv[0] = '-';
766 case PARSE_OPT_NON_OPTION:
767 case PARSE_OPT_COMPLETE:
769 BUG("parse_short_opt() cannot return these");
777 if (!arg[2] /* "--" */ ||
778 !strcmp(arg + 2, "end-of-options")) {
779 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
786 if (internal_help && !strcmp(arg + 2, "help-all"))
787 return usage_with_options_internal(ctx, usagestr, options, 1, 0);
788 if (internal_help && !strcmp(arg + 2, "help"))
790 switch (parse_long_opt(ctx, arg + 2, options)) {
791 case PARSE_OPT_ERROR:
792 return PARSE_OPT_ERROR;
793 case PARSE_OPT_UNKNOWN:
797 case PARSE_OPT_NON_OPTION:
798 case PARSE_OPT_COMPLETE:
799 BUG("parse_long_opt() cannot return these");
805 if (ctx->flags & PARSE_OPT_ONE_SHOT)
807 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
808 return PARSE_OPT_UNKNOWN;
809 ctx->out[ctx->cpidx++] = ctx->argv[0];
812 return PARSE_OPT_DONE;
815 return usage_with_options_internal(ctx, usagestr, options, 0, 0);
818 int parse_options_end(struct parse_opt_ctx_t *ctx)
820 if (ctx->flags & PARSE_OPT_ONE_SHOT)
821 return ctx->total - ctx->argc;
823 MOVE_ARRAY(ctx->out + ctx->cpidx, ctx->argv, ctx->argc);
824 ctx->out[ctx->cpidx + ctx->argc] = NULL;
825 return ctx->cpidx + ctx->argc;
828 int parse_options(int argc, const char **argv, const char *prefix,
829 const struct option *options, const char * const usagestr[],
832 struct parse_opt_ctx_t ctx;
833 struct option *real_options;
835 disallow_abbreviated_options =
836 git_env_bool("GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS", 0);
838 memset(&ctx, 0, sizeof(ctx));
839 real_options = preprocess_options(&ctx, options);
841 options = real_options;
842 parse_options_start_1(&ctx, argc, argv, prefix, options, flags);
843 switch (parse_options_step(&ctx, options, usagestr)) {
845 case PARSE_OPT_ERROR:
847 case PARSE_OPT_COMPLETE:
849 case PARSE_OPT_NON_OPTION:
852 default: /* PARSE_OPT_UNKNOWN */
853 if (ctx.argv[0][1] == '-') {
854 error(_("unknown option `%s'"), ctx.argv[0] + 2);
855 } else if (isascii(*ctx.opt)) {
856 error(_("unknown switch `%c'"), *ctx.opt);
858 error(_("unknown non-ascii option in string: `%s'"),
861 usage_with_options(usagestr, options);
864 precompose_argv(argc, argv);
866 free(ctx.alias_groups);
867 return parse_options_end(&ctx);
870 static int usage_argh(const struct option *opts, FILE *outfile)
873 int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
874 !opts->argh || !!strpbrk(opts->argh, "()<>[]|");
875 if (opts->flags & PARSE_OPT_OPTARG)
877 s = literal ? "[=%s]" : "[=<%s>]";
879 s = literal ? "[%s]" : "[<%s>]";
881 s = literal ? " %s" : " <%s>";
882 return utf8_fprintf(outfile, s, opts->argh ? _(opts->argh) : _("..."));
885 #define USAGE_OPTS_WIDTH 24
888 static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
889 const char * const *usagestr,
890 const struct option *opts, int full, int err)
892 FILE *outfile = err ? stderr : stdout;
896 return PARSE_OPT_HELP;
898 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
899 fprintf(outfile, "cat <<\\EOF\n");
901 fprintf_ln(outfile, _("usage: %s"), _(*usagestr++));
902 while (*usagestr && **usagestr)
904 * TRANSLATORS: the colon here should align with the
905 * one in "usage: %s" translation.
907 fprintf_ln(outfile, _(" or: %s"), _(*usagestr++));
910 fprintf_ln(outfile, _(" %s"), _(*usagestr));
912 fputc('\n', outfile);
918 for (; opts->type != OPTION_END; opts++) {
922 if (opts->type == OPTION_GROUP) {
923 fputc('\n', outfile);
926 fprintf(outfile, "%s\n", _(opts->help));
929 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
933 fputc('\n', outfile);
937 pos = fprintf(outfile, " ");
938 if (opts->short_name) {
939 if (opts->flags & PARSE_OPT_NODASH)
940 pos += fprintf(outfile, "%c", opts->short_name);
942 pos += fprintf(outfile, "-%c", opts->short_name);
944 if (opts->long_name && opts->short_name)
945 pos += fprintf(outfile, ", ");
947 pos += fprintf(outfile, "--%s", opts->long_name);
948 if (opts->type == OPTION_NUMBER)
949 pos += utf8_fprintf(outfile, _("-NUM"));
951 if ((opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
952 !(opts->flags & PARSE_OPT_NOARG))
953 pos += usage_argh(opts, outfile);
955 if (pos <= USAGE_OPTS_WIDTH)
956 pad = USAGE_OPTS_WIDTH - pos;
958 fputc('\n', outfile);
959 pad = USAGE_OPTS_WIDTH;
961 if (opts->type == OPTION_ALIAS) {
962 fprintf(outfile, "%*s", pad + USAGE_GAP, "");
963 fprintf_ln(outfile, _("alias of --%s"),
964 (const char *)opts->value);
967 fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", _(opts->help));
969 fputc('\n', outfile);
971 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
972 fputs("EOF\n", outfile);
974 return PARSE_OPT_HELP;
977 void NORETURN usage_with_options(const char * const *usagestr,
978 const struct option *opts)
980 usage_with_options_internal(NULL, usagestr, opts, 0, 1);
984 void NORETURN usage_msg_opt(const char *msg,
985 const char * const *usagestr,
986 const struct option *options)
988 fprintf(stderr, "fatal: %s\n\n", msg);
989 usage_with_options(usagestr, options);
992 const char *optname(const struct option *opt, int flags)
994 static struct strbuf sb = STRBUF_INIT;
997 if (flags & OPT_SHORT)
998 strbuf_addf(&sb, "switch `%c'", opt->short_name);
999 else if (flags & OPT_UNSET)
1000 strbuf_addf(&sb, "option `no-%s'", opt->long_name);
1002 strbuf_addf(&sb, "option `%s'", opt->long_name);