]> git.ipfire.org Git - thirdparty/git.git/blame - parse-options.c
git-multi-pack-index.txt: change "folder" to "directory"
[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;
4a59fd13 313 if (!rest) {
7f275b91 314 /* abbreviated? */
baa4adc6
NTND
315 if (!(p->flags & PARSE_OPT_KEEP_UNKNOWN) &&
316 !strncmp(long_name, arg, arg_end - arg)) {
7f275b91 317is_abbreviated:
5c387428
NTND
318 if (abbrev_option &&
319 !is_alias(p, abbrev_option, options)) {
243e0614
JS
320 /*
321 * If this is abbreviated, it is
322 * ambiguous. So when there is no
323 * exact match later, we need to
324 * error out.
325 */
326 ambiguous_option = abbrev_option;
327 ambiguous_flags = abbrev_flags;
328 }
7f275b91
JS
329 if (!(flags & OPT_UNSET) && *arg_end)
330 p->opt = arg_end + 1;
331 abbrev_option = options;
0f1930c5 332 abbrev_flags = flags ^ opt_flags;
7f275b91
JS
333 continue;
334 }
6bbfd1fa
AS
335 /* negation allowed? */
336 if (options->flags & PARSE_OPT_NONEG)
337 continue;
7f275b91 338 /* negated and abbreviated very much? */
59556548 339 if (starts_with("no-", arg)) {
7f275b91
JS
340 flags |= OPT_UNSET;
341 goto is_abbreviated;
342 }
343 /* negated? */
59556548 344 if (!starts_with(arg, "no-")) {
145136a9 345 if (skip_prefix(long_name, "no-", &long_name)) {
0f1930c5
RS
346 opt_flags |= OPT_UNSET;
347 goto again;
348 }
4a59fd13 349 continue;
0f1930c5 350 }
4a59fd13 351 flags |= OPT_UNSET;
cf4fff57
JK
352 if (!skip_prefix(arg + 3, long_name, &rest)) {
353 /* abbreviated and negated? */
354 if (starts_with(long_name, arg + 3))
355 goto is_abbreviated;
356 else
357 continue;
358 }
4a59fd13
PH
359 }
360 if (*rest) {
361 if (*rest != '=')
362 continue;
363 p->opt = rest + 1;
364 }
11588263 365 return get_value(p, options, all_opts, flags ^ opt_flags);
4a59fd13 366 }
243e0614 367
b02e7d5d
JS
368 if (disallow_abbreviated_options && (ambiguous_option || abbrev_option))
369 die("disallowed abbreviated or ambiguous option '%.*s'",
370 (int)(arg_end - arg), arg);
371
3bb0923f 372 if (ambiguous_option) {
89003426
NTND
373 error(_("ambiguous option: %s "
374 "(could be --%s%s or --%s%s)"),
243e0614
JS
375 arg,
376 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
377 ambiguous_option->long_name,
378 (abbrev_flags & OPT_UNSET) ? "no-" : "",
379 abbrev_option->long_name);
f41179f1 380 return PARSE_OPT_HELP;
3bb0923f 381 }
7f275b91 382 if (abbrev_option)
11588263 383 return get_value(p, abbrev_option, all_opts, abbrev_flags);
f41179f1 384 return PARSE_OPT_UNKNOWN;
4a59fd13
PH
385}
386
51a9949e
RS
387static int parse_nodash_opt(struct parse_opt_ctx_t *p, const char *arg,
388 const struct option *options)
389{
11588263
JH
390 const struct option *all_opts = options;
391
51a9949e
RS
392 for (; options->type != OPTION_END; options++) {
393 if (!(options->flags & PARSE_OPT_NODASH))
394 continue;
51a9949e 395 if (options->short_name == arg[0] && arg[1] == '\0')
11588263 396 return get_value(p, options, all_opts, OPT_SHORT);
51a9949e
RS
397 }
398 return -2;
399}
400
1121a878 401static void check_typos(const char *arg, const struct option *options)
3a9f0f41
PH
402{
403 if (strlen(arg) < 3)
404 return;
405
59556548 406 if (starts_with(arg, "no-")) {
395518cf 407 error(_("did you mean `--%s` (with two dashes)?"), arg);
3a9f0f41
PH
408 exit(129);
409 }
410
411 for (; options->type != OPTION_END; options++) {
412 if (!options->long_name)
413 continue;
59556548 414 if (starts_with(options->long_name, arg)) {
395518cf 415 error(_("did you mean `--%s` (with two dashes)?"), arg);
3a9f0f41
PH
416 exit(129);
417 }
418 }
419}
420
cb9d398c
PH
421static void parse_options_check(const struct option *opts)
422{
423 int err = 0;
af465af8 424 char short_opts[128];
cb9d398c 425
af465af8 426 memset(short_opts, '\0', sizeof(short_opts));
cb9d398c
PH
427 for (; opts->type != OPTION_END; opts++) {
428 if ((opts->flags & PARSE_OPT_LASTARG_DEFAULT) &&
1e5ce570
JN
429 (opts->flags & PARSE_OPT_OPTARG))
430 err |= optbug(opts, "uses incompatible flags "
431 "LASTARG_DEFAULT and OPTARG");
af465af8
JH
432 if (opts->short_name) {
433 if (0x7F <= opts->short_name)
434 err |= optbug(opts, "invalid short name");
435 else if (short_opts[opts->short_name]++)
436 err |= optbug(opts, "short name already used");
437 }
a02dd4ff
JN
438 if (opts->flags & PARSE_OPT_NODASH &&
439 ((opts->flags & PARSE_OPT_OPTARG) ||
440 !(opts->flags & PARSE_OPT_NOARG) ||
441 !(opts->flags & PARSE_OPT_NONEG) ||
442 opts->long_name))
443 err |= optbug(opts, "uses feature "
444 "not supported for dashless options");
5c400ed2 445 switch (opts->type) {
b04ba2bb 446 case OPTION_COUNTUP:
5c400ed2
JN
447 case OPTION_BIT:
448 case OPTION_NEGBIT:
449 case OPTION_SET_INT:
5c400ed2
JN
450 case OPTION_NUMBER:
451 if ((opts->flags & PARSE_OPT_OPTARG) ||
452 !(opts->flags & PARSE_OPT_NOARG))
453 err |= optbug(opts, "should not accept an argument");
bf3ff338
NTND
454 break;
455 case OPTION_CALLBACK:
3ebbe289
NTND
456 if (!opts->callback && !opts->ll_callback)
457 BUG("OPTION_CALLBACK needs one callback");
458 if (opts->callback && opts->ll_callback)
459 BUG("OPTION_CALLBACK can't have two callbacks");
bf3ff338
NTND
460 break;
461 case OPTION_LOWLEVEL_CALLBACK:
462 if (!opts->ll_callback)
463 BUG("OPTION_LOWLEVEL_CALLBACK needs a callback");
464 if (opts->callback)
465 BUG("OPTION_LOWLEVEL_CALLBACK needs no high level callback");
466 break;
5c387428
NTND
467 case OPTION_ALIAS:
468 BUG("OPT_ALIAS() should not remain at this point. "
469 "Are you using parse_options_step() directly?\n"
470 "That case is not supported yet.");
5c400ed2
JN
471 default:
472 ; /* ok. (usually accepts an argument) */
473 }
b6c2a0d4
JH
474 if (opts->argh &&
475 strcspn(opts->argh, " _") != strlen(opts->argh))
476 err |= optbug(opts, "multi-word argh should use dash to separate words");
cb9d398c 477 }
cb9d398c 478 if (err)
1e5ce570 479 exit(128);
cb9d398c
PH
480}
481
5c387428
NTND
482static void parse_options_start_1(struct parse_opt_ctx_t *ctx,
483 int argc, const char **argv, const char *prefix,
484 const struct option *options, int flags)
7e7bbcb4 485{
202fbb33
NTND
486 ctx->argc = argc;
487 ctx->argv = argv;
488 if (!(flags & PARSE_OPT_ONE_SHOT)) {
489 ctx->argc--;
490 ctx->argv++;
491 }
492 ctx->total = ctx->argc;
493 ctx->out = argv;
37782920 494 ctx->prefix = prefix;
a32a4eaa 495 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
7e7bbcb4 496 ctx->flags = flags;
0d260f9a 497 if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
202fbb33
NTND
498 (flags & PARSE_OPT_STOP_AT_NON_OPTION) &&
499 !(flags & PARSE_OPT_ONE_SHOT))
48a5499e 500 BUG("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
202fbb33
NTND
501 if ((flags & PARSE_OPT_ONE_SHOT) &&
502 (flags & PARSE_OPT_KEEP_ARGV0))
503 BUG("Can't keep argv0 if you don't have it");
9ca1169f 504 parse_options_check(options);
7e7bbcb4
PH
505}
506
5c387428
NTND
507void parse_options_start(struct parse_opt_ctx_t *ctx,
508 int argc, const char **argv, const char *prefix,
509 const struct option *options, int flags)
510{
511 memset(ctx, 0, sizeof(*ctx));
512 parse_options_start_1(ctx, argc, argv, prefix, options, flags);
513}
514
a0abe5e3
RZ
515static void show_negated_gitcomp(const struct option *opts, int show_all,
516 int nr_noopts)
b221b5ab
NTND
517{
518 int printed_dashdash = 0;
519
520 for (; opts->type != OPTION_END; opts++) {
521 int has_unset_form = 0;
522 const char *name;
523
524 if (!opts->long_name)
525 continue;
a0abe5e3
RZ
526 if (!show_all &&
527 (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE)))
b221b5ab
NTND
528 continue;
529 if (opts->flags & PARSE_OPT_NONEG)
530 continue;
531
532 switch (opts->type) {
533 case OPTION_STRING:
534 case OPTION_FILENAME:
535 case OPTION_INTEGER:
536 case OPTION_MAGNITUDE:
537 case OPTION_CALLBACK:
538 case OPTION_BIT:
539 case OPTION_NEGBIT:
540 case OPTION_COUNTUP:
541 case OPTION_SET_INT:
542 has_unset_form = 1;
543 break;
544 default:
545 break;
546 }
547 if (!has_unset_form)
548 continue;
549
550 if (skip_prefix(opts->long_name, "no-", &name)) {
551 if (nr_noopts < 0)
552 printf(" --%s", name);
553 } else if (nr_noopts >= 0) {
554 if (nr_noopts && !printed_dashdash) {
555 printf(" --");
556 printed_dashdash = 1;
557 }
558 printf(" --no-%s", opts->long_name);
559 nr_noopts++;
560 }
561 }
562}
563
a0abe5e3 564static int show_gitcomp(const struct option *opts, int show_all)
b9d7f4b4 565{
b221b5ab
NTND
566 const struct option *original_opts = opts;
567 int nr_noopts = 0;
568
b9d7f4b4
NTND
569 for (; opts->type != OPTION_END; opts++) {
570 const char *suffix = "";
571
572 if (!opts->long_name)
573 continue;
a0abe5e3 574 if (!show_all &&
ca2d62b7 575 (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE | PARSE_OPT_FROM_ALIAS)))
b9d7f4b4
NTND
576 continue;
577
578 switch (opts->type) {
579 case OPTION_GROUP:
580 continue;
581 case OPTION_STRING:
582 case OPTION_FILENAME:
583 case OPTION_INTEGER:
584 case OPTION_MAGNITUDE:
585 case OPTION_CALLBACK:
586 if (opts->flags & PARSE_OPT_NOARG)
587 break;
588 if (opts->flags & PARSE_OPT_OPTARG)
589 break;
590 if (opts->flags & PARSE_OPT_LASTARG_DEFAULT)
591 break;
592 suffix = "=";
593 break;
594 default:
595 break;
596 }
ebc4a04e
NTND
597 if (opts->flags & PARSE_OPT_COMP_ARG)
598 suffix = "=";
b221b5ab
NTND
599 if (starts_with(opts->long_name, "no-"))
600 nr_noopts++;
b9d7f4b4
NTND
601 printf(" --%s%s", opts->long_name, suffix);
602 }
a0abe5e3
RZ
603 show_negated_gitcomp(original_opts, show_all, -1);
604 show_negated_gitcomp(original_opts, show_all, nr_noopts);
b9d7f4b4 605 fputc('\n', stdout);
a92ec7ef 606 return PARSE_OPT_COMPLETE;
b9d7f4b4
NTND
607}
608
5c387428
NTND
609/*
610 * Scan and may produce a new option[] array, which should be used
611 * instead of the original 'options'.
612 *
15beaaa3 613 * Right now this is only used to preprocess and substitute
5c387428 614 * OPTION_ALIAS.
64cc539f
AH
615 *
616 * The returned options should be freed using free_preprocessed_options.
5c387428
NTND
617 */
618static struct option *preprocess_options(struct parse_opt_ctx_t *ctx,
619 const struct option *options)
620{
621 struct option *newopt;
622 int i, nr, alias;
623 int nr_aliases = 0;
624
625 for (nr = 0; options[nr].type != OPTION_END; nr++) {
626 if (options[nr].type == OPTION_ALIAS)
627 nr_aliases++;
628 }
629
630 if (!nr_aliases)
631 return NULL;
632
633 ALLOC_ARRAY(newopt, nr + 1);
634 COPY_ARRAY(newopt, options, nr + 1);
635
636 /* each alias has two string pointers and NULL */
637 CALLOC_ARRAY(ctx->alias_groups, 3 * (nr_aliases + 1));
638
639 for (alias = 0, i = 0; i < nr; i++) {
640 int short_name;
641 const char *long_name;
642 const char *source;
7c280589 643 struct strbuf help = STRBUF_INIT;
5c387428
NTND
644 int j;
645
646 if (newopt[i].type != OPTION_ALIAS)
647 continue;
648
649 short_name = newopt[i].short_name;
650 long_name = newopt[i].long_name;
651 source = newopt[i].value;
652
653 if (!long_name)
654 BUG("An alias must have long option name");
7c280589 655 strbuf_addf(&help, _("alias of --%s"), source);
5c387428
NTND
656
657 for (j = 0; j < nr; j++) {
658 const char *name = options[j].long_name;
659
660 if (!name || strcmp(name, source))
661 continue;
662
663 if (options[j].type == OPTION_ALIAS)
664 BUG("No please. Nested aliases are not supported.");
665
5c387428
NTND
666 memcpy(newopt + i, options + j, sizeof(*newopt));
667 newopt[i].short_name = short_name;
668 newopt[i].long_name = long_name;
7c280589 669 newopt[i].help = strbuf_detach(&help, NULL);
64cc539f 670 newopt[i].flags |= PARSE_OPT_FROM_ALIAS;
5c387428
NTND
671 break;
672 }
673
674 if (j == nr)
675 BUG("could not find source option '%s' of alias '%s'",
676 source, newopt[i].long_name);
677 ctx->alias_groups[alias * 3 + 0] = newopt[i].long_name;
678 ctx->alias_groups[alias * 3 + 1] = options[j].long_name;
679 ctx->alias_groups[alias * 3 + 2] = NULL;
680 alias++;
681 }
682
683 return newopt;
684}
685
64cc539f
AH
686static void free_preprocessed_options(struct option *options)
687{
688 int i;
689
690 if (!options)
691 return;
692
693 for (i = 0; options[i].type != OPTION_END; i++) {
694 if (options[i].flags & PARSE_OPT_FROM_ALIAS)
695 free((void *)options[i].help);
696 }
697 free(options);
698}
699
47e9cd28
TR
700static int usage_with_options_internal(struct parse_opt_ctx_t *,
701 const char * const *,
9c7304e3 702 const struct option *, int, int);
dd3bf0f4 703
ff43ec3e
PH
704int parse_options_step(struct parse_opt_ctx_t *ctx,
705 const struct option *options,
706 const char * const usagestr[])
4a59fd13 707{
b92891f9
RS
708 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
709
26141b5b
PH
710 /* we must reset ->opt, unknown short option leave it dangling */
711 ctx->opt = NULL;
712
ff43ec3e
PH
713 for (; ctx->argc; ctx->argc--, ctx->argv++) {
714 const char *arg = ctx->argv[0];
4a59fd13 715
202fbb33
NTND
716 if (ctx->flags & PARSE_OPT_ONE_SHOT &&
717 ctx->argc != ctx->total)
718 break;
719
4a59fd13 720 if (*arg != '-' || !arg[1]) {
51a9949e
RS
721 if (parse_nodash_opt(ctx, arg, options) == 0)
722 continue;
ff43ec3e 723 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
979240fe 724 return PARSE_OPT_NON_OPTION;
ff43ec3e 725 ctx->out[ctx->cpidx++] = ctx->argv[0];
4a59fd13
PH
726 continue;
727 }
728
5ad0d3d5
RS
729 /* lone -h asks for help */
730 if (internal_help && ctx->total == 1 && !strcmp(arg + 1, "h"))
731 goto show_usage;
732
a0abe5e3
RZ
733 /*
734 * lone --git-completion-helper and --git-completion-helper-all
735 * are asked by git-completion.bash
736 */
737 if (ctx->total == 1 && !strcmp(arg, "--git-completion-helper"))
738 return show_gitcomp(options, 0);
739 if (ctx->total == 1 && !strcmp(arg, "--git-completion-helper-all"))
740 return show_gitcomp(options, 1);
b9d7f4b4 741
4a59fd13 742 if (arg[1] != '-') {
ff43ec3e 743 ctx->opt = arg + 1;
07fe54db 744 switch (parse_short_opt(ctx, options)) {
f41179f1 745 case PARSE_OPT_ERROR:
3bb0923f 746 return PARSE_OPT_ERROR;
f41179f1 747 case PARSE_OPT_UNKNOWN:
38916c5b
RS
748 if (ctx->opt)
749 check_typos(arg + 1, options);
5ad0d3d5
RS
750 if (internal_help && *ctx->opt == 'h')
751 goto show_usage;
b5ce3a54 752 goto unknown;
f41179f1
NTND
753 case PARSE_OPT_NON_OPTION:
754 case PARSE_OPT_HELP:
755 case PARSE_OPT_COMPLETE:
756 BUG("parse_short_opt() cannot return these");
757 case PARSE_OPT_DONE:
758 break;
07fe54db 759 }
ff43ec3e 760 if (ctx->opt)
3a9f0f41 761 check_typos(arg + 1, options);
ff43ec3e 762 while (ctx->opt) {
07fe54db 763 switch (parse_short_opt(ctx, options)) {
f41179f1 764 case PARSE_OPT_ERROR:
3bb0923f 765 return PARSE_OPT_ERROR;
f41179f1 766 case PARSE_OPT_UNKNOWN:
5ad0d3d5
RS
767 if (internal_help && *ctx->opt == 'h')
768 goto show_usage;
769
26141b5b
PH
770 /* fake a short option thing to hide the fact that we may have
771 * started to parse aggregated stuff
772 *
773 * This is leaky, too bad.
774 */
775 ctx->argv[0] = xstrdup(ctx->opt - 1);
776 *(char *)ctx->argv[0] = '-';
b5ce3a54 777 goto unknown;
f41179f1
NTND
778 case PARSE_OPT_NON_OPTION:
779 case PARSE_OPT_COMPLETE:
780 case PARSE_OPT_HELP:
781 BUG("parse_short_opt() cannot return these");
782 case PARSE_OPT_DONE:
783 break;
07fe54db 784 }
3a9f0f41 785 }
4a59fd13
PH
786 continue;
787 }
788
51b4594b
JK
789 if (!arg[2] /* "--" */ ||
790 !strcmp(arg + 2, "end-of-options")) {
ff43ec3e
PH
791 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
792 ctx->argc--;
793 ctx->argv++;
4a59fd13
PH
794 }
795 break;
796 }
797
b92891f9 798 if (internal_help && !strcmp(arg + 2, "help-all"))
47e9cd28 799 return usage_with_options_internal(ctx, usagestr, options, 1, 0);
b92891f9 800 if (internal_help && !strcmp(arg + 2, "help"))
ac20ff6d 801 goto show_usage;
07fe54db 802 switch (parse_long_opt(ctx, arg + 2, options)) {
f41179f1 803 case PARSE_OPT_ERROR:
3bb0923f 804 return PARSE_OPT_ERROR;
f41179f1 805 case PARSE_OPT_UNKNOWN:
b5ce3a54 806 goto unknown;
f41179f1 807 case PARSE_OPT_HELP:
3bb0923f 808 goto show_usage;
f41179f1
NTND
809 case PARSE_OPT_NON_OPTION:
810 case PARSE_OPT_COMPLETE:
811 BUG("parse_long_opt() cannot return these");
812 case PARSE_OPT_DONE:
813 break;
07fe54db 814 }
b5ce3a54
RS
815 continue;
816unknown:
202fbb33
NTND
817 if (ctx->flags & PARSE_OPT_ONE_SHOT)
818 break;
b5ce3a54
RS
819 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
820 return PARSE_OPT_UNKNOWN;
821 ctx->out[ctx->cpidx++] = ctx->argv[0];
822 ctx->opt = NULL;
ff43ec3e
PH
823 }
824 return PARSE_OPT_DONE;
ac20ff6d 825
ac20ff6d 826 show_usage:
3bb0923f 827 return usage_with_options_internal(ctx, usagestr, options, 0, 0);
ff43ec3e
PH
828}
829
830int parse_options_end(struct parse_opt_ctx_t *ctx)
831{
202fbb33
NTND
832 if (ctx->flags & PARSE_OPT_ONE_SHOT)
833 return ctx->total - ctx->argc;
834
f919ffeb 835 MOVE_ARRAY(ctx->out + ctx->cpidx, ctx->argv, ctx->argc);
ff43ec3e
PH
836 ctx->out[ctx->cpidx + ctx->argc] = NULL;
837 return ctx->cpidx + ctx->argc;
838}
839
37782920
SB
840int parse_options(int argc, const char **argv, const char *prefix,
841 const struct option *options, const char * const usagestr[],
842 int flags)
ff43ec3e
PH
843{
844 struct parse_opt_ctx_t ctx;
5c387428 845 struct option *real_options;
ff43ec3e 846
b02e7d5d
JS
847 disallow_abbreviated_options =
848 git_env_bool("GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS", 0);
849
5c387428
NTND
850 memset(&ctx, 0, sizeof(ctx));
851 real_options = preprocess_options(&ctx, options);
852 if (real_options)
853 options = real_options;
854 parse_options_start_1(&ctx, argc, argv, prefix, options, flags);
ff43ec3e
PH
855 switch (parse_options_step(&ctx, options, usagestr)) {
856 case PARSE_OPT_HELP:
3bb0923f 857 case PARSE_OPT_ERROR:
ff43ec3e 858 exit(129);
a92ec7ef
NTND
859 case PARSE_OPT_COMPLETE:
860 exit(0);
979240fe 861 case PARSE_OPT_NON_OPTION:
ff43ec3e
PH
862 case PARSE_OPT_DONE:
863 break;
864 default: /* PARSE_OPT_UNKNOWN */
07fe54db 865 if (ctx.argv[0][1] == '-') {
89003426 866 error(_("unknown option `%s'"), ctx.argv[0] + 2);
b141a478 867 } else if (isascii(*ctx.opt)) {
89003426 868 error(_("unknown switch `%c'"), *ctx.opt);
b141a478 869 } else {
89003426 870 error(_("unknown non-ascii option in string: `%s'"),
b141a478 871 ctx.argv[0]);
07fe54db
PH
872 }
873 usage_with_options(usagestr, options);
4a59fd13
PH
874 }
875
5c327502 876 precompose_argv_prefix(argc, argv, NULL);
64cc539f 877 free_preprocessed_options(real_options);
5c387428 878 free(ctx.alias_groups);
7e7bbcb4 879 return parse_options_end(&ctx);
4a59fd13 880}
d7a38c54 881
9c7304e3 882static int usage_argh(const struct option *opts, FILE *outfile)
29f25d49
SB
883{
884 const char *s;
5f0df44c
RS
885 int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
886 !opts->argh || !!strpbrk(opts->argh, "()<>[]|");
29f25d49
SB
887 if (opts->flags & PARSE_OPT_OPTARG)
888 if (opts->long_name)
889 s = literal ? "[=%s]" : "[=<%s>]";
890 else
891 s = literal ? "[%s]" : "[<%s>]";
892 else
893 s = literal ? " %s" : " <%s>";
c0821965 894 return utf8_fprintf(outfile, s, opts->argh ? _(opts->argh) : _("..."));
29f25d49
SB
895}
896
d7a38c54
PH
897#define USAGE_OPTS_WIDTH 24
898#define USAGE_GAP 2
899
47e9cd28
TR
900static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
901 const char * const *usagestr,
902 const struct option *opts, int full, int err)
d7a38c54 903{
9c7304e3 904 FILE *outfile = err ? stderr : stdout;
a6304fa4 905 int need_newline;
9c7304e3 906
4631cfc2
ÆAB
907 const char *usage_prefix = _("usage: %s");
908 /*
909 * The translation could be anything, but we can count on
910 * msgfmt(1)'s --check option to have asserted that "%s" is in
911 * the translation. So compute the length of the "usage: "
912 * part. We are assuming that the translator wasn't overly
913 * clever and used e.g. "%1$s" instead of "%s", there's only
914 * one "%s" in "usage_prefix" above, so there's no reason to
915 * do so even with a RTL language.
916 */
917 size_t usage_len = strlen(usage_prefix) - strlen("%s");
918 /*
919 * TRANSLATORS: the colon here should align with the
920 * one in "usage: %s" translation.
921 */
922 const char *or_prefix = _(" or: %s");
923 /*
924 * TRANSLATORS: You should only need to translate this format
925 * string if your language is a RTL language (e.g. Arabic,
926 * Hebrew etc.), not if it's a LTR language (e.g. German,
927 * Russian, Chinese etc.).
928 *
929 * When a translated usage string has an embedded "\n" it's
930 * because options have wrapped to the next line. The line
931 * after the "\n" will then be padded to align with the
932 * command name, such as N_("git cmd [opt]\n<8
933 * spaces>[opt2]"), where the 8 spaces are the same length as
934 * "git cmd ".
935 *
936 * This format string prints out that already-translated
937 * line. The "%*s" is whitespace padding to account for the
938 * padding at the start of the line that we add in this
939 * function. The "%s" is a line in the (hopefully already
940 * translated) N_() usage string, which contained embedded
941 * newlines before we split it up.
942 */
943 const char *usage_continued = _("%*s%s");
944 const char *prefix = usage_prefix;
945 int saw_empty_line = 0;
946
49b61802
RS
947 if (!usagestr)
948 return PARSE_OPT_HELP;
949
47e9cd28
TR
950 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
951 fprintf(outfile, "cat <<\\EOF\n");
952
44d86e91 953 while (*usagestr) {
4631cfc2
ÆAB
954 const char *str = _(*usagestr++);
955 struct string_list list = STRING_LIST_INIT_DUP;
956 unsigned int j;
957
958 if (!saw_empty_line && !*str)
959 saw_empty_line = 1;
960
961 string_list_split(&list, str, '\n', -1);
962 for (j = 0; j < list.nr; j++) {
963 const char *line = list.items[j].string;
964
965 if (saw_empty_line && *line)
966 fprintf_ln(outfile, _(" %s"), line);
967 else if (saw_empty_line)
968 fputc('\n', outfile);
969 else if (!j)
970 fprintf_ln(outfile, prefix, line);
971 else
972 fprintf_ln(outfile, usage_continued,
973 (int)usage_len, "", line);
974 }
975 string_list_clear(&list, 0);
976
977 prefix = or_prefix;
44d86e91 978 }
d7a38c54 979
a6304fa4 980 need_newline = 1;
d7a38c54
PH
981
982 for (; opts->type != OPTION_END; opts++) {
983 size_t pos;
984 int pad;
985
986 if (opts->type == OPTION_GROUP) {
9c7304e3 987 fputc('\n', outfile);
a6304fa4 988 need_newline = 0;
d7a38c54 989 if (*opts->help)
54e6dc7d 990 fprintf(outfile, "%s\n", _(opts->help));
d7a38c54
PH
991 continue;
992 }
dd3bf0f4
PH
993 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
994 continue;
d7a38c54 995
a6304fa4
BC
996 if (need_newline) {
997 fputc('\n', outfile);
998 need_newline = 0;
999 }
1000
9c7304e3 1001 pos = fprintf(outfile, " ");
cbb08c2e 1002 if (opts->short_name) {
51a9949e 1003 if (opts->flags & PARSE_OPT_NODASH)
9c7304e3 1004 pos += fprintf(outfile, "%c", opts->short_name);
51a9949e 1005 else
9c7304e3 1006 pos += fprintf(outfile, "-%c", opts->short_name);
51a9949e 1007 }
d7a38c54 1008 if (opts->long_name && opts->short_name)
9c7304e3 1009 pos += fprintf(outfile, ", ");
d7a38c54 1010 if (opts->long_name)
cbb08c2e 1011 pos += fprintf(outfile, "--%s", opts->long_name);
e0319ff5 1012 if (opts->type == OPTION_NUMBER)
c0821965 1013 pos += utf8_fprintf(outfile, _("-NUM"));
d7a38c54 1014
b57c68a6
JN
1015 if ((opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
1016 !(opts->flags & PARSE_OPT_NOARG))
9c7304e3 1017 pos += usage_argh(opts, outfile);
d7a38c54 1018
f389c808
AR
1019 if (pos <= USAGE_OPTS_WIDTH)
1020 pad = USAGE_OPTS_WIDTH - pos;
d7a38c54 1021 else {
9c7304e3 1022 fputc('\n', outfile);
d7a38c54
PH
1023 pad = USAGE_OPTS_WIDTH;
1024 }
5c387428
NTND
1025 if (opts->type == OPTION_ALIAS) {
1026 fprintf(outfile, "%*s", pad + USAGE_GAP, "");
1027 fprintf_ln(outfile, _("alias of --%s"),
1028 (const char *)opts->value);
1029 continue;
1030 }
54e6dc7d 1031 fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", _(opts->help));
d7a38c54 1032 }
9c7304e3 1033 fputc('\n', outfile);
f389c808 1034
47e9cd28
TR
1035 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
1036 fputs("EOF\n", outfile);
1037
ee68b87a 1038 return PARSE_OPT_HELP;
d7a38c54 1039}
0ce865b1 1040
c2e86add 1041void NORETURN usage_with_options(const char * const *usagestr,
ff43ec3e 1042 const struct option *opts)
dd3bf0f4 1043{
47e9cd28 1044 usage_with_options_internal(NULL, usagestr, opts, 0, 1);
ff43ec3e 1045 exit(129);
dd3bf0f4
PH
1046}
1047
c2e86add 1048void NORETURN usage_msg_opt(const char *msg,
451bb210
CC
1049 const char * const *usagestr,
1050 const struct option *options)
1051{
87433261 1052 fprintf(stderr, "fatal: %s\n\n", msg);
451bb210
CC
1053 usage_with_options(usagestr, options);
1054}
1055
9440b831 1056const char *optname(const struct option *opt, int flags)
a469a101 1057{
9440b831
NTND
1058 static struct strbuf sb = STRBUF_INIT;
1059
1060 strbuf_reset(&sb);
a469a101 1061 if (flags & OPT_SHORT)
9440b831
NTND
1062 strbuf_addf(&sb, "switch `%c'", opt->short_name);
1063 else if (flags & OPT_UNSET)
1064 strbuf_addf(&sb, "option `no-%s'", opt->long_name);
1065 else
1066 strbuf_addf(&sb, "option `%s'", opt->long_name);
1067
1068 return sb.buf;
a469a101 1069}