Merge branch 'master' of git://git.bogomips.org/git-svn
[git/git.git] / parse-options.c
CommitLineData
4a59fd13
PH
1#include "git-compat-util.h"
2#include "parse-options.h"
26141b5b 3#include "cache.h"
269defdf 4#include "commit.h"
73e9da01 5#include "color.h"
c0821965 6#include "utf8.h"
4a59fd13 7
47e9cd28
TR
8static int parse_options_usage(struct parse_opt_ctx_t *ctx,
9 const char * const *usagestr,
9c7304e3 10 const struct option *opts, int err);
41064ebc 11
4a59fd13
PH
12#define OPT_SHORT 1
13#define OPT_UNSET 2
14
1f275b7c 15int optbug(const struct option *opt, const char *reason)
1e5ce570 16{
af465af8
JH
17 if (opt->long_name) {
18 if (opt->short_name)
19 return error("BUG: switch '%c' (--%s) %s",
20 opt->short_name, opt->long_name, reason);
1e5ce570 21 return error("BUG: option '%s' %s", opt->long_name, reason);
af465af8 22 }
1e5ce570
JN
23 return error("BUG: switch '%c' %s", opt->short_name, reason);
24}
25
1cc6985c
PH
26static int get_arg(struct parse_opt_ctx_t *p, const struct option *opt,
27 int flags, const char **arg)
28{
29 if (p->opt) {
30 *arg = p->opt;
31 p->opt = NULL;
32 } else if (p->argc == 1 && (opt->flags & PARSE_OPT_LASTARG_DEFAULT)) {
33 *arg = (const char *)opt->defval;
d5d745f9 34 } else if (p->argc > 1) {
1cc6985c
PH
35 p->argc--;
36 *arg = *++p->argv;
37 } else
38 return opterror(opt, "requires a value", flags);
39 return 0;
40}
41
df217ed6
SB
42static void fix_filename(const char *prefix, const char **file)
43{
44 if (!file || !*file || !prefix || is_absolute_path(*file)
45 || !strcmp("-", *file))
46 return;
47 *file = xstrdup(prefix_filename(prefix, strlen(prefix), *file));
48}
49
11588263
JH
50static 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
7e7bbcb4 82static int get_value(struct parse_opt_ctx_t *p,
11588263
JH
83 const struct option *opt,
84 const struct option *all_opts,
85 int flags)
4a59fd13 86{
ffe659f9 87 const char *s, *arg;
db7244bd 88 const int unset = flags & OPT_UNSET;
df217ed6 89 int err;
4a59fd13 90
db7244bd 91 if (unset && p->opt)
4a59fd13 92 return opterror(opt, "takes no value", flags);
db7244bd
PH
93 if (unset && (opt->flags & PARSE_OPT_NONEG))
94 return opterror(opt, "isn't available", flags);
c1f4ec9e
SB
95 if (!(flags & OPT_SHORT) && p->opt && (opt->flags & PARSE_OPT_NOARG))
96 return opterror(opt, "takes no value", flags);
db7244bd 97
db7244bd 98 switch (opt->type) {
b0b3a8b6
JN
99 case OPTION_LOWLEVEL_CALLBACK:
100 return (*(parse_opt_ll_cb *)opt->callback)(p, opt, unset);
101
db7244bd
PH
102 case OPTION_BIT:
103 if (unset)
104 *(int *)opt->value &= ~opt->defval;
4a59fd13 105 else
db7244bd
PH
106 *(int *)opt->value |= opt->defval;
107 return 0;
108
2f4b97f9
RS
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
b04ba2bb 116 case OPTION_COUNTUP:
db7244bd
PH
117 *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
118 return 0;
119
120 case OPTION_SET_INT:
121 *(int *)opt->value = unset ? 0 : opt->defval;
122 return 0;
123
11588263
JH
124 case OPTION_CMDMODE:
125 /*
126 * Giving the same mode option twice, although is unnecessary,
127 * is not a grave error, so let it pass.
128 */
129 if (*(int *)opt->value && *(int *)opt->value != opt->defval)
130 return opt_command_mode_error(opt, all_opts, flags);
131 *(int *)opt->value = opt->defval;
132 return 0;
133
4a59fd13 134 case OPTION_STRING:
1cc6985c 135 if (unset)
db7244bd 136 *(const char **)opt->value = NULL;
1cc6985c 137 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
ffe659f9 138 *(const char **)opt->value = (const char *)opt->defval;
1cc6985c
PH
139 else
140 return get_arg(p, opt, flags, (const char **)opt->value);
4a59fd13
PH
141 return 0;
142
df217ed6
SB
143 case OPTION_FILENAME:
144 err = 0;
145 if (unset)
146 *(const char **)opt->value = NULL;
147 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
148 *(const char **)opt->value = (const char *)opt->defval;
149 else
150 err = get_arg(p, opt, flags, (const char **)opt->value);
151
152 if (!err)
153 fix_filename(p->prefix, (const char **)opt->value);
154 return err;
155
ffe659f9 156 case OPTION_CALLBACK:
db7244bd 157 if (unset)
07fe54db 158 return (*opt->callback)(opt, NULL, 1) ? (-1) : 0;
db7244bd 159 if (opt->flags & PARSE_OPT_NOARG)
07fe54db 160 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
c43a2483 161 if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
07fe54db 162 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
1cc6985c
PH
163 if (get_arg(p, opt, flags, &arg))
164 return -1;
165 return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
ffe659f9 166
4a59fd13 167 case OPTION_INTEGER:
db7244bd 168 if (unset) {
4a59fd13
PH
169 *(int *)opt->value = 0;
170 return 0;
171 }
c43a2483 172 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
ffe659f9
PH
173 *(int *)opt->value = opt->defval;
174 return 0;
175 }
1cc6985c
PH
176 if (get_arg(p, opt, flags, &arg))
177 return -1;
178 *(int *)opt->value = strtol(arg, (char **)&s, 10);
4a59fd13
PH
179 if (*s)
180 return opterror(opt, "expects a numerical value", flags);
181 return 0;
182
2a514ed8
CB
183 case OPTION_MAGNITUDE:
184 if (unset) {
185 *(unsigned long *)opt->value = 0;
186 return 0;
187 }
188 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
189 *(unsigned long *)opt->value = opt->defval;
190 return 0;
191 }
192 if (get_arg(p, opt, flags, &arg))
193 return -1;
194 if (!git_parse_ulong(arg, opt->value))
195 return opterror(opt,
196 "expects a non-negative integer value with an optional k/m/g suffix",
197 flags);
198 return 0;
199
4a59fd13
PH
200 default:
201 die("should not happen, someone must be hit on the forehead");
202 }
203}
204
7e7bbcb4 205static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)
4a59fd13 206{
11588263 207 const struct option *all_opts = options;
e0319ff5
RS
208 const struct option *numopt = NULL;
209
4a59fd13
PH
210 for (; options->type != OPTION_END; options++) {
211 if (options->short_name == *p->opt) {
212 p->opt = p->opt[1] ? p->opt + 1 : NULL;
11588263 213 return get_value(p, options, all_opts, OPT_SHORT);
4a59fd13 214 }
e0319ff5
RS
215
216 /*
217 * Handle the numerical option later, explicit one-digit
218 * options take precedence over it.
219 */
220 if (options->type == OPTION_NUMBER)
221 numopt = options;
222 }
223 if (numopt && isdigit(*p->opt)) {
224 size_t len = 1;
225 char *arg;
226 int rc;
227
228 while (isdigit(p->opt[len]))
229 len++;
230 arg = xmemdupz(p->opt, len);
231 p->opt = p->opt[len] ? p->opt + len : NULL;
232 rc = (*numopt->callback)(numopt, arg, 0) ? (-1) : 0;
233 free(arg);
234 return rc;
4a59fd13 235 }
07fe54db 236 return -2;
4a59fd13
PH
237}
238
7e7bbcb4 239static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
4a59fd13
PH
240 const struct option *options)
241{
11588263 242 const struct option *all_opts = options;
2c5495f7 243 const char *arg_end = strchrnul(arg, '=');
243e0614
JS
244 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
245 int abbrev_flags = 0, ambiguous_flags = 0;
7f275b91 246
4a59fd13 247 for (; options->type != OPTION_END; options++) {
0f1930c5
RS
248 const char *rest, *long_name = options->long_name;
249 int flags = 0, opt_flags = 0;
4a59fd13 250
0f1930c5 251 if (!long_name)
4a59fd13
PH
252 continue;
253
0f1930c5 254again:
cf4fff57
JK
255 if (!skip_prefix(arg, long_name, &rest))
256 rest = NULL;
580d5bff
PH
257 if (options->type == OPTION_ARGUMENT) {
258 if (!rest)
259 continue;
260 if (*rest == '=')
261 return opterror(options, "takes no value", flags);
262 if (*rest)
263 continue;
264 p->out[p->cpidx++] = arg - 2;
265 return 0;
266 }
4a59fd13 267 if (!rest) {
7f275b91 268 /* abbreviated? */
0f1930c5 269 if (!strncmp(long_name, arg, arg_end - arg)) {
7f275b91 270is_abbreviated:
243e0614
JS
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 }
7f275b91
JS
281 if (!(flags & OPT_UNSET) && *arg_end)
282 p->opt = arg_end + 1;
283 abbrev_option = options;
0f1930c5 284 abbrev_flags = flags ^ opt_flags;
7f275b91
JS
285 continue;
286 }
6bbfd1fa
AS
287 /* negation allowed? */
288 if (options->flags & PARSE_OPT_NONEG)
289 continue;
7f275b91 290 /* negated and abbreviated very much? */
59556548 291 if (starts_with("no-", arg)) {
7f275b91
JS
292 flags |= OPT_UNSET;
293 goto is_abbreviated;
294 }
295 /* negated? */
59556548
CC
296 if (!starts_with(arg, "no-")) {
297 if (starts_with(long_name, "no-")) {
0f1930c5
RS
298 long_name += 3;
299 opt_flags |= OPT_UNSET;
300 goto again;
301 }
4a59fd13 302 continue;
0f1930c5 303 }
4a59fd13 304 flags |= OPT_UNSET;
cf4fff57
JK
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 }
4a59fd13
PH
312 }
313 if (*rest) {
314 if (*rest != '=')
315 continue;
316 p->opt = rest + 1;
317 }
11588263 318 return get_value(p, options, all_opts, flags ^ opt_flags);
4a59fd13 319 }
243e0614
JS
320
321 if (ambiguous_option)
322 return 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);
7f275b91 329 if (abbrev_option)
11588263 330 return get_value(p, abbrev_option, all_opts, abbrev_flags);
07fe54db 331 return -2;
4a59fd13
PH
332}
333
51a9949e
RS
334static int parse_nodash_opt(struct parse_opt_ctx_t *p, const char *arg,
335 const struct option *options)
336{
11588263
JH
337 const struct option *all_opts = options;
338
51a9949e
RS
339 for (; options->type != OPTION_END; options++) {
340 if (!(options->flags & PARSE_OPT_NODASH))
341 continue;
51a9949e 342 if (options->short_name == arg[0] && arg[1] == '\0')
11588263 343 return get_value(p, options, all_opts, OPT_SHORT);
51a9949e
RS
344 }
345 return -2;
346}
347
1121a878 348static void check_typos(const char *arg, const struct option *options)
3a9f0f41
PH
349{
350 if (strlen(arg) < 3)
351 return;
352
59556548 353 if (starts_with(arg, "no-")) {
3a9f0f41
PH
354 error ("did you mean `--%s` (with two dashes ?)", arg);
355 exit(129);
356 }
357
358 for (; options->type != OPTION_END; options++) {
359 if (!options->long_name)
360 continue;
59556548 361 if (starts_with(options->long_name, arg)) {
3a9f0f41
PH
362 error ("did you mean `--%s` (with two dashes ?)", arg);
363 exit(129);
364 }
365 }
366}
367
cb9d398c
PH
368static void parse_options_check(const struct option *opts)
369{
370 int err = 0;
af465af8 371 char short_opts[128];
cb9d398c 372
af465af8 373 memset(short_opts, '\0', sizeof(short_opts));
cb9d398c
PH
374 for (; opts->type != OPTION_END; opts++) {
375 if ((opts->flags & PARSE_OPT_LASTARG_DEFAULT) &&
1e5ce570
JN
376 (opts->flags & PARSE_OPT_OPTARG))
377 err |= optbug(opts, "uses incompatible flags "
378 "LASTARG_DEFAULT and OPTARG");
af465af8
JH
379 if (opts->short_name) {
380 if (0x7F <= opts->short_name)
381 err |= optbug(opts, "invalid short name");
382 else if (short_opts[opts->short_name]++)
383 err |= optbug(opts, "short name already used");
384 }
a02dd4ff
JN
385 if (opts->flags & PARSE_OPT_NODASH &&
386 ((opts->flags & PARSE_OPT_OPTARG) ||
387 !(opts->flags & PARSE_OPT_NOARG) ||
388 !(opts->flags & PARSE_OPT_NONEG) ||
389 opts->long_name))
390 err |= optbug(opts, "uses feature "
391 "not supported for dashless options");
5c400ed2 392 switch (opts->type) {
b04ba2bb 393 case OPTION_COUNTUP:
5c400ed2
JN
394 case OPTION_BIT:
395 case OPTION_NEGBIT:
396 case OPTION_SET_INT:
5c400ed2
JN
397 case OPTION_NUMBER:
398 if ((opts->flags & PARSE_OPT_OPTARG) ||
399 !(opts->flags & PARSE_OPT_NOARG))
400 err |= optbug(opts, "should not accept an argument");
401 default:
402 ; /* ok. (usually accepts an argument) */
403 }
b6c2a0d4
JH
404 if (opts->argh &&
405 strcspn(opts->argh, " _") != strlen(opts->argh))
406 err |= optbug(opts, "multi-word argh should use dash to separate words");
cb9d398c 407 }
cb9d398c 408 if (err)
1e5ce570 409 exit(128);
cb9d398c
PH
410}
411
7e7bbcb4 412void parse_options_start(struct parse_opt_ctx_t *ctx,
37782920 413 int argc, const char **argv, const char *prefix,
9ca1169f 414 const struct option *options, int flags)
7e7bbcb4
PH
415{
416 memset(ctx, 0, sizeof(*ctx));
417 ctx->argc = argc - 1;
418 ctx->argv = argv + 1;
419 ctx->out = argv;
37782920 420 ctx->prefix = prefix;
a32a4eaa 421 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
7e7bbcb4 422 ctx->flags = flags;
0d260f9a
RS
423 if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
424 (flags & PARSE_OPT_STOP_AT_NON_OPTION))
425 die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
9ca1169f 426 parse_options_check(options);
7e7bbcb4
PH
427}
428
47e9cd28
TR
429static int usage_with_options_internal(struct parse_opt_ctx_t *,
430 const char * const *,
9c7304e3 431 const struct option *, int, int);
dd3bf0f4 432
ff43ec3e
PH
433int parse_options_step(struct parse_opt_ctx_t *ctx,
434 const struct option *options,
435 const char * const usagestr[])
4a59fd13 436{
b92891f9
RS
437 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
438
26141b5b
PH
439 /* we must reset ->opt, unknown short option leave it dangling */
440 ctx->opt = NULL;
441
ff43ec3e
PH
442 for (; ctx->argc; ctx->argc--, ctx->argv++) {
443 const char *arg = ctx->argv[0];
4a59fd13
PH
444
445 if (*arg != '-' || !arg[1]) {
51a9949e
RS
446 if (parse_nodash_opt(ctx, arg, options) == 0)
447 continue;
ff43ec3e 448 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
979240fe 449 return PARSE_OPT_NON_OPTION;
ff43ec3e 450 ctx->out[ctx->cpidx++] = ctx->argv[0];
4a59fd13
PH
451 continue;
452 }
453
454 if (arg[1] != '-') {
ff43ec3e 455 ctx->opt = arg + 1;
b92891f9 456 if (internal_help && *ctx->opt == 'h')
47e9cd28 457 return parse_options_usage(ctx, usagestr, options, 0);
07fe54db
PH
458 switch (parse_short_opt(ctx, options)) {
459 case -1:
47e9cd28 460 return parse_options_usage(ctx, usagestr, options, 1);
07fe54db 461 case -2:
38916c5b
RS
462 if (ctx->opt)
463 check_typos(arg + 1, options);
b5ce3a54 464 goto unknown;
07fe54db 465 }
ff43ec3e 466 if (ctx->opt)
3a9f0f41 467 check_typos(arg + 1, options);
ff43ec3e 468 while (ctx->opt) {
b92891f9 469 if (internal_help && *ctx->opt == 'h')
47e9cd28 470 return parse_options_usage(ctx, usagestr, options, 0);
07fe54db
PH
471 switch (parse_short_opt(ctx, options)) {
472 case -1:
47e9cd28 473 return parse_options_usage(ctx, usagestr, options, 1);
07fe54db 474 case -2:
26141b5b
PH
475 /* fake a short option thing to hide the fact that we may have
476 * started to parse aggregated stuff
477 *
478 * This is leaky, too bad.
479 */
480 ctx->argv[0] = xstrdup(ctx->opt - 1);
481 *(char *)ctx->argv[0] = '-';
b5ce3a54 482 goto unknown;
07fe54db 483 }
3a9f0f41 484 }
4a59fd13
PH
485 continue;
486 }
487
488 if (!arg[2]) { /* "--" */
ff43ec3e
PH
489 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
490 ctx->argc--;
491 ctx->argv++;
4a59fd13
PH
492 }
493 break;
494 }
495
b92891f9 496 if (internal_help && !strcmp(arg + 2, "help-all"))
47e9cd28 497 return usage_with_options_internal(ctx, usagestr, options, 1, 0);
b92891f9 498 if (internal_help && !strcmp(arg + 2, "help"))
47e9cd28 499 return parse_options_usage(ctx, usagestr, options, 0);
07fe54db
PH
500 switch (parse_long_opt(ctx, arg + 2, options)) {
501 case -1:
47e9cd28 502 return parse_options_usage(ctx, usagestr, options, 1);
07fe54db 503 case -2:
b5ce3a54 504 goto unknown;
07fe54db 505 }
b5ce3a54
RS
506 continue;
507unknown:
508 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
509 return PARSE_OPT_UNKNOWN;
510 ctx->out[ctx->cpidx++] = ctx->argv[0];
511 ctx->opt = NULL;
ff43ec3e
PH
512 }
513 return PARSE_OPT_DONE;
514}
515
516int parse_options_end(struct parse_opt_ctx_t *ctx)
517{
518 memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out));
519 ctx->out[ctx->cpidx + ctx->argc] = NULL;
520 return ctx->cpidx + ctx->argc;
521}
522
37782920
SB
523int parse_options(int argc, const char **argv, const char *prefix,
524 const struct option *options, const char * const usagestr[],
525 int flags)
ff43ec3e
PH
526{
527 struct parse_opt_ctx_t ctx;
528
9ca1169f 529 parse_options_start(&ctx, argc, argv, prefix, options, flags);
ff43ec3e
PH
530 switch (parse_options_step(&ctx, options, usagestr)) {
531 case PARSE_OPT_HELP:
532 exit(129);
979240fe 533 case PARSE_OPT_NON_OPTION:
ff43ec3e
PH
534 case PARSE_OPT_DONE:
535 break;
536 default: /* PARSE_OPT_UNKNOWN */
07fe54db
PH
537 if (ctx.argv[0][1] == '-') {
538 error("unknown option `%s'", ctx.argv[0] + 2);
b141a478 539 } else if (isascii(*ctx.opt)) {
07fe54db 540 error("unknown switch `%c'", *ctx.opt);
b141a478
EFL
541 } else {
542 error("unknown non-ascii option in string: `%s'",
543 ctx.argv[0]);
07fe54db
PH
544 }
545 usage_with_options(usagestr, options);
4a59fd13
PH
546 }
547
76759c7d 548 precompose_argv(argc, argv);
7e7bbcb4 549 return parse_options_end(&ctx);
4a59fd13 550}
d7a38c54 551
9c7304e3 552static int usage_argh(const struct option *opts, FILE *outfile)
29f25d49
SB
553{
554 const char *s;
34aec9f5 555 int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) || !opts->argh;
29f25d49
SB
556 if (opts->flags & PARSE_OPT_OPTARG)
557 if (opts->long_name)
558 s = literal ? "[=%s]" : "[=<%s>]";
559 else
560 s = literal ? "[%s]" : "[<%s>]";
561 else
562 s = literal ? " %s" : " <%s>";
c0821965 563 return utf8_fprintf(outfile, s, opts->argh ? _(opts->argh) : _("..."));
29f25d49
SB
564}
565
d7a38c54
PH
566#define USAGE_OPTS_WIDTH 24
567#define USAGE_GAP 2
568
47e9cd28
TR
569static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
570 const char * const *usagestr,
571 const struct option *opts, int full, int err)
d7a38c54 572{
9c7304e3
GS
573 FILE *outfile = err ? stderr : stdout;
574
49b61802
RS
575 if (!usagestr)
576 return PARSE_OPT_HELP;
577
47e9cd28
TR
578 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
579 fprintf(outfile, "cat <<\\EOF\n");
580
54e6dc7d 581 fprintf_ln(outfile, _("usage: %s"), _(*usagestr++));
f389c808 582 while (*usagestr && **usagestr)
54e6dc7d
NTND
583 /* TRANSLATORS: the colon here should align with the
584 one in "usage: %s" translation */
585 fprintf_ln(outfile, _(" or: %s"), _(*usagestr++));
44d86e91 586 while (*usagestr) {
54e6dc7d
NTND
587 if (**usagestr)
588 fprintf_ln(outfile, _(" %s"), _(*usagestr));
589 else
590 putchar('\n');
44d86e91
JK
591 usagestr++;
592 }
d7a38c54
PH
593
594 if (opts->type != OPTION_GROUP)
9c7304e3 595 fputc('\n', outfile);
d7a38c54
PH
596
597 for (; opts->type != OPTION_END; opts++) {
598 size_t pos;
599 int pad;
600
601 if (opts->type == OPTION_GROUP) {
9c7304e3 602 fputc('\n', outfile);
d7a38c54 603 if (*opts->help)
54e6dc7d 604 fprintf(outfile, "%s\n", _(opts->help));
d7a38c54
PH
605 continue;
606 }
dd3bf0f4
PH
607 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
608 continue;
d7a38c54 609
9c7304e3 610 pos = fprintf(outfile, " ");
cbb08c2e 611 if (opts->short_name) {
51a9949e 612 if (opts->flags & PARSE_OPT_NODASH)
9c7304e3 613 pos += fprintf(outfile, "%c", opts->short_name);
51a9949e 614 else
9c7304e3 615 pos += fprintf(outfile, "-%c", opts->short_name);
51a9949e 616 }
d7a38c54 617 if (opts->long_name && opts->short_name)
9c7304e3 618 pos += fprintf(outfile, ", ");
d7a38c54 619 if (opts->long_name)
cbb08c2e 620 pos += fprintf(outfile, "--%s", opts->long_name);
e0319ff5 621 if (opts->type == OPTION_NUMBER)
c0821965 622 pos += utf8_fprintf(outfile, _("-NUM"));
d7a38c54 623
b57c68a6
JN
624 if ((opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
625 !(opts->flags & PARSE_OPT_NOARG))
9c7304e3 626 pos += usage_argh(opts, outfile);
d7a38c54 627
f389c808
AR
628 if (pos <= USAGE_OPTS_WIDTH)
629 pad = USAGE_OPTS_WIDTH - pos;
d7a38c54 630 else {
9c7304e3 631 fputc('\n', outfile);
d7a38c54
PH
632 pad = USAGE_OPTS_WIDTH;
633 }
54e6dc7d 634 fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", _(opts->help));
d7a38c54 635 }
9c7304e3 636 fputc('\n', outfile);
f389c808 637
47e9cd28
TR
638 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
639 fputs("EOF\n", outfile);
640
ee68b87a 641 return PARSE_OPT_HELP;
d7a38c54 642}
0ce865b1 643
c2e86add 644void NORETURN usage_with_options(const char * const *usagestr,
ff43ec3e 645 const struct option *opts)
dd3bf0f4 646{
47e9cd28 647 usage_with_options_internal(NULL, usagestr, opts, 0, 1);
ff43ec3e 648 exit(129);
dd3bf0f4
PH
649}
650
c2e86add 651void NORETURN usage_msg_opt(const char *msg,
451bb210
CC
652 const char * const *usagestr,
653 const struct option *options)
654{
655 fprintf(stderr, "%s\n\n", msg);
656 usage_with_options(usagestr, options);
657}
658
47e9cd28
TR
659static int parse_options_usage(struct parse_opt_ctx_t *ctx,
660 const char * const *usagestr,
9c7304e3 661 const struct option *opts, int err)
ee68b87a 662{
47e9cd28 663 return usage_with_options_internal(ctx, usagestr, opts, 0, err);
ee68b87a
PH
664}
665
a469a101
JK
666#undef opterror
667int opterror(const struct option *opt, const char *reason, int flags)
668{
669 if (flags & OPT_SHORT)
670 return error("switch `%c' %s", opt->short_name, reason);
671 if (flags & OPT_UNSET)
672 return error("option `no-%s' %s", opt->long_name, reason);
673 return error("option `%s' %s", opt->long_name, reason);
674}