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