]> git.ipfire.org Git - thirdparty/git.git/blame - parse-options.c
Merge branch 'ab/config-multi-and-nonbool'
[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
d3428345
ÆAB
11enum opt_parsed {
12 OPT_LONG = 0,
13 OPT_SHORT = 1<<0,
14 OPT_UNSET = 1<<1,
15};
4a59fd13 16
53ca5694 17static void optbug(const struct option *opt, const char *reason)
1e5ce570 18{
53ca5694
ÆAB
19 if (opt->long_name && opt->short_name)
20 bug("switch '%c' (--%s) %s", opt->short_name,
21 opt->long_name, reason);
22 else if (opt->long_name)
23 bug("option '%s' %s", opt->long_name, reason);
24 else
25 bug("switch '%c' %s", opt->short_name, reason);
1e5ce570
JN
26}
27
d3428345 28static const char *optname(const struct option *opt, enum opt_parsed flags)
3c2047a7
ÆAB
29{
30 static struct strbuf sb = STRBUF_INIT;
31
32 strbuf_reset(&sb);
33 if (flags & OPT_SHORT)
34 strbuf_addf(&sb, "switch `%c'", opt->short_name);
35 else if (flags & OPT_UNSET)
36 strbuf_addf(&sb, "option `no-%s'", opt->long_name);
d3428345 37 else if (flags == OPT_LONG)
3c2047a7 38 strbuf_addf(&sb, "option `%s'", opt->long_name);
d3428345
ÆAB
39 else
40 BUG("optname() got unknown flags %d", flags);
3c2047a7
ÆAB
41
42 return sb.buf;
43}
44
f41179f1
NTND
45static enum parse_opt_result get_arg(struct parse_opt_ctx_t *p,
46 const struct option *opt,
d3428345 47 enum opt_parsed flags, const char **arg)
1cc6985c
PH
48{
49 if (p->opt) {
50 *arg = p->opt;
51 p->opt = NULL;
52 } else if (p->argc == 1 && (opt->flags & PARSE_OPT_LASTARG_DEFAULT)) {
53 *arg = (const char *)opt->defval;
d5d745f9 54 } else if (p->argc > 1) {
1cc6985c
PH
55 p->argc--;
56 *arg = *++p->argv;
57 } else
9440b831 58 return error(_("%s requires a value"), optname(opt, flags));
1cc6985c
PH
59 return 0;
60}
61
7ce4088a 62static void fix_filename(const char *prefix, char **file)
df217ed6 63{
7ce4088a
JK
64 if (!file || !*file)
65 ; /* leave as NULL */
7ce4088a 66 else
0bbe1031 67 *file = prefix_filename_except_for_dash(prefix, *file);
df217ed6
SB
68}
69
f41179f1
NTND
70static enum parse_opt_result opt_command_mode_error(
71 const struct option *opt,
72 const struct option *all_opts,
d3428345 73 enum opt_parsed flags)
11588263
JH
74{
75 const struct option *that;
11588263
JH
76 struct strbuf that_name = STRBUF_INIT;
77
78 /*
79 * Find the other option that was used to set the variable
80 * already, and report that this is not compatible with it.
81 */
82 for (that = all_opts; that->type != OPTION_END; that++) {
83 if (that == opt ||
bc8620b4 84 !(that->flags & PARSE_OPT_CMDMODE) ||
11588263
JH
85 that->value != opt->value ||
86 that->defval != *(int *)opt->value)
87 continue;
88
89 if (that->long_name)
90 strbuf_addf(&that_name, "--%s", that->long_name);
91 else
92 strbuf_addf(&that_name, "-%c", that->short_name);
9440b831
NTND
93 error(_("%s is incompatible with %s"),
94 optname(opt, flags), that_name.buf);
11588263 95 strbuf_release(&that_name);
f41179f1 96 return PARSE_OPT_ERROR;
11588263 97 }
9440b831
NTND
98 return error(_("%s : incompatible with something else"),
99 optname(opt, flags));
11588263
JH
100}
101
f41179f1
NTND
102static enum parse_opt_result get_value(struct parse_opt_ctx_t *p,
103 const struct option *opt,
104 const struct option *all_opts,
d3428345 105 enum opt_parsed flags)
4a59fd13 106{
ffe659f9 107 const char *s, *arg;
db7244bd 108 const int unset = flags & OPT_UNSET;
df217ed6 109 int err;
4a59fd13 110
db7244bd 111 if (unset && p->opt)
9440b831 112 return error(_("%s takes no value"), optname(opt, flags));
db7244bd 113 if (unset && (opt->flags & PARSE_OPT_NONEG))
9440b831 114 return error(_("%s isn't available"), optname(opt, flags));
c1f4ec9e 115 if (!(flags & OPT_SHORT) && p->opt && (opt->flags & PARSE_OPT_NOARG))
9440b831 116 return error(_("%s takes no value"), optname(opt, flags));
db7244bd 117
bc8620b4
PB
118 /*
119 * Giving the same mode option twice, although unnecessary,
120 * is not a grave error, so let it pass.
121 */
122 if ((opt->flags & PARSE_OPT_CMDMODE) &&
123 *(int *)opt->value && *(int *)opt->value != opt->defval)
124 return opt_command_mode_error(opt, all_opts, flags);
125
db7244bd 126 switch (opt->type) {
b0b3a8b6 127 case OPTION_LOWLEVEL_CALLBACK:
3ebbe289 128 return opt->ll_callback(p, opt, NULL, unset);
b0b3a8b6 129
db7244bd
PH
130 case OPTION_BIT:
131 if (unset)
132 *(int *)opt->value &= ~opt->defval;
4a59fd13 133 else
db7244bd
PH
134 *(int *)opt->value |= opt->defval;
135 return 0;
136
2f4b97f9
RS
137 case OPTION_NEGBIT:
138 if (unset)
139 *(int *)opt->value |= opt->defval;
140 else
141 *(int *)opt->value &= ~opt->defval;
142 return 0;
143
f62470c6
NTND
144 case OPTION_BITOP:
145 if (unset)
146 BUG("BITOP can't have unset form");
147 *(int *)opt->value &= ~opt->extra;
148 *(int *)opt->value |= opt->defval;
149 return 0;
150
b04ba2bb 151 case OPTION_COUNTUP:
e0070e8b
PB
152 if (*(int *)opt->value < 0)
153 *(int *)opt->value = 0;
db7244bd
PH
154 *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
155 return 0;
156
157 case OPTION_SET_INT:
158 *(int *)opt->value = unset ? 0 : opt->defval;
159 return 0;
160
4a59fd13 161 case OPTION_STRING:
1cc6985c 162 if (unset)
db7244bd 163 *(const char **)opt->value = NULL;
1cc6985c 164 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
ffe659f9 165 *(const char **)opt->value = (const char *)opt->defval;
1cc6985c
PH
166 else
167 return get_arg(p, opt, flags, (const char **)opt->value);
4a59fd13
PH
168 return 0;
169
df217ed6
SB
170 case OPTION_FILENAME:
171 err = 0;
172 if (unset)
173 *(const char **)opt->value = NULL;
174 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
175 *(const char **)opt->value = (const char *)opt->defval;
176 else
177 err = get_arg(p, opt, flags, (const char **)opt->value);
178
179 if (!err)
7ce4088a 180 fix_filename(p->prefix, (char **)opt->value);
df217ed6
SB
181 return err;
182
ffe659f9 183 case OPTION_CALLBACK:
3ebbe289
NTND
184 {
185 const char *p_arg = NULL;
186 int p_unset;
187
db7244bd 188 if (unset)
3ebbe289
NTND
189 p_unset = 1;
190 else if (opt->flags & PARSE_OPT_NOARG)
191 p_unset = 0;
192 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
193 p_unset = 0;
194 else if (get_arg(p, opt, flags, &arg))
1cc6985c 195 return -1;
3ebbe289
NTND
196 else {
197 p_unset = 0;
198 p_arg = arg;
199 }
200 if (opt->callback)
201 return (*opt->callback)(opt, p_arg, p_unset) ? (-1) : 0;
202 else
203 return (*opt->ll_callback)(p, opt, p_arg, p_unset);
204 }
4a59fd13 205 case OPTION_INTEGER:
db7244bd 206 if (unset) {
4a59fd13
PH
207 *(int *)opt->value = 0;
208 return 0;
209 }
c43a2483 210 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
ffe659f9
PH
211 *(int *)opt->value = opt->defval;
212 return 0;
213 }
1cc6985c
PH
214 if (get_arg(p, opt, flags, &arg))
215 return -1;
f7e68a08
NTND
216 if (!*arg)
217 return error(_("%s expects a numerical value"),
218 optname(opt, flags));
1cc6985c 219 *(int *)opt->value = strtol(arg, (char **)&s, 10);
4a59fd13 220 if (*s)
9440b831
NTND
221 return error(_("%s expects a numerical value"),
222 optname(opt, flags));
4a59fd13
PH
223 return 0;
224
2a514ed8
CB
225 case OPTION_MAGNITUDE:
226 if (unset) {
227 *(unsigned long *)opt->value = 0;
228 return 0;
229 }
230 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
231 *(unsigned long *)opt->value = opt->defval;
232 return 0;
233 }
234 if (get_arg(p, opt, flags, &arg))
235 return -1;
236 if (!git_parse_ulong(arg, opt->value))
9440b831
NTND
237 return error(_("%s expects a non-negative integer value"
238 " with an optional k/m/g suffix"),
239 optname(opt, flags));
2a514ed8
CB
240 return 0;
241
4a59fd13 242 default:
48a5499e 243 BUG("opt->type %d should not happen", opt->type);
4a59fd13
PH
244 }
245}
246
f41179f1
NTND
247static enum parse_opt_result parse_short_opt(struct parse_opt_ctx_t *p,
248 const struct option *options)
4a59fd13 249{
11588263 250 const struct option *all_opts = options;
e0319ff5
RS
251 const struct option *numopt = NULL;
252
4a59fd13
PH
253 for (; options->type != OPTION_END; options++) {
254 if (options->short_name == *p->opt) {
255 p->opt = p->opt[1] ? p->opt + 1 : NULL;
11588263 256 return get_value(p, options, all_opts, OPT_SHORT);
4a59fd13 257 }
e0319ff5
RS
258
259 /*
260 * Handle the numerical option later, explicit one-digit
261 * options take precedence over it.
262 */
263 if (options->type == OPTION_NUMBER)
264 numopt = options;
265 }
266 if (numopt && isdigit(*p->opt)) {
267 size_t len = 1;
268 char *arg;
269 int rc;
270
271 while (isdigit(p->opt[len]))
272 len++;
273 arg = xmemdupz(p->opt, len);
274 p->opt = p->opt[len] ? p->opt + len : NULL;
3ebbe289
NTND
275 if (numopt->callback)
276 rc = (*numopt->callback)(numopt, arg, 0) ? (-1) : 0;
277 else
278 rc = (*numopt->ll_callback)(p, numopt, arg, 0);
e0319ff5
RS
279 free(arg);
280 return rc;
4a59fd13 281 }
f41179f1 282 return PARSE_OPT_UNKNOWN;
4a59fd13
PH
283}
284
5c387428
NTND
285static int has_string(const char *it, const char **array)
286{
287 while (*array)
288 if (!strcmp(it, *(array++)))
289 return 1;
290 return 0;
291}
292
293static int is_alias(struct parse_opt_ctx_t *ctx,
294 const struct option *one_opt,
295 const struct option *another_opt)
296{
297 const char **group;
298
299 if (!ctx->alias_groups)
300 return 0;
301
302 if (!one_opt->long_name || !another_opt->long_name)
303 return 0;
304
305 for (group = ctx->alias_groups; *group; group += 3) {
306 /* it and other are from the same family? */
307 if (has_string(one_opt->long_name, group) &&
308 has_string(another_opt->long_name, group))
309 return 1;
310 }
311 return 0;
312}
313
f41179f1
NTND
314static enum parse_opt_result parse_long_opt(
315 struct parse_opt_ctx_t *p, const char *arg,
316 const struct option *options)
4a59fd13 317{
11588263 318 const struct option *all_opts = options;
2c5495f7 319 const char *arg_end = strchrnul(arg, '=');
243e0614 320 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
d3428345 321 enum opt_parsed abbrev_flags = OPT_LONG, ambiguous_flags = OPT_LONG;
7f275b91 322
4a59fd13 323 for (; options->type != OPTION_END; options++) {
0f1930c5 324 const char *rest, *long_name = options->long_name;
d3428345 325 enum opt_parsed flags = OPT_LONG, opt_flags = OPT_LONG;
4a59fd13 326
fa83cc83
SG
327 if (options->type == OPTION_SUBCOMMAND)
328 continue;
0f1930c5 329 if (!long_name)
4a59fd13
PH
330 continue;
331
0f1930c5 332again:
cf4fff57
JK
333 if (!skip_prefix(arg, long_name, &rest))
334 rest = NULL;
4a59fd13 335 if (!rest) {
7f275b91 336 /* abbreviated? */
99d86d60 337 if (!(p->flags & PARSE_OPT_KEEP_UNKNOWN_OPT) &&
baa4adc6 338 !strncmp(long_name, arg, arg_end - arg)) {
7f275b91 339is_abbreviated:
5c387428
NTND
340 if (abbrev_option &&
341 !is_alias(p, abbrev_option, options)) {
243e0614
JS
342 /*
343 * If this is abbreviated, it is
344 * ambiguous. So when there is no
345 * exact match later, we need to
346 * error out.
347 */
348 ambiguous_option = abbrev_option;
349 ambiguous_flags = abbrev_flags;
350 }
7f275b91
JS
351 if (!(flags & OPT_UNSET) && *arg_end)
352 p->opt = arg_end + 1;
353 abbrev_option = options;
0f1930c5 354 abbrev_flags = flags ^ opt_flags;
7f275b91
JS
355 continue;
356 }
6bbfd1fa
AS
357 /* negation allowed? */
358 if (options->flags & PARSE_OPT_NONEG)
359 continue;
7f275b91 360 /* negated and abbreviated very much? */
59556548 361 if (starts_with("no-", arg)) {
7f275b91
JS
362 flags |= OPT_UNSET;
363 goto is_abbreviated;
364 }
365 /* negated? */
59556548 366 if (!starts_with(arg, "no-")) {
145136a9 367 if (skip_prefix(long_name, "no-", &long_name)) {
0f1930c5
RS
368 opt_flags |= OPT_UNSET;
369 goto again;
370 }
4a59fd13 371 continue;
0f1930c5 372 }
4a59fd13 373 flags |= OPT_UNSET;
cf4fff57
JK
374 if (!skip_prefix(arg + 3, long_name, &rest)) {
375 /* abbreviated and negated? */
376 if (starts_with(long_name, arg + 3))
377 goto is_abbreviated;
378 else
379 continue;
380 }
4a59fd13
PH
381 }
382 if (*rest) {
383 if (*rest != '=')
384 continue;
385 p->opt = rest + 1;
386 }
11588263 387 return get_value(p, options, all_opts, flags ^ opt_flags);
4a59fd13 388 }
243e0614 389
b02e7d5d
JS
390 if (disallow_abbreviated_options && (ambiguous_option || abbrev_option))
391 die("disallowed abbreviated or ambiguous option '%.*s'",
392 (int)(arg_end - arg), arg);
393
3bb0923f 394 if (ambiguous_option) {
89003426
NTND
395 error(_("ambiguous option: %s "
396 "(could be --%s%s or --%s%s)"),
243e0614
JS
397 arg,
398 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
399 ambiguous_option->long_name,
400 (abbrev_flags & OPT_UNSET) ? "no-" : "",
401 abbrev_option->long_name);
f41179f1 402 return PARSE_OPT_HELP;
3bb0923f 403 }
7f275b91 404 if (abbrev_option)
11588263 405 return get_value(p, abbrev_option, all_opts, abbrev_flags);
f41179f1 406 return PARSE_OPT_UNKNOWN;
4a59fd13
PH
407}
408
68611f51
ÆAB
409static enum parse_opt_result parse_nodash_opt(struct parse_opt_ctx_t *p,
410 const char *arg,
411 const struct option *options)
51a9949e 412{
11588263
JH
413 const struct option *all_opts = options;
414
51a9949e
RS
415 for (; options->type != OPTION_END; options++) {
416 if (!(options->flags & PARSE_OPT_NODASH))
417 continue;
51a9949e 418 if (options->short_name == arg[0] && arg[1] == '\0')
11588263 419 return get_value(p, options, all_opts, OPT_SHORT);
51a9949e 420 }
68611f51 421 return PARSE_OPT_ERROR;
51a9949e
RS
422}
423
fa83cc83
SG
424static enum parse_opt_result parse_subcommand(const char *arg,
425 const struct option *options)
426{
427 for (; options->type != OPTION_END; options++)
428 if (options->type == OPTION_SUBCOMMAND &&
429 !strcmp(options->long_name, arg)) {
430 *(parse_opt_subcommand_fn **)options->value = options->subcommand_fn;
431 return PARSE_OPT_SUBCOMMAND;
432 }
433
434 return PARSE_OPT_UNKNOWN;
435}
436
1121a878 437static void check_typos(const char *arg, const struct option *options)
3a9f0f41
PH
438{
439 if (strlen(arg) < 3)
440 return;
441
59556548 442 if (starts_with(arg, "no-")) {
395518cf 443 error(_("did you mean `--%s` (with two dashes)?"), arg);
3a9f0f41
PH
444 exit(129);
445 }
446
447 for (; options->type != OPTION_END; options++) {
448 if (!options->long_name)
449 continue;
59556548 450 if (starts_with(options->long_name, arg)) {
395518cf 451 error(_("did you mean `--%s` (with two dashes)?"), arg);
3a9f0f41
PH
452 exit(129);
453 }
454 }
455}
456
cb9d398c
PH
457static void parse_options_check(const struct option *opts)
458{
af465af8 459 char short_opts[128];
fa83cc83 460 void *subcommand_value = NULL;
cb9d398c 461
af465af8 462 memset(short_opts, '\0', sizeof(short_opts));
cb9d398c
PH
463 for (; opts->type != OPTION_END; opts++) {
464 if ((opts->flags & PARSE_OPT_LASTARG_DEFAULT) &&
1e5ce570 465 (opts->flags & PARSE_OPT_OPTARG))
53ca5694
ÆAB
466 optbug(opts, "uses incompatible flags "
467 "LASTARG_DEFAULT and OPTARG");
af465af8
JH
468 if (opts->short_name) {
469 if (0x7F <= opts->short_name)
53ca5694 470 optbug(opts, "invalid short name");
af465af8 471 else if (short_opts[opts->short_name]++)
53ca5694 472 optbug(opts, "short name already used");
af465af8 473 }
a02dd4ff
JN
474 if (opts->flags & PARSE_OPT_NODASH &&
475 ((opts->flags & PARSE_OPT_OPTARG) ||
476 !(opts->flags & PARSE_OPT_NOARG) ||
477 !(opts->flags & PARSE_OPT_NONEG) ||
478 opts->long_name))
53ca5694
ÆAB
479 optbug(opts, "uses feature "
480 "not supported for dashless options");
5c400ed2 481 switch (opts->type) {
b04ba2bb 482 case OPTION_COUNTUP:
5c400ed2
JN
483 case OPTION_BIT:
484 case OPTION_NEGBIT:
485 case OPTION_SET_INT:
5c400ed2
JN
486 case OPTION_NUMBER:
487 if ((opts->flags & PARSE_OPT_OPTARG) ||
488 !(opts->flags & PARSE_OPT_NOARG))
53ca5694 489 optbug(opts, "should not accept an argument");
bf3ff338
NTND
490 break;
491 case OPTION_CALLBACK:
3ebbe289 492 if (!opts->callback && !opts->ll_callback)
5b2f5d92
ÆAB
493 optbug(opts, "OPTION_CALLBACK needs one callback");
494 else if (opts->callback && opts->ll_callback)
495 optbug(opts, "OPTION_CALLBACK can't have two callbacks");
bf3ff338
NTND
496 break;
497 case OPTION_LOWLEVEL_CALLBACK:
498 if (!opts->ll_callback)
5b2f5d92 499 optbug(opts, "OPTION_LOWLEVEL_CALLBACK needs a callback");
bf3ff338 500 if (opts->callback)
5b2f5d92 501 optbug(opts, "OPTION_LOWLEVEL_CALLBACK needs no high level callback");
bf3ff338 502 break;
5c387428 503 case OPTION_ALIAS:
5b2f5d92
ÆAB
504 optbug(opts, "OPT_ALIAS() should not remain at this point. "
505 "Are you using parse_options_step() directly?\n"
506 "That case is not supported yet.");
507 break;
fa83cc83
SG
508 case OPTION_SUBCOMMAND:
509 if (!opts->value || !opts->subcommand_fn)
510 optbug(opts, "OPTION_SUBCOMMAND needs a value and a subcommand function");
511 if (!subcommand_value)
512 subcommand_value = opts->value;
513 else if (subcommand_value != opts->value)
514 optbug(opts, "all OPTION_SUBCOMMANDs need the same value");
515 break;
5c400ed2
JN
516 default:
517 ; /* ok. (usually accepts an argument) */
518 }
b6c2a0d4
JH
519 if (opts->argh &&
520 strcspn(opts->argh, " _") != strlen(opts->argh))
53ca5694 521 optbug(opts, "multi-word argh should use dash to separate words");
cb9d398c 522 }
53ca5694 523 BUG_if_bug("invalid 'struct option'");
cb9d398c
PH
524}
525
fa83cc83
SG
526static int has_subcommands(const struct option *options)
527{
528 for (; options->type != OPTION_END; options++)
529 if (options->type == OPTION_SUBCOMMAND)
530 return 1;
531 return 0;
532}
533
5c387428
NTND
534static void parse_options_start_1(struct parse_opt_ctx_t *ctx,
535 int argc, const char **argv, const char *prefix,
3f9ab7cc
ÆAB
536 const struct option *options,
537 enum parse_opt_flags flags)
7e7bbcb4 538{
202fbb33
NTND
539 ctx->argc = argc;
540 ctx->argv = argv;
541 if (!(flags & PARSE_OPT_ONE_SHOT)) {
542 ctx->argc--;
543 ctx->argv++;
544 }
545 ctx->total = ctx->argc;
546 ctx->out = argv;
37782920 547 ctx->prefix = prefix;
a32a4eaa 548 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
7e7bbcb4 549 ctx->flags = flags;
fa83cc83
SG
550 ctx->has_subcommands = has_subcommands(options);
551 if (!ctx->has_subcommands && (flags & PARSE_OPT_SUBCOMMAND_OPTIONAL))
552 BUG("Using PARSE_OPT_SUBCOMMAND_OPTIONAL without subcommands");
553 if (ctx->has_subcommands) {
554 if (flags & PARSE_OPT_STOP_AT_NON_OPTION)
555 BUG("subcommands are incompatible with PARSE_OPT_STOP_AT_NON_OPTION");
556 if (!(flags & PARSE_OPT_SUBCOMMAND_OPTIONAL)) {
557 if (flags & PARSE_OPT_KEEP_UNKNOWN_OPT)
558 BUG("subcommands are incompatible with PARSE_OPT_KEEP_UNKNOWN_OPT unless in combination with PARSE_OPT_SUBCOMMAND_OPTIONAL");
559 if (flags & PARSE_OPT_KEEP_DASHDASH)
560 BUG("subcommands are incompatible with PARSE_OPT_KEEP_DASHDASH unless in combination with PARSE_OPT_SUBCOMMAND_OPTIONAL");
561 }
562 }
99d86d60 563 if ((flags & PARSE_OPT_KEEP_UNKNOWN_OPT) &&
202fbb33
NTND
564 (flags & PARSE_OPT_STOP_AT_NON_OPTION) &&
565 !(flags & PARSE_OPT_ONE_SHOT))
48a5499e 566 BUG("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
202fbb33
NTND
567 if ((flags & PARSE_OPT_ONE_SHOT) &&
568 (flags & PARSE_OPT_KEEP_ARGV0))
569 BUG("Can't keep argv0 if you don't have it");
9ca1169f 570 parse_options_check(options);
7e7bbcb4
PH
571}
572
5c387428
NTND
573void parse_options_start(struct parse_opt_ctx_t *ctx,
574 int argc, const char **argv, const char *prefix,
3f9ab7cc
ÆAB
575 const struct option *options,
576 enum parse_opt_flags flags)
5c387428
NTND
577{
578 memset(ctx, 0, sizeof(*ctx));
579 parse_options_start_1(ctx, argc, argv, prefix, options, flags);
580}
581
a0abe5e3
RZ
582static void show_negated_gitcomp(const struct option *opts, int show_all,
583 int nr_noopts)
b221b5ab
NTND
584{
585 int printed_dashdash = 0;
586
587 for (; opts->type != OPTION_END; opts++) {
588 int has_unset_form = 0;
589 const char *name;
590
591 if (!opts->long_name)
592 continue;
a0abe5e3
RZ
593 if (!show_all &&
594 (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE)))
b221b5ab
NTND
595 continue;
596 if (opts->flags & PARSE_OPT_NONEG)
597 continue;
598
599 switch (opts->type) {
600 case OPTION_STRING:
601 case OPTION_FILENAME:
602 case OPTION_INTEGER:
603 case OPTION_MAGNITUDE:
604 case OPTION_CALLBACK:
605 case OPTION_BIT:
606 case OPTION_NEGBIT:
607 case OPTION_COUNTUP:
608 case OPTION_SET_INT:
609 has_unset_form = 1;
610 break;
611 default:
612 break;
613 }
614 if (!has_unset_form)
615 continue;
616
617 if (skip_prefix(opts->long_name, "no-", &name)) {
618 if (nr_noopts < 0)
619 printf(" --%s", name);
620 } else if (nr_noopts >= 0) {
621 if (nr_noopts && !printed_dashdash) {
622 printf(" --");
623 printed_dashdash = 1;
624 }
625 printf(" --no-%s", opts->long_name);
626 nr_noopts++;
627 }
628 }
629}
630
a0abe5e3 631static int show_gitcomp(const struct option *opts, int show_all)
b9d7f4b4 632{
b221b5ab
NTND
633 const struct option *original_opts = opts;
634 int nr_noopts = 0;
635
b9d7f4b4 636 for (; opts->type != OPTION_END; opts++) {
fa83cc83 637 const char *prefix = "--";
b9d7f4b4
NTND
638 const char *suffix = "";
639
640 if (!opts->long_name)
641 continue;
a0abe5e3 642 if (!show_all &&
ca2d62b7 643 (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE | PARSE_OPT_FROM_ALIAS)))
b9d7f4b4
NTND
644 continue;
645
646 switch (opts->type) {
fa83cc83
SG
647 case OPTION_SUBCOMMAND:
648 prefix = "";
649 break;
b9d7f4b4
NTND
650 case OPTION_GROUP:
651 continue;
652 case OPTION_STRING:
653 case OPTION_FILENAME:
654 case OPTION_INTEGER:
655 case OPTION_MAGNITUDE:
656 case OPTION_CALLBACK:
657 if (opts->flags & PARSE_OPT_NOARG)
658 break;
659 if (opts->flags & PARSE_OPT_OPTARG)
660 break;
661 if (opts->flags & PARSE_OPT_LASTARG_DEFAULT)
662 break;
663 suffix = "=";
664 break;
665 default:
666 break;
667 }
ebc4a04e
NTND
668 if (opts->flags & PARSE_OPT_COMP_ARG)
669 suffix = "=";
b221b5ab
NTND
670 if (starts_with(opts->long_name, "no-"))
671 nr_noopts++;
fa83cc83
SG
672 printf("%s%s%s%s", opts == original_opts ? "" : " ",
673 prefix, opts->long_name, suffix);
b9d7f4b4 674 }
a0abe5e3
RZ
675 show_negated_gitcomp(original_opts, show_all, -1);
676 show_negated_gitcomp(original_opts, show_all, nr_noopts);
b9d7f4b4 677 fputc('\n', stdout);
a92ec7ef 678 return PARSE_OPT_COMPLETE;
b9d7f4b4
NTND
679}
680
5c387428
NTND
681/*
682 * Scan and may produce a new option[] array, which should be used
683 * instead of the original 'options'.
684 *
15beaaa3 685 * Right now this is only used to preprocess and substitute
5c387428 686 * OPTION_ALIAS.
64cc539f
AH
687 *
688 * The returned options should be freed using free_preprocessed_options.
5c387428
NTND
689 */
690static struct option *preprocess_options(struct parse_opt_ctx_t *ctx,
691 const struct option *options)
692{
693 struct option *newopt;
694 int i, nr, alias;
695 int nr_aliases = 0;
696
697 for (nr = 0; options[nr].type != OPTION_END; nr++) {
698 if (options[nr].type == OPTION_ALIAS)
699 nr_aliases++;
700 }
701
702 if (!nr_aliases)
703 return NULL;
704
6e578410 705 DUP_ARRAY(newopt, options, nr + 1);
5c387428
NTND
706
707 /* each alias has two string pointers and NULL */
708 CALLOC_ARRAY(ctx->alias_groups, 3 * (nr_aliases + 1));
709
710 for (alias = 0, i = 0; i < nr; i++) {
711 int short_name;
712 const char *long_name;
713 const char *source;
7c280589 714 struct strbuf help = STRBUF_INIT;
5c387428
NTND
715 int j;
716
717 if (newopt[i].type != OPTION_ALIAS)
718 continue;
719
720 short_name = newopt[i].short_name;
721 long_name = newopt[i].long_name;
722 source = newopt[i].value;
723
724 if (!long_name)
725 BUG("An alias must have long option name");
7c280589 726 strbuf_addf(&help, _("alias of --%s"), source);
5c387428
NTND
727
728 for (j = 0; j < nr; j++) {
729 const char *name = options[j].long_name;
730
731 if (!name || strcmp(name, source))
732 continue;
733
734 if (options[j].type == OPTION_ALIAS)
735 BUG("No please. Nested aliases are not supported.");
736
5c387428
NTND
737 memcpy(newopt + i, options + j, sizeof(*newopt));
738 newopt[i].short_name = short_name;
739 newopt[i].long_name = long_name;
7c280589 740 newopt[i].help = strbuf_detach(&help, NULL);
64cc539f 741 newopt[i].flags |= PARSE_OPT_FROM_ALIAS;
5c387428
NTND
742 break;
743 }
744
745 if (j == nr)
746 BUG("could not find source option '%s' of alias '%s'",
747 source, newopt[i].long_name);
748 ctx->alias_groups[alias * 3 + 0] = newopt[i].long_name;
749 ctx->alias_groups[alias * 3 + 1] = options[j].long_name;
750 ctx->alias_groups[alias * 3 + 2] = NULL;
751 alias++;
752 }
753
754 return newopt;
755}
756
64cc539f
AH
757static void free_preprocessed_options(struct option *options)
758{
759 int i;
760
761 if (!options)
762 return;
763
764 for (i = 0; options[i].type != OPTION_END; i++) {
765 if (options[i].flags & PARSE_OPT_FROM_ALIAS)
766 free((void *)options[i].help);
767 }
768 free(options);
769}
770
352e7613
ÆAB
771static enum parse_opt_result usage_with_options_internal(struct parse_opt_ctx_t *,
772 const char * const *,
773 const struct option *,
774 int, int);
dd3bf0f4 775
352e7613
ÆAB
776enum parse_opt_result parse_options_step(struct parse_opt_ctx_t *ctx,
777 const struct option *options,
778 const char * const usagestr[])
4a59fd13 779{
b92891f9
RS
780 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
781
26141b5b
PH
782 /* we must reset ->opt, unknown short option leave it dangling */
783 ctx->opt = NULL;
784
ff43ec3e
PH
785 for (; ctx->argc; ctx->argc--, ctx->argv++) {
786 const char *arg = ctx->argv[0];
4a59fd13 787
202fbb33
NTND
788 if (ctx->flags & PARSE_OPT_ONE_SHOT &&
789 ctx->argc != ctx->total)
790 break;
791
4a59fd13 792 if (*arg != '-' || !arg[1]) {
51a9949e
RS
793 if (parse_nodash_opt(ctx, arg, options) == 0)
794 continue;
fa83cc83
SG
795 if (!ctx->has_subcommands) {
796 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
797 return PARSE_OPT_NON_OPTION;
798 ctx->out[ctx->cpidx++] = ctx->argv[0];
799 continue;
800 }
801 switch (parse_subcommand(arg, options)) {
802 case PARSE_OPT_SUBCOMMAND:
803 return PARSE_OPT_SUBCOMMAND;
804 case PARSE_OPT_UNKNOWN:
805 if (ctx->flags & PARSE_OPT_SUBCOMMAND_OPTIONAL)
806 /*
807 * arg is neither a short or long
808 * option nor a subcommand. Since
809 * this command has a default
810 * operation mode, we have to treat
811 * this arg and all remaining args
812 * as args meant to that default
813 * operation mode.
814 * So we are done parsing.
815 */
816 return PARSE_OPT_DONE;
817 error(_("unknown subcommand: `%s'"), arg);
818 usage_with_options(usagestr, options);
819 case PARSE_OPT_COMPLETE:
820 case PARSE_OPT_HELP:
821 case PARSE_OPT_ERROR:
822 case PARSE_OPT_DONE:
823 case PARSE_OPT_NON_OPTION:
824 /* Impossible. */
825 BUG("parse_subcommand() cannot return these");
826 }
4a59fd13
PH
827 }
828
5ad0d3d5
RS
829 /* lone -h asks for help */
830 if (internal_help && ctx->total == 1 && !strcmp(arg + 1, "h"))
831 goto show_usage;
832
a0abe5e3
RZ
833 /*
834 * lone --git-completion-helper and --git-completion-helper-all
835 * are asked by git-completion.bash
836 */
837 if (ctx->total == 1 && !strcmp(arg, "--git-completion-helper"))
838 return show_gitcomp(options, 0);
839 if (ctx->total == 1 && !strcmp(arg, "--git-completion-helper-all"))
840 return show_gitcomp(options, 1);
b9d7f4b4 841
4a59fd13 842 if (arg[1] != '-') {
ff43ec3e 843 ctx->opt = arg + 1;
07fe54db 844 switch (parse_short_opt(ctx, options)) {
f41179f1 845 case PARSE_OPT_ERROR:
3bb0923f 846 return PARSE_OPT_ERROR;
f41179f1 847 case PARSE_OPT_UNKNOWN:
38916c5b
RS
848 if (ctx->opt)
849 check_typos(arg + 1, options);
5ad0d3d5
RS
850 if (internal_help && *ctx->opt == 'h')
851 goto show_usage;
b5ce3a54 852 goto unknown;
f41179f1 853 case PARSE_OPT_NON_OPTION:
fa83cc83 854 case PARSE_OPT_SUBCOMMAND:
f41179f1
NTND
855 case PARSE_OPT_HELP:
856 case PARSE_OPT_COMPLETE:
857 BUG("parse_short_opt() cannot return these");
858 case PARSE_OPT_DONE:
859 break;
07fe54db 860 }
ff43ec3e 861 if (ctx->opt)
3a9f0f41 862 check_typos(arg + 1, options);
ff43ec3e 863 while (ctx->opt) {
07fe54db 864 switch (parse_short_opt(ctx, options)) {
f41179f1 865 case PARSE_OPT_ERROR:
3bb0923f 866 return PARSE_OPT_ERROR;
f41179f1 867 case PARSE_OPT_UNKNOWN:
5ad0d3d5
RS
868 if (internal_help && *ctx->opt == 'h')
869 goto show_usage;
870
26141b5b
PH
871 /* fake a short option thing to hide the fact that we may have
872 * started to parse aggregated stuff
873 *
874 * This is leaky, too bad.
875 */
876 ctx->argv[0] = xstrdup(ctx->opt - 1);
877 *(char *)ctx->argv[0] = '-';
b5ce3a54 878 goto unknown;
f41179f1 879 case PARSE_OPT_NON_OPTION:
fa83cc83 880 case PARSE_OPT_SUBCOMMAND:
f41179f1
NTND
881 case PARSE_OPT_COMPLETE:
882 case PARSE_OPT_HELP:
883 BUG("parse_short_opt() cannot return these");
884 case PARSE_OPT_DONE:
885 break;
07fe54db 886 }
3a9f0f41 887 }
4a59fd13
PH
888 continue;
889 }
890
51b4594b
JK
891 if (!arg[2] /* "--" */ ||
892 !strcmp(arg + 2, "end-of-options")) {
ff43ec3e
PH
893 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
894 ctx->argc--;
895 ctx->argv++;
4a59fd13
PH
896 }
897 break;
898 }
899
b92891f9 900 if (internal_help && !strcmp(arg + 2, "help-all"))
47e9cd28 901 return usage_with_options_internal(ctx, usagestr, options, 1, 0);
b92891f9 902 if (internal_help && !strcmp(arg + 2, "help"))
ac20ff6d 903 goto show_usage;
07fe54db 904 switch (parse_long_opt(ctx, arg + 2, options)) {
f41179f1 905 case PARSE_OPT_ERROR:
3bb0923f 906 return PARSE_OPT_ERROR;
f41179f1 907 case PARSE_OPT_UNKNOWN:
b5ce3a54 908 goto unknown;
f41179f1 909 case PARSE_OPT_HELP:
3bb0923f 910 goto show_usage;
f41179f1 911 case PARSE_OPT_NON_OPTION:
fa83cc83 912 case PARSE_OPT_SUBCOMMAND:
f41179f1
NTND
913 case PARSE_OPT_COMPLETE:
914 BUG("parse_long_opt() cannot return these");
915 case PARSE_OPT_DONE:
916 break;
07fe54db 917 }
b5ce3a54
RS
918 continue;
919unknown:
202fbb33
NTND
920 if (ctx->flags & PARSE_OPT_ONE_SHOT)
921 break;
fa83cc83
SG
922 if (ctx->has_subcommands &&
923 (ctx->flags & PARSE_OPT_SUBCOMMAND_OPTIONAL) &&
924 (ctx->flags & PARSE_OPT_KEEP_UNKNOWN_OPT)) {
925 /*
926 * Found an unknown option given to a command with
927 * subcommands that has a default operation mode:
928 * we treat this option and all remaining args as
929 * arguments meant to that default operation mode.
930 * So we are done parsing.
931 */
932 return PARSE_OPT_DONE;
933 }
99d86d60 934 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN_OPT))
b5ce3a54
RS
935 return PARSE_OPT_UNKNOWN;
936 ctx->out[ctx->cpidx++] = ctx->argv[0];
937 ctx->opt = NULL;
ff43ec3e
PH
938 }
939 return PARSE_OPT_DONE;
ac20ff6d 940
ac20ff6d 941 show_usage:
3bb0923f 942 return usage_with_options_internal(ctx, usagestr, options, 0, 0);
ff43ec3e
PH
943}
944
945int parse_options_end(struct parse_opt_ctx_t *ctx)
946{
202fbb33
NTND
947 if (ctx->flags & PARSE_OPT_ONE_SHOT)
948 return ctx->total - ctx->argc;
949
f919ffeb 950 MOVE_ARRAY(ctx->out + ctx->cpidx, ctx->argv, ctx->argc);
ff43ec3e
PH
951 ctx->out[ctx->cpidx + ctx->argc] = NULL;
952 return ctx->cpidx + ctx->argc;
953}
954
06a199f3
ÆAB
955int parse_options(int argc, const char **argv,
956 const char *prefix,
957 const struct option *options,
958 const char * const usagestr[],
959 enum parse_opt_flags flags)
ff43ec3e
PH
960{
961 struct parse_opt_ctx_t ctx;
5c387428 962 struct option *real_options;
ff43ec3e 963
b02e7d5d
JS
964 disallow_abbreviated_options =
965 git_env_bool("GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS", 0);
966
5c387428
NTND
967 memset(&ctx, 0, sizeof(ctx));
968 real_options = preprocess_options(&ctx, options);
969 if (real_options)
970 options = real_options;
971 parse_options_start_1(&ctx, argc, argv, prefix, options, flags);
ff43ec3e
PH
972 switch (parse_options_step(&ctx, options, usagestr)) {
973 case PARSE_OPT_HELP:
3bb0923f 974 case PARSE_OPT_ERROR:
ff43ec3e 975 exit(129);
a92ec7ef
NTND
976 case PARSE_OPT_COMPLETE:
977 exit(0);
979240fe 978 case PARSE_OPT_NON_OPTION:
fa83cc83
SG
979 case PARSE_OPT_SUBCOMMAND:
980 break;
ff43ec3e 981 case PARSE_OPT_DONE:
fa83cc83
SG
982 if (ctx.has_subcommands &&
983 !(flags & PARSE_OPT_SUBCOMMAND_OPTIONAL)) {
984 error(_("need a subcommand"));
985 usage_with_options(usagestr, options);
986 }
ff43ec3e 987 break;
1b887353 988 case PARSE_OPT_UNKNOWN:
07fe54db 989 if (ctx.argv[0][1] == '-') {
89003426 990 error(_("unknown option `%s'"), ctx.argv[0] + 2);
b141a478 991 } else if (isascii(*ctx.opt)) {
89003426 992 error(_("unknown switch `%c'"), *ctx.opt);
b141a478 993 } else {
89003426 994 error(_("unknown non-ascii option in string: `%s'"),
b141a478 995 ctx.argv[0]);
07fe54db
PH
996 }
997 usage_with_options(usagestr, options);
4a59fd13
PH
998 }
999
5c327502 1000 precompose_argv_prefix(argc, argv, NULL);
64cc539f 1001 free_preprocessed_options(real_options);
5c387428 1002 free(ctx.alias_groups);
7e7bbcb4 1003 return parse_options_end(&ctx);
4a59fd13 1004}
d7a38c54 1005
9c7304e3 1006static int usage_argh(const struct option *opts, FILE *outfile)
29f25d49
SB
1007{
1008 const char *s;
5f0df44c
RS
1009 int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
1010 !opts->argh || !!strpbrk(opts->argh, "()<>[]|");
29f25d49
SB
1011 if (opts->flags & PARSE_OPT_OPTARG)
1012 if (opts->long_name)
1013 s = literal ? "[=%s]" : "[=<%s>]";
1014 else
1015 s = literal ? "[%s]" : "[<%s>]";
1016 else
1017 s = literal ? " %s" : " <%s>";
c0821965 1018 return utf8_fprintf(outfile, s, opts->argh ? _(opts->argh) : _("..."));
29f25d49
SB
1019}
1020
d7a38c54
PH
1021#define USAGE_OPTS_WIDTH 24
1022#define USAGE_GAP 2
1023
352e7613
ÆAB
1024static enum parse_opt_result usage_with_options_internal(struct parse_opt_ctx_t *ctx,
1025 const char * const *usagestr,
1026 const struct option *opts,
1027 int full, int err)
d7a38c54 1028{
9c7304e3 1029 FILE *outfile = err ? stderr : stdout;
a6304fa4 1030 int need_newline;
9c7304e3 1031
4631cfc2
ÆAB
1032 const char *usage_prefix = _("usage: %s");
1033 /*
1034 * The translation could be anything, but we can count on
1035 * msgfmt(1)'s --check option to have asserted that "%s" is in
1036 * the translation. So compute the length of the "usage: "
1037 * part. We are assuming that the translator wasn't overly
1038 * clever and used e.g. "%1$s" instead of "%s", there's only
1039 * one "%s" in "usage_prefix" above, so there's no reason to
1040 * do so even with a RTL language.
1041 */
1042 size_t usage_len = strlen(usage_prefix) - strlen("%s");
1043 /*
1044 * TRANSLATORS: the colon here should align with the
1045 * one in "usage: %s" translation.
1046 */
1047 const char *or_prefix = _(" or: %s");
1048 /*
1049 * TRANSLATORS: You should only need to translate this format
1050 * string if your language is a RTL language (e.g. Arabic,
1051 * Hebrew etc.), not if it's a LTR language (e.g. German,
1052 * Russian, Chinese etc.).
1053 *
1054 * When a translated usage string has an embedded "\n" it's
1055 * because options have wrapped to the next line. The line
1056 * after the "\n" will then be padded to align with the
1057 * command name, such as N_("git cmd [opt]\n<8
1058 * spaces>[opt2]"), where the 8 spaces are the same length as
1059 * "git cmd ".
1060 *
1061 * This format string prints out that already-translated
1062 * line. The "%*s" is whitespace padding to account for the
1063 * padding at the start of the line that we add in this
1064 * function. The "%s" is a line in the (hopefully already
1065 * translated) N_() usage string, which contained embedded
1066 * newlines before we split it up.
1067 */
1068 const char *usage_continued = _("%*s%s");
1069 const char *prefix = usage_prefix;
1070 int saw_empty_line = 0;
1071
49b61802
RS
1072 if (!usagestr)
1073 return PARSE_OPT_HELP;
1074
47e9cd28
TR
1075 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
1076 fprintf(outfile, "cat <<\\EOF\n");
1077
44d86e91 1078 while (*usagestr) {
4631cfc2
ÆAB
1079 const char *str = _(*usagestr++);
1080 struct string_list list = STRING_LIST_INIT_DUP;
1081 unsigned int j;
1082
1083 if (!saw_empty_line && !*str)
1084 saw_empty_line = 1;
1085
1086 string_list_split(&list, str, '\n', -1);
1087 for (j = 0; j < list.nr; j++) {
1088 const char *line = list.items[j].string;
1089
1090 if (saw_empty_line && *line)
1091 fprintf_ln(outfile, _(" %s"), line);
1092 else if (saw_empty_line)
1093 fputc('\n', outfile);
1094 else if (!j)
1095 fprintf_ln(outfile, prefix, line);
1096 else
1097 fprintf_ln(outfile, usage_continued,
1098 (int)usage_len, "", line);
1099 }
1100 string_list_clear(&list, 0);
1101
1102 prefix = or_prefix;
44d86e91 1103 }
d7a38c54 1104
a6304fa4 1105 need_newline = 1;
d7a38c54
PH
1106
1107 for (; opts->type != OPTION_END; opts++) {
1108 size_t pos;
1109 int pad;
1110
fa83cc83
SG
1111 if (opts->type == OPTION_SUBCOMMAND)
1112 continue;
d7a38c54 1113 if (opts->type == OPTION_GROUP) {
9c7304e3 1114 fputc('\n', outfile);
a6304fa4 1115 need_newline = 0;
d7a38c54 1116 if (*opts->help)
54e6dc7d 1117 fprintf(outfile, "%s\n", _(opts->help));
d7a38c54
PH
1118 continue;
1119 }
dd3bf0f4
PH
1120 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
1121 continue;
d7a38c54 1122
a6304fa4
BC
1123 if (need_newline) {
1124 fputc('\n', outfile);
1125 need_newline = 0;
1126 }
1127
9c7304e3 1128 pos = fprintf(outfile, " ");
cbb08c2e 1129 if (opts->short_name) {
51a9949e 1130 if (opts->flags & PARSE_OPT_NODASH)
9c7304e3 1131 pos += fprintf(outfile, "%c", opts->short_name);
51a9949e 1132 else
9c7304e3 1133 pos += fprintf(outfile, "-%c", opts->short_name);
51a9949e 1134 }
d7a38c54 1135 if (opts->long_name && opts->short_name)
9c7304e3 1136 pos += fprintf(outfile, ", ");
d7a38c54 1137 if (opts->long_name)
cbb08c2e 1138 pos += fprintf(outfile, "--%s", opts->long_name);
e0319ff5 1139 if (opts->type == OPTION_NUMBER)
c0821965 1140 pos += utf8_fprintf(outfile, _("-NUM"));
d7a38c54 1141
b57c68a6
JN
1142 if ((opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
1143 !(opts->flags & PARSE_OPT_NOARG))
9c7304e3 1144 pos += usage_argh(opts, outfile);
d7a38c54 1145
f389c808
AR
1146 if (pos <= USAGE_OPTS_WIDTH)
1147 pad = USAGE_OPTS_WIDTH - pos;
d7a38c54 1148 else {
9c7304e3 1149 fputc('\n', outfile);
d7a38c54
PH
1150 pad = USAGE_OPTS_WIDTH;
1151 }
5c387428
NTND
1152 if (opts->type == OPTION_ALIAS) {
1153 fprintf(outfile, "%*s", pad + USAGE_GAP, "");
1154 fprintf_ln(outfile, _("alias of --%s"),
1155 (const char *)opts->value);
1156 continue;
1157 }
54e6dc7d 1158 fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", _(opts->help));
d7a38c54 1159 }
9c7304e3 1160 fputc('\n', outfile);
f389c808 1161
47e9cd28
TR
1162 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
1163 fputs("EOF\n", outfile);
1164
ee68b87a 1165 return PARSE_OPT_HELP;
d7a38c54 1166}
0ce865b1 1167
c2e86add 1168void NORETURN usage_with_options(const char * const *usagestr,
ff43ec3e 1169 const struct option *opts)
dd3bf0f4 1170{
47e9cd28 1171 usage_with_options_internal(NULL, usagestr, opts, 0, 1);
ff43ec3e 1172 exit(129);
dd3bf0f4
PH
1173}
1174
c2e86add 1175void NORETURN usage_msg_opt(const char *msg,
451bb210
CC
1176 const char * const *usagestr,
1177 const struct option *options)
1178{
e081a7c3 1179 die_message("%s\n", msg); /* The extra \n is intentional */
451bb210
CC
1180 usage_with_options(usagestr, options);
1181}
fa476be8
ÆAB
1182
1183void NORETURN usage_msg_optf(const char * const fmt,
1184 const char * const *usagestr,
1185 const struct option *options, ...)
1186{
1187 struct strbuf msg = STRBUF_INIT;
1188 va_list ap;
1189 va_start(ap, options);
1190 strbuf_vaddf(&msg, fmt, ap);
1191 va_end(ap);
1192
1193 usage_msg_opt(msg.buf, usagestr, options);
1194}
d21d5ddf 1195
a699367b
JNA
1196void die_for_incompatible_opt4(int opt1, const char *opt1_name,
1197 int opt2, const char *opt2_name,
1198 int opt3, const char *opt3_name,
1199 int opt4, const char *opt4_name)
1200{
1201 int count = 0;
1202 const char *options[4];
1203
1204 if (opt1)
1205 options[count++] = opt1_name;
1206 if (opt2)
1207 options[count++] = opt2_name;
1208 if (opt3)
1209 options[count++] = opt3_name;
1210 if (opt4)
1211 options[count++] = opt4_name;
1212 switch (count) {
1213 case 4:
1214 die(_("options '%s', '%s', '%s', and '%s' cannot be used together"),
1215 opt1_name, opt2_name, opt3_name, opt4_name);
1216 break;
1217 case 3:
1218 die(_("options '%s', '%s', and '%s' cannot be used together"),
1219 options[0], options[1], options[2]);
1220 break;
1221 case 2:
1222 die(_("options '%s' and '%s' cannot be used together"),
1223 options[0], options[1]);
1224 break;
1225 default:
1226 break;
1227 }
1228}