Merge branch 'sg/subtree-signed-commits' into pu
[git] / parse-options.c
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
12 int 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
23 static 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 opterror(opt, "requires a value", flags);
36         return 0;
37 }
38
39 static void fix_filename(const char *prefix, const char **file)
40 {
41         if (!file || !*file || is_absolute_path(*file) ||
42             !strcmp("-", *file))
43                 return;
44         if (**file == '~')
45                 *file = expand_user_path(*file, 0);
46         else if (prefix)
47                 *file = prefix_filename(prefix, *file);
48 }
49
50 static int opt_command_mode_error(const struct option *opt,
51                                   const struct option *all_opts,
52                                   int flags)
53 {
54         const struct option *that;
55         struct strbuf message = STRBUF_INIT;
56         struct strbuf that_name = STRBUF_INIT;
57
58         /*
59          * Find the other option that was used to set the variable
60          * already, and report that this is not compatible with it.
61          */
62         for (that = all_opts; that->type != OPTION_END; that++) {
63                 if (that == opt ||
64                     that->type != OPTION_CMDMODE ||
65                     that->value != opt->value ||
66                     that->defval != *(int *)opt->value)
67                         continue;
68
69                 if (that->long_name)
70                         strbuf_addf(&that_name, "--%s", that->long_name);
71                 else
72                         strbuf_addf(&that_name, "-%c", that->short_name);
73                 strbuf_addf(&message, ": incompatible with %s", that_name.buf);
74                 strbuf_release(&that_name);
75                 opterror(opt, message.buf, flags);
76                 strbuf_release(&message);
77                 return -1;
78         }
79         return opterror(opt, ": incompatible with something else", flags);
80 }
81
82 static int get_value(struct parse_opt_ctx_t *p,
83                      const struct option *opt,
84                      const struct option *all_opts,
85                      int flags)
86 {
87         const char *s, *arg;
88         const int unset = flags & OPT_UNSET;
89         int err;
90
91         if (unset && p->opt)
92                 return opterror(opt, "takes no value", flags);
93         if (unset && (opt->flags & PARSE_OPT_NONEG))
94                 return opterror(opt, "isn't available", flags);
95         if (!(flags & OPT_SHORT) && p->opt && (opt->flags & PARSE_OPT_NOARG))
96                 return opterror(opt, "takes no value", flags);
97
98         switch (opt->type) {
99         case OPTION_LOWLEVEL_CALLBACK:
100                 return (*(parse_opt_ll_cb *)opt->callback)(p, opt, unset);
101
102         case OPTION_BIT:
103                 if (unset)
104                         *(int *)opt->value &= ~opt->defval;
105                 else
106                         *(int *)opt->value |= opt->defval;
107                 return 0;
108
109         case OPTION_NEGBIT:
110                 if (unset)
111                         *(int *)opt->value |= opt->defval;
112                 else
113                         *(int *)opt->value &= ~opt->defval;
114                 return 0;
115
116         case OPTION_COUNTUP:
117                 if (*(int *)opt->value < 0)
118                         *(int *)opt->value = 0;
119                 *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
120                 return 0;
121
122         case OPTION_SET_INT:
123                 *(int *)opt->value = unset ? 0 : opt->defval;
124                 return 0;
125
126         case OPTION_CMDMODE:
127                 /*
128                  * Giving the same mode option twice, although is unnecessary,
129                  * is not a grave error, so let it pass.
130                  */
131                 if (*(int *)opt->value && *(int *)opt->value != opt->defval)
132                         return opt_command_mode_error(opt, all_opts, flags);
133                 *(int *)opt->value = opt->defval;
134                 return 0;
135
136         case OPTION_STRING:
137                 if (unset)
138                         *(const char **)opt->value = NULL;
139                 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
140                         *(const char **)opt->value = (const char *)opt->defval;
141                 else
142                         return get_arg(p, opt, flags, (const char **)opt->value);
143                 return 0;
144
145         case OPTION_FILENAME:
146                 err = 0;
147                 if (unset)
148                         *(const char **)opt->value = NULL;
149                 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
150                         *(const char **)opt->value = (const char *)opt->defval;
151                 else
152                         err = get_arg(p, opt, flags, (const char **)opt->value);
153
154                 if (!err)
155                         fix_filename(p->prefix, (const char **)opt->value);
156                 return err;
157
158         case OPTION_CALLBACK:
159                 if (unset)
160                         return (*opt->callback)(opt, NULL, 1) ? (-1) : 0;
161                 if (opt->flags & PARSE_OPT_NOARG)
162                         return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
163                 if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
164                         return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
165                 if (get_arg(p, opt, flags, &arg))
166                         return -1;
167                 return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
168
169         case OPTION_INTEGER:
170                 if (unset) {
171                         *(int *)opt->value = 0;
172                         return 0;
173                 }
174                 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
175                         *(int *)opt->value = opt->defval;
176                         return 0;
177                 }
178                 if (get_arg(p, opt, flags, &arg))
179                         return -1;
180                 *(int *)opt->value = strtol(arg, (char **)&s, 10);
181                 if (*s)
182                         return opterror(opt, "expects a numerical value", flags);
183                 return 0;
184
185         case OPTION_MAGNITUDE:
186                 if (unset) {
187                         *(unsigned long *)opt->value = 0;
188                         return 0;
189                 }
190                 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
191                         *(unsigned long *)opt->value = opt->defval;
192                         return 0;
193                 }
194                 if (get_arg(p, opt, flags, &arg))
195                         return -1;
196                 if (!git_parse_ulong(arg, opt->value))
197                         return opterror(opt,
198                                 "expects a non-negative integer value with an optional k/m/g suffix",
199                                 flags);
200                 return 0;
201
202         default:
203                 die("should not happen, someone must be hit on the forehead");
204         }
205 }
206
207 static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)
208 {
209         const struct option *all_opts = options;
210         const struct option *numopt = NULL;
211
212         for (; options->type != OPTION_END; options++) {
213                 if (options->short_name == *p->opt) {
214                         p->opt = p->opt[1] ? p->opt + 1 : NULL;
215                         return get_value(p, options, all_opts, OPT_SHORT);
216                 }
217
218                 /*
219                  * Handle the numerical option later, explicit one-digit
220                  * options take precedence over it.
221                  */
222                 if (options->type == OPTION_NUMBER)
223                         numopt = options;
224         }
225         if (numopt && isdigit(*p->opt)) {
226                 size_t len = 1;
227                 char *arg;
228                 int rc;
229
230                 while (isdigit(p->opt[len]))
231                         len++;
232                 arg = xmemdupz(p->opt, len);
233                 p->opt = p->opt[len] ? p->opt + len : NULL;
234                 rc = (*numopt->callback)(numopt, arg, 0) ? (-1) : 0;
235                 free(arg);
236                 return rc;
237         }
238         return -2;
239 }
240
241 static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
242                           const struct option *options)
243 {
244         const struct option *all_opts = options;
245         const char *arg_end = strchrnul(arg, '=');
246         const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
247         int abbrev_flags = 0, ambiguous_flags = 0;
248
249         for (; options->type != OPTION_END; options++) {
250                 const char *rest, *long_name = options->long_name;
251                 int flags = 0, opt_flags = 0;
252
253                 if (!long_name)
254                         continue;
255
256 again:
257                 if (!skip_prefix(arg, long_name, &rest))
258                         rest = NULL;
259                 if (options->type == OPTION_ARGUMENT) {
260                         if (!rest)
261                                 continue;
262                         if (*rest == '=')
263                                 return opterror(options, "takes no value", flags);
264                         if (*rest)
265                                 continue;
266                         p->out[p->cpidx++] = arg - 2;
267                         return 0;
268                 }
269                 if (!rest) {
270                         /* abbreviated? */
271                         if (!strncmp(long_name, arg, arg_end - arg)) {
272 is_abbreviated:
273                                 if (abbrev_option) {
274                                         /*
275                                          * If this is abbreviated, it is
276                                          * ambiguous. So when there is no
277                                          * exact match later, we need to
278                                          * error out.
279                                          */
280                                         ambiguous_option = abbrev_option;
281                                         ambiguous_flags = abbrev_flags;
282                                 }
283                                 if (!(flags & OPT_UNSET) && *arg_end)
284                                         p->opt = arg_end + 1;
285                                 abbrev_option = options;
286                                 abbrev_flags = flags ^ opt_flags;
287                                 continue;
288                         }
289                         /* negation allowed? */
290                         if (options->flags & PARSE_OPT_NONEG)
291                                 continue;
292                         /* negated and abbreviated very much? */
293                         if (starts_with("no-", arg)) {
294                                 flags |= OPT_UNSET;
295                                 goto is_abbreviated;
296                         }
297                         /* negated? */
298                         if (!starts_with(arg, "no-")) {
299                                 if (starts_with(long_name, "no-")) {
300                                         long_name += 3;
301                                         opt_flags |= OPT_UNSET;
302                                         goto again;
303                                 }
304                                 continue;
305                         }
306                         flags |= OPT_UNSET;
307                         if (!skip_prefix(arg + 3, long_name, &rest)) {
308                                 /* abbreviated and negated? */
309                                 if (starts_with(long_name, arg + 3))
310                                         goto is_abbreviated;
311                                 else
312                                         continue;
313                         }
314                 }
315                 if (*rest) {
316                         if (*rest != '=')
317                                 continue;
318                         p->opt = rest + 1;
319                 }
320                 return get_value(p, options, all_opts, flags ^ opt_flags);
321         }
322
323         if (ambiguous_option)
324                 return error("Ambiguous option: %s "
325                         "(could be --%s%s or --%s%s)",
326                         arg,
327                         (ambiguous_flags & OPT_UNSET) ?  "no-" : "",
328                         ambiguous_option->long_name,
329                         (abbrev_flags & OPT_UNSET) ?  "no-" : "",
330                         abbrev_option->long_name);
331         if (abbrev_option)
332                 return get_value(p, abbrev_option, all_opts, abbrev_flags);
333         return -2;
334 }
335
336 static 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
350 static 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
370 static 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
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)
417 {
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                 die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
428         parse_options_check(options);
429 }
430
431 /*
432  * TODO: we are not completing the --no-XXX form yet because there are
433  * many options that do not suppress it properly.
434  */
435 static int show_gitcomp(struct parse_opt_ctx_t *ctx,
436                         const struct option *opts)
437 {
438         for (; opts->type != OPTION_END; opts++) {
439                 const char *suffix = "";
440
441                 if (!opts->long_name)
442                         continue;
443                 if (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE))
444                         continue;
445
446                 switch (opts->type) {
447                 case OPTION_GROUP:
448                         continue;
449                 case OPTION_STRING:
450                 case OPTION_FILENAME:
451                 case OPTION_INTEGER:
452                 case OPTION_MAGNITUDE:
453                 case OPTION_CALLBACK:
454                         if (opts->flags & PARSE_OPT_NOARG)
455                                 break;
456                         if (opts->flags & PARSE_OPT_OPTARG)
457                                 break;
458                         if (opts->flags & PARSE_OPT_LASTARG_DEFAULT)
459                                 break;
460                         suffix = "=";
461                         break;
462                 default:
463                         break;
464                 }
465                 if (opts->flags & PARSE_OPT_COMP_ARG)
466                         suffix = "=";
467                 printf(" --%s%s", opts->long_name, suffix);
468         }
469         fputc('\n', stdout);
470         exit(0);
471 }
472
473 static int usage_with_options_internal(struct parse_opt_ctx_t *,
474                                        const char * const *,
475                                        const struct option *, int, int);
476
477 int parse_options_step(struct parse_opt_ctx_t *ctx,
478                        const struct option *options,
479                        const char * const usagestr[])
480 {
481         int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
482         int err = 0;
483
484         /* we must reset ->opt, unknown short option leave it dangling */
485         ctx->opt = NULL;
486
487         for (; ctx->argc; ctx->argc--, ctx->argv++) {
488                 const char *arg = ctx->argv[0];
489
490                 if (*arg != '-' || !arg[1]) {
491                         if (parse_nodash_opt(ctx, arg, options) == 0)
492                                 continue;
493                         if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
494                                 return PARSE_OPT_NON_OPTION;
495                         ctx->out[ctx->cpidx++] = ctx->argv[0];
496                         continue;
497                 }
498
499                 /* lone -h asks for help */
500                 if (internal_help && ctx->total == 1 && !strcmp(arg + 1, "h"))
501                         goto show_usage;
502
503                 /* lone --git-completion-helper is asked by git-completion.bash */
504                 if (ctx->total == 1 && !strcmp(arg + 1, "-git-completion-helper"))
505                         return show_gitcomp(ctx, options);
506
507                 if (arg[1] != '-') {
508                         ctx->opt = arg + 1;
509                         switch (parse_short_opt(ctx, options)) {
510                         case -1:
511                                 goto show_usage_error;
512                         case -2:
513                                 if (ctx->opt)
514                                         check_typos(arg + 1, options);
515                                 if (internal_help && *ctx->opt == 'h')
516                                         goto show_usage;
517                                 goto unknown;
518                         }
519                         if (ctx->opt)
520                                 check_typos(arg + 1, options);
521                         while (ctx->opt) {
522                                 switch (parse_short_opt(ctx, options)) {
523                                 case -1:
524                                         goto show_usage_error;
525                                 case -2:
526                                         if (internal_help && *ctx->opt == 'h')
527                                                 goto show_usage;
528
529                                         /* fake a short option thing to hide the fact that we may have
530                                          * started to parse aggregated stuff
531                                          *
532                                          * This is leaky, too bad.
533                                          */
534                                         ctx->argv[0] = xstrdup(ctx->opt - 1);
535                                         *(char *)ctx->argv[0] = '-';
536                                         goto unknown;
537                                 }
538                         }
539                         continue;
540                 }
541
542                 if (!arg[2]) { /* "--" */
543                         if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
544                                 ctx->argc--;
545                                 ctx->argv++;
546                         }
547                         break;
548                 }
549
550                 if (internal_help && !strcmp(arg + 2, "help-all"))
551                         return usage_with_options_internal(ctx, usagestr, options, 1, 0);
552                 if (internal_help && !strcmp(arg + 2, "help"))
553                         goto show_usage;
554                 switch (parse_long_opt(ctx, arg + 2, options)) {
555                 case -1:
556                         goto show_usage_error;
557                 case -2:
558                         goto unknown;
559                 }
560                 continue;
561 unknown:
562                 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
563                         return PARSE_OPT_UNKNOWN;
564                 ctx->out[ctx->cpidx++] = ctx->argv[0];
565                 ctx->opt = NULL;
566         }
567         return PARSE_OPT_DONE;
568
569  show_usage_error:
570         err = 1;
571  show_usage:
572         return usage_with_options_internal(ctx, usagestr, options, 0, err);
573 }
574
575 int parse_options_end(struct parse_opt_ctx_t *ctx)
576 {
577         MOVE_ARRAY(ctx->out + ctx->cpidx, ctx->argv, ctx->argc);
578         ctx->out[ctx->cpidx + ctx->argc] = NULL;
579         return ctx->cpidx + ctx->argc;
580 }
581
582 int parse_options(int argc, const char **argv, const char *prefix,
583                   const struct option *options, const char * const usagestr[],
584                   int flags)
585 {
586         struct parse_opt_ctx_t ctx;
587
588         parse_options_start(&ctx, argc, argv, prefix, options, flags);
589         switch (parse_options_step(&ctx, options, usagestr)) {
590         case PARSE_OPT_HELP:
591                 exit(129);
592         case PARSE_OPT_NON_OPTION:
593         case PARSE_OPT_DONE:
594                 break;
595         default: /* PARSE_OPT_UNKNOWN */
596                 if (ctx.argv[0][1] == '-') {
597                         error("unknown option `%s'", ctx.argv[0] + 2);
598                 } else if (isascii(*ctx.opt)) {
599                         error("unknown switch `%c'", *ctx.opt);
600                 } else {
601                         error("unknown non-ascii option in string: `%s'",
602                               ctx.argv[0]);
603                 }
604                 usage_with_options(usagestr, options);
605         }
606
607         precompose_argv(argc, argv);
608         return parse_options_end(&ctx);
609 }
610
611 static int usage_argh(const struct option *opts, FILE *outfile)
612 {
613         const char *s;
614         int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) || !opts->argh;
615         if (opts->flags & PARSE_OPT_OPTARG)
616                 if (opts->long_name)
617                         s = literal ? "[=%s]" : "[=<%s>]";
618                 else
619                         s = literal ? "[%s]" : "[<%s>]";
620         else
621                 s = literal ? " %s" : " <%s>";
622         return utf8_fprintf(outfile, s, opts->argh ? _(opts->argh) : _("..."));
623 }
624
625 #define USAGE_OPTS_WIDTH 24
626 #define USAGE_GAP         2
627
628 static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
629                                        const char * const *usagestr,
630                                        const struct option *opts, int full, int err)
631 {
632         FILE *outfile = err ? stderr : stdout;
633         int need_newline;
634
635         if (!usagestr)
636                 return PARSE_OPT_HELP;
637
638         if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
639                 fprintf(outfile, "cat <<\\EOF\n");
640
641         fprintf_ln(outfile, _("usage: %s"), _(*usagestr++));
642         while (*usagestr && **usagestr)
643                 /*
644                  * TRANSLATORS: the colon here should align with the
645                  * one in "usage: %s" translation.
646                  */
647                 fprintf_ln(outfile, _("   or: %s"), _(*usagestr++));
648         while (*usagestr) {
649                 if (**usagestr)
650                         fprintf_ln(outfile, _("    %s"), _(*usagestr));
651                 else
652                         fputc('\n', outfile);
653                 usagestr++;
654         }
655
656         need_newline = 1;
657
658         for (; opts->type != OPTION_END; opts++) {
659                 size_t pos;
660                 int pad;
661
662                 if (opts->type == OPTION_GROUP) {
663                         fputc('\n', outfile);
664                         need_newline = 0;
665                         if (*opts->help)
666                                 fprintf(outfile, "%s\n", _(opts->help));
667                         continue;
668                 }
669                 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
670                         continue;
671
672                 if (need_newline) {
673                         fputc('\n', outfile);
674                         need_newline = 0;
675                 }
676
677                 pos = fprintf(outfile, "    ");
678                 if (opts->short_name) {
679                         if (opts->flags & PARSE_OPT_NODASH)
680                                 pos += fprintf(outfile, "%c", opts->short_name);
681                         else
682                                 pos += fprintf(outfile, "-%c", opts->short_name);
683                 }
684                 if (opts->long_name && opts->short_name)
685                         pos += fprintf(outfile, ", ");
686                 if (opts->long_name)
687                         pos += fprintf(outfile, "--%s", opts->long_name);
688                 if (opts->type == OPTION_NUMBER)
689                         pos += utf8_fprintf(outfile, _("-NUM"));
690
691                 if ((opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
692                     !(opts->flags & PARSE_OPT_NOARG))
693                         pos += usage_argh(opts, outfile);
694
695                 if (pos <= USAGE_OPTS_WIDTH)
696                         pad = USAGE_OPTS_WIDTH - pos;
697                 else {
698                         fputc('\n', outfile);
699                         pad = USAGE_OPTS_WIDTH;
700                 }
701                 fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", _(opts->help));
702         }
703         fputc('\n', outfile);
704
705         if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
706                 fputs("EOF\n", outfile);
707
708         return PARSE_OPT_HELP;
709 }
710
711 void NORETURN usage_with_options(const char * const *usagestr,
712                         const struct option *opts)
713 {
714         usage_with_options_internal(NULL, usagestr, opts, 0, 1);
715         exit(129);
716 }
717
718 void NORETURN usage_msg_opt(const char *msg,
719                    const char * const *usagestr,
720                    const struct option *options)
721 {
722         fprintf(stderr, "fatal: %s\n\n", msg);
723         usage_with_options(usagestr, options);
724 }
725
726 #undef opterror
727 int opterror(const struct option *opt, const char *reason, int flags)
728 {
729         if (flags & OPT_SHORT)
730                 return error("switch `%c' %s", opt->short_name, reason);
731         if (flags & OPT_UNSET)
732                 return error("option `no-%s' %s", opt->long_name, reason);
733         return error("option `%s' %s", opt->long_name, reason);
734 }