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