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