parse-options: avoid magic return codes
[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"
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
f41179f1
NTND
23static enum parse_opt_result get_arg(struct parse_opt_ctx_t *p,
24 const struct option *opt,
25 int flags, const char **arg)
1cc6985c
PH
26{
27 if (p->opt) {
28 *arg = p->opt;
29 p->opt = NULL;
30 } else if (p->argc == 1 && (opt->flags & PARSE_OPT_LASTARG_DEFAULT)) {
31 *arg = (const char *)opt->defval;
d5d745f9 32 } else if (p->argc > 1) {
1cc6985c
PH
33 p->argc--;
34 *arg = *++p->argv;
35 } else
9440b831 36 return error(_("%s requires a value"), optname(opt, flags));
1cc6985c
PH
37 return 0;
38}
39
df217ed6
SB
40static void fix_filename(const char *prefix, const char **file)
41{
42 if (!file || !*file || !prefix || is_absolute_path(*file)
43 || !strcmp("-", *file))
44 return;
e4da43b1 45 *file = prefix_filename(prefix, *file);
df217ed6
SB
46}
47
f41179f1
NTND
48static enum parse_opt_result opt_command_mode_error(
49 const struct option *opt,
50 const struct option *all_opts,
51 int flags)
11588263
JH
52{
53 const struct option *that;
11588263
JH
54 struct strbuf that_name = STRBUF_INIT;
55
56 /*
57 * Find the other option that was used to set the variable
58 * already, and report that this is not compatible with it.
59 */
60 for (that = all_opts; that->type != OPTION_END; that++) {
61 if (that == opt ||
62 that->type != OPTION_CMDMODE ||
63 that->value != opt->value ||
64 that->defval != *(int *)opt->value)
65 continue;
66
67 if (that->long_name)
68 strbuf_addf(&that_name, "--%s", that->long_name);
69 else
70 strbuf_addf(&that_name, "-%c", that->short_name);
9440b831
NTND
71 error(_("%s is incompatible with %s"),
72 optname(opt, flags), that_name.buf);
11588263 73 strbuf_release(&that_name);
f41179f1 74 return PARSE_OPT_ERROR;
11588263 75 }
9440b831
NTND
76 return error(_("%s : incompatible with something else"),
77 optname(opt, flags));
11588263
JH
78}
79
f41179f1
NTND
80static enum parse_opt_result get_value(struct parse_opt_ctx_t *p,
81 const struct option *opt,
82 const struct option *all_opts,
83 int flags)
4a59fd13 84{
ffe659f9 85 const char *s, *arg;
db7244bd 86 const int unset = flags & OPT_UNSET;
df217ed6 87 int err;
4a59fd13 88
db7244bd 89 if (unset && p->opt)
9440b831 90 return error(_("%s takes no value"), optname(opt, flags));
db7244bd 91 if (unset && (opt->flags & PARSE_OPT_NONEG))
9440b831 92 return error(_("%s isn't available"), optname(opt, flags));
c1f4ec9e 93 if (!(flags & OPT_SHORT) && p->opt && (opt->flags & PARSE_OPT_NOARG))
9440b831 94 return error(_("%s takes no value"), optname(opt, flags));
db7244bd 95
db7244bd 96 switch (opt->type) {
b0b3a8b6 97 case OPTION_LOWLEVEL_CALLBACK:
bf3ff338 98 return opt->ll_callback(p, opt, unset);
b0b3a8b6 99
db7244bd
PH
100 case OPTION_BIT:
101 if (unset)
102 *(int *)opt->value &= ~opt->defval;
4a59fd13 103 else
db7244bd
PH
104 *(int *)opt->value |= opt->defval;
105 return 0;
106
2f4b97f9
RS
107 case OPTION_NEGBIT:
108 if (unset)
109 *(int *)opt->value |= opt->defval;
110 else
111 *(int *)opt->value &= ~opt->defval;
112 return 0;
113
f62470c6
NTND
114 case OPTION_BITOP:
115 if (unset)
116 BUG("BITOP can't have unset form");
117 *(int *)opt->value &= ~opt->extra;
118 *(int *)opt->value |= opt->defval;
119 return 0;
120
b04ba2bb 121 case OPTION_COUNTUP:
e0070e8b
PB
122 if (*(int *)opt->value < 0)
123 *(int *)opt->value = 0;
db7244bd
PH
124 *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
125 return 0;
126
127 case OPTION_SET_INT:
128 *(int *)opt->value = unset ? 0 : opt->defval;
129 return 0;
130
11588263
JH
131 case OPTION_CMDMODE:
132 /*
133 * Giving the same mode option twice, although is unnecessary,
134 * is not a grave error, so let it pass.
135 */
136 if (*(int *)opt->value && *(int *)opt->value != opt->defval)
137 return opt_command_mode_error(opt, all_opts, flags);
138 *(int *)opt->value = opt->defval;
139 return 0;
140
4a59fd13 141 case OPTION_STRING:
1cc6985c 142 if (unset)
db7244bd 143 *(const char **)opt->value = NULL;
1cc6985c 144 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
ffe659f9 145 *(const char **)opt->value = (const char *)opt->defval;
1cc6985c
PH
146 else
147 return get_arg(p, opt, flags, (const char **)opt->value);
4a59fd13
PH
148 return 0;
149
df217ed6
SB
150 case OPTION_FILENAME:
151 err = 0;
152 if (unset)
153 *(const char **)opt->value = NULL;
154 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
155 *(const char **)opt->value = (const char *)opt->defval;
156 else
157 err = get_arg(p, opt, flags, (const char **)opt->value);
158
159 if (!err)
160 fix_filename(p->prefix, (const char **)opt->value);
161 return err;
162
ffe659f9 163 case OPTION_CALLBACK:
db7244bd 164 if (unset)
07fe54db 165 return (*opt->callback)(opt, NULL, 1) ? (-1) : 0;
db7244bd 166 if (opt->flags & PARSE_OPT_NOARG)
07fe54db 167 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
c43a2483 168 if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
07fe54db 169 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
1cc6985c
PH
170 if (get_arg(p, opt, flags, &arg))
171 return -1;
172 return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
ffe659f9 173
4a59fd13 174 case OPTION_INTEGER:
db7244bd 175 if (unset) {
4a59fd13
PH
176 *(int *)opt->value = 0;
177 return 0;
178 }
c43a2483 179 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
ffe659f9
PH
180 *(int *)opt->value = opt->defval;
181 return 0;
182 }
1cc6985c
PH
183 if (get_arg(p, opt, flags, &arg))
184 return -1;
185 *(int *)opt->value = strtol(arg, (char **)&s, 10);
4a59fd13 186 if (*s)
9440b831
NTND
187 return error(_("%s expects a numerical value"),
188 optname(opt, flags));
4a59fd13
PH
189 return 0;
190
2a514ed8
CB
191 case OPTION_MAGNITUDE:
192 if (unset) {
193 *(unsigned long *)opt->value = 0;
194 return 0;
195 }
196 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
197 *(unsigned long *)opt->value = opt->defval;
198 return 0;
199 }
200 if (get_arg(p, opt, flags, &arg))
201 return -1;
202 if (!git_parse_ulong(arg, opt->value))
9440b831
NTND
203 return error(_("%s expects a non-negative integer value"
204 " with an optional k/m/g suffix"),
205 optname(opt, flags));
2a514ed8
CB
206 return 0;
207
4a59fd13 208 default:
48a5499e 209 BUG("opt->type %d should not happen", opt->type);
4a59fd13
PH
210 }
211}
212
f41179f1
NTND
213static enum parse_opt_result parse_short_opt(struct parse_opt_ctx_t *p,
214 const struct option *options)
4a59fd13 215{
11588263 216 const struct option *all_opts = options;
e0319ff5
RS
217 const struct option *numopt = NULL;
218
4a59fd13
PH
219 for (; options->type != OPTION_END; options++) {
220 if (options->short_name == *p->opt) {
221 p->opt = p->opt[1] ? p->opt + 1 : NULL;
11588263 222 return get_value(p, options, all_opts, OPT_SHORT);
4a59fd13 223 }
e0319ff5
RS
224
225 /*
226 * Handle the numerical option later, explicit one-digit
227 * options take precedence over it.
228 */
229 if (options->type == OPTION_NUMBER)
230 numopt = options;
231 }
232 if (numopt && isdigit(*p->opt)) {
233 size_t len = 1;
234 char *arg;
235 int rc;
236
237 while (isdigit(p->opt[len]))
238 len++;
239 arg = xmemdupz(p->opt, len);
240 p->opt = p->opt[len] ? p->opt + len : NULL;
241 rc = (*numopt->callback)(numopt, arg, 0) ? (-1) : 0;
242 free(arg);
243 return rc;
4a59fd13 244 }
f41179f1 245 return PARSE_OPT_UNKNOWN;
4a59fd13
PH
246}
247
f41179f1
NTND
248static enum parse_opt_result parse_long_opt(
249 struct parse_opt_ctx_t *p, const char *arg,
250 const struct option *options)
4a59fd13 251{
11588263 252 const struct option *all_opts = options;
2c5495f7 253 const char *arg_end = strchrnul(arg, '=');
243e0614
JS
254 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
255 int abbrev_flags = 0, ambiguous_flags = 0;
7f275b91 256
4a59fd13 257 for (; options->type != OPTION_END; options++) {
0f1930c5
RS
258 const char *rest, *long_name = options->long_name;
259 int flags = 0, opt_flags = 0;
4a59fd13 260
0f1930c5 261 if (!long_name)
4a59fd13
PH
262 continue;
263
0f1930c5 264again:
cf4fff57
JK
265 if (!skip_prefix(arg, long_name, &rest))
266 rest = NULL;
580d5bff
PH
267 if (options->type == OPTION_ARGUMENT) {
268 if (!rest)
269 continue;
270 if (*rest == '=')
9440b831
NTND
271 return error(_("%s takes no value"),
272 optname(options, flags));
580d5bff
PH
273 if (*rest)
274 continue;
275 p->out[p->cpidx++] = arg - 2;
f41179f1 276 return PARSE_OPT_DONE;
580d5bff 277 }
4a59fd13 278 if (!rest) {
7f275b91 279 /* abbreviated? */
baa4adc6
NTND
280 if (!(p->flags & PARSE_OPT_KEEP_UNKNOWN) &&
281 !strncmp(long_name, arg, arg_end - arg)) {
7f275b91 282is_abbreviated:
243e0614
JS
283 if (abbrev_option) {
284 /*
285 * If this is abbreviated, it is
286 * ambiguous. So when there is no
287 * exact match later, we need to
288 * error out.
289 */
290 ambiguous_option = abbrev_option;
291 ambiguous_flags = abbrev_flags;
292 }
7f275b91
JS
293 if (!(flags & OPT_UNSET) && *arg_end)
294 p->opt = arg_end + 1;
295 abbrev_option = options;
0f1930c5 296 abbrev_flags = flags ^ opt_flags;
7f275b91
JS
297 continue;
298 }
6bbfd1fa
AS
299 /* negation allowed? */
300 if (options->flags & PARSE_OPT_NONEG)
301 continue;
7f275b91 302 /* negated and abbreviated very much? */
59556548 303 if (starts_with("no-", arg)) {
7f275b91
JS
304 flags |= OPT_UNSET;
305 goto is_abbreviated;
306 }
307 /* negated? */
59556548
CC
308 if (!starts_with(arg, "no-")) {
309 if (starts_with(long_name, "no-")) {
0f1930c5
RS
310 long_name += 3;
311 opt_flags |= OPT_UNSET;
312 goto again;
313 }
4a59fd13 314 continue;
0f1930c5 315 }
4a59fd13 316 flags |= OPT_UNSET;
cf4fff57
JK
317 if (!skip_prefix(arg + 3, long_name, &rest)) {
318 /* abbreviated and negated? */
319 if (starts_with(long_name, arg + 3))
320 goto is_abbreviated;
321 else
322 continue;
323 }
4a59fd13
PH
324 }
325 if (*rest) {
326 if (*rest != '=')
327 continue;
328 p->opt = rest + 1;
329 }
11588263 330 return get_value(p, options, all_opts, flags ^ opt_flags);
4a59fd13 331 }
243e0614 332
3bb0923f 333 if (ambiguous_option) {
89003426
NTND
334 error(_("ambiguous option: %s "
335 "(could be --%s%s or --%s%s)"),
243e0614
JS
336 arg,
337 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
338 ambiguous_option->long_name,
339 (abbrev_flags & OPT_UNSET) ? "no-" : "",
340 abbrev_option->long_name);
f41179f1 341 return PARSE_OPT_HELP;
3bb0923f 342 }
7f275b91 343 if (abbrev_option)
11588263 344 return get_value(p, abbrev_option, all_opts, abbrev_flags);
f41179f1 345 return PARSE_OPT_UNKNOWN;
4a59fd13
PH
346}
347
51a9949e
RS
348static int parse_nodash_opt(struct parse_opt_ctx_t *p, const char *arg,
349 const struct option *options)
350{
11588263
JH
351 const struct option *all_opts = options;
352
51a9949e
RS
353 for (; options->type != OPTION_END; options++) {
354 if (!(options->flags & PARSE_OPT_NODASH))
355 continue;
51a9949e 356 if (options->short_name == arg[0] && arg[1] == '\0')
11588263 357 return get_value(p, options, all_opts, OPT_SHORT);
51a9949e
RS
358 }
359 return -2;
360}
361
1121a878 362static void check_typos(const char *arg, const struct option *options)
3a9f0f41
PH
363{
364 if (strlen(arg) < 3)
365 return;
366
59556548 367 if (starts_with(arg, "no-")) {
89003426 368 error(_("did you mean `--%s` (with two dashes ?)"), arg);
3a9f0f41
PH
369 exit(129);
370 }
371
372 for (; options->type != OPTION_END; options++) {
373 if (!options->long_name)
374 continue;
59556548 375 if (starts_with(options->long_name, arg)) {
89003426 376 error(_("did you mean `--%s` (with two dashes ?)"), arg);
3a9f0f41
PH
377 exit(129);
378 }
379 }
380}
381
cb9d398c
PH
382static void parse_options_check(const struct option *opts)
383{
384 int err = 0;
af465af8 385 char short_opts[128];
cb9d398c 386
af465af8 387 memset(short_opts, '\0', sizeof(short_opts));
cb9d398c
PH
388 for (; opts->type != OPTION_END; opts++) {
389 if ((opts->flags & PARSE_OPT_LASTARG_DEFAULT) &&
1e5ce570
JN
390 (opts->flags & PARSE_OPT_OPTARG))
391 err |= optbug(opts, "uses incompatible flags "
392 "LASTARG_DEFAULT and OPTARG");
af465af8
JH
393 if (opts->short_name) {
394 if (0x7F <= opts->short_name)
395 err |= optbug(opts, "invalid short name");
396 else if (short_opts[opts->short_name]++)
397 err |= optbug(opts, "short name already used");
398 }
a02dd4ff
JN
399 if (opts->flags & PARSE_OPT_NODASH &&
400 ((opts->flags & PARSE_OPT_OPTARG) ||
401 !(opts->flags & PARSE_OPT_NOARG) ||
402 !(opts->flags & PARSE_OPT_NONEG) ||
403 opts->long_name))
404 err |= optbug(opts, "uses feature "
405 "not supported for dashless options");
5c400ed2 406 switch (opts->type) {
b04ba2bb 407 case OPTION_COUNTUP:
5c400ed2
JN
408 case OPTION_BIT:
409 case OPTION_NEGBIT:
410 case OPTION_SET_INT:
5c400ed2
JN
411 case OPTION_NUMBER:
412 if ((opts->flags & PARSE_OPT_OPTARG) ||
413 !(opts->flags & PARSE_OPT_NOARG))
414 err |= optbug(opts, "should not accept an argument");
bf3ff338
NTND
415 break;
416 case OPTION_CALLBACK:
417 if (!opts->callback)
418 BUG("OPTION_CALLBACK needs a callback");
419 if (opts->ll_callback)
420 BUG("OPTION_CALLBACK needs no ll_callback");
421 break;
422 case OPTION_LOWLEVEL_CALLBACK:
423 if (!opts->ll_callback)
424 BUG("OPTION_LOWLEVEL_CALLBACK needs a callback");
425 if (opts->callback)
426 BUG("OPTION_LOWLEVEL_CALLBACK needs no high level callback");
427 break;
5c400ed2
JN
428 default:
429 ; /* ok. (usually accepts an argument) */
430 }
b6c2a0d4
JH
431 if (opts->argh &&
432 strcspn(opts->argh, " _") != strlen(opts->argh))
433 err |= optbug(opts, "multi-word argh should use dash to separate words");
cb9d398c 434 }
cb9d398c 435 if (err)
1e5ce570 436 exit(128);
cb9d398c
PH
437}
438
7e7bbcb4 439void parse_options_start(struct parse_opt_ctx_t *ctx,
37782920 440 int argc, const char **argv, const char *prefix,
9ca1169f 441 const struct option *options, int flags)
7e7bbcb4
PH
442{
443 memset(ctx, 0, sizeof(*ctx));
202fbb33
NTND
444 ctx->argc = argc;
445 ctx->argv = argv;
446 if (!(flags & PARSE_OPT_ONE_SHOT)) {
447 ctx->argc--;
448 ctx->argv++;
449 }
450 ctx->total = ctx->argc;
451 ctx->out = argv;
37782920 452 ctx->prefix = prefix;
a32a4eaa 453 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
7e7bbcb4 454 ctx->flags = flags;
0d260f9a 455 if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
202fbb33
NTND
456 (flags & PARSE_OPT_STOP_AT_NON_OPTION) &&
457 !(flags & PARSE_OPT_ONE_SHOT))
48a5499e 458 BUG("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
202fbb33
NTND
459 if ((flags & PARSE_OPT_ONE_SHOT) &&
460 (flags & PARSE_OPT_KEEP_ARGV0))
461 BUG("Can't keep argv0 if you don't have it");
9ca1169f 462 parse_options_check(options);
7e7bbcb4
PH
463}
464
b221b5ab
NTND
465static void show_negated_gitcomp(const struct option *opts, int nr_noopts)
466{
467 int printed_dashdash = 0;
468
469 for (; opts->type != OPTION_END; opts++) {
470 int has_unset_form = 0;
471 const char *name;
472
473 if (!opts->long_name)
474 continue;
475 if (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE))
476 continue;
477 if (opts->flags & PARSE_OPT_NONEG)
478 continue;
479
480 switch (opts->type) {
481 case OPTION_STRING:
482 case OPTION_FILENAME:
483 case OPTION_INTEGER:
484 case OPTION_MAGNITUDE:
485 case OPTION_CALLBACK:
486 case OPTION_BIT:
487 case OPTION_NEGBIT:
488 case OPTION_COUNTUP:
489 case OPTION_SET_INT:
490 has_unset_form = 1;
491 break;
492 default:
493 break;
494 }
495 if (!has_unset_form)
496 continue;
497
498 if (skip_prefix(opts->long_name, "no-", &name)) {
499 if (nr_noopts < 0)
500 printf(" --%s", name);
501 } else if (nr_noopts >= 0) {
502 if (nr_noopts && !printed_dashdash) {
503 printf(" --");
504 printed_dashdash = 1;
505 }
506 printf(" --no-%s", opts->long_name);
507 nr_noopts++;
508 }
509 }
510}
511
b9d7f4b4
NTND
512static int show_gitcomp(struct parse_opt_ctx_t *ctx,
513 const struct option *opts)
514{
b221b5ab
NTND
515 const struct option *original_opts = opts;
516 int nr_noopts = 0;
517
b9d7f4b4
NTND
518 for (; opts->type != OPTION_END; opts++) {
519 const char *suffix = "";
520
521 if (!opts->long_name)
522 continue;
523 if (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE))
524 continue;
525
526 switch (opts->type) {
527 case OPTION_GROUP:
528 continue;
529 case OPTION_STRING:
530 case OPTION_FILENAME:
531 case OPTION_INTEGER:
532 case OPTION_MAGNITUDE:
533 case OPTION_CALLBACK:
534 if (opts->flags & PARSE_OPT_NOARG)
535 break;
536 if (opts->flags & PARSE_OPT_OPTARG)
537 break;
538 if (opts->flags & PARSE_OPT_LASTARG_DEFAULT)
539 break;
540 suffix = "=";
541 break;
542 default:
543 break;
544 }
ebc4a04e
NTND
545 if (opts->flags & PARSE_OPT_COMP_ARG)
546 suffix = "=";
b221b5ab
NTND
547 if (starts_with(opts->long_name, "no-"))
548 nr_noopts++;
b9d7f4b4
NTND
549 printf(" --%s%s", opts->long_name, suffix);
550 }
b221b5ab
NTND
551 show_negated_gitcomp(original_opts, -1);
552 show_negated_gitcomp(original_opts, nr_noopts);
b9d7f4b4 553 fputc('\n', stdout);
a92ec7ef 554 return PARSE_OPT_COMPLETE;
b9d7f4b4
NTND
555}
556
47e9cd28
TR
557static int usage_with_options_internal(struct parse_opt_ctx_t *,
558 const char * const *,
9c7304e3 559 const struct option *, int, int);
dd3bf0f4 560
ff43ec3e
PH
561int parse_options_step(struct parse_opt_ctx_t *ctx,
562 const struct option *options,
563 const char * const usagestr[])
4a59fd13 564{
b92891f9
RS
565 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
566
26141b5b
PH
567 /* we must reset ->opt, unknown short option leave it dangling */
568 ctx->opt = NULL;
569
ff43ec3e
PH
570 for (; ctx->argc; ctx->argc--, ctx->argv++) {
571 const char *arg = ctx->argv[0];
4a59fd13 572
202fbb33
NTND
573 if (ctx->flags & PARSE_OPT_ONE_SHOT &&
574 ctx->argc != ctx->total)
575 break;
576
4a59fd13 577 if (*arg != '-' || !arg[1]) {
51a9949e
RS
578 if (parse_nodash_opt(ctx, arg, options) == 0)
579 continue;
ff43ec3e 580 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
979240fe 581 return PARSE_OPT_NON_OPTION;
ff43ec3e 582 ctx->out[ctx->cpidx++] = ctx->argv[0];
4a59fd13
PH
583 continue;
584 }
585
5ad0d3d5
RS
586 /* lone -h asks for help */
587 if (internal_help && ctx->total == 1 && !strcmp(arg + 1, "h"))
588 goto show_usage;
589
b9d7f4b4
NTND
590 /* lone --git-completion-helper is asked by git-completion.bash */
591 if (ctx->total == 1 && !strcmp(arg + 1, "-git-completion-helper"))
592 return show_gitcomp(ctx, options);
593
4a59fd13 594 if (arg[1] != '-') {
ff43ec3e 595 ctx->opt = arg + 1;
07fe54db 596 switch (parse_short_opt(ctx, options)) {
f41179f1 597 case PARSE_OPT_ERROR:
3bb0923f 598 return PARSE_OPT_ERROR;
f41179f1 599 case PARSE_OPT_UNKNOWN:
38916c5b
RS
600 if (ctx->opt)
601 check_typos(arg + 1, options);
5ad0d3d5
RS
602 if (internal_help && *ctx->opt == 'h')
603 goto show_usage;
b5ce3a54 604 goto unknown;
f41179f1
NTND
605 case PARSE_OPT_NON_OPTION:
606 case PARSE_OPT_HELP:
607 case PARSE_OPT_COMPLETE:
608 BUG("parse_short_opt() cannot return these");
609 case PARSE_OPT_DONE:
610 break;
07fe54db 611 }
ff43ec3e 612 if (ctx->opt)
3a9f0f41 613 check_typos(arg + 1, options);
ff43ec3e 614 while (ctx->opt) {
07fe54db 615 switch (parse_short_opt(ctx, options)) {
f41179f1 616 case PARSE_OPT_ERROR:
3bb0923f 617 return PARSE_OPT_ERROR;
f41179f1 618 case PARSE_OPT_UNKNOWN:
5ad0d3d5
RS
619 if (internal_help && *ctx->opt == 'h')
620 goto show_usage;
621
26141b5b
PH
622 /* fake a short option thing to hide the fact that we may have
623 * started to parse aggregated stuff
624 *
625 * This is leaky, too bad.
626 */
627 ctx->argv[0] = xstrdup(ctx->opt - 1);
628 *(char *)ctx->argv[0] = '-';
b5ce3a54 629 goto unknown;
f41179f1
NTND
630 case PARSE_OPT_NON_OPTION:
631 case PARSE_OPT_COMPLETE:
632 case PARSE_OPT_HELP:
633 BUG("parse_short_opt() cannot return these");
634 case PARSE_OPT_DONE:
635 break;
07fe54db 636 }
3a9f0f41 637 }
4a59fd13
PH
638 continue;
639 }
640
641 if (!arg[2]) { /* "--" */
ff43ec3e
PH
642 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
643 ctx->argc--;
644 ctx->argv++;
4a59fd13
PH
645 }
646 break;
647 }
648
b92891f9 649 if (internal_help && !strcmp(arg + 2, "help-all"))
47e9cd28 650 return usage_with_options_internal(ctx, usagestr, options, 1, 0);
b92891f9 651 if (internal_help && !strcmp(arg + 2, "help"))
ac20ff6d 652 goto show_usage;
07fe54db 653 switch (parse_long_opt(ctx, arg + 2, options)) {
f41179f1 654 case PARSE_OPT_ERROR:
3bb0923f 655 return PARSE_OPT_ERROR;
f41179f1 656 case PARSE_OPT_UNKNOWN:
b5ce3a54 657 goto unknown;
f41179f1 658 case PARSE_OPT_HELP:
3bb0923f 659 goto show_usage;
f41179f1
NTND
660 case PARSE_OPT_NON_OPTION:
661 case PARSE_OPT_COMPLETE:
662 BUG("parse_long_opt() cannot return these");
663 case PARSE_OPT_DONE:
664 break;
07fe54db 665 }
b5ce3a54
RS
666 continue;
667unknown:
202fbb33
NTND
668 if (ctx->flags & PARSE_OPT_ONE_SHOT)
669 break;
b5ce3a54
RS
670 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
671 return PARSE_OPT_UNKNOWN;
672 ctx->out[ctx->cpidx++] = ctx->argv[0];
673 ctx->opt = NULL;
ff43ec3e
PH
674 }
675 return PARSE_OPT_DONE;
ac20ff6d 676
ac20ff6d 677 show_usage:
3bb0923f 678 return usage_with_options_internal(ctx, usagestr, options, 0, 0);
ff43ec3e
PH
679}
680
681int parse_options_end(struct parse_opt_ctx_t *ctx)
682{
202fbb33
NTND
683 if (ctx->flags & PARSE_OPT_ONE_SHOT)
684 return ctx->total - ctx->argc;
685
f919ffeb 686 MOVE_ARRAY(ctx->out + ctx->cpidx, ctx->argv, ctx->argc);
ff43ec3e
PH
687 ctx->out[ctx->cpidx + ctx->argc] = NULL;
688 return ctx->cpidx + ctx->argc;
689}
690
37782920
SB
691int parse_options(int argc, const char **argv, const char *prefix,
692 const struct option *options, const char * const usagestr[],
693 int flags)
ff43ec3e
PH
694{
695 struct parse_opt_ctx_t ctx;
696
9ca1169f 697 parse_options_start(&ctx, argc, argv, prefix, options, flags);
ff43ec3e
PH
698 switch (parse_options_step(&ctx, options, usagestr)) {
699 case PARSE_OPT_HELP:
3bb0923f 700 case PARSE_OPT_ERROR:
ff43ec3e 701 exit(129);
a92ec7ef
NTND
702 case PARSE_OPT_COMPLETE:
703 exit(0);
979240fe 704 case PARSE_OPT_NON_OPTION:
ff43ec3e
PH
705 case PARSE_OPT_DONE:
706 break;
707 default: /* PARSE_OPT_UNKNOWN */
07fe54db 708 if (ctx.argv[0][1] == '-') {
89003426 709 error(_("unknown option `%s'"), ctx.argv[0] + 2);
b141a478 710 } else if (isascii(*ctx.opt)) {
89003426 711 error(_("unknown switch `%c'"), *ctx.opt);
b141a478 712 } else {
89003426 713 error(_("unknown non-ascii option in string: `%s'"),
b141a478 714 ctx.argv[0]);
07fe54db
PH
715 }
716 usage_with_options(usagestr, options);
4a59fd13
PH
717 }
718
76759c7d 719 precompose_argv(argc, argv);
7e7bbcb4 720 return parse_options_end(&ctx);
4a59fd13 721}
d7a38c54 722
9c7304e3 723static int usage_argh(const struct option *opts, FILE *outfile)
29f25d49
SB
724{
725 const char *s;
5f0df44c
RS
726 int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
727 !opts->argh || !!strpbrk(opts->argh, "()<>[]|");
29f25d49
SB
728 if (opts->flags & PARSE_OPT_OPTARG)
729 if (opts->long_name)
730 s = literal ? "[=%s]" : "[=<%s>]";
731 else
732 s = literal ? "[%s]" : "[<%s>]";
733 else
734 s = literal ? " %s" : " <%s>";
c0821965 735 return utf8_fprintf(outfile, s, opts->argh ? _(opts->argh) : _("..."));
29f25d49
SB
736}
737
d7a38c54
PH
738#define USAGE_OPTS_WIDTH 24
739#define USAGE_GAP 2
740
47e9cd28
TR
741static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
742 const char * const *usagestr,
743 const struct option *opts, int full, int err)
d7a38c54 744{
9c7304e3 745 FILE *outfile = err ? stderr : stdout;
a6304fa4 746 int need_newline;
9c7304e3 747
49b61802
RS
748 if (!usagestr)
749 return PARSE_OPT_HELP;
750
47e9cd28
TR
751 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
752 fprintf(outfile, "cat <<\\EOF\n");
753
54e6dc7d 754 fprintf_ln(outfile, _("usage: %s"), _(*usagestr++));
f389c808 755 while (*usagestr && **usagestr)
66f5f6dc
ÆAB
756 /*
757 * TRANSLATORS: the colon here should align with the
758 * one in "usage: %s" translation.
759 */
54e6dc7d 760 fprintf_ln(outfile, _(" or: %s"), _(*usagestr++));
44d86e91 761 while (*usagestr) {
54e6dc7d
NTND
762 if (**usagestr)
763 fprintf_ln(outfile, _(" %s"), _(*usagestr));
764 else
1a9bf1e1 765 fputc('\n', outfile);
44d86e91
JK
766 usagestr++;
767 }
d7a38c54 768
a6304fa4 769 need_newline = 1;
d7a38c54
PH
770
771 for (; opts->type != OPTION_END; opts++) {
772 size_t pos;
773 int pad;
774
775 if (opts->type == OPTION_GROUP) {
9c7304e3 776 fputc('\n', outfile);
a6304fa4 777 need_newline = 0;
d7a38c54 778 if (*opts->help)
54e6dc7d 779 fprintf(outfile, "%s\n", _(opts->help));
d7a38c54
PH
780 continue;
781 }
dd3bf0f4
PH
782 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
783 continue;
d7a38c54 784
a6304fa4
BC
785 if (need_newline) {
786 fputc('\n', outfile);
787 need_newline = 0;
788 }
789
9c7304e3 790 pos = fprintf(outfile, " ");
cbb08c2e 791 if (opts->short_name) {
51a9949e 792 if (opts->flags & PARSE_OPT_NODASH)
9c7304e3 793 pos += fprintf(outfile, "%c", opts->short_name);
51a9949e 794 else
9c7304e3 795 pos += fprintf(outfile, "-%c", opts->short_name);
51a9949e 796 }
d7a38c54 797 if (opts->long_name && opts->short_name)
9c7304e3 798 pos += fprintf(outfile, ", ");
d7a38c54 799 if (opts->long_name)
cbb08c2e 800 pos += fprintf(outfile, "--%s", opts->long_name);
e0319ff5 801 if (opts->type == OPTION_NUMBER)
c0821965 802 pos += utf8_fprintf(outfile, _("-NUM"));
d7a38c54 803
b57c68a6
JN
804 if ((opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
805 !(opts->flags & PARSE_OPT_NOARG))
9c7304e3 806 pos += usage_argh(opts, outfile);
d7a38c54 807
f389c808
AR
808 if (pos <= USAGE_OPTS_WIDTH)
809 pad = USAGE_OPTS_WIDTH - pos;
d7a38c54 810 else {
9c7304e3 811 fputc('\n', outfile);
d7a38c54
PH
812 pad = USAGE_OPTS_WIDTH;
813 }
54e6dc7d 814 fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", _(opts->help));
d7a38c54 815 }
9c7304e3 816 fputc('\n', outfile);
f389c808 817
47e9cd28
TR
818 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
819 fputs("EOF\n", outfile);
820
ee68b87a 821 return PARSE_OPT_HELP;
d7a38c54 822}
0ce865b1 823
c2e86add 824void NORETURN usage_with_options(const char * const *usagestr,
ff43ec3e 825 const struct option *opts)
dd3bf0f4 826{
47e9cd28 827 usage_with_options_internal(NULL, usagestr, opts, 0, 1);
ff43ec3e 828 exit(129);
dd3bf0f4
PH
829}
830
c2e86add 831void NORETURN usage_msg_opt(const char *msg,
451bb210
CC
832 const char * const *usagestr,
833 const struct option *options)
834{
87433261 835 fprintf(stderr, "fatal: %s\n\n", msg);
451bb210
CC
836 usage_with_options(usagestr, options);
837}
838
9440b831 839const char *optname(const struct option *opt, int flags)
a469a101 840{
9440b831
NTND
841 static struct strbuf sb = STRBUF_INIT;
842
843 strbuf_reset(&sb);
a469a101 844 if (flags & OPT_SHORT)
9440b831
NTND
845 strbuf_addf(&sb, "switch `%c'", opt->short_name);
846 else if (flags & OPT_UNSET)
847 strbuf_addf(&sb, "option `no-%s'", opt->long_name);
848 else
849 strbuf_addf(&sb, "option `%s'", opt->long_name);
850
851 return sb.buf;
a469a101 852}