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"
8
9#define OPT_SHORT 1
10#define OPT_UNSET 2
11
12int optbug(const struct option *opt, const char *reason)
13{
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);
19 }
20 return error("BUG: switch '%c' %s", opt->short_name, reason);
21}
22
23static int get_arg(struct parse_opt_ctx_t *p, const struct option *opt,
24 int flags, const char **arg)
25{
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;
37}
38
39static void fix_filename(const char *prefix, const char **file)
40{
41 if (!file || !*file || !prefix || is_absolute_path(*file)
42 || !strcmp("-", *file))
43 return;
44 *file = prefix_filename(prefix, *file);
45}
46
47static int opt_command_mode_error(const struct option *opt,
48 const struct option *all_opts,
49 int flags)
50{
51 const struct option *that;
52 struct strbuf that_name = STRBUF_INIT;
53
54 /*
55 * Find the other option that was used to set the variable
56 * already, and report that this is not compatible with it.
57 */
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;
64
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;
73 }
74 return error(_("%s : incompatible with something else"),
75 optname(opt, flags));
76}
77
78static int get_value(struct parse_opt_ctx_t *p,
79 const struct option *opt,
80 const struct option *all_opts,
81 int flags)
82{
83 const char *s, *arg;
84 const int unset = flags & OPT_UNSET;
85 int err;
86
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));
93
94 switch (opt->type) {
95 case OPTION_LOWLEVEL_CALLBACK:
96 return (*(parse_opt_ll_cb *)opt->callback)(p, opt, unset);
97
98 case OPTION_BIT:
99 if (unset)
100 *(int *)opt->value &= ~opt->defval;
101 else
102 *(int *)opt->value |= opt->defval;
103 return 0;
104
105 case OPTION_NEGBIT:
106 if (unset)
107 *(int *)opt->value |= opt->defval;
108 else
109 *(int *)opt->value &= ~opt->defval;
110 return 0;
111
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;
117
118 case OPTION_SET_INT:
119 *(int *)opt->value = unset ? 0 : opt->defval;
120 return 0;
121
122 case OPTION_CMDMODE:
123 /*
124 * Giving the same mode option twice, although is unnecessary,
125 * is not a grave error, so let it pass.
126 */
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;
131
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;
140
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);
149
150 if (!err)
151 fix_filename(p->prefix, (const char **)opt->value);
152 return err;
153
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;
164
165 case OPTION_INTEGER:
166 if (unset) {
167 *(int *)opt->value = 0;
168 return 0;
169 }
170 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
171 *(int *)opt->value = opt->defval;
172 return 0;
173 }
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;
181
182 case OPTION_MAGNITUDE:
183 if (unset) {
184 *(unsigned long *)opt->value = 0;
185 return 0;
186 }
187 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
188 *(unsigned long *)opt->value = opt->defval;
189 return 0;
190 }
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;
198
199 default:
200 BUG("opt->type %d should not happen", opt->type);
201 }
202}
203
204static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)
205{
206 const struct option *all_opts = options;
207 const struct option *numopt = NULL;
208
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);
213 }
214
215 /*
216 * Handle the numerical option later, explicit one-digit
217 * options take precedence over it.
218 */
219 if (options->type == OPTION_NUMBER)
220 numopt = options;
221 }
222 if (numopt && isdigit(*p->opt)) {
223 size_t len = 1;
224 char *arg;
225 int rc;
226
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;
234 }
235 return -2;
236}
237
238static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
239 const struct option *options)
240{
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;
245
246 for (; options->type != OPTION_END; options++) {
247 const char *rest, *long_name = options->long_name;
248 int flags = 0, opt_flags = 0;
249
250 if (!long_name)
251 continue;
252
253again:
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;
266 }
267 if (!rest) {
268 /* abbreviated? */
269 if (!strncmp(long_name, arg, arg_end - arg)) {
270is_abbreviated:
271 if (abbrev_option) {
272 /*
273 * If this is abbreviated, it is
274 * ambiguous. So when there is no
275 * exact match later, we need to
276 * error out.
277 */
278 ambiguous_option = abbrev_option;
279 ambiguous_flags = abbrev_flags;
280 }
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;
286 }
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;
294 }
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;
301 }
302 continue;
303 }
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;
311 }
312 }
313 if (*rest) {
314 if (*rest != '=')
315 continue;
316 p->opt = rest + 1;
317 }
318 return get_value(p, options, all_opts, flags ^ opt_flags);
319 }
320
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;
330 }
331 if (abbrev_option)
332 return get_value(p, abbrev_option, all_opts, abbrev_flags);
333 return -2;
334}
335
336static int parse_nodash_opt(struct parse_opt_ctx_t *p, const char *arg,
337 const struct option *options)
338{
339 const struct option *all_opts = options;
340
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);
346 }
347 return -2;
348}
349
350static void check_typos(const char *arg, const struct option *options)
351{
352 if (strlen(arg) < 3)
353 return;
354
355 if (starts_with(arg, "no-")) {
356 error(_("did you mean `--%s` (with two dashes ?)"), arg);
357 exit(129);
358 }
359
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);
366 }
367 }
368}
369
370static void parse_options_check(const struct option *opts)
371{
372 int err = 0;
373 char short_opts[128];
374
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");
386 }
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) */
405 }
406 if (opts->argh &&
407 strcspn(opts->argh, " _") != strlen(opts->argh))
408 err |= optbug(opts, "multi-word argh should use dash to separate words");
409 }
410 if (err)
411 exit(128);
412}
413
414void 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)
417{
418 memset(ctx, 0, sizeof(*ctx));
419 ctx->argc = argc;
420 ctx->argv = argv;
421 if (!(flags & PARSE_OPT_ONE_SHOT)) {
422 ctx->argc--;
423 ctx->argv++;
424 }
425 ctx->total = ctx->argc;
426 ctx->out = argv;
427 ctx->prefix = prefix;
428 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
429 ctx->flags = flags;
430 if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
431 (flags & PARSE_OPT_STOP_AT_NON_OPTION) &&
432 !(flags & PARSE_OPT_ONE_SHOT))
433 BUG("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
434 if ((flags & PARSE_OPT_ONE_SHOT) &&
435 (flags & PARSE_OPT_KEEP_ARGV0))
436 BUG("Can't keep argv0 if you don't have it");
437 parse_options_check(options);
438}
439
440static void show_negated_gitcomp(const struct option *opts, int nr_noopts)
441{
442 int printed_dashdash = 0;
443
444 for (; opts->type != OPTION_END; opts++) {
445 int has_unset_form = 0;
446 const char *name;
447
448 if (!opts->long_name)
449 continue;
450 if (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE))
451 continue;
452 if (opts->flags & PARSE_OPT_NONEG)
453 continue;
454
455 switch (opts->type) {
456 case OPTION_STRING:
457 case OPTION_FILENAME:
458 case OPTION_INTEGER:
459 case OPTION_MAGNITUDE:
460 case OPTION_CALLBACK:
461 case OPTION_BIT:
462 case OPTION_NEGBIT:
463 case OPTION_COUNTUP:
464 case OPTION_SET_INT:
465 has_unset_form = 1;
466 break;
467 default:
468 break;
469 }
470 if (!has_unset_form)
471 continue;
472
473 if (skip_prefix(opts->long_name, "no-", &name)) {
474 if (nr_noopts < 0)
475 printf(" --%s", name);
476 } else if (nr_noopts >= 0) {
477 if (nr_noopts && !printed_dashdash) {
478 printf(" --");
479 printed_dashdash = 1;
480 }
481 printf(" --no-%s", opts->long_name);
482 nr_noopts++;
483 }
484 }
485}
486
487static int show_gitcomp(struct parse_opt_ctx_t *ctx,
488 const struct option *opts)
489{
490 const struct option *original_opts = opts;
491 int nr_noopts = 0;
492
493 for (; opts->type != OPTION_END; opts++) {
494 const char *suffix = "";
495
496 if (!opts->long_name)
497 continue;
498 if (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE))
499 continue;
500
501 switch (opts->type) {
502 case OPTION_GROUP:
503 continue;
504 case OPTION_STRING:
505 case OPTION_FILENAME:
506 case OPTION_INTEGER:
507 case OPTION_MAGNITUDE:
508 case OPTION_CALLBACK:
509 if (opts->flags & PARSE_OPT_NOARG)
510 break;
511 if (opts->flags & PARSE_OPT_OPTARG)
512 break;
513 if (opts->flags & PARSE_OPT_LASTARG_DEFAULT)
514 break;
515 suffix = "=";
516 break;
517 default:
518 break;
519 }
520 if (opts->flags & PARSE_OPT_COMP_ARG)
521 suffix = "=";
522 if (starts_with(opts->long_name, "no-"))
523 nr_noopts++;
524 printf(" --%s%s", opts->long_name, suffix);
525 }
526 show_negated_gitcomp(original_opts, -1);
527 show_negated_gitcomp(original_opts, nr_noopts);
528 fputc('\n', stdout);
529 return PARSE_OPT_COMPLETE;
530}
531
532static int usage_with_options_internal(struct parse_opt_ctx_t *,
533 const char * const *,
534 const struct option *, int, int);
535
536int parse_options_step(struct parse_opt_ctx_t *ctx,
537 const struct option *options,
538 const char * const usagestr[])
539{
540 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
541
542 /* we must reset ->opt, unknown short option leave it dangling */
543 ctx->opt = NULL;
544
545 for (; ctx->argc; ctx->argc--, ctx->argv++) {
546 const char *arg = ctx->argv[0];
547
548 if (ctx->flags & PARSE_OPT_ONE_SHOT &&
549 ctx->argc != ctx->total)
550 break;
551
552 if (*arg != '-' || !arg[1]) {
553 if (parse_nodash_opt(ctx, arg, options) == 0)
554 continue;
555 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
556 return PARSE_OPT_NON_OPTION;
557 ctx->out[ctx->cpidx++] = ctx->argv[0];
558 continue;
559 }
560
561 /* lone -h asks for help */
562 if (internal_help && ctx->total == 1 && !strcmp(arg + 1, "h"))
563 goto show_usage;
564
565 /* lone --git-completion-helper is asked by git-completion.bash */
566 if (ctx->total == 1 && !strcmp(arg + 1, "-git-completion-helper"))
567 return show_gitcomp(ctx, options);
568
569 if (arg[1] != '-') {
570 ctx->opt = arg + 1;
571 switch (parse_short_opt(ctx, options)) {
572 case -1:
573 return PARSE_OPT_ERROR;
574 case -2:
575 if (ctx->opt)
576 check_typos(arg + 1, options);
577 if (internal_help && *ctx->opt == 'h')
578 goto show_usage;
579 goto unknown;
580 }
581 if (ctx->opt)
582 check_typos(arg + 1, options);
583 while (ctx->opt) {
584 switch (parse_short_opt(ctx, options)) {
585 case -1:
586 return PARSE_OPT_ERROR;
587 case -2:
588 if (internal_help && *ctx->opt == 'h')
589 goto show_usage;
590
591 /* fake a short option thing to hide the fact that we may have
592 * started to parse aggregated stuff
593 *
594 * This is leaky, too bad.
595 */
596 ctx->argv[0] = xstrdup(ctx->opt - 1);
597 *(char *)ctx->argv[0] = '-';
598 goto unknown;
599 }
600 }
601 continue;
602 }
603
604 if (!arg[2]) { /* "--" */
605 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
606 ctx->argc--;
607 ctx->argv++;
608 }
609 break;
610 }
611
612 if (internal_help && !strcmp(arg + 2, "help-all"))
613 return usage_with_options_internal(ctx, usagestr, options, 1, 0);
614 if (internal_help && !strcmp(arg + 2, "help"))
615 goto show_usage;
616 switch (parse_long_opt(ctx, arg + 2, options)) {
617 case -1:
618 return PARSE_OPT_ERROR;
619 case -2:
620 goto unknown;
621 case -3:
622 goto show_usage;
623 }
624 continue;
625unknown:
626 if (ctx->flags & PARSE_OPT_ONE_SHOT)
627 break;
628 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
629 return PARSE_OPT_UNKNOWN;
630 ctx->out[ctx->cpidx++] = ctx->argv[0];
631 ctx->opt = NULL;
632 }
633 return PARSE_OPT_DONE;
634
635 show_usage:
636 return usage_with_options_internal(ctx, usagestr, options, 0, 0);
637}
638
639int parse_options_end(struct parse_opt_ctx_t *ctx)
640{
641 if (ctx->flags & PARSE_OPT_ONE_SHOT)
642 return ctx->total - ctx->argc;
643
644 MOVE_ARRAY(ctx->out + ctx->cpidx, ctx->argv, ctx->argc);
645 ctx->out[ctx->cpidx + ctx->argc] = NULL;
646 return ctx->cpidx + ctx->argc;
647}
648
649int parse_options(int argc, const char **argv, const char *prefix,
650 const struct option *options, const char * const usagestr[],
651 int flags)
652{
653 struct parse_opt_ctx_t ctx;
654
655 parse_options_start(&ctx, argc, argv, prefix, options, flags);
656 switch (parse_options_step(&ctx, options, usagestr)) {
657 case PARSE_OPT_HELP:
658 case PARSE_OPT_ERROR:
659 exit(129);
660 case PARSE_OPT_COMPLETE:
661 exit(0);
662 case PARSE_OPT_NON_OPTION:
663 case PARSE_OPT_DONE:
664 break;
665 default: /* PARSE_OPT_UNKNOWN */
666 if (ctx.argv[0][1] == '-') {
667 error(_("unknown option `%s'"), ctx.argv[0] + 2);
668 } else if (isascii(*ctx.opt)) {
669 error(_("unknown switch `%c'"), *ctx.opt);
670 } else {
671 error(_("unknown non-ascii option in string: `%s'"),
672 ctx.argv[0]);
673 }
674 usage_with_options(usagestr, options);
675 }
676
677 precompose_argv(argc, argv);
678 return parse_options_end(&ctx);
679}
680
681static int usage_argh(const struct option *opts, FILE *outfile)
682{
683 const char *s;
684 int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
685 !opts->argh || !!strpbrk(opts->argh, "()<>[]|");
686 if (opts->flags & PARSE_OPT_OPTARG)
687 if (opts->long_name)
688 s = literal ? "[=%s]" : "[=<%s>]";
689 else
690 s = literal ? "[%s]" : "[<%s>]";
691 else
692 s = literal ? " %s" : " <%s>";
693 return utf8_fprintf(outfile, s, opts->argh ? _(opts->argh) : _("..."));
694}
695
696#define USAGE_OPTS_WIDTH 24
697#define USAGE_GAP 2
698
699static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
700 const char * const *usagestr,
701 const struct option *opts, int full, int err)
702{
703 FILE *outfile = err ? stderr : stdout;
704 int need_newline;
705
706 if (!usagestr)
707 return PARSE_OPT_HELP;
708
709 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
710 fprintf(outfile, "cat <<\\EOF\n");
711
712 fprintf_ln(outfile, _("usage: %s"), _(*usagestr++));
713 while (*usagestr && **usagestr)
714 /*
715 * TRANSLATORS: the colon here should align with the
716 * one in "usage: %s" translation.
717 */
718 fprintf_ln(outfile, _(" or: %s"), _(*usagestr++));
719 while (*usagestr) {
720 if (**usagestr)
721 fprintf_ln(outfile, _(" %s"), _(*usagestr));
722 else
723 fputc('\n', outfile);
724 usagestr++;
725 }
726
727 need_newline = 1;
728
729 for (; opts->type != OPTION_END; opts++) {
730 size_t pos;
731 int pad;
732
733 if (opts->type == OPTION_GROUP) {
734 fputc('\n', outfile);
735 need_newline = 0;
736 if (*opts->help)
737 fprintf(outfile, "%s\n", _(opts->help));
738 continue;
739 }
740 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
741 continue;
742
743 if (need_newline) {
744 fputc('\n', outfile);
745 need_newline = 0;
746 }
747
748 pos = fprintf(outfile, " ");
749 if (opts->short_name) {
750 if (opts->flags & PARSE_OPT_NODASH)
751 pos += fprintf(outfile, "%c", opts->short_name);
752 else
753 pos += fprintf(outfile, "-%c", opts->short_name);
754 }
755 if (opts->long_name && opts->short_name)
756 pos += fprintf(outfile, ", ");
757 if (opts->long_name)
758 pos += fprintf(outfile, "--%s", opts->long_name);
759 if (opts->type == OPTION_NUMBER)
760 pos += utf8_fprintf(outfile, _("-NUM"));
761
762 if ((opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
763 !(opts->flags & PARSE_OPT_NOARG))
764 pos += usage_argh(opts, outfile);
765
766 if (pos <= USAGE_OPTS_WIDTH)
767 pad = USAGE_OPTS_WIDTH - pos;
768 else {
769 fputc('\n', outfile);
770 pad = USAGE_OPTS_WIDTH;
771 }
772 fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", _(opts->help));
773 }
774 fputc('\n', outfile);
775
776 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
777 fputs("EOF\n", outfile);
778
779 return PARSE_OPT_HELP;
780}
781
782void NORETURN usage_with_options(const char * const *usagestr,
783 const struct option *opts)
784{
785 usage_with_options_internal(NULL, usagestr, opts, 0, 1);
786 exit(129);
787}
788
789void NORETURN usage_msg_opt(const char *msg,
790 const char * const *usagestr,
791 const struct option *options)
792{
793 fprintf(stderr, "fatal: %s\n\n", msg);
794 usage_with_options(usagestr, options);
795}
796
797const char *optname(const struct option *opt, int flags)
798{
799 static struct strbuf sb = STRBUF_INIT;
800
801 strbuf_reset(&sb);
802 if (flags & OPT_SHORT)
803 strbuf_addf(&sb, "switch `%c'", opt->short_name);
804 else if (flags & OPT_UNSET)
805 strbuf_addf(&sb, "option `no-%s'", opt->long_name);
806 else
807 strbuf_addf(&sb, "option `%s'", opt->long_name);
808
809 return sb.buf;
810}