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