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