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