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