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