]> git.ipfire.org Git - thirdparty/git.git/blame - parse-options.c
parse-options.c: turn some die() to BUG()
[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"
b2141fc1 4#include "config.h"
269defdf 5#include "commit.h"
73e9da01 6#include "color.h"
c0821965 7#include "utf8.h"
4a59fd13
PH
8
9#define OPT_SHORT 1
10#define OPT_UNSET 2
11
1f275b7c 12int optbug(const struct option *opt, const char *reason)
1e5ce570 13{
af465af8
JH
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);
1e5ce570 18 return error("BUG: option '%s' %s", opt->long_name, reason);
af465af8 19 }
1e5ce570
JN
20 return error("BUG: switch '%c' %s", opt->short_name, reason);
21}
22
1cc6985c
PH
23static int get_arg(struct parse_opt_ctx_t *p, const struct option *opt,
24 int flags, const char **arg)
25{
26 if (p->opt) {
27 *arg = p->opt;
28 p->opt = NULL;
29 } else if (p->argc == 1 && (opt->flags & PARSE_OPT_LASTARG_DEFAULT)) {
30 *arg = (const char *)opt->defval;
d5d745f9 31 } else if (p->argc > 1) {
1cc6985c
PH
32 p->argc--;
33 *arg = *++p->argv;
34 } else
9440b831 35 return error(_("%s requires a value"), optname(opt, flags));
1cc6985c
PH
36 return 0;
37}
38
df217ed6
SB
39static void fix_filename(const char *prefix, const char **file)
40{
41 if (!file || !*file || !prefix || is_absolute_path(*file)
42 || !strcmp("-", *file))
43 return;
e4da43b1 44 *file = prefix_filename(prefix, *file);
df217ed6
SB
45}
46
11588263
JH
47static int opt_command_mode_error(const struct option *opt,
48 const struct option *all_opts,
49 int flags)
50{
51 const struct option *that;
11588263
JH
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);
9440b831
NTND
69 error(_("%s is incompatible with %s"),
70 optname(opt, flags), that_name.buf);
11588263 71 strbuf_release(&that_name);
11588263
JH
72 return -1;
73 }
9440b831
NTND
74 return error(_("%s : incompatible with something else"),
75 optname(opt, flags));
11588263
JH
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)
9440b831 88 return error(_("%s takes no value"), optname(opt, flags));
db7244bd 89 if (unset && (opt->flags & PARSE_OPT_NONEG))
9440b831 90 return error(_("%s isn't available"), optname(opt, flags));
c1f4ec9e 91 if (!(flags & OPT_SHORT) && p->opt && (opt->flags & PARSE_OPT_NOARG))
9440b831 92 return error(_("%s takes no value"), optname(opt, 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:
e0070e8b
PB
113 if (*(int *)opt->value < 0)
114 *(int *)opt->value = 0;
db7244bd
PH
115 *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
116 return 0;
117
118 case OPTION_SET_INT:
119 *(int *)opt->value = unset ? 0 : opt->defval;
120 return 0;
121
11588263
JH
122 case OPTION_CMDMODE:
123 /*
124 * Giving the same mode option twice, although is unnecessary,
125 * is not a grave error, so let it pass.
126 */
127 if (*(int *)opt->value && *(int *)opt->value != opt->defval)
128 return opt_command_mode_error(opt, all_opts, flags);
129 *(int *)opt->value = opt->defval;
130 return 0;
131
4a59fd13 132 case OPTION_STRING:
1cc6985c 133 if (unset)
db7244bd 134 *(const char **)opt->value = NULL;
1cc6985c 135 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
ffe659f9 136 *(const char **)opt->value = (const char *)opt->defval;
1cc6985c
PH
137 else
138 return get_arg(p, opt, flags, (const char **)opt->value);
4a59fd13
PH
139 return 0;
140
df217ed6
SB
141 case OPTION_FILENAME:
142 err = 0;
143 if (unset)
144 *(const char **)opt->value = NULL;
145 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
146 *(const char **)opt->value = (const char *)opt->defval;
147 else
148 err = get_arg(p, opt, flags, (const char **)opt->value);
149
150 if (!err)
151 fix_filename(p->prefix, (const char **)opt->value);
152 return err;
153
ffe659f9 154 case OPTION_CALLBACK:
db7244bd 155 if (unset)
07fe54db 156 return (*opt->callback)(opt, NULL, 1) ? (-1) : 0;
db7244bd 157 if (opt->flags & PARSE_OPT_NOARG)
07fe54db 158 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
c43a2483 159 if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
07fe54db 160 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
1cc6985c
PH
161 if (get_arg(p, opt, flags, &arg))
162 return -1;
163 return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
ffe659f9 164
4a59fd13 165 case OPTION_INTEGER:
db7244bd 166 if (unset) {
4a59fd13
PH
167 *(int *)opt->value = 0;
168 return 0;
169 }
c43a2483 170 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
ffe659f9
PH
171 *(int *)opt->value = opt->defval;
172 return 0;
173 }
1cc6985c
PH
174 if (get_arg(p, opt, flags, &arg))
175 return -1;
176 *(int *)opt->value = strtol(arg, (char **)&s, 10);
4a59fd13 177 if (*s)
9440b831
NTND
178 return error(_("%s expects a numerical value"),
179 optname(opt, flags));
4a59fd13
PH
180 return 0;
181
2a514ed8
CB
182 case OPTION_MAGNITUDE:
183 if (unset) {
184 *(unsigned long *)opt->value = 0;
185 return 0;
186 }
187 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
188 *(unsigned long *)opt->value = opt->defval;
189 return 0;
190 }
191 if (get_arg(p, opt, flags, &arg))
192 return -1;
193 if (!git_parse_ulong(arg, opt->value))
9440b831
NTND
194 return error(_("%s expects a non-negative integer value"
195 " with an optional k/m/g suffix"),
196 optname(opt, flags));
2a514ed8
CB
197 return 0;
198
4a59fd13 199 default:
48a5499e 200 BUG("opt->type %d should not happen", opt->type);
4a59fd13
PH
201 }
202}
203
7e7bbcb4 204static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)
4a59fd13 205{
11588263 206 const struct option *all_opts = options;
e0319ff5
RS
207 const struct option *numopt = NULL;
208
4a59fd13
PH
209 for (; options->type != OPTION_END; options++) {
210 if (options->short_name == *p->opt) {
211 p->opt = p->opt[1] ? p->opt + 1 : NULL;
11588263 212 return get_value(p, options, all_opts, OPT_SHORT);
4a59fd13 213 }
e0319ff5
RS
214
215 /*
216 * Handle the numerical option later, explicit one-digit
217 * options take precedence over it.
218 */
219 if (options->type == OPTION_NUMBER)
220 numopt = options;
221 }
222 if (numopt && isdigit(*p->opt)) {
223 size_t len = 1;
224 char *arg;
225 int rc;
226
227 while (isdigit(p->opt[len]))
228 len++;
229 arg = xmemdupz(p->opt, len);
230 p->opt = p->opt[len] ? p->opt + len : NULL;
231 rc = (*numopt->callback)(numopt, arg, 0) ? (-1) : 0;
232 free(arg);
233 return rc;
4a59fd13 234 }
07fe54db 235 return -2;
4a59fd13
PH
236}
237
7e7bbcb4 238static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
4a59fd13
PH
239 const struct option *options)
240{
11588263 241 const struct option *all_opts = options;
2c5495f7 242 const char *arg_end = strchrnul(arg, '=');
243e0614
JS
243 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
244 int abbrev_flags = 0, ambiguous_flags = 0;
7f275b91 245
4a59fd13 246 for (; options->type != OPTION_END; options++) {
0f1930c5
RS
247 const char *rest, *long_name = options->long_name;
248 int flags = 0, opt_flags = 0;
4a59fd13 249
0f1930c5 250 if (!long_name)
4a59fd13
PH
251 continue;
252
0f1930c5 253again:
cf4fff57
JK
254 if (!skip_prefix(arg, long_name, &rest))
255 rest = NULL;
580d5bff
PH
256 if (options->type == OPTION_ARGUMENT) {
257 if (!rest)
258 continue;
259 if (*rest == '=')
9440b831
NTND
260 return error(_("%s takes no value"),
261 optname(options, flags));
580d5bff
PH
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 320
3bb0923f
PSU
321 if (ambiguous_option) {
322 error("Ambiguous option: %s "
243e0614
JS
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);
3bb0923f
PSU
329 return -3;
330 }
7f275b91 331 if (abbrev_option)
11588263 332 return get_value(p, abbrev_option, all_opts, abbrev_flags);
07fe54db 333 return -2;
4a59fd13
PH
334}
335
51a9949e
RS
336static int parse_nodash_opt(struct parse_opt_ctx_t *p, const char *arg,
337 const struct option *options)
338{
11588263
JH
339 const struct option *all_opts = options;
340
51a9949e
RS
341 for (; options->type != OPTION_END; options++) {
342 if (!(options->flags & PARSE_OPT_NODASH))
343 continue;
51a9949e 344 if (options->short_name == arg[0] && arg[1] == '\0')
11588263 345 return get_value(p, options, all_opts, OPT_SHORT);
51a9949e
RS
346 }
347 return -2;
348}
349
1121a878 350static void check_typos(const char *arg, const struct option *options)
3a9f0f41
PH
351{
352 if (strlen(arg) < 3)
353 return;
354
59556548 355 if (starts_with(arg, "no-")) {
3a9f0f41
PH
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;
59556548 363 if (starts_with(options->long_name, arg)) {
3a9f0f41
PH
364 error ("did you mean `--%s` (with two dashes ?)", arg);
365 exit(129);
366 }
367 }
368}
369
cb9d398c
PH
370static void parse_options_check(const struct option *opts)
371{
372 int err = 0;
af465af8 373 char short_opts[128];
cb9d398c 374
af465af8 375 memset(short_opts, '\0', sizeof(short_opts));
cb9d398c
PH
376 for (; opts->type != OPTION_END; opts++) {
377 if ((opts->flags & PARSE_OPT_LASTARG_DEFAULT) &&
1e5ce570
JN
378 (opts->flags & PARSE_OPT_OPTARG))
379 err |= optbug(opts, "uses incompatible flags "
380 "LASTARG_DEFAULT and OPTARG");
af465af8
JH
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 }
a02dd4ff
JN
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");
5c400ed2 394 switch (opts->type) {
b04ba2bb 395 case OPTION_COUNTUP:
5c400ed2
JN
396 case OPTION_BIT:
397 case OPTION_NEGBIT:
398 case OPTION_SET_INT:
5c400ed2
JN
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 }
b6c2a0d4
JH
406 if (opts->argh &&
407 strcspn(opts->argh, " _") != strlen(opts->argh))
408 err |= optbug(opts, "multi-word argh should use dash to separate words");
cb9d398c 409 }
cb9d398c 410 if (err)
1e5ce570 411 exit(128);
cb9d398c
PH
412}
413
7e7bbcb4 414void parse_options_start(struct parse_opt_ctx_t *ctx,
37782920 415 int argc, const char **argv, const char *prefix,
9ca1169f 416 const struct option *options, int flags)
7e7bbcb4
PH
417{
418 memset(ctx, 0, sizeof(*ctx));
5ad0d3d5 419 ctx->argc = ctx->total = argc - 1;
7e7bbcb4
PH
420 ctx->argv = argv + 1;
421 ctx->out = argv;
37782920 422 ctx->prefix = prefix;
a32a4eaa 423 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
7e7bbcb4 424 ctx->flags = flags;
0d260f9a
RS
425 if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
426 (flags & PARSE_OPT_STOP_AT_NON_OPTION))
48a5499e 427 BUG("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
9ca1169f 428 parse_options_check(options);
7e7bbcb4
PH
429}
430
b221b5ab
NTND
431static void show_negated_gitcomp(const struct option *opts, int nr_noopts)
432{
433 int printed_dashdash = 0;
434
435 for (; opts->type != OPTION_END; opts++) {
436 int has_unset_form = 0;
437 const char *name;
438
439 if (!opts->long_name)
440 continue;
441 if (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE))
442 continue;
443 if (opts->flags & PARSE_OPT_NONEG)
444 continue;
445
446 switch (opts->type) {
447 case OPTION_STRING:
448 case OPTION_FILENAME:
449 case OPTION_INTEGER:
450 case OPTION_MAGNITUDE:
451 case OPTION_CALLBACK:
452 case OPTION_BIT:
453 case OPTION_NEGBIT:
454 case OPTION_COUNTUP:
455 case OPTION_SET_INT:
456 has_unset_form = 1;
457 break;
458 default:
459 break;
460 }
461 if (!has_unset_form)
462 continue;
463
464 if (skip_prefix(opts->long_name, "no-", &name)) {
465 if (nr_noopts < 0)
466 printf(" --%s", name);
467 } else if (nr_noopts >= 0) {
468 if (nr_noopts && !printed_dashdash) {
469 printf(" --");
470 printed_dashdash = 1;
471 }
472 printf(" --no-%s", opts->long_name);
473 nr_noopts++;
474 }
475 }
476}
477
b9d7f4b4
NTND
478static int show_gitcomp(struct parse_opt_ctx_t *ctx,
479 const struct option *opts)
480{
b221b5ab
NTND
481 const struct option *original_opts = opts;
482 int nr_noopts = 0;
483
b9d7f4b4
NTND
484 for (; opts->type != OPTION_END; opts++) {
485 const char *suffix = "";
486
487 if (!opts->long_name)
488 continue;
489 if (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE))
490 continue;
491
492 switch (opts->type) {
493 case OPTION_GROUP:
494 continue;
495 case OPTION_STRING:
496 case OPTION_FILENAME:
497 case OPTION_INTEGER:
498 case OPTION_MAGNITUDE:
499 case OPTION_CALLBACK:
500 if (opts->flags & PARSE_OPT_NOARG)
501 break;
502 if (opts->flags & PARSE_OPT_OPTARG)
503 break;
504 if (opts->flags & PARSE_OPT_LASTARG_DEFAULT)
505 break;
506 suffix = "=";
507 break;
508 default:
509 break;
510 }
ebc4a04e
NTND
511 if (opts->flags & PARSE_OPT_COMP_ARG)
512 suffix = "=";
b221b5ab
NTND
513 if (starts_with(opts->long_name, "no-"))
514 nr_noopts++;
b9d7f4b4
NTND
515 printf(" --%s%s", opts->long_name, suffix);
516 }
b221b5ab
NTND
517 show_negated_gitcomp(original_opts, -1);
518 show_negated_gitcomp(original_opts, nr_noopts);
b9d7f4b4
NTND
519 fputc('\n', stdout);
520 exit(0);
521}
522
47e9cd28
TR
523static int usage_with_options_internal(struct parse_opt_ctx_t *,
524 const char * const *,
9c7304e3 525 const struct option *, int, int);
dd3bf0f4 526
ff43ec3e
PH
527int parse_options_step(struct parse_opt_ctx_t *ctx,
528 const struct option *options,
529 const char * const usagestr[])
4a59fd13 530{
b92891f9
RS
531 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
532
26141b5b
PH
533 /* we must reset ->opt, unknown short option leave it dangling */
534 ctx->opt = NULL;
535
ff43ec3e
PH
536 for (; ctx->argc; ctx->argc--, ctx->argv++) {
537 const char *arg = ctx->argv[0];
4a59fd13
PH
538
539 if (*arg != '-' || !arg[1]) {
51a9949e
RS
540 if (parse_nodash_opt(ctx, arg, options) == 0)
541 continue;
ff43ec3e 542 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
979240fe 543 return PARSE_OPT_NON_OPTION;
ff43ec3e 544 ctx->out[ctx->cpidx++] = ctx->argv[0];
4a59fd13
PH
545 continue;
546 }
547
5ad0d3d5
RS
548 /* lone -h asks for help */
549 if (internal_help && ctx->total == 1 && !strcmp(arg + 1, "h"))
550 goto show_usage;
551
b9d7f4b4
NTND
552 /* lone --git-completion-helper is asked by git-completion.bash */
553 if (ctx->total == 1 && !strcmp(arg + 1, "-git-completion-helper"))
554 return show_gitcomp(ctx, options);
555
4a59fd13 556 if (arg[1] != '-') {
ff43ec3e 557 ctx->opt = arg + 1;
07fe54db
PH
558 switch (parse_short_opt(ctx, options)) {
559 case -1:
3bb0923f 560 return PARSE_OPT_ERROR;
07fe54db 561 case -2:
38916c5b
RS
562 if (ctx->opt)
563 check_typos(arg + 1, options);
5ad0d3d5
RS
564 if (internal_help && *ctx->opt == 'h')
565 goto show_usage;
b5ce3a54 566 goto unknown;
07fe54db 567 }
ff43ec3e 568 if (ctx->opt)
3a9f0f41 569 check_typos(arg + 1, options);
ff43ec3e 570 while (ctx->opt) {
07fe54db
PH
571 switch (parse_short_opt(ctx, options)) {
572 case -1:
3bb0923f 573 return PARSE_OPT_ERROR;
07fe54db 574 case -2:
5ad0d3d5
RS
575 if (internal_help && *ctx->opt == 'h')
576 goto show_usage;
577
26141b5b
PH
578 /* fake a short option thing to hide the fact that we may have
579 * started to parse aggregated stuff
580 *
581 * This is leaky, too bad.
582 */
583 ctx->argv[0] = xstrdup(ctx->opt - 1);
584 *(char *)ctx->argv[0] = '-';
b5ce3a54 585 goto unknown;
07fe54db 586 }
3a9f0f41 587 }
4a59fd13
PH
588 continue;
589 }
590
591 if (!arg[2]) { /* "--" */
ff43ec3e
PH
592 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
593 ctx->argc--;
594 ctx->argv++;
4a59fd13
PH
595 }
596 break;
597 }
598
b92891f9 599 if (internal_help && !strcmp(arg + 2, "help-all"))
47e9cd28 600 return usage_with_options_internal(ctx, usagestr, options, 1, 0);
b92891f9 601 if (internal_help && !strcmp(arg + 2, "help"))
ac20ff6d 602 goto show_usage;
07fe54db
PH
603 switch (parse_long_opt(ctx, arg + 2, options)) {
604 case -1:
3bb0923f 605 return PARSE_OPT_ERROR;
07fe54db 606 case -2:
b5ce3a54 607 goto unknown;
3bb0923f
PSU
608 case -3:
609 goto show_usage;
07fe54db 610 }
b5ce3a54
RS
611 continue;
612unknown:
613 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
614 return PARSE_OPT_UNKNOWN;
615 ctx->out[ctx->cpidx++] = ctx->argv[0];
616 ctx->opt = NULL;
ff43ec3e
PH
617 }
618 return PARSE_OPT_DONE;
ac20ff6d 619
ac20ff6d 620 show_usage:
3bb0923f 621 return usage_with_options_internal(ctx, usagestr, options, 0, 0);
ff43ec3e
PH
622}
623
624int parse_options_end(struct parse_opt_ctx_t *ctx)
625{
f919ffeb 626 MOVE_ARRAY(ctx->out + ctx->cpidx, ctx->argv, ctx->argc);
ff43ec3e
PH
627 ctx->out[ctx->cpidx + ctx->argc] = NULL;
628 return ctx->cpidx + ctx->argc;
629}
630
37782920
SB
631int parse_options(int argc, const char **argv, const char *prefix,
632 const struct option *options, const char * const usagestr[],
633 int flags)
ff43ec3e
PH
634{
635 struct parse_opt_ctx_t ctx;
636
9ca1169f 637 parse_options_start(&ctx, argc, argv, prefix, options, flags);
ff43ec3e
PH
638 switch (parse_options_step(&ctx, options, usagestr)) {
639 case PARSE_OPT_HELP:
3bb0923f 640 case PARSE_OPT_ERROR:
ff43ec3e 641 exit(129);
979240fe 642 case PARSE_OPT_NON_OPTION:
ff43ec3e
PH
643 case PARSE_OPT_DONE:
644 break;
645 default: /* PARSE_OPT_UNKNOWN */
07fe54db
PH
646 if (ctx.argv[0][1] == '-') {
647 error("unknown option `%s'", ctx.argv[0] + 2);
b141a478 648 } else if (isascii(*ctx.opt)) {
07fe54db 649 error("unknown switch `%c'", *ctx.opt);
b141a478
EFL
650 } else {
651 error("unknown non-ascii option in string: `%s'",
652 ctx.argv[0]);
07fe54db
PH
653 }
654 usage_with_options(usagestr, options);
4a59fd13
PH
655 }
656
76759c7d 657 precompose_argv(argc, argv);
7e7bbcb4 658 return parse_options_end(&ctx);
4a59fd13 659}
d7a38c54 660
9c7304e3 661static int usage_argh(const struct option *opts, FILE *outfile)
29f25d49
SB
662{
663 const char *s;
5f0df44c
RS
664 int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
665 !opts->argh || !!strpbrk(opts->argh, "()<>[]|");
29f25d49
SB
666 if (opts->flags & PARSE_OPT_OPTARG)
667 if (opts->long_name)
668 s = literal ? "[=%s]" : "[=<%s>]";
669 else
670 s = literal ? "[%s]" : "[<%s>]";
671 else
672 s = literal ? " %s" : " <%s>";
c0821965 673 return utf8_fprintf(outfile, s, opts->argh ? _(opts->argh) : _("..."));
29f25d49
SB
674}
675
d7a38c54
PH
676#define USAGE_OPTS_WIDTH 24
677#define USAGE_GAP 2
678
47e9cd28
TR
679static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
680 const char * const *usagestr,
681 const struct option *opts, int full, int err)
d7a38c54 682{
9c7304e3 683 FILE *outfile = err ? stderr : stdout;
a6304fa4 684 int need_newline;
9c7304e3 685
49b61802
RS
686 if (!usagestr)
687 return PARSE_OPT_HELP;
688
47e9cd28
TR
689 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
690 fprintf(outfile, "cat <<\\EOF\n");
691
54e6dc7d 692 fprintf_ln(outfile, _("usage: %s"), _(*usagestr++));
f389c808 693 while (*usagestr && **usagestr)
66f5f6dc
ÆAB
694 /*
695 * TRANSLATORS: the colon here should align with the
696 * one in "usage: %s" translation.
697 */
54e6dc7d 698 fprintf_ln(outfile, _(" or: %s"), _(*usagestr++));
44d86e91 699 while (*usagestr) {
54e6dc7d
NTND
700 if (**usagestr)
701 fprintf_ln(outfile, _(" %s"), _(*usagestr));
702 else
1a9bf1e1 703 fputc('\n', outfile);
44d86e91
JK
704 usagestr++;
705 }
d7a38c54 706
a6304fa4 707 need_newline = 1;
d7a38c54
PH
708
709 for (; opts->type != OPTION_END; opts++) {
710 size_t pos;
711 int pad;
712
713 if (opts->type == OPTION_GROUP) {
9c7304e3 714 fputc('\n', outfile);
a6304fa4 715 need_newline = 0;
d7a38c54 716 if (*opts->help)
54e6dc7d 717 fprintf(outfile, "%s\n", _(opts->help));
d7a38c54
PH
718 continue;
719 }
dd3bf0f4
PH
720 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
721 continue;
d7a38c54 722
a6304fa4
BC
723 if (need_newline) {
724 fputc('\n', outfile);
725 need_newline = 0;
726 }
727
9c7304e3 728 pos = fprintf(outfile, " ");
cbb08c2e 729 if (opts->short_name) {
51a9949e 730 if (opts->flags & PARSE_OPT_NODASH)
9c7304e3 731 pos += fprintf(outfile, "%c", opts->short_name);
51a9949e 732 else
9c7304e3 733 pos += fprintf(outfile, "-%c", opts->short_name);
51a9949e 734 }
d7a38c54 735 if (opts->long_name && opts->short_name)
9c7304e3 736 pos += fprintf(outfile, ", ");
d7a38c54 737 if (opts->long_name)
cbb08c2e 738 pos += fprintf(outfile, "--%s", opts->long_name);
e0319ff5 739 if (opts->type == OPTION_NUMBER)
c0821965 740 pos += utf8_fprintf(outfile, _("-NUM"));
d7a38c54 741
b57c68a6
JN
742 if ((opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
743 !(opts->flags & PARSE_OPT_NOARG))
9c7304e3 744 pos += usage_argh(opts, outfile);
d7a38c54 745
f389c808
AR
746 if (pos <= USAGE_OPTS_WIDTH)
747 pad = USAGE_OPTS_WIDTH - pos;
d7a38c54 748 else {
9c7304e3 749 fputc('\n', outfile);
d7a38c54
PH
750 pad = USAGE_OPTS_WIDTH;
751 }
54e6dc7d 752 fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", _(opts->help));
d7a38c54 753 }
9c7304e3 754 fputc('\n', outfile);
f389c808 755
47e9cd28
TR
756 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
757 fputs("EOF\n", outfile);
758
ee68b87a 759 return PARSE_OPT_HELP;
d7a38c54 760}
0ce865b1 761
c2e86add 762void NORETURN usage_with_options(const char * const *usagestr,
ff43ec3e 763 const struct option *opts)
dd3bf0f4 764{
47e9cd28 765 usage_with_options_internal(NULL, usagestr, opts, 0, 1);
ff43ec3e 766 exit(129);
dd3bf0f4
PH
767}
768
c2e86add 769void NORETURN usage_msg_opt(const char *msg,
451bb210
CC
770 const char * const *usagestr,
771 const struct option *options)
772{
87433261 773 fprintf(stderr, "fatal: %s\n\n", msg);
451bb210
CC
774 usage_with_options(usagestr, options);
775}
776
9440b831 777const char *optname(const struct option *opt, int flags)
a469a101 778{
9440b831
NTND
779 static struct strbuf sb = STRBUF_INIT;
780
781 strbuf_reset(&sb);
a469a101 782 if (flags & OPT_SHORT)
9440b831
NTND
783 strbuf_addf(&sb, "switch `%c'", opt->short_name);
784 else if (flags & OPT_UNSET)
785 strbuf_addf(&sb, "option `no-%s'", opt->long_name);
786 else
787 strbuf_addf(&sb, "option `%s'", opt->long_name);
788
789 return sb.buf;
a469a101 790}