]> git.ipfire.org Git - thirdparty/git.git/blame - parse-options.c
Merge branch 'jt/t5500-unflake'
[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 ||
bc8620b4 64 !(that->flags & PARSE_OPT_CMDMODE) ||
11588263
JH
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
bc8620b4
PB
98 /*
99 * Giving the same mode option twice, although unnecessary,
100 * is not a grave error, so let it pass.
101 */
102 if ((opt->flags & PARSE_OPT_CMDMODE) &&
103 *(int *)opt->value && *(int *)opt->value != opt->defval)
104 return opt_command_mode_error(opt, all_opts, flags);
105
db7244bd 106 switch (opt->type) {
b0b3a8b6 107 case OPTION_LOWLEVEL_CALLBACK:
3ebbe289 108 return opt->ll_callback(p, opt, NULL, unset);
b0b3a8b6 109
db7244bd
PH
110 case OPTION_BIT:
111 if (unset)
112 *(int *)opt->value &= ~opt->defval;
4a59fd13 113 else
db7244bd
PH
114 *(int *)opt->value |= opt->defval;
115 return 0;
116
2f4b97f9
RS
117 case OPTION_NEGBIT:
118 if (unset)
119 *(int *)opt->value |= opt->defval;
120 else
121 *(int *)opt->value &= ~opt->defval;
122 return 0;
123
f62470c6
NTND
124 case OPTION_BITOP:
125 if (unset)
126 BUG("BITOP can't have unset form");
127 *(int *)opt->value &= ~opt->extra;
128 *(int *)opt->value |= opt->defval;
129 return 0;
130
b04ba2bb 131 case OPTION_COUNTUP:
e0070e8b
PB
132 if (*(int *)opt->value < 0)
133 *(int *)opt->value = 0;
db7244bd
PH
134 *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
135 return 0;
136
137 case OPTION_SET_INT:
138 *(int *)opt->value = unset ? 0 : 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;
f7e68a08
NTND
196 if (!*arg)
197 return error(_("%s expects a numerical value"),
198 optname(opt, flags));
1cc6985c 199 *(int *)opt->value = strtol(arg, (char **)&s, 10);
4a59fd13 200 if (*s)
9440b831
NTND
201 return error(_("%s expects a numerical value"),
202 optname(opt, flags));
4a59fd13
PH
203 return 0;
204
2a514ed8
CB
205 case OPTION_MAGNITUDE:
206 if (unset) {
207 *(unsigned long *)opt->value = 0;
208 return 0;
209 }
210 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
211 *(unsigned long *)opt->value = opt->defval;
212 return 0;
213 }
214 if (get_arg(p, opt, flags, &arg))
215 return -1;
216 if (!git_parse_ulong(arg, opt->value))
9440b831
NTND
217 return error(_("%s expects a non-negative integer value"
218 " with an optional k/m/g suffix"),
219 optname(opt, flags));
2a514ed8
CB
220 return 0;
221
4a59fd13 222 default:
48a5499e 223 BUG("opt->type %d should not happen", opt->type);
4a59fd13
PH
224 }
225}
226
f41179f1
NTND
227static enum parse_opt_result parse_short_opt(struct parse_opt_ctx_t *p,
228 const struct option *options)
4a59fd13 229{
11588263 230 const struct option *all_opts = options;
e0319ff5
RS
231 const struct option *numopt = NULL;
232
4a59fd13
PH
233 for (; options->type != OPTION_END; options++) {
234 if (options->short_name == *p->opt) {
235 p->opt = p->opt[1] ? p->opt + 1 : NULL;
11588263 236 return get_value(p, options, all_opts, OPT_SHORT);
4a59fd13 237 }
e0319ff5
RS
238
239 /*
240 * Handle the numerical option later, explicit one-digit
241 * options take precedence over it.
242 */
243 if (options->type == OPTION_NUMBER)
244 numopt = options;
245 }
246 if (numopt && isdigit(*p->opt)) {
247 size_t len = 1;
248 char *arg;
249 int rc;
250
251 while (isdigit(p->opt[len]))
252 len++;
253 arg = xmemdupz(p->opt, len);
254 p->opt = p->opt[len] ? p->opt + len : NULL;
3ebbe289
NTND
255 if (numopt->callback)
256 rc = (*numopt->callback)(numopt, arg, 0) ? (-1) : 0;
257 else
258 rc = (*numopt->ll_callback)(p, numopt, arg, 0);
e0319ff5
RS
259 free(arg);
260 return rc;
4a59fd13 261 }
f41179f1 262 return PARSE_OPT_UNKNOWN;
4a59fd13
PH
263}
264
5c387428
NTND
265static int has_string(const char *it, const char **array)
266{
267 while (*array)
268 if (!strcmp(it, *(array++)))
269 return 1;
270 return 0;
271}
272
273static int is_alias(struct parse_opt_ctx_t *ctx,
274 const struct option *one_opt,
275 const struct option *another_opt)
276{
277 const char **group;
278
279 if (!ctx->alias_groups)
280 return 0;
281
282 if (!one_opt->long_name || !another_opt->long_name)
283 return 0;
284
285 for (group = ctx->alias_groups; *group; group += 3) {
286 /* it and other are from the same family? */
287 if (has_string(one_opt->long_name, group) &&
288 has_string(another_opt->long_name, group))
289 return 1;
290 }
291 return 0;
292}
293
f41179f1
NTND
294static enum parse_opt_result parse_long_opt(
295 struct parse_opt_ctx_t *p, const char *arg,
296 const struct option *options)
4a59fd13 297{
11588263 298 const struct option *all_opts = options;
2c5495f7 299 const char *arg_end = strchrnul(arg, '=');
243e0614
JS
300 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
301 int abbrev_flags = 0, ambiguous_flags = 0;
7f275b91 302
4a59fd13 303 for (; options->type != OPTION_END; options++) {
0f1930c5
RS
304 const char *rest, *long_name = options->long_name;
305 int flags = 0, opt_flags = 0;
4a59fd13 306
0f1930c5 307 if (!long_name)
4a59fd13
PH
308 continue;
309
0f1930c5 310again:
cf4fff57
JK
311 if (!skip_prefix(arg, long_name, &rest))
312 rest = NULL;
580d5bff
PH
313 if (options->type == OPTION_ARGUMENT) {
314 if (!rest)
315 continue;
316 if (*rest == '=')
9440b831
NTND
317 return error(_("%s takes no value"),
318 optname(options, flags));
580d5bff
PH
319 if (*rest)
320 continue;
1a85b49b
JS
321 if (options->value)
322 *(int *)options->value = options->defval;
580d5bff 323 p->out[p->cpidx++] = arg - 2;
f41179f1 324 return PARSE_OPT_DONE;
580d5bff 325 }
4a59fd13 326 if (!rest) {
7f275b91 327 /* abbreviated? */
baa4adc6
NTND
328 if (!(p->flags & PARSE_OPT_KEEP_UNKNOWN) &&
329 !strncmp(long_name, arg, arg_end - arg)) {
7f275b91 330is_abbreviated:
5c387428
NTND
331 if (abbrev_option &&
332 !is_alias(p, abbrev_option, options)) {
243e0614
JS
333 /*
334 * If this is abbreviated, it is
335 * ambiguous. So when there is no
336 * exact match later, we need to
337 * error out.
338 */
339 ambiguous_option = abbrev_option;
340 ambiguous_flags = abbrev_flags;
341 }
7f275b91
JS
342 if (!(flags & OPT_UNSET) && *arg_end)
343 p->opt = arg_end + 1;
344 abbrev_option = options;
0f1930c5 345 abbrev_flags = flags ^ opt_flags;
7f275b91
JS
346 continue;
347 }
6bbfd1fa
AS
348 /* negation allowed? */
349 if (options->flags & PARSE_OPT_NONEG)
350 continue;
7f275b91 351 /* negated and abbreviated very much? */
59556548 352 if (starts_with("no-", arg)) {
7f275b91
JS
353 flags |= OPT_UNSET;
354 goto is_abbreviated;
355 }
356 /* negated? */
59556548 357 if (!starts_with(arg, "no-")) {
145136a9 358 if (skip_prefix(long_name, "no-", &long_name)) {
0f1930c5
RS
359 opt_flags |= OPT_UNSET;
360 goto again;
361 }
4a59fd13 362 continue;
0f1930c5 363 }
4a59fd13 364 flags |= OPT_UNSET;
cf4fff57
JK
365 if (!skip_prefix(arg + 3, long_name, &rest)) {
366 /* abbreviated and negated? */
367 if (starts_with(long_name, arg + 3))
368 goto is_abbreviated;
369 else
370 continue;
371 }
4a59fd13
PH
372 }
373 if (*rest) {
374 if (*rest != '=')
375 continue;
376 p->opt = rest + 1;
377 }
11588263 378 return get_value(p, options, all_opts, flags ^ opt_flags);
4a59fd13 379 }
243e0614 380
b02e7d5d
JS
381 if (disallow_abbreviated_options && (ambiguous_option || abbrev_option))
382 die("disallowed abbreviated or ambiguous option '%.*s'",
383 (int)(arg_end - arg), arg);
384
3bb0923f 385 if (ambiguous_option) {
89003426
NTND
386 error(_("ambiguous option: %s "
387 "(could be --%s%s or --%s%s)"),
243e0614
JS
388 arg,
389 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
390 ambiguous_option->long_name,
391 (abbrev_flags & OPT_UNSET) ? "no-" : "",
392 abbrev_option->long_name);
f41179f1 393 return PARSE_OPT_HELP;
3bb0923f 394 }
7f275b91 395 if (abbrev_option)
11588263 396 return get_value(p, abbrev_option, all_opts, abbrev_flags);
f41179f1 397 return PARSE_OPT_UNKNOWN;
4a59fd13
PH
398}
399
51a9949e
RS
400static int parse_nodash_opt(struct parse_opt_ctx_t *p, const char *arg,
401 const struct option *options)
402{
11588263
JH
403 const struct option *all_opts = options;
404
51a9949e
RS
405 for (; options->type != OPTION_END; options++) {
406 if (!(options->flags & PARSE_OPT_NODASH))
407 continue;
51a9949e 408 if (options->short_name == arg[0] && arg[1] == '\0')
11588263 409 return get_value(p, options, all_opts, OPT_SHORT);
51a9949e
RS
410 }
411 return -2;
412}
413
1121a878 414static void check_typos(const char *arg, const struct option *options)
3a9f0f41
PH
415{
416 if (strlen(arg) < 3)
417 return;
418
59556548 419 if (starts_with(arg, "no-")) {
395518cf 420 error(_("did you mean `--%s` (with two dashes)?"), arg);
3a9f0f41
PH
421 exit(129);
422 }
423
424 for (; options->type != OPTION_END; options++) {
425 if (!options->long_name)
426 continue;
59556548 427 if (starts_with(options->long_name, arg)) {
395518cf 428 error(_("did you mean `--%s` (with two dashes)?"), arg);
3a9f0f41
PH
429 exit(129);
430 }
431 }
432}
433
cb9d398c
PH
434static void parse_options_check(const struct option *opts)
435{
436 int err = 0;
af465af8 437 char short_opts[128];
cb9d398c 438
af465af8 439 memset(short_opts, '\0', sizeof(short_opts));
cb9d398c
PH
440 for (; opts->type != OPTION_END; opts++) {
441 if ((opts->flags & PARSE_OPT_LASTARG_DEFAULT) &&
1e5ce570
JN
442 (opts->flags & PARSE_OPT_OPTARG))
443 err |= optbug(opts, "uses incompatible flags "
444 "LASTARG_DEFAULT and OPTARG");
af465af8
JH
445 if (opts->short_name) {
446 if (0x7F <= opts->short_name)
447 err |= optbug(opts, "invalid short name");
448 else if (short_opts[opts->short_name]++)
449 err |= optbug(opts, "short name already used");
450 }
a02dd4ff
JN
451 if (opts->flags & PARSE_OPT_NODASH &&
452 ((opts->flags & PARSE_OPT_OPTARG) ||
453 !(opts->flags & PARSE_OPT_NOARG) ||
454 !(opts->flags & PARSE_OPT_NONEG) ||
455 opts->long_name))
456 err |= optbug(opts, "uses feature "
457 "not supported for dashless options");
5c400ed2 458 switch (opts->type) {
b04ba2bb 459 case OPTION_COUNTUP:
5c400ed2
JN
460 case OPTION_BIT:
461 case OPTION_NEGBIT:
462 case OPTION_SET_INT:
5c400ed2
JN
463 case OPTION_NUMBER:
464 if ((opts->flags & PARSE_OPT_OPTARG) ||
465 !(opts->flags & PARSE_OPT_NOARG))
466 err |= optbug(opts, "should not accept an argument");
bf3ff338
NTND
467 break;
468 case OPTION_CALLBACK:
3ebbe289
NTND
469 if (!opts->callback && !opts->ll_callback)
470 BUG("OPTION_CALLBACK needs one callback");
471 if (opts->callback && opts->ll_callback)
472 BUG("OPTION_CALLBACK can't have two callbacks");
bf3ff338
NTND
473 break;
474 case OPTION_LOWLEVEL_CALLBACK:
475 if (!opts->ll_callback)
476 BUG("OPTION_LOWLEVEL_CALLBACK needs a callback");
477 if (opts->callback)
478 BUG("OPTION_LOWLEVEL_CALLBACK needs no high level callback");
479 break;
5c387428
NTND
480 case OPTION_ALIAS:
481 BUG("OPT_ALIAS() should not remain at this point. "
482 "Are you using parse_options_step() directly?\n"
483 "That case is not supported yet.");
5c400ed2
JN
484 default:
485 ; /* ok. (usually accepts an argument) */
486 }
b6c2a0d4
JH
487 if (opts->argh &&
488 strcspn(opts->argh, " _") != strlen(opts->argh))
489 err |= optbug(opts, "multi-word argh should use dash to separate words");
cb9d398c 490 }
cb9d398c 491 if (err)
1e5ce570 492 exit(128);
cb9d398c
PH
493}
494
5c387428
NTND
495static void parse_options_start_1(struct parse_opt_ctx_t *ctx,
496 int argc, const char **argv, const char *prefix,
497 const struct option *options, int flags)
7e7bbcb4 498{
202fbb33
NTND
499 ctx->argc = argc;
500 ctx->argv = argv;
501 if (!(flags & PARSE_OPT_ONE_SHOT)) {
502 ctx->argc--;
503 ctx->argv++;
504 }
505 ctx->total = ctx->argc;
506 ctx->out = argv;
37782920 507 ctx->prefix = prefix;
a32a4eaa 508 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
7e7bbcb4 509 ctx->flags = flags;
0d260f9a 510 if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
202fbb33
NTND
511 (flags & PARSE_OPT_STOP_AT_NON_OPTION) &&
512 !(flags & PARSE_OPT_ONE_SHOT))
48a5499e 513 BUG("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
202fbb33
NTND
514 if ((flags & PARSE_OPT_ONE_SHOT) &&
515 (flags & PARSE_OPT_KEEP_ARGV0))
516 BUG("Can't keep argv0 if you don't have it");
9ca1169f 517 parse_options_check(options);
7e7bbcb4
PH
518}
519
5c387428
NTND
520void parse_options_start(struct parse_opt_ctx_t *ctx,
521 int argc, const char **argv, const char *prefix,
522 const struct option *options, int flags)
523{
524 memset(ctx, 0, sizeof(*ctx));
525 parse_options_start_1(ctx, argc, argv, prefix, options, flags);
526}
527
b221b5ab
NTND
528static void show_negated_gitcomp(const struct option *opts, int nr_noopts)
529{
530 int printed_dashdash = 0;
531
532 for (; opts->type != OPTION_END; opts++) {
533 int has_unset_form = 0;
534 const char *name;
535
536 if (!opts->long_name)
537 continue;
538 if (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE))
539 continue;
540 if (opts->flags & PARSE_OPT_NONEG)
541 continue;
542
543 switch (opts->type) {
544 case OPTION_STRING:
545 case OPTION_FILENAME:
546 case OPTION_INTEGER:
547 case OPTION_MAGNITUDE:
548 case OPTION_CALLBACK:
549 case OPTION_BIT:
550 case OPTION_NEGBIT:
551 case OPTION_COUNTUP:
552 case OPTION_SET_INT:
553 has_unset_form = 1;
554 break;
555 default:
556 break;
557 }
558 if (!has_unset_form)
559 continue;
560
561 if (skip_prefix(opts->long_name, "no-", &name)) {
562 if (nr_noopts < 0)
563 printf(" --%s", name);
564 } else if (nr_noopts >= 0) {
565 if (nr_noopts && !printed_dashdash) {
566 printf(" --");
567 printed_dashdash = 1;
568 }
569 printf(" --no-%s", opts->long_name);
570 nr_noopts++;
571 }
572 }
573}
574
5205749d 575static int show_gitcomp(const struct option *opts)
b9d7f4b4 576{
b221b5ab
NTND
577 const struct option *original_opts = opts;
578 int nr_noopts = 0;
579
b9d7f4b4
NTND
580 for (; opts->type != OPTION_END; opts++) {
581 const char *suffix = "";
582
583 if (!opts->long_name)
584 continue;
585 if (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE))
586 continue;
587
588 switch (opts->type) {
589 case OPTION_GROUP:
590 continue;
591 case OPTION_STRING:
592 case OPTION_FILENAME:
593 case OPTION_INTEGER:
594 case OPTION_MAGNITUDE:
595 case OPTION_CALLBACK:
596 if (opts->flags & PARSE_OPT_NOARG)
597 break;
598 if (opts->flags & PARSE_OPT_OPTARG)
599 break;
600 if (opts->flags & PARSE_OPT_LASTARG_DEFAULT)
601 break;
602 suffix = "=";
603 break;
604 default:
605 break;
606 }
ebc4a04e
NTND
607 if (opts->flags & PARSE_OPT_COMP_ARG)
608 suffix = "=";
b221b5ab
NTND
609 if (starts_with(opts->long_name, "no-"))
610 nr_noopts++;
b9d7f4b4
NTND
611 printf(" --%s%s", opts->long_name, suffix);
612 }
b221b5ab
NTND
613 show_negated_gitcomp(original_opts, -1);
614 show_negated_gitcomp(original_opts, nr_noopts);
b9d7f4b4 615 fputc('\n', stdout);
a92ec7ef 616 return PARSE_OPT_COMPLETE;
b9d7f4b4
NTND
617}
618
5c387428
NTND
619/*
620 * Scan and may produce a new option[] array, which should be used
621 * instead of the original 'options'.
622 *
15beaaa3 623 * Right now this is only used to preprocess and substitute
5c387428
NTND
624 * OPTION_ALIAS.
625 */
626static struct option *preprocess_options(struct parse_opt_ctx_t *ctx,
627 const struct option *options)
628{
629 struct option *newopt;
630 int i, nr, alias;
631 int nr_aliases = 0;
632
633 for (nr = 0; options[nr].type != OPTION_END; nr++) {
634 if (options[nr].type == OPTION_ALIAS)
635 nr_aliases++;
636 }
637
638 if (!nr_aliases)
639 return NULL;
640
641 ALLOC_ARRAY(newopt, nr + 1);
642 COPY_ARRAY(newopt, options, nr + 1);
643
644 /* each alias has two string pointers and NULL */
645 CALLOC_ARRAY(ctx->alias_groups, 3 * (nr_aliases + 1));
646
647 for (alias = 0, i = 0; i < nr; i++) {
648 int short_name;
649 const char *long_name;
650 const char *source;
7c280589 651 struct strbuf help = STRBUF_INIT;
5c387428
NTND
652 int j;
653
654 if (newopt[i].type != OPTION_ALIAS)
655 continue;
656
657 short_name = newopt[i].short_name;
658 long_name = newopt[i].long_name;
659 source = newopt[i].value;
660
661 if (!long_name)
662 BUG("An alias must have long option name");
7c280589 663 strbuf_addf(&help, _("alias of --%s"), source);
5c387428
NTND
664
665 for (j = 0; j < nr; j++) {
666 const char *name = options[j].long_name;
667
668 if (!name || strcmp(name, source))
669 continue;
670
671 if (options[j].type == OPTION_ALIAS)
672 BUG("No please. Nested aliases are not supported.");
673
5c387428
NTND
674 memcpy(newopt + i, options + j, sizeof(*newopt));
675 newopt[i].short_name = short_name;
676 newopt[i].long_name = long_name;
7c280589 677 newopt[i].help = strbuf_detach(&help, NULL);
5c387428
NTND
678 break;
679 }
680
681 if (j == nr)
682 BUG("could not find source option '%s' of alias '%s'",
683 source, newopt[i].long_name);
684 ctx->alias_groups[alias * 3 + 0] = newopt[i].long_name;
685 ctx->alias_groups[alias * 3 + 1] = options[j].long_name;
686 ctx->alias_groups[alias * 3 + 2] = NULL;
687 alias++;
688 }
689
690 return newopt;
691}
692
47e9cd28
TR
693static int usage_with_options_internal(struct parse_opt_ctx_t *,
694 const char * const *,
9c7304e3 695 const struct option *, int, int);
dd3bf0f4 696
ff43ec3e
PH
697int parse_options_step(struct parse_opt_ctx_t *ctx,
698 const struct option *options,
699 const char * const usagestr[])
4a59fd13 700{
b92891f9
RS
701 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
702
26141b5b
PH
703 /* we must reset ->opt, unknown short option leave it dangling */
704 ctx->opt = NULL;
705
ff43ec3e
PH
706 for (; ctx->argc; ctx->argc--, ctx->argv++) {
707 const char *arg = ctx->argv[0];
4a59fd13 708
202fbb33
NTND
709 if (ctx->flags & PARSE_OPT_ONE_SHOT &&
710 ctx->argc != ctx->total)
711 break;
712
4a59fd13 713 if (*arg != '-' || !arg[1]) {
51a9949e
RS
714 if (parse_nodash_opt(ctx, arg, options) == 0)
715 continue;
ff43ec3e 716 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
979240fe 717 return PARSE_OPT_NON_OPTION;
ff43ec3e 718 ctx->out[ctx->cpidx++] = ctx->argv[0];
4a59fd13
PH
719 continue;
720 }
721
5ad0d3d5
RS
722 /* lone -h asks for help */
723 if (internal_help && ctx->total == 1 && !strcmp(arg + 1, "h"))
724 goto show_usage;
725
b9d7f4b4
NTND
726 /* lone --git-completion-helper is asked by git-completion.bash */
727 if (ctx->total == 1 && !strcmp(arg + 1, "-git-completion-helper"))
5205749d 728 return show_gitcomp(options);
b9d7f4b4 729
4a59fd13 730 if (arg[1] != '-') {
ff43ec3e 731 ctx->opt = arg + 1;
07fe54db 732 switch (parse_short_opt(ctx, options)) {
f41179f1 733 case PARSE_OPT_ERROR:
3bb0923f 734 return PARSE_OPT_ERROR;
f41179f1 735 case PARSE_OPT_UNKNOWN:
38916c5b
RS
736 if (ctx->opt)
737 check_typos(arg + 1, options);
5ad0d3d5
RS
738 if (internal_help && *ctx->opt == 'h')
739 goto show_usage;
b5ce3a54 740 goto unknown;
f41179f1
NTND
741 case PARSE_OPT_NON_OPTION:
742 case PARSE_OPT_HELP:
743 case PARSE_OPT_COMPLETE:
744 BUG("parse_short_opt() cannot return these");
745 case PARSE_OPT_DONE:
746 break;
07fe54db 747 }
ff43ec3e 748 if (ctx->opt)
3a9f0f41 749 check_typos(arg + 1, options);
ff43ec3e 750 while (ctx->opt) {
07fe54db 751 switch (parse_short_opt(ctx, options)) {
f41179f1 752 case PARSE_OPT_ERROR:
3bb0923f 753 return PARSE_OPT_ERROR;
f41179f1 754 case PARSE_OPT_UNKNOWN:
5ad0d3d5
RS
755 if (internal_help && *ctx->opt == 'h')
756 goto show_usage;
757
26141b5b
PH
758 /* fake a short option thing to hide the fact that we may have
759 * started to parse aggregated stuff
760 *
761 * This is leaky, too bad.
762 */
763 ctx->argv[0] = xstrdup(ctx->opt - 1);
764 *(char *)ctx->argv[0] = '-';
b5ce3a54 765 goto unknown;
f41179f1
NTND
766 case PARSE_OPT_NON_OPTION:
767 case PARSE_OPT_COMPLETE:
768 case PARSE_OPT_HELP:
769 BUG("parse_short_opt() cannot return these");
770 case PARSE_OPT_DONE:
771 break;
07fe54db 772 }
3a9f0f41 773 }
4a59fd13
PH
774 continue;
775 }
776
51b4594b
JK
777 if (!arg[2] /* "--" */ ||
778 !strcmp(arg + 2, "end-of-options")) {
ff43ec3e
PH
779 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
780 ctx->argc--;
781 ctx->argv++;
4a59fd13
PH
782 }
783 break;
784 }
785
b92891f9 786 if (internal_help && !strcmp(arg + 2, "help-all"))
47e9cd28 787 return usage_with_options_internal(ctx, usagestr, options, 1, 0);
b92891f9 788 if (internal_help && !strcmp(arg + 2, "help"))
ac20ff6d 789 goto show_usage;
07fe54db 790 switch (parse_long_opt(ctx, arg + 2, options)) {
f41179f1 791 case PARSE_OPT_ERROR:
3bb0923f 792 return PARSE_OPT_ERROR;
f41179f1 793 case PARSE_OPT_UNKNOWN:
b5ce3a54 794 goto unknown;
f41179f1 795 case PARSE_OPT_HELP:
3bb0923f 796 goto show_usage;
f41179f1
NTND
797 case PARSE_OPT_NON_OPTION:
798 case PARSE_OPT_COMPLETE:
799 BUG("parse_long_opt() cannot return these");
800 case PARSE_OPT_DONE:
801 break;
07fe54db 802 }
b5ce3a54
RS
803 continue;
804unknown:
202fbb33
NTND
805 if (ctx->flags & PARSE_OPT_ONE_SHOT)
806 break;
b5ce3a54
RS
807 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
808 return PARSE_OPT_UNKNOWN;
809 ctx->out[ctx->cpidx++] = ctx->argv[0];
810 ctx->opt = NULL;
ff43ec3e
PH
811 }
812 return PARSE_OPT_DONE;
ac20ff6d 813
ac20ff6d 814 show_usage:
3bb0923f 815 return usage_with_options_internal(ctx, usagestr, options, 0, 0);
ff43ec3e
PH
816}
817
818int parse_options_end(struct parse_opt_ctx_t *ctx)
819{
202fbb33
NTND
820 if (ctx->flags & PARSE_OPT_ONE_SHOT)
821 return ctx->total - ctx->argc;
822
f919ffeb 823 MOVE_ARRAY(ctx->out + ctx->cpidx, ctx->argv, ctx->argc);
ff43ec3e
PH
824 ctx->out[ctx->cpidx + ctx->argc] = NULL;
825 return ctx->cpidx + ctx->argc;
826}
827
37782920
SB
828int parse_options(int argc, const char **argv, const char *prefix,
829 const struct option *options, const char * const usagestr[],
830 int flags)
ff43ec3e
PH
831{
832 struct parse_opt_ctx_t ctx;
5c387428 833 struct option *real_options;
ff43ec3e 834
b02e7d5d
JS
835 disallow_abbreviated_options =
836 git_env_bool("GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS", 0);
837
5c387428
NTND
838 memset(&ctx, 0, sizeof(ctx));
839 real_options = preprocess_options(&ctx, options);
840 if (real_options)
841 options = real_options;
842 parse_options_start_1(&ctx, argc, argv, prefix, options, flags);
ff43ec3e
PH
843 switch (parse_options_step(&ctx, options, usagestr)) {
844 case PARSE_OPT_HELP:
3bb0923f 845 case PARSE_OPT_ERROR:
ff43ec3e 846 exit(129);
a92ec7ef
NTND
847 case PARSE_OPT_COMPLETE:
848 exit(0);
979240fe 849 case PARSE_OPT_NON_OPTION:
ff43ec3e
PH
850 case PARSE_OPT_DONE:
851 break;
852 default: /* PARSE_OPT_UNKNOWN */
07fe54db 853 if (ctx.argv[0][1] == '-') {
89003426 854 error(_("unknown option `%s'"), ctx.argv[0] + 2);
b141a478 855 } else if (isascii(*ctx.opt)) {
89003426 856 error(_("unknown switch `%c'"), *ctx.opt);
b141a478 857 } else {
89003426 858 error(_("unknown non-ascii option in string: `%s'"),
b141a478 859 ctx.argv[0]);
07fe54db
PH
860 }
861 usage_with_options(usagestr, options);
4a59fd13
PH
862 }
863
76759c7d 864 precompose_argv(argc, argv);
5c387428
NTND
865 free(real_options);
866 free(ctx.alias_groups);
7e7bbcb4 867 return parse_options_end(&ctx);
4a59fd13 868}
d7a38c54 869
9c7304e3 870static int usage_argh(const struct option *opts, FILE *outfile)
29f25d49
SB
871{
872 const char *s;
5f0df44c
RS
873 int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
874 !opts->argh || !!strpbrk(opts->argh, "()<>[]|");
29f25d49
SB
875 if (opts->flags & PARSE_OPT_OPTARG)
876 if (opts->long_name)
877 s = literal ? "[=%s]" : "[=<%s>]";
878 else
879 s = literal ? "[%s]" : "[<%s>]";
880 else
881 s = literal ? " %s" : " <%s>";
c0821965 882 return utf8_fprintf(outfile, s, opts->argh ? _(opts->argh) : _("..."));
29f25d49
SB
883}
884
d7a38c54
PH
885#define USAGE_OPTS_WIDTH 24
886#define USAGE_GAP 2
887
47e9cd28
TR
888static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
889 const char * const *usagestr,
890 const struct option *opts, int full, int err)
d7a38c54 891{
9c7304e3 892 FILE *outfile = err ? stderr : stdout;
a6304fa4 893 int need_newline;
9c7304e3 894
49b61802
RS
895 if (!usagestr)
896 return PARSE_OPT_HELP;
897
47e9cd28
TR
898 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
899 fprintf(outfile, "cat <<\\EOF\n");
900
54e6dc7d 901 fprintf_ln(outfile, _("usage: %s"), _(*usagestr++));
f389c808 902 while (*usagestr && **usagestr)
66f5f6dc
ÆAB
903 /*
904 * TRANSLATORS: the colon here should align with the
905 * one in "usage: %s" translation.
906 */
54e6dc7d 907 fprintf_ln(outfile, _(" or: %s"), _(*usagestr++));
44d86e91 908 while (*usagestr) {
54e6dc7d
NTND
909 if (**usagestr)
910 fprintf_ln(outfile, _(" %s"), _(*usagestr));
911 else
1a9bf1e1 912 fputc('\n', outfile);
44d86e91
JK
913 usagestr++;
914 }
d7a38c54 915
a6304fa4 916 need_newline = 1;
d7a38c54
PH
917
918 for (; opts->type != OPTION_END; opts++) {
919 size_t pos;
920 int pad;
921
922 if (opts->type == OPTION_GROUP) {
9c7304e3 923 fputc('\n', outfile);
a6304fa4 924 need_newline = 0;
d7a38c54 925 if (*opts->help)
54e6dc7d 926 fprintf(outfile, "%s\n", _(opts->help));
d7a38c54
PH
927 continue;
928 }
dd3bf0f4
PH
929 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
930 continue;
d7a38c54 931
a6304fa4
BC
932 if (need_newline) {
933 fputc('\n', outfile);
934 need_newline = 0;
935 }
936
9c7304e3 937 pos = fprintf(outfile, " ");
cbb08c2e 938 if (opts->short_name) {
51a9949e 939 if (opts->flags & PARSE_OPT_NODASH)
9c7304e3 940 pos += fprintf(outfile, "%c", opts->short_name);
51a9949e 941 else
9c7304e3 942 pos += fprintf(outfile, "-%c", opts->short_name);
51a9949e 943 }
d7a38c54 944 if (opts->long_name && opts->short_name)
9c7304e3 945 pos += fprintf(outfile, ", ");
d7a38c54 946 if (opts->long_name)
cbb08c2e 947 pos += fprintf(outfile, "--%s", opts->long_name);
e0319ff5 948 if (opts->type == OPTION_NUMBER)
c0821965 949 pos += utf8_fprintf(outfile, _("-NUM"));
d7a38c54 950
b57c68a6
JN
951 if ((opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
952 !(opts->flags & PARSE_OPT_NOARG))
9c7304e3 953 pos += usage_argh(opts, outfile);
d7a38c54 954
f389c808
AR
955 if (pos <= USAGE_OPTS_WIDTH)
956 pad = USAGE_OPTS_WIDTH - pos;
d7a38c54 957 else {
9c7304e3 958 fputc('\n', outfile);
d7a38c54
PH
959 pad = USAGE_OPTS_WIDTH;
960 }
5c387428
NTND
961 if (opts->type == OPTION_ALIAS) {
962 fprintf(outfile, "%*s", pad + USAGE_GAP, "");
963 fprintf_ln(outfile, _("alias of --%s"),
964 (const char *)opts->value);
965 continue;
966 }
54e6dc7d 967 fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", _(opts->help));
d7a38c54 968 }
9c7304e3 969 fputc('\n', outfile);
f389c808 970
47e9cd28
TR
971 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
972 fputs("EOF\n", outfile);
973
ee68b87a 974 return PARSE_OPT_HELP;
d7a38c54 975}
0ce865b1 976
c2e86add 977void NORETURN usage_with_options(const char * const *usagestr,
ff43ec3e 978 const struct option *opts)
dd3bf0f4 979{
47e9cd28 980 usage_with_options_internal(NULL, usagestr, opts, 0, 1);
ff43ec3e 981 exit(129);
dd3bf0f4
PH
982}
983
c2e86add 984void NORETURN usage_msg_opt(const char *msg,
451bb210
CC
985 const char * const *usagestr,
986 const struct option *options)
987{
87433261 988 fprintf(stderr, "fatal: %s\n\n", msg);
451bb210
CC
989 usage_with_options(usagestr, options);
990}
991
9440b831 992const char *optname(const struct option *opt, int flags)
a469a101 993{
9440b831
NTND
994 static struct strbuf sb = STRBUF_INIT;
995
996 strbuf_reset(&sb);
a469a101 997 if (flags & OPT_SHORT)
9440b831
NTND
998 strbuf_addf(&sb, "switch `%c'", opt->short_name);
999 else if (flags & OPT_UNSET)
1000 strbuf_addf(&sb, "option `no-%s'", opt->long_name);
1001 else
1002 strbuf_addf(&sb, "option `%s'", opt->long_name);
1003
1004 return sb.buf;
a469a101 1005}