Merge branch 'nd/help-align-command-desc'
[git/raj.git] / parse-options.c
blob9f84bacce64e72d117be2bdbe91db6d4c190e257
1 #include "git-compat-util.h"
2 #include "parse-options.h"
3 #include "cache.h"
4 #include "config.h"
5 #include "commit.h"
6 #include "color.h"
7 #include "utf8.h"
9 #define OPT_SHORT 1
10 #define OPT_UNSET 2
12 int optbug(const struct option *opt, const char *reason)
14 if (opt->long_name) {
15 if (opt->short_name)
16 return error("BUG: switch '%c' (--%s) %s",
17 opt->short_name, opt->long_name, reason);
18 return error("BUG: option '%s' %s", opt->long_name, reason);
20 return error("BUG: switch '%c' %s", opt->short_name, reason);
23 static int get_arg(struct parse_opt_ctx_t *p, const struct option *opt,
24 int flags, const char **arg)
26 if (p->opt) {
27 *arg = p->opt;
28 p->opt = NULL;
29 } else if (p->argc == 1 && (opt->flags & PARSE_OPT_LASTARG_DEFAULT)) {
30 *arg = (const char *)opt->defval;
31 } else if (p->argc > 1) {
32 p->argc--;
33 *arg = *++p->argv;
34 } else
35 return error(_("%s requires a value"), optname(opt, flags));
36 return 0;
39 static void fix_filename(const char *prefix, const char **file)
41 if (!file || !*file || !prefix || is_absolute_path(*file)
42 || !strcmp("-", *file))
43 return;
44 *file = prefix_filename(prefix, *file);
47 static int opt_command_mode_error(const struct option *opt,
48 const struct option *all_opts,
49 int flags)
51 const struct option *that;
52 struct strbuf that_name = STRBUF_INIT;
55 * Find the other option that was used to set the variable
56 * already, and report that this is not compatible with it.
58 for (that = all_opts; that->type != OPTION_END; that++) {
59 if (that == opt ||
60 that->type != OPTION_CMDMODE ||
61 that->value != opt->value ||
62 that->defval != *(int *)opt->value)
63 continue;
65 if (that->long_name)
66 strbuf_addf(&that_name, "--%s", that->long_name);
67 else
68 strbuf_addf(&that_name, "-%c", that->short_name);
69 error(_("%s is incompatible with %s"),
70 optname(opt, flags), that_name.buf);
71 strbuf_release(&that_name);
72 return -1;
74 return error(_("%s : incompatible with something else"),
75 optname(opt, flags));
78 static int get_value(struct parse_opt_ctx_t *p,
79 const struct option *opt,
80 const struct option *all_opts,
81 int flags)
83 const char *s, *arg;
84 const int unset = flags & OPT_UNSET;
85 int err;
87 if (unset && p->opt)
88 return error(_("%s takes no value"), optname(opt, flags));
89 if (unset && (opt->flags & PARSE_OPT_NONEG))
90 return error(_("%s isn't available"), optname(opt, flags));
91 if (!(flags & OPT_SHORT) && p->opt && (opt->flags & PARSE_OPT_NOARG))
92 return error(_("%s takes no value"), optname(opt, flags));
94 switch (opt->type) {
95 case OPTION_LOWLEVEL_CALLBACK:
96 return (*(parse_opt_ll_cb *)opt->callback)(p, opt, unset);
98 case OPTION_BIT:
99 if (unset)
100 *(int *)opt->value &= ~opt->defval;
101 else
102 *(int *)opt->value |= opt->defval;
103 return 0;
105 case OPTION_NEGBIT:
106 if (unset)
107 *(int *)opt->value |= opt->defval;
108 else
109 *(int *)opt->value &= ~opt->defval;
110 return 0;
112 case OPTION_COUNTUP:
113 if (*(int *)opt->value < 0)
114 *(int *)opt->value = 0;
115 *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
116 return 0;
118 case OPTION_SET_INT:
119 *(int *)opt->value = unset ? 0 : opt->defval;
120 return 0;
122 case OPTION_CMDMODE:
124 * Giving the same mode option twice, although is unnecessary,
125 * is not a grave error, so let it pass.
127 if (*(int *)opt->value && *(int *)opt->value != opt->defval)
128 return opt_command_mode_error(opt, all_opts, flags);
129 *(int *)opt->value = opt->defval;
130 return 0;
132 case OPTION_STRING:
133 if (unset)
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;
137 else
138 return get_arg(p, opt, flags, (const char **)opt->value);
139 return 0;
141 case OPTION_FILENAME:
142 err = 0;
143 if (unset)
144 *(const char **)opt->value = NULL;
145 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
146 *(const char **)opt->value = (const char *)opt->defval;
147 else
148 err = get_arg(p, opt, flags, (const char **)opt->value);
150 if (!err)
151 fix_filename(p->prefix, (const char **)opt->value);
152 return err;
154 case OPTION_CALLBACK:
155 if (unset)
156 return (*opt->callback)(opt, NULL, 1) ? (-1) : 0;
157 if (opt->flags & PARSE_OPT_NOARG)
158 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
159 if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
160 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
161 if (get_arg(p, opt, flags, &arg))
162 return -1;
163 return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
165 case OPTION_INTEGER:
166 if (unset) {
167 *(int *)opt->value = 0;
168 return 0;
170 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
171 *(int *)opt->value = opt->defval;
172 return 0;
174 if (get_arg(p, opt, flags, &arg))
175 return -1;
176 *(int *)opt->value = strtol(arg, (char **)&s, 10);
177 if (*s)
178 return error(_("%s expects a numerical value"),
179 optname(opt, flags));
180 return 0;
182 case OPTION_MAGNITUDE:
183 if (unset) {
184 *(unsigned long *)opt->value = 0;
185 return 0;
187 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
188 *(unsigned long *)opt->value = opt->defval;
189 return 0;
191 if (get_arg(p, opt, flags, &arg))
192 return -1;
193 if (!git_parse_ulong(arg, opt->value))
194 return error(_("%s expects a non-negative integer value"
195 " with an optional k/m/g suffix"),
196 optname(opt, flags));
197 return 0;
199 default:
200 BUG("opt->type %d should not happen", opt->type);
204 static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)
206 const struct option *all_opts = options;
207 const struct option *numopt = NULL;
209 for (; options->type != OPTION_END; options++) {
210 if (options->short_name == *p->opt) {
211 p->opt = p->opt[1] ? p->opt + 1 : NULL;
212 return get_value(p, options, all_opts, OPT_SHORT);
216 * Handle the numerical option later, explicit one-digit
217 * options take precedence over it.
219 if (options->type == OPTION_NUMBER)
220 numopt = options;
222 if (numopt && isdigit(*p->opt)) {
223 size_t len = 1;
224 char *arg;
225 int rc;
227 while (isdigit(p->opt[len]))
228 len++;
229 arg = xmemdupz(p->opt, len);
230 p->opt = p->opt[len] ? p->opt + len : NULL;
231 rc = (*numopt->callback)(numopt, arg, 0) ? (-1) : 0;
232 free(arg);
233 return rc;
235 return -2;
238 static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
239 const struct option *options)
241 const struct option *all_opts = options;
242 const char *arg_end = strchrnul(arg, '=');
243 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
244 int abbrev_flags = 0, ambiguous_flags = 0;
246 for (; options->type != OPTION_END; options++) {
247 const char *rest, *long_name = options->long_name;
248 int flags = 0, opt_flags = 0;
250 if (!long_name)
251 continue;
253 again:
254 if (!skip_prefix(arg, long_name, &rest))
255 rest = NULL;
256 if (options->type == OPTION_ARGUMENT) {
257 if (!rest)
258 continue;
259 if (*rest == '=')
260 return error(_("%s takes no value"),
261 optname(options, flags));
262 if (*rest)
263 continue;
264 p->out[p->cpidx++] = arg - 2;
265 return 0;
267 if (!rest) {
268 /* abbreviated? */
269 if (!strncmp(long_name, arg, arg_end - arg)) {
270 is_abbreviated:
271 if (abbrev_option) {
273 * If this is abbreviated, it is
274 * ambiguous. So when there is no
275 * exact match later, we need to
276 * error out.
278 ambiguous_option = abbrev_option;
279 ambiguous_flags = abbrev_flags;
281 if (!(flags & OPT_UNSET) && *arg_end)
282 p->opt = arg_end + 1;
283 abbrev_option = options;
284 abbrev_flags = flags ^ opt_flags;
285 continue;
287 /* negation allowed? */
288 if (options->flags & PARSE_OPT_NONEG)
289 continue;
290 /* negated and abbreviated very much? */
291 if (starts_with("no-", arg)) {
292 flags |= OPT_UNSET;
293 goto is_abbreviated;
295 /* negated? */
296 if (!starts_with(arg, "no-")) {
297 if (starts_with(long_name, "no-")) {
298 long_name += 3;
299 opt_flags |= OPT_UNSET;
300 goto again;
302 continue;
304 flags |= OPT_UNSET;
305 if (!skip_prefix(arg + 3, long_name, &rest)) {
306 /* abbreviated and negated? */
307 if (starts_with(long_name, arg + 3))
308 goto is_abbreviated;
309 else
310 continue;
313 if (*rest) {
314 if (*rest != '=')
315 continue;
316 p->opt = rest + 1;
318 return get_value(p, options, all_opts, flags ^ opt_flags);
321 if (ambiguous_option) {
322 error(_("ambiguous option: %s "
323 "(could be --%s%s or --%s%s)"),
324 arg,
325 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
326 ambiguous_option->long_name,
327 (abbrev_flags & OPT_UNSET) ? "no-" : "",
328 abbrev_option->long_name);
329 return -3;
331 if (abbrev_option)
332 return get_value(p, abbrev_option, all_opts, abbrev_flags);
333 return -2;
336 static int parse_nodash_opt(struct parse_opt_ctx_t *p, const char *arg,
337 const struct option *options)
339 const struct option *all_opts = options;
341 for (; options->type != OPTION_END; options++) {
342 if (!(options->flags & PARSE_OPT_NODASH))
343 continue;
344 if (options->short_name == arg[0] && arg[1] == '\0')
345 return get_value(p, options, all_opts, OPT_SHORT);
347 return -2;
350 static void check_typos(const char *arg, const struct option *options)
352 if (strlen(arg) < 3)
353 return;
355 if (starts_with(arg, "no-")) {
356 error(_("did you mean `--%s` (with two dashes ?)"), arg);
357 exit(129);
360 for (; options->type != OPTION_END; options++) {
361 if (!options->long_name)
362 continue;
363 if (starts_with(options->long_name, arg)) {
364 error(_("did you mean `--%s` (with two dashes ?)"), arg);
365 exit(129);
370 static void parse_options_check(const struct option *opts)
372 int err = 0;
373 char short_opts[128];
375 memset(short_opts, '\0', sizeof(short_opts));
376 for (; opts->type != OPTION_END; opts++) {
377 if ((opts->flags & PARSE_OPT_LASTARG_DEFAULT) &&
378 (opts->flags & PARSE_OPT_OPTARG))
379 err |= optbug(opts, "uses incompatible flags "
380 "LASTARG_DEFAULT and OPTARG");
381 if (opts->short_name) {
382 if (0x7F <= opts->short_name)
383 err |= optbug(opts, "invalid short name");
384 else if (short_opts[opts->short_name]++)
385 err |= optbug(opts, "short name already used");
387 if (opts->flags & PARSE_OPT_NODASH &&
388 ((opts->flags & PARSE_OPT_OPTARG) ||
389 !(opts->flags & PARSE_OPT_NOARG) ||
390 !(opts->flags & PARSE_OPT_NONEG) ||
391 opts->long_name))
392 err |= optbug(opts, "uses feature "
393 "not supported for dashless options");
394 switch (opts->type) {
395 case OPTION_COUNTUP:
396 case OPTION_BIT:
397 case OPTION_NEGBIT:
398 case OPTION_SET_INT:
399 case OPTION_NUMBER:
400 if ((opts->flags & PARSE_OPT_OPTARG) ||
401 !(opts->flags & PARSE_OPT_NOARG))
402 err |= optbug(opts, "should not accept an argument");
403 default:
404 ; /* ok. (usually accepts an argument) */
406 if (opts->argh &&
407 strcspn(opts->argh, " _") != strlen(opts->argh))
408 err |= optbug(opts, "multi-word argh should use dash to separate words");
410 if (err)
411 exit(128);
414 void parse_options_start(struct parse_opt_ctx_t *ctx,
415 int argc, const char **argv, const char *prefix,
416 const struct option *options, int flags)
418 memset(ctx, 0, sizeof(*ctx));
419 ctx->argc = ctx->total = argc - 1;
420 ctx->argv = argv + 1;
421 ctx->out = argv;
422 ctx->prefix = prefix;
423 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
424 ctx->flags = flags;
425 if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
426 (flags & PARSE_OPT_STOP_AT_NON_OPTION))
427 BUG("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
428 parse_options_check(options);
431 static void show_negated_gitcomp(const struct option *opts, int nr_noopts)
433 int printed_dashdash = 0;
435 for (; opts->type != OPTION_END; opts++) {
436 int has_unset_form = 0;
437 const char *name;
439 if (!opts->long_name)
440 continue;
441 if (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE))
442 continue;
443 if (opts->flags & PARSE_OPT_NONEG)
444 continue;
446 switch (opts->type) {
447 case OPTION_STRING:
448 case OPTION_FILENAME:
449 case OPTION_INTEGER:
450 case OPTION_MAGNITUDE:
451 case OPTION_CALLBACK:
452 case OPTION_BIT:
453 case OPTION_NEGBIT:
454 case OPTION_COUNTUP:
455 case OPTION_SET_INT:
456 has_unset_form = 1;
457 break;
458 default:
459 break;
461 if (!has_unset_form)
462 continue;
464 if (skip_prefix(opts->long_name, "no-", &name)) {
465 if (nr_noopts < 0)
466 printf(" --%s", name);
467 } else if (nr_noopts >= 0) {
468 if (nr_noopts && !printed_dashdash) {
469 printf(" --");
470 printed_dashdash = 1;
472 printf(" --no-%s", opts->long_name);
473 nr_noopts++;
478 static int show_gitcomp(struct parse_opt_ctx_t *ctx,
479 const struct option *opts)
481 const struct option *original_opts = opts;
482 int nr_noopts = 0;
484 for (; opts->type != OPTION_END; opts++) {
485 const char *suffix = "";
487 if (!opts->long_name)
488 continue;
489 if (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE))
490 continue;
492 switch (opts->type) {
493 case OPTION_GROUP:
494 continue;
495 case OPTION_STRING:
496 case OPTION_FILENAME:
497 case OPTION_INTEGER:
498 case OPTION_MAGNITUDE:
499 case OPTION_CALLBACK:
500 if (opts->flags & PARSE_OPT_NOARG)
501 break;
502 if (opts->flags & PARSE_OPT_OPTARG)
503 break;
504 if (opts->flags & PARSE_OPT_LASTARG_DEFAULT)
505 break;
506 suffix = "=";
507 break;
508 default:
509 break;
511 if (opts->flags & PARSE_OPT_COMP_ARG)
512 suffix = "=";
513 if (starts_with(opts->long_name, "no-"))
514 nr_noopts++;
515 printf(" --%s%s", opts->long_name, suffix);
517 show_negated_gitcomp(original_opts, -1);
518 show_negated_gitcomp(original_opts, nr_noopts);
519 fputc('\n', stdout);
520 return PARSE_OPT_COMPLETE;
523 static int usage_with_options_internal(struct parse_opt_ctx_t *,
524 const char * const *,
525 const struct option *, int, int);
527 int parse_options_step(struct parse_opt_ctx_t *ctx,
528 const struct option *options,
529 const char * const usagestr[])
531 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
533 /* we must reset ->opt, unknown short option leave it dangling */
534 ctx->opt = NULL;
536 for (; ctx->argc; ctx->argc--, ctx->argv++) {
537 const char *arg = ctx->argv[0];
539 if (*arg != '-' || !arg[1]) {
540 if (parse_nodash_opt(ctx, arg, options) == 0)
541 continue;
542 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
543 return PARSE_OPT_NON_OPTION;
544 ctx->out[ctx->cpidx++] = ctx->argv[0];
545 continue;
548 /* lone -h asks for help */
549 if (internal_help && ctx->total == 1 && !strcmp(arg + 1, "h"))
550 goto show_usage;
552 /* lone --git-completion-helper is asked by git-completion.bash */
553 if (ctx->total == 1 && !strcmp(arg + 1, "-git-completion-helper"))
554 return show_gitcomp(ctx, options);
556 if (arg[1] != '-') {
557 ctx->opt = arg + 1;
558 switch (parse_short_opt(ctx, options)) {
559 case -1:
560 return PARSE_OPT_ERROR;
561 case -2:
562 if (ctx->opt)
563 check_typos(arg + 1, options);
564 if (internal_help && *ctx->opt == 'h')
565 goto show_usage;
566 goto unknown;
568 if (ctx->opt)
569 check_typos(arg + 1, options);
570 while (ctx->opt) {
571 switch (parse_short_opt(ctx, options)) {
572 case -1:
573 return PARSE_OPT_ERROR;
574 case -2:
575 if (internal_help && *ctx->opt == 'h')
576 goto show_usage;
578 /* fake a short option thing to hide the fact that we may have
579 * started to parse aggregated stuff
581 * This is leaky, too bad.
583 ctx->argv[0] = xstrdup(ctx->opt - 1);
584 *(char *)ctx->argv[0] = '-';
585 goto unknown;
588 continue;
591 if (!arg[2]) { /* "--" */
592 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
593 ctx->argc--;
594 ctx->argv++;
596 break;
599 if (internal_help && !strcmp(arg + 2, "help-all"))
600 return usage_with_options_internal(ctx, usagestr, options, 1, 0);
601 if (internal_help && !strcmp(arg + 2, "help"))
602 goto show_usage;
603 switch (parse_long_opt(ctx, arg + 2, options)) {
604 case -1:
605 return PARSE_OPT_ERROR;
606 case -2:
607 goto unknown;
608 case -3:
609 goto show_usage;
611 continue;
612 unknown:
613 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
614 return PARSE_OPT_UNKNOWN;
615 ctx->out[ctx->cpidx++] = ctx->argv[0];
616 ctx->opt = NULL;
618 return PARSE_OPT_DONE;
620 show_usage:
621 return usage_with_options_internal(ctx, usagestr, options, 0, 0);
624 int parse_options_end(struct parse_opt_ctx_t *ctx)
626 MOVE_ARRAY(ctx->out + ctx->cpidx, ctx->argv, ctx->argc);
627 ctx->out[ctx->cpidx + ctx->argc] = NULL;
628 return ctx->cpidx + ctx->argc;
631 int parse_options(int argc, const char **argv, const char *prefix,
632 const struct option *options, const char * const usagestr[],
633 int flags)
635 struct parse_opt_ctx_t ctx;
637 parse_options_start(&ctx, argc, argv, prefix, options, flags);
638 switch (parse_options_step(&ctx, options, usagestr)) {
639 case PARSE_OPT_HELP:
640 case PARSE_OPT_ERROR:
641 exit(129);
642 case PARSE_OPT_COMPLETE:
643 exit(0);
644 case PARSE_OPT_NON_OPTION:
645 case PARSE_OPT_DONE:
646 break;
647 default: /* PARSE_OPT_UNKNOWN */
648 if (ctx.argv[0][1] == '-') {
649 error(_("unknown option `%s'"), ctx.argv[0] + 2);
650 } else if (isascii(*ctx.opt)) {
651 error(_("unknown switch `%c'"), *ctx.opt);
652 } else {
653 error(_("unknown non-ascii option in string: `%s'"),
654 ctx.argv[0]);
656 usage_with_options(usagestr, options);
659 precompose_argv(argc, argv);
660 return parse_options_end(&ctx);
663 static int usage_argh(const struct option *opts, FILE *outfile)
665 const char *s;
666 int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
667 !opts->argh || !!strpbrk(opts->argh, "()<>[]|");
668 if (opts->flags & PARSE_OPT_OPTARG)
669 if (opts->long_name)
670 s = literal ? "[=%s]" : "[=<%s>]";
671 else
672 s = literal ? "[%s]" : "[<%s>]";
673 else
674 s = literal ? " %s" : " <%s>";
675 return utf8_fprintf(outfile, s, opts->argh ? _(opts->argh) : _("..."));
678 #define USAGE_OPTS_WIDTH 24
679 #define USAGE_GAP 2
681 static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
682 const char * const *usagestr,
683 const struct option *opts, int full, int err)
685 FILE *outfile = err ? stderr : stdout;
686 int need_newline;
688 if (!usagestr)
689 return PARSE_OPT_HELP;
691 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
692 fprintf(outfile, "cat <<\\EOF\n");
694 fprintf_ln(outfile, _("usage: %s"), _(*usagestr++));
695 while (*usagestr && **usagestr)
697 * TRANSLATORS: the colon here should align with the
698 * one in "usage: %s" translation.
700 fprintf_ln(outfile, _(" or: %s"), _(*usagestr++));
701 while (*usagestr) {
702 if (**usagestr)
703 fprintf_ln(outfile, _(" %s"), _(*usagestr));
704 else
705 fputc('\n', outfile);
706 usagestr++;
709 need_newline = 1;
711 for (; opts->type != OPTION_END; opts++) {
712 size_t pos;
713 int pad;
715 if (opts->type == OPTION_GROUP) {
716 fputc('\n', outfile);
717 need_newline = 0;
718 if (*opts->help)
719 fprintf(outfile, "%s\n", _(opts->help));
720 continue;
722 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
723 continue;
725 if (need_newline) {
726 fputc('\n', outfile);
727 need_newline = 0;
730 pos = fprintf(outfile, " ");
731 if (opts->short_name) {
732 if (opts->flags & PARSE_OPT_NODASH)
733 pos += fprintf(outfile, "%c", opts->short_name);
734 else
735 pos += fprintf(outfile, "-%c", opts->short_name);
737 if (opts->long_name && opts->short_name)
738 pos += fprintf(outfile, ", ");
739 if (opts->long_name)
740 pos += fprintf(outfile, "--%s", opts->long_name);
741 if (opts->type == OPTION_NUMBER)
742 pos += utf8_fprintf(outfile, _("-NUM"));
744 if ((opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
745 !(opts->flags & PARSE_OPT_NOARG))
746 pos += usage_argh(opts, outfile);
748 if (pos <= USAGE_OPTS_WIDTH)
749 pad = USAGE_OPTS_WIDTH - pos;
750 else {
751 fputc('\n', outfile);
752 pad = USAGE_OPTS_WIDTH;
754 fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", _(opts->help));
756 fputc('\n', outfile);
758 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
759 fputs("EOF\n", outfile);
761 return PARSE_OPT_HELP;
764 void NORETURN usage_with_options(const char * const *usagestr,
765 const struct option *opts)
767 usage_with_options_internal(NULL, usagestr, opts, 0, 1);
768 exit(129);
771 void NORETURN usage_msg_opt(const char *msg,
772 const char * const *usagestr,
773 const struct option *options)
775 fprintf(stderr, "fatal: %s\n\n", msg);
776 usage_with_options(usagestr, options);
779 const char *optname(const struct option *opt, int flags)
781 static struct strbuf sb = STRBUF_INIT;
783 strbuf_reset(&sb);
784 if (flags & OPT_SHORT)
785 strbuf_addf(&sb, "switch `%c'", opt->short_name);
786 else if (flags & OPT_UNSET)
787 strbuf_addf(&sb, "option `no-%s'", opt->long_name);
788 else
789 strbuf_addf(&sb, "option `%s'", opt->long_name);
791 return sb.buf;