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