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