blob: 093eaf2db80f82d86260d929bb514f3d84c39efa [file] [log] [blame]
Pierre Habouzit4a59fd12007-10-15 01:35:37 +02001#include "git-compat-util.h"
2#include "parse-options.h"
Elijah Newren0b027f62023-03-21 06:25:58 +00003#include "abspath.h"
Calvin Wanb1bda752023-09-29 14:20:51 -07004#include "parse.h"
Jake Goulding269defd2009-01-26 09:13:23 -05005#include "commit.h"
Mark Lodato73e9da02010-02-16 23:55:58 -05006#include "color.h"
Elijah Newrenf394e092023-03-21 06:25:54 +00007#include "gettext.h"
Elijah Newrend4a4f922023-04-22 20:17:26 +00008#include "strbuf.h"
Calvin Wanb1bda752023-09-29 14:20:51 -07009#include "string-list.h"
Jiang Xinc0821962013-02-09 14:31:09 +080010#include "utf8.h"
Pierre Habouzit4a59fd12007-10-15 01:35:37 +020011
Johannes Schindelinb02e7d52019-04-12 02:37:24 -070012static int disallow_abbreviated_options;
13
Ævar Arnfjörð Bjarmasond3428342021-10-08 21:07:46 +020014enum opt_parsed {
15 OPT_LONG = 0,
16 OPT_SHORT = 1<<0,
17 OPT_UNSET = 1<<1,
18};
Pierre Habouzit4a59fd12007-10-15 01:35:37 +020019
Ævar Arnfjörð Bjarmason53ca5692022-06-02 14:25:34 +020020static void optbug(const struct option *opt, const char *reason)
Jonathan Nieder1e5ce572010-12-02 00:01:18 -060021{
Ævar Arnfjörð Bjarmason53ca5692022-06-02 14:25:34 +020022 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);
Jonathan Nieder1e5ce572010-12-02 00:01:18 -060029}
30
Ævar Arnfjörð Bjarmasond3428342021-10-08 21:07:46 +020031static const char *optname(const struct option *opt, enum opt_parsed flags)
Ævar Arnfjörð Bjarmason3c2047a2021-10-08 21:07:42 +020032{
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);
Ævar Arnfjörð Bjarmasond3428342021-10-08 21:07:46 +020040 else if (flags == OPT_LONG)
Ævar Arnfjörð Bjarmason3c2047a2021-10-08 21:07:42 +020041 strbuf_addf(&sb, "option `%s'", opt->long_name);
Ævar Arnfjörð Bjarmasond3428342021-10-08 21:07:46 +020042 else
43 BUG("optname() got unknown flags %d", flags);
Ævar Arnfjörð Bjarmason3c2047a2021-10-08 21:07:42 +020044
45 return sb.buf;
46}
47
Nguyễn Thái Ngọc Duyf41179f2019-01-27 07:35:27 +070048static enum parse_opt_result get_arg(struct parse_opt_ctx_t *p,
49 const struct option *opt,
Ævar Arnfjörð Bjarmasond3428342021-10-08 21:07:46 +020050 enum opt_parsed flags, const char **arg)
Pierre Habouzit1cc69852008-07-08 12:34:08 +020051{
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;
Olivier Marind5d745f2008-07-21 20:30:36 +020057 } else if (p->argc > 1) {
Pierre Habouzit1cc69852008-07-08 12:34:08 +020058 p->argc--;
59 *arg = *++p->argv;
60 } else
Nguyễn Thái Ngọc Duy9440b832018-11-10 06:16:11 +010061 return error(_("%s requires a value"), optname(opt, flags));
Pierre Habouzit1cc69852008-07-08 12:34:08 +020062 return 0;
63}
64
Jeff King7ce40882023-03-04 05:31:22 -050065static void fix_filename(const char *prefix, char **file)
Stephen Boyddf217ed2009-05-23 11:53:13 -070066{
Jeff King7ce40882023-03-04 05:31:22 -050067 if (!file || !*file)
68 ; /* leave as NULL */
Jeff King7ce40882023-03-04 05:31:22 -050069 else
Jeff King0bbe1032023-03-04 05:31:47 -050070 *file = prefix_filename_except_for_dash(prefix, *file);
Stephen Boyddf217ed2009-05-23 11:53:13 -070071}
72
Nguyễn Thái Ngọc Duyf41179f2019-01-27 07:35:27 +070073static enum parse_opt_result opt_command_mode_error(
74 const struct option *opt,
75 const struct option *all_opts,
Ævar Arnfjörð Bjarmasond3428342021-10-08 21:07:46 +020076 enum opt_parsed flags)
Junio C Hamano11588262013-07-30 12:06:01 -070077{
78 const struct option *that;
Junio C Hamano11588262013-07-30 12:06:01 -070079 struct strbuf that_name = STRBUF_INIT;
80
81 /*
82 * Find the other option that was used to set the variable
83 * already, and report that this is not compatible with it.
84 */
85 for (that = all_opts; that->type != OPTION_END; that++) {
86 if (that == opt ||
Paolo Bonzinibc8620b2020-02-20 15:15:16 +010087 !(that->flags & PARSE_OPT_CMDMODE) ||
Junio C Hamano11588262013-07-30 12:06:01 -070088 that->value != opt->value ||
89 that->defval != *(int *)opt->value)
90 continue;
91
92 if (that->long_name)
93 strbuf_addf(&that_name, "--%s", that->long_name);
94 else
95 strbuf_addf(&that_name, "-%c", that->short_name);
Nguyễn Thái Ngọc Duy9440b832018-11-10 06:16:11 +010096 error(_("%s is incompatible with %s"),
97 optname(opt, flags), that_name.buf);
Junio C Hamano11588262013-07-30 12:06:01 -070098 strbuf_release(&that_name);
Nguyễn Thái Ngọc Duyf41179f2019-01-27 07:35:27 +070099 return PARSE_OPT_ERROR;
Junio C Hamano11588262013-07-30 12:06:01 -0700100 }
Nguyễn Thái Ngọc Duy9440b832018-11-10 06:16:11 +0100101 return error(_("%s : incompatible with something else"),
102 optname(opt, flags));
Junio C Hamano11588262013-07-30 12:06:01 -0700103}
104
Nguyễn Thái Ngọc Duyf41179f2019-01-27 07:35:27 +0700105static enum parse_opt_result get_value(struct parse_opt_ctx_t *p,
106 const struct option *opt,
107 const struct option *all_opts,
Ævar Arnfjörð Bjarmasond3428342021-10-08 21:07:46 +0200108 enum opt_parsed flags)
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200109{
Pierre Habouzitffe659f2007-10-15 01:45:45 +0200110 const char *s, *arg;
Pierre Habouzitdb7244b2007-11-07 11:20:27 +0100111 const int unset = flags & OPT_UNSET;
Stephen Boyddf217ed2009-05-23 11:53:13 -0700112 int err;
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200113
Pierre Habouzitdb7244b2007-11-07 11:20:27 +0100114 if (unset && p->opt)
Nguyễn Thái Ngọc Duy9440b832018-11-10 06:16:11 +0100115 return error(_("%s takes no value"), optname(opt, flags));
Pierre Habouzitdb7244b2007-11-07 11:20:27 +0100116 if (unset && (opt->flags & PARSE_OPT_NONEG))
Nguyễn Thái Ngọc Duy9440b832018-11-10 06:16:11 +0100117 return error(_("%s isn't available"), optname(opt, flags));
Stephen Boydc1f4ec92010-12-01 17:30:40 -0600118 if (!(flags & OPT_SHORT) && p->opt && (opt->flags & PARSE_OPT_NOARG))
Nguyễn Thái Ngọc Duy9440b832018-11-10 06:16:11 +0100119 return error(_("%s takes no value"), optname(opt, flags));
Pierre Habouzitdb7244b2007-11-07 11:20:27 +0100120
Paolo Bonzinibc8620b2020-02-20 15:15:16 +0100121 /*
122 * Giving the same mode option twice, although unnecessary,
123 * is not a grave error, so let it pass.
124 */
125 if ((opt->flags & PARSE_OPT_CMDMODE) &&
126 *(int *)opt->value && *(int *)opt->value != opt->defval)
127 return opt_command_mode_error(opt, all_opts, flags);
128
Pierre Habouzitdb7244b2007-11-07 11:20:27 +0100129 switch (opt->type) {
Jonathan Niederb0b3a8b2010-12-01 17:32:16 -0600130 case OPTION_LOWLEVEL_CALLBACK:
Nguyễn Thái Ngọc Duy3ebbe282019-01-27 07:35:28 +0700131 return opt->ll_callback(p, opt, NULL, unset);
Jonathan Niederb0b3a8b2010-12-01 17:32:16 -0600132
Pierre Habouzitdb7244b2007-11-07 11:20:27 +0100133 case OPTION_BIT:
134 if (unset)
135 *(int *)opt->value &= ~opt->defval;
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200136 else
Pierre Habouzitdb7244b2007-11-07 11:20:27 +0100137 *(int *)opt->value |= opt->defval;
138 return 0;
139
René Scharfe2f4b97f2009-05-07 21:44:17 +0200140 case OPTION_NEGBIT:
141 if (unset)
142 *(int *)opt->value |= opt->defval;
143 else
144 *(int *)opt->value &= ~opt->defval;
145 return 0;
146
Nguyễn Thái Ngọc Duyf62470c2019-01-27 07:35:25 +0700147 case OPTION_BITOP:
148 if (unset)
149 BUG("BITOP can't have unset form");
150 *(int *)opt->value &= ~opt->extra;
151 *(int *)opt->value |= opt->defval;
152 return 0;
153
Junio C Hamanob04ba2b2011-09-27 16:56:49 -0700154 case OPTION_COUNTUP:
Pranit Bauvae0070e82016-05-05 15:20:00 +0530155 if (*(int *)opt->value < 0)
156 *(int *)opt->value = 0;
Pierre Habouzitdb7244b2007-11-07 11:20:27 +0100157 *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
158 return 0;
159
160 case OPTION_SET_INT:
161 *(int *)opt->value = unset ? 0 : opt->defval;
162 return 0;
163
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200164 case OPTION_STRING:
Pierre Habouzit1cc69852008-07-08 12:34:08 +0200165 if (unset)
Pierre Habouzitdb7244b2007-11-07 11:20:27 +0100166 *(const char **)opt->value = NULL;
Pierre Habouzit1cc69852008-07-08 12:34:08 +0200167 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
Pierre Habouzitffe659f2007-10-15 01:45:45 +0200168 *(const char **)opt->value = (const char *)opt->defval;
Pierre Habouzit1cc69852008-07-08 12:34:08 +0200169 else
170 return get_arg(p, opt, flags, (const char **)opt->value);
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200171 return 0;
172
Stephen Boyddf217ed2009-05-23 11:53:13 -0700173 case OPTION_FILENAME:
174 err = 0;
175 if (unset)
176 *(const char **)opt->value = NULL;
177 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
178 *(const char **)opt->value = (const char *)opt->defval;
179 else
180 err = get_arg(p, opt, flags, (const char **)opt->value);
181
182 if (!err)
Jeff King7ce40882023-03-04 05:31:22 -0500183 fix_filename(p->prefix, (char **)opt->value);
Stephen Boyddf217ed2009-05-23 11:53:13 -0700184 return err;
185
Pierre Habouzitffe659f2007-10-15 01:45:45 +0200186 case OPTION_CALLBACK:
Nguyễn Thái Ngọc Duy3ebbe282019-01-27 07:35:28 +0700187 {
188 const char *p_arg = NULL;
189 int p_unset;
Pierre Habouzitffe659f2007-10-15 01:45:45 +0200190
Nguyễn Thái Ngọc Duy3ebbe282019-01-27 07:35:28 +0700191 if (unset)
192 p_unset = 1;
193 else if (opt->flags & PARSE_OPT_NOARG)
194 p_unset = 0;
195 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
196 p_unset = 0;
197 else if (get_arg(p, opt, flags, &arg))
198 return -1;
199 else {
200 p_unset = 0;
201 p_arg = arg;
202 }
203 if (opt->callback)
204 return (*opt->callback)(opt, p_arg, p_unset) ? (-1) : 0;
205 else
206 return (*opt->ll_callback)(p, opt, p_arg, p_unset);
207 }
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200208 case OPTION_INTEGER:
Pierre Habouzitdb7244b2007-11-07 11:20:27 +0100209 if (unset) {
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200210 *(int *)opt->value = 0;
211 return 0;
212 }
Pierre Habouzitc43a2482007-12-21 11:41:41 +0100213 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
Pierre Habouzitffe659f2007-10-15 01:45:45 +0200214 *(int *)opt->value = opt->defval;
215 return 0;
216 }
Pierre Habouzit1cc69852008-07-08 12:34:08 +0200217 if (get_arg(p, opt, flags, &arg))
218 return -1;
Nguyễn Thái Ngọc Duyf7e68a02019-05-29 16:11:16 +0700219 if (!*arg)
220 return error(_("%s expects a numerical value"),
221 optname(opt, flags));
Pierre Habouzit1cc69852008-07-08 12:34:08 +0200222 *(int *)opt->value = strtol(arg, (char **)&s, 10);
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200223 if (*s)
Nguyễn Thái Ngọc Duy9440b832018-11-10 06:16:11 +0100224 return error(_("%s expects a numerical value"),
225 optname(opt, flags));
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200226 return 0;
227
Charles Bailey2a514ed2015-06-21 19:25:44 +0100228 case OPTION_MAGNITUDE:
229 if (unset) {
230 *(unsigned long *)opt->value = 0;
231 return 0;
232 }
233 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
234 *(unsigned long *)opt->value = opt->defval;
235 return 0;
236 }
237 if (get_arg(p, opt, flags, &arg))
238 return -1;
239 if (!git_parse_ulong(arg, opt->value))
Nguyễn Thái Ngọc Duy9440b832018-11-10 06:16:11 +0100240 return error(_("%s expects a non-negative integer value"
241 " with an optional k/m/g suffix"),
242 optname(opt, flags));
Charles Bailey2a514ed2015-06-21 19:25:44 +0100243 return 0;
244
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200245 default:
Nguyễn Thái Ngọc Duy48a54992018-11-10 06:16:12 +0100246 BUG("opt->type %d should not happen", opt->type);
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200247 }
248}
249
Nguyễn Thái Ngọc Duyf41179f2019-01-27 07:35:27 +0700250static enum parse_opt_result parse_short_opt(struct parse_opt_ctx_t *p,
251 const struct option *options)
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200252{
Junio C Hamano11588262013-07-30 12:06:01 -0700253 const struct option *all_opts = options;
René Scharfee0319ff2009-05-07 21:45:08 +0200254 const struct option *numopt = NULL;
255
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200256 for (; options->type != OPTION_END; options++) {
257 if (options->short_name == *p->opt) {
258 p->opt = p->opt[1] ? p->opt + 1 : NULL;
Junio C Hamano11588262013-07-30 12:06:01 -0700259 return get_value(p, options, all_opts, OPT_SHORT);
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200260 }
René Scharfee0319ff2009-05-07 21:45:08 +0200261
262 /*
263 * Handle the numerical option later, explicit one-digit
264 * options take precedence over it.
265 */
266 if (options->type == OPTION_NUMBER)
267 numopt = options;
268 }
269 if (numopt && isdigit(*p->opt)) {
270 size_t len = 1;
271 char *arg;
272 int rc;
273
274 while (isdigit(p->opt[len]))
275 len++;
276 arg = xmemdupz(p->opt, len);
277 p->opt = p->opt[len] ? p->opt + len : NULL;
Nguyễn Thái Ngọc Duy3ebbe282019-01-27 07:35:28 +0700278 if (numopt->callback)
279 rc = (*numopt->callback)(numopt, arg, 0) ? (-1) : 0;
280 else
281 rc = (*numopt->ll_callback)(p, numopt, arg, 0);
René Scharfee0319ff2009-05-07 21:45:08 +0200282 free(arg);
283 return rc;
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200284 }
Nguyễn Thái Ngọc Duyf41179f2019-01-27 07:35:27 +0700285 return PARSE_OPT_UNKNOWN;
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200286}
287
Nguyễn Thái Ngọc Duy5c387422019-04-29 17:05:25 +0700288static int has_string(const char *it, const char **array)
289{
290 while (*array)
291 if (!strcmp(it, *(array++)))
292 return 1;
293 return 0;
294}
295
296static int is_alias(struct parse_opt_ctx_t *ctx,
297 const struct option *one_opt,
298 const struct option *another_opt)
299{
300 const char **group;
301
302 if (!ctx->alias_groups)
303 return 0;
304
305 if (!one_opt->long_name || !another_opt->long_name)
306 return 0;
307
308 for (group = ctx->alias_groups; *group; group += 3) {
309 /* it and other are from the same family? */
310 if (has_string(one_opt->long_name, group) &&
311 has_string(another_opt->long_name, group))
312 return 1;
313 }
314 return 0;
315}
316
Nguyễn Thái Ngọc Duyf41179f2019-01-27 07:35:27 +0700317static enum parse_opt_result parse_long_opt(
318 struct parse_opt_ctx_t *p, const char *arg,
319 const struct option *options)
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200320{
Junio C Hamano11588262013-07-30 12:06:01 -0700321 const struct option *all_opts = options;
Rohit Mani2c5495f2014-03-07 22:48:31 -0800322 const char *arg_end = strchrnul(arg, '=');
Johannes Schindelin243e0612007-11-05 13:15:21 +0000323 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
Ævar Arnfjörð Bjarmasond3428342021-10-08 21:07:46 +0200324 enum opt_parsed abbrev_flags = OPT_LONG, ambiguous_flags = OPT_LONG;
Johannes Schindelin7f275b92007-10-14 17:54:06 +0100325
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200326 for (; options->type != OPTION_END; options++) {
René Scharfe0f1930c52012-02-25 20:14:54 +0100327 const char *rest, *long_name = options->long_name;
Ævar Arnfjörð Bjarmasond3428342021-10-08 21:07:46 +0200328 enum opt_parsed flags = OPT_LONG, opt_flags = OPT_LONG;
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200329
SZEDER Gáborfa83cc82022-08-19 18:04:00 +0200330 if (options->type == OPTION_SUBCOMMAND)
331 continue;
René Scharfe0f1930c52012-02-25 20:14:54 +0100332 if (!long_name)
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200333 continue;
334
René Scharfe0f1930c52012-02-25 20:14:54 +0100335again:
Jeff Kingcf4fff52014-06-18 15:44:19 -0400336 if (!skip_prefix(arg, long_name, &rest))
337 rest = NULL;
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200338 if (!rest) {
Johannes Schindelin7f275b92007-10-14 17:54:06 +0100339 /* abbreviated? */
SZEDER Gábor99d86d62022-08-19 18:03:57 +0200340 if (!(p->flags & PARSE_OPT_KEEP_UNKNOWN_OPT) &&
Nguyễn Thái Ngọc Duybaa4adc2019-01-27 07:35:24 +0700341 !strncmp(long_name, arg, arg_end - arg)) {
Johannes Schindelin7f275b92007-10-14 17:54:06 +0100342is_abbreviated:
Nguyễn Thái Ngọc Duy5c387422019-04-29 17:05:25 +0700343 if (abbrev_option &&
344 !is_alias(p, abbrev_option, options)) {
Johannes Schindelin243e0612007-11-05 13:15:21 +0000345 /*
346 * If this is abbreviated, it is
347 * ambiguous. So when there is no
348 * exact match later, we need to
349 * error out.
350 */
351 ambiguous_option = abbrev_option;
352 ambiguous_flags = abbrev_flags;
353 }
Johannes Schindelin7f275b92007-10-14 17:54:06 +0100354 if (!(flags & OPT_UNSET) && *arg_end)
355 p->opt = arg_end + 1;
356 abbrev_option = options;
René Scharfe0f1930c52012-02-25 20:14:54 +0100357 abbrev_flags = flags ^ opt_flags;
Johannes Schindelin7f275b92007-10-14 17:54:06 +0100358 continue;
359 }
Andreas Schwab6bbfd1f2009-09-25 20:44:44 +0200360 /* negation allowed? */
361 if (options->flags & PARSE_OPT_NONEG)
362 continue;
Johannes Schindelin7f275b92007-10-14 17:54:06 +0100363 /* negated and abbreviated very much? */
Christian Couder59556542013-11-30 21:55:40 +0100364 if (starts_with("no-", arg)) {
Johannes Schindelin7f275b92007-10-14 17:54:06 +0100365 flags |= OPT_UNSET;
366 goto is_abbreviated;
367 }
368 /* negated? */
Christian Couder59556542013-11-30 21:55:40 +0100369 if (!starts_with(arg, "no-")) {
Junio C Hamano145136a2020-01-30 11:35:46 -0800370 if (skip_prefix(long_name, "no-", &long_name)) {
René Scharfe0f1930c52012-02-25 20:14:54 +0100371 opt_flags |= OPT_UNSET;
372 goto again;
373 }
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200374 continue;
René Scharfe0f1930c52012-02-25 20:14:54 +0100375 }
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200376 flags |= OPT_UNSET;
Jeff Kingcf4fff52014-06-18 15:44:19 -0400377 if (!skip_prefix(arg + 3, long_name, &rest)) {
378 /* abbreviated and negated? */
379 if (starts_with(long_name, arg + 3))
380 goto is_abbreviated;
381 else
382 continue;
383 }
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200384 }
385 if (*rest) {
386 if (*rest != '=')
387 continue;
388 p->opt = rest + 1;
389 }
Junio C Hamano11588262013-07-30 12:06:01 -0700390 return get_value(p, options, all_opts, flags ^ opt_flags);
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200391 }
Johannes Schindelin243e0612007-11-05 13:15:21 +0000392
Johannes Schindelinb02e7d52019-04-12 02:37:24 -0700393 if (disallow_abbreviated_options && (ambiguous_option || abbrev_option))
394 die("disallowed abbreviated or ambiguous option '%.*s'",
395 (int)(arg_end - arg), arg);
396
Paul-Sebastian Ungureanu3bb09232018-03-22 20:43:51 +0200397 if (ambiguous_option) {
Nguyễn Thái Ngọc Duy89003422018-11-10 06:16:13 +0100398 error(_("ambiguous option: %s "
399 "(could be --%s%s or --%s%s)"),
Johannes Schindelin243e0612007-11-05 13:15:21 +0000400 arg,
401 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
402 ambiguous_option->long_name,
403 (abbrev_flags & OPT_UNSET) ? "no-" : "",
404 abbrev_option->long_name);
Nguyễn Thái Ngọc Duyf41179f2019-01-27 07:35:27 +0700405 return PARSE_OPT_HELP;
Paul-Sebastian Ungureanu3bb09232018-03-22 20:43:51 +0200406 }
Johannes Schindelin7f275b92007-10-14 17:54:06 +0100407 if (abbrev_option)
Junio C Hamano11588262013-07-30 12:06:01 -0700408 return get_value(p, abbrev_option, all_opts, abbrev_flags);
Nguyễn Thái Ngọc Duyf41179f2019-01-27 07:35:27 +0700409 return PARSE_OPT_UNKNOWN;
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200410}
411
Ævar Arnfjörð Bjarmason68611f52021-11-10 02:27:04 +0100412static enum parse_opt_result parse_nodash_opt(struct parse_opt_ctx_t *p,
413 const char *arg,
414 const struct option *options)
René Scharfe51a99492009-05-07 21:45:42 +0200415{
Junio C Hamano11588262013-07-30 12:06:01 -0700416 const struct option *all_opts = options;
417
René Scharfe51a99492009-05-07 21:45:42 +0200418 for (; options->type != OPTION_END; options++) {
419 if (!(options->flags & PARSE_OPT_NODASH))
420 continue;
René Scharfe51a99492009-05-07 21:45:42 +0200421 if (options->short_name == arg[0] && arg[1] == '\0')
Junio C Hamano11588262013-07-30 12:06:01 -0700422 return get_value(p, options, all_opts, OPT_SHORT);
René Scharfe51a99492009-05-07 21:45:42 +0200423 }
Ævar Arnfjörð Bjarmason68611f52021-11-10 02:27:04 +0100424 return PARSE_OPT_ERROR;
René Scharfe51a99492009-05-07 21:45:42 +0200425}
426
SZEDER Gáborfa83cc82022-08-19 18:04:00 +0200427static enum parse_opt_result parse_subcommand(const char *arg,
428 const struct option *options)
429{
430 for (; options->type != OPTION_END; options++)
431 if (options->type == OPTION_SUBCOMMAND &&
432 !strcmp(options->long_name, arg)) {
433 *(parse_opt_subcommand_fn **)options->value = options->subcommand_fn;
434 return PARSE_OPT_SUBCOMMAND;
435 }
436
437 return PARSE_OPT_UNKNOWN;
438}
439
Nanako Shiraishi1121a872008-07-16 19:42:18 +0900440static void check_typos(const char *arg, const struct option *options)
Pierre Habouzit3a9f0f42008-01-26 12:26:57 +0100441{
442 if (strlen(arg) < 3)
443 return;
444
Christian Couder59556542013-11-30 21:55:40 +0100445 if (starts_with(arg, "no-")) {
Jacques Bodin-Hullin395518c2020-02-05 13:07:23 +0000446 error(_("did you mean `--%s` (with two dashes)?"), arg);
Pierre Habouzit3a9f0f42008-01-26 12:26:57 +0100447 exit(129);
448 }
449
450 for (; options->type != OPTION_END; options++) {
451 if (!options->long_name)
452 continue;
Christian Couder59556542013-11-30 21:55:40 +0100453 if (starts_with(options->long_name, arg)) {
Jacques Bodin-Hullin395518c2020-02-05 13:07:23 +0000454 error(_("did you mean `--%s` (with two dashes)?"), arg);
Pierre Habouzit3a9f0f42008-01-26 12:26:57 +0100455 exit(129);
456 }
457 }
458}
459
Pierre Habouzitcb9d3982009-06-09 10:23:44 +0200460static void parse_options_check(const struct option *opts)
461{
Junio C Hamanoaf465af2014-09-03 12:42:37 -0700462 char short_opts[128];
SZEDER Gáborfa83cc82022-08-19 18:04:00 +0200463 void *subcommand_value = NULL;
Pierre Habouzitcb9d3982009-06-09 10:23:44 +0200464
Junio C Hamanoaf465af2014-09-03 12:42:37 -0700465 memset(short_opts, '\0', sizeof(short_opts));
Pierre Habouzitcb9d3982009-06-09 10:23:44 +0200466 for (; opts->type != OPTION_END; opts++) {
467 if ((opts->flags & PARSE_OPT_LASTARG_DEFAULT) &&
Jonathan Nieder1e5ce572010-12-02 00:01:18 -0600468 (opts->flags & PARSE_OPT_OPTARG))
Ævar Arnfjörð Bjarmason53ca5692022-06-02 14:25:34 +0200469 optbug(opts, "uses incompatible flags "
470 "LASTARG_DEFAULT and OPTARG");
Junio C Hamanoaf465af2014-09-03 12:42:37 -0700471 if (opts->short_name) {
472 if (0x7F <= opts->short_name)
Ævar Arnfjörð Bjarmason53ca5692022-06-02 14:25:34 +0200473 optbug(opts, "invalid short name");
Junio C Hamanoaf465af2014-09-03 12:42:37 -0700474 else if (short_opts[opts->short_name]++)
Ævar Arnfjörð Bjarmason53ca5692022-06-02 14:25:34 +0200475 optbug(opts, "short name already used");
Junio C Hamanoaf465af2014-09-03 12:42:37 -0700476 }
Jonathan Niedera02dd4f2010-12-02 00:05:05 -0600477 if (opts->flags & PARSE_OPT_NODASH &&
478 ((opts->flags & PARSE_OPT_OPTARG) ||
479 !(opts->flags & PARSE_OPT_NOARG) ||
480 !(opts->flags & PARSE_OPT_NONEG) ||
481 opts->long_name))
Ævar Arnfjörð Bjarmason53ca5692022-06-02 14:25:34 +0200482 optbug(opts, "uses feature "
483 "not supported for dashless options");
René Scharfe3284b9382023-08-08 22:05:57 +0200484 if (opts->type == OPTION_SET_INT && !opts->defval &&
485 opts->long_name && !(opts->flags & PARSE_OPT_NONEG))
486 optbug(opts, "OPTION_SET_INT 0 should not be negatable");
Jonathan Nieder5c400ed2010-12-02 00:08:57 -0600487 switch (opts->type) {
Junio C Hamanob04ba2b2011-09-27 16:56:49 -0700488 case OPTION_COUNTUP:
Jonathan Nieder5c400ed2010-12-02 00:08:57 -0600489 case OPTION_BIT:
490 case OPTION_NEGBIT:
491 case OPTION_SET_INT:
Jonathan Nieder5c400ed2010-12-02 00:08:57 -0600492 case OPTION_NUMBER:
493 if ((opts->flags & PARSE_OPT_OPTARG) ||
494 !(opts->flags & PARSE_OPT_NOARG))
Ævar Arnfjörð Bjarmason53ca5692022-06-02 14:25:34 +0200495 optbug(opts, "should not accept an argument");
Nguyễn Thái Ngọc Duybf3ff332019-01-27 07:35:26 +0700496 break;
497 case OPTION_CALLBACK:
Nguyễn Thái Ngọc Duy3ebbe282019-01-27 07:35:28 +0700498 if (!opts->callback && !opts->ll_callback)
Ævar Arnfjörð Bjarmason5b2f5d92022-06-02 14:25:35 +0200499 optbug(opts, "OPTION_CALLBACK needs one callback");
500 else if (opts->callback && opts->ll_callback)
501 optbug(opts, "OPTION_CALLBACK can't have two callbacks");
Nguyễn Thái Ngọc Duybf3ff332019-01-27 07:35:26 +0700502 break;
503 case OPTION_LOWLEVEL_CALLBACK:
504 if (!opts->ll_callback)
Ævar Arnfjörð Bjarmason5b2f5d92022-06-02 14:25:35 +0200505 optbug(opts, "OPTION_LOWLEVEL_CALLBACK needs a callback");
Nguyễn Thái Ngọc Duybf3ff332019-01-27 07:35:26 +0700506 if (opts->callback)
Ævar Arnfjörð Bjarmason5b2f5d92022-06-02 14:25:35 +0200507 optbug(opts, "OPTION_LOWLEVEL_CALLBACK needs no high level callback");
Nguyễn Thái Ngọc Duybf3ff332019-01-27 07:35:26 +0700508 break;
Nguyễn Thái Ngọc Duy5c387422019-04-29 17:05:25 +0700509 case OPTION_ALIAS:
Ævar Arnfjörð Bjarmason5b2f5d92022-06-02 14:25:35 +0200510 optbug(opts, "OPT_ALIAS() should not remain at this point. "
511 "Are you using parse_options_step() directly?\n"
512 "That case is not supported yet.");
513 break;
SZEDER Gáborfa83cc82022-08-19 18:04:00 +0200514 case OPTION_SUBCOMMAND:
515 if (!opts->value || !opts->subcommand_fn)
516 optbug(opts, "OPTION_SUBCOMMAND needs a value and a subcommand function");
517 if (!subcommand_value)
518 subcommand_value = opts->value;
519 else if (subcommand_value != opts->value)
520 optbug(opts, "all OPTION_SUBCOMMANDs need the same value");
521 break;
Jonathan Nieder5c400ed2010-12-02 00:08:57 -0600522 default:
523 ; /* ok. (usually accepts an argument) */
524 }
Junio C Hamanob6c2a0d2014-03-23 16:04:36 -0700525 if (opts->argh &&
526 strcspn(opts->argh, " _") != strlen(opts->argh))
Ævar Arnfjörð Bjarmason53ca5692022-06-02 14:25:34 +0200527 optbug(opts, "multi-word argh should use dash to separate words");
Pierre Habouzitcb9d3982009-06-09 10:23:44 +0200528 }
Ævar Arnfjörð Bjarmason53ca5692022-06-02 14:25:34 +0200529 BUG_if_bug("invalid 'struct option'");
Pierre Habouzitcb9d3982009-06-09 10:23:44 +0200530}
531
SZEDER Gáborfa83cc82022-08-19 18:04:00 +0200532static int has_subcommands(const struct option *options)
533{
534 for (; options->type != OPTION_END; options++)
535 if (options->type == OPTION_SUBCOMMAND)
536 return 1;
537 return 0;
538}
539
Nguyễn Thái Ngọc Duy5c387422019-04-29 17:05:25 +0700540static void parse_options_start_1(struct parse_opt_ctx_t *ctx,
541 int argc, const char **argv, const char *prefix,
Ævar Arnfjörð Bjarmason3f9ab7c2021-10-08 21:07:38 +0200542 const struct option *options,
543 enum parse_opt_flags flags)
Pierre Habouzit7e7bbcb2008-06-23 21:59:37 +0200544{
Nguyễn Thái Ngọc Duy202fbb32019-01-27 07:35:23 +0700545 ctx->argc = argc;
546 ctx->argv = argv;
547 if (!(flags & PARSE_OPT_ONE_SHOT)) {
548 ctx->argc--;
549 ctx->argv++;
550 }
551 ctx->total = ctx->argc;
552 ctx->out = argv;
Stephen Boyd37782922009-05-23 11:53:12 -0700553 ctx->prefix = prefix;
Pierre Habouzita32a4ea2008-06-24 00:31:31 +0200554 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
Pierre Habouzit7e7bbcb2008-06-23 21:59:37 +0200555 ctx->flags = flags;
SZEDER Gáborfa83cc82022-08-19 18:04:00 +0200556 ctx->has_subcommands = has_subcommands(options);
557 if (!ctx->has_subcommands && (flags & PARSE_OPT_SUBCOMMAND_OPTIONAL))
558 BUG("Using PARSE_OPT_SUBCOMMAND_OPTIONAL without subcommands");
559 if (ctx->has_subcommands) {
560 if (flags & PARSE_OPT_STOP_AT_NON_OPTION)
561 BUG("subcommands are incompatible with PARSE_OPT_STOP_AT_NON_OPTION");
562 if (!(flags & PARSE_OPT_SUBCOMMAND_OPTIONAL)) {
563 if (flags & PARSE_OPT_KEEP_UNKNOWN_OPT)
564 BUG("subcommands are incompatible with PARSE_OPT_KEEP_UNKNOWN_OPT unless in combination with PARSE_OPT_SUBCOMMAND_OPTIONAL");
565 if (flags & PARSE_OPT_KEEP_DASHDASH)
566 BUG("subcommands are incompatible with PARSE_OPT_KEEP_DASHDASH unless in combination with PARSE_OPT_SUBCOMMAND_OPTIONAL");
567 }
568 }
SZEDER Gábor99d86d62022-08-19 18:03:57 +0200569 if ((flags & PARSE_OPT_KEEP_UNKNOWN_OPT) &&
Nguyễn Thái Ngọc Duy202fbb32019-01-27 07:35:23 +0700570 (flags & PARSE_OPT_STOP_AT_NON_OPTION) &&
571 !(flags & PARSE_OPT_ONE_SHOT))
Nguyễn Thái Ngọc Duy48a54992018-11-10 06:16:12 +0100572 BUG("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
Nguyễn Thái Ngọc Duy202fbb32019-01-27 07:35:23 +0700573 if ((flags & PARSE_OPT_ONE_SHOT) &&
574 (flags & PARSE_OPT_KEEP_ARGV0))
575 BUG("Can't keep argv0 if you don't have it");
Stephen Boyd9ca11692010-12-05 23:57:42 -0800576 parse_options_check(options);
Pierre Habouzit7e7bbcb2008-06-23 21:59:37 +0200577}
578
Nguyễn Thái Ngọc Duy5c387422019-04-29 17:05:25 +0700579void parse_options_start(struct parse_opt_ctx_t *ctx,
580 int argc, const char **argv, const char *prefix,
Ævar Arnfjörð Bjarmason3f9ab7c2021-10-08 21:07:38 +0200581 const struct option *options,
582 enum parse_opt_flags flags)
Nguyễn Thái Ngọc Duy5c387422019-04-29 17:05:25 +0700583{
584 memset(ctx, 0, sizeof(*ctx));
585 parse_options_start_1(ctx, argc, argv, prefix, options, flags);
586}
587
Ryan Zoellera0abe5e2020-08-19 23:06:08 +0000588static void show_negated_gitcomp(const struct option *opts, int show_all,
589 int nr_noopts)
Nguyễn Thái Ngọc Duyb221b5a2018-06-06 11:41:39 +0200590{
591 int printed_dashdash = 0;
592
593 for (; opts->type != OPTION_END; opts++) {
594 int has_unset_form = 0;
595 const char *name;
596
597 if (!opts->long_name)
598 continue;
Ryan Zoellera0abe5e2020-08-19 23:06:08 +0000599 if (!show_all &&
600 (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE)))
Nguyễn Thái Ngọc Duyb221b5a2018-06-06 11:41:39 +0200601 continue;
602 if (opts->flags & PARSE_OPT_NONEG)
603 continue;
604
605 switch (opts->type) {
606 case OPTION_STRING:
607 case OPTION_FILENAME:
608 case OPTION_INTEGER:
609 case OPTION_MAGNITUDE:
610 case OPTION_CALLBACK:
611 case OPTION_BIT:
612 case OPTION_NEGBIT:
613 case OPTION_COUNTUP:
614 case OPTION_SET_INT:
615 has_unset_form = 1;
616 break;
617 default:
618 break;
619 }
620 if (!has_unset_form)
621 continue;
622
623 if (skip_prefix(opts->long_name, "no-", &name)) {
624 if (nr_noopts < 0)
625 printf(" --%s", name);
626 } else if (nr_noopts >= 0) {
627 if (nr_noopts && !printed_dashdash) {
628 printf(" --");
629 printed_dashdash = 1;
630 }
631 printf(" --no-%s", opts->long_name);
632 nr_noopts++;
633 }
634 }
635}
636
Ryan Zoellera0abe5e2020-08-19 23:06:08 +0000637static int show_gitcomp(const struct option *opts, int show_all)
Nguyễn Thái Ngọc Duyb9d7f4b2018-02-09 18:01:40 +0700638{
Nguyễn Thái Ngọc Duyb221b5a2018-06-06 11:41:39 +0200639 const struct option *original_opts = opts;
640 int nr_noopts = 0;
641
Nguyễn Thái Ngọc Duyb9d7f4b2018-02-09 18:01:40 +0700642 for (; opts->type != OPTION_END; opts++) {
SZEDER Gáborfa83cc82022-08-19 18:04:00 +0200643 const char *prefix = "--";
Nguyễn Thái Ngọc Duyb9d7f4b2018-02-09 18:01:40 +0700644 const char *suffix = "";
645
646 if (!opts->long_name)
647 continue;
Ryan Zoellera0abe5e2020-08-19 23:06:08 +0000648 if (!show_all &&
Philippe Blainca2d62b2021-07-16 01:55:43 +0000649 (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE | PARSE_OPT_FROM_ALIAS)))
Nguyễn Thái Ngọc Duyb9d7f4b2018-02-09 18:01:40 +0700650 continue;
651
652 switch (opts->type) {
SZEDER Gáborfa83cc82022-08-19 18:04:00 +0200653 case OPTION_SUBCOMMAND:
654 prefix = "";
655 break;
Nguyễn Thái Ngọc Duyb9d7f4b2018-02-09 18:01:40 +0700656 case OPTION_GROUP:
657 continue;
658 case OPTION_STRING:
659 case OPTION_FILENAME:
660 case OPTION_INTEGER:
661 case OPTION_MAGNITUDE:
662 case OPTION_CALLBACK:
663 if (opts->flags & PARSE_OPT_NOARG)
664 break;
665 if (opts->flags & PARSE_OPT_OPTARG)
666 break;
667 if (opts->flags & PARSE_OPT_LASTARG_DEFAULT)
668 break;
669 suffix = "=";
670 break;
671 default:
672 break;
673 }
Nguyễn Thái Ngọc Duyebc4a042018-02-09 18:02:12 +0700674 if (opts->flags & PARSE_OPT_COMP_ARG)
675 suffix = "=";
Nguyễn Thái Ngọc Duyb221b5a2018-06-06 11:41:39 +0200676 if (starts_with(opts->long_name, "no-"))
677 nr_noopts++;
SZEDER Gáborfa83cc82022-08-19 18:04:00 +0200678 printf("%s%s%s%s", opts == original_opts ? "" : " ",
679 prefix, opts->long_name, suffix);
Nguyễn Thái Ngọc Duyb9d7f4b2018-02-09 18:01:40 +0700680 }
Ryan Zoellera0abe5e2020-08-19 23:06:08 +0000681 show_negated_gitcomp(original_opts, show_all, -1);
682 show_negated_gitcomp(original_opts, show_all, nr_noopts);
Nguyễn Thái Ngọc Duyb9d7f4b2018-02-09 18:01:40 +0700683 fputc('\n', stdout);
Nguyễn Thái Ngọc Duya92ec7e2018-12-11 16:35:01 +0100684 return PARSE_OPT_COMPLETE;
Nguyễn Thái Ngọc Duyb9d7f4b2018-02-09 18:01:40 +0700685}
686
Nguyễn Thái Ngọc Duy5c387422019-04-29 17:05:25 +0700687/*
688 * Scan and may produce a new option[] array, which should be used
689 * instead of the original 'options'.
690 *
Elijah Newren15beaaa2019-11-05 17:07:23 +0000691 * Right now this is only used to preprocess and substitute
Nguyễn Thái Ngọc Duy5c387422019-04-29 17:05:25 +0700692 * OPTION_ALIAS.
Andrzej Hunt64cc5392021-03-21 16:58:36 +0000693 *
694 * The returned options should be freed using free_preprocessed_options.
Nguyễn Thái Ngọc Duy5c387422019-04-29 17:05:25 +0700695 */
696static struct option *preprocess_options(struct parse_opt_ctx_t *ctx,
697 const struct option *options)
698{
699 struct option *newopt;
700 int i, nr, alias;
701 int nr_aliases = 0;
702
703 for (nr = 0; options[nr].type != OPTION_END; nr++) {
704 if (options[nr].type == OPTION_ALIAS)
705 nr_aliases++;
706 }
707
708 if (!nr_aliases)
709 return NULL;
710
René Scharfe6e578412023-01-01 22:16:48 +0100711 DUP_ARRAY(newopt, options, nr + 1);
Nguyễn Thái Ngọc Duy5c387422019-04-29 17:05:25 +0700712
713 /* each alias has two string pointers and NULL */
714 CALLOC_ARRAY(ctx->alias_groups, 3 * (nr_aliases + 1));
715
716 for (alias = 0, i = 0; i < nr; i++) {
717 int short_name;
718 const char *long_name;
719 const char *source;
Junio C Hamano7c280582020-03-16 13:22:54 -0700720 struct strbuf help = STRBUF_INIT;
Nguyễn Thái Ngọc Duy5c387422019-04-29 17:05:25 +0700721 int j;
722
723 if (newopt[i].type != OPTION_ALIAS)
724 continue;
725
726 short_name = newopt[i].short_name;
727 long_name = newopt[i].long_name;
728 source = newopt[i].value;
729
730 if (!long_name)
731 BUG("An alias must have long option name");
Junio C Hamano7c280582020-03-16 13:22:54 -0700732 strbuf_addf(&help, _("alias of --%s"), source);
Nguyễn Thái Ngọc Duy5c387422019-04-29 17:05:25 +0700733
734 for (j = 0; j < nr; j++) {
735 const char *name = options[j].long_name;
736
737 if (!name || strcmp(name, source))
738 continue;
739
740 if (options[j].type == OPTION_ALIAS)
741 BUG("No please. Nested aliases are not supported.");
742
Nguyễn Thái Ngọc Duy5c387422019-04-29 17:05:25 +0700743 memcpy(newopt + i, options + j, sizeof(*newopt));
744 newopt[i].short_name = short_name;
745 newopt[i].long_name = long_name;
Junio C Hamano7c280582020-03-16 13:22:54 -0700746 newopt[i].help = strbuf_detach(&help, NULL);
Andrzej Hunt64cc5392021-03-21 16:58:36 +0000747 newopt[i].flags |= PARSE_OPT_FROM_ALIAS;
Nguyễn Thái Ngọc Duy5c387422019-04-29 17:05:25 +0700748 break;
749 }
750
751 if (j == nr)
752 BUG("could not find source option '%s' of alias '%s'",
753 source, newopt[i].long_name);
754 ctx->alias_groups[alias * 3 + 0] = newopt[i].long_name;
755 ctx->alias_groups[alias * 3 + 1] = options[j].long_name;
756 ctx->alias_groups[alias * 3 + 2] = NULL;
757 alias++;
758 }
759
760 return newopt;
761}
762
Andrzej Hunt64cc5392021-03-21 16:58:36 +0000763static void free_preprocessed_options(struct option *options)
764{
765 int i;
766
767 if (!options)
768 return;
769
770 for (i = 0; options[i].type != OPTION_END; i++) {
771 if (options[i].flags & PARSE_OPT_FROM_ALIAS)
772 free((void *)options[i].help);
773 }
774 free(options);
775}
776
Ævar Arnfjörð Bjarmason352e7612021-10-08 21:07:39 +0200777static enum parse_opt_result usage_with_options_internal(struct parse_opt_ctx_t *,
778 const char * const *,
779 const struct option *,
780 int, int);
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200781
Ævar Arnfjörð Bjarmason352e7612021-10-08 21:07:39 +0200782enum parse_opt_result parse_options_step(struct parse_opt_ctx_t *ctx,
783 const struct option *options,
784 const char * const usagestr[])
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200785{
René Scharfeb92891f2009-03-08 19:15:08 +0100786 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
787
Pierre Habouzit26141b52008-06-23 22:55:11 +0200788 /* we must reset ->opt, unknown short option leave it dangling */
789 ctx->opt = NULL;
790
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200791 for (; ctx->argc; ctx->argc--, ctx->argv++) {
792 const char *arg = ctx->argv[0];
793
Nguyễn Thái Ngọc Duy202fbb32019-01-27 07:35:23 +0700794 if (ctx->flags & PARSE_OPT_ONE_SHOT &&
795 ctx->argc != ctx->total)
796 break;
797
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200798 if (*arg != '-' || !arg[1]) {
René Scharfe51a99492009-05-07 21:45:42 +0200799 if (parse_nodash_opt(ctx, arg, options) == 0)
800 continue;
SZEDER Gáborfa83cc82022-08-19 18:04:00 +0200801 if (!ctx->has_subcommands) {
802 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
803 return PARSE_OPT_NON_OPTION;
804 ctx->out[ctx->cpidx++] = ctx->argv[0];
805 continue;
806 }
807 switch (parse_subcommand(arg, options)) {
808 case PARSE_OPT_SUBCOMMAND:
809 return PARSE_OPT_SUBCOMMAND;
810 case PARSE_OPT_UNKNOWN:
811 if (ctx->flags & PARSE_OPT_SUBCOMMAND_OPTIONAL)
812 /*
813 * arg is neither a short or long
814 * option nor a subcommand. Since
815 * this command has a default
816 * operation mode, we have to treat
817 * this arg and all remaining args
818 * as args meant to that default
819 * operation mode.
820 * So we are done parsing.
821 */
822 return PARSE_OPT_DONE;
823 error(_("unknown subcommand: `%s'"), arg);
824 usage_with_options(usagestr, options);
825 case PARSE_OPT_COMPLETE:
826 case PARSE_OPT_HELP:
827 case PARSE_OPT_ERROR:
828 case PARSE_OPT_DONE:
829 case PARSE_OPT_NON_OPTION:
830 /* Impossible. */
831 BUG("parse_subcommand() cannot return these");
832 }
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200833 }
834
René Scharfe5ad0d3d2015-11-17 11:25:38 +0100835 /* lone -h asks for help */
836 if (internal_help && ctx->total == 1 && !strcmp(arg + 1, "h"))
837 goto show_usage;
838
Ryan Zoellera0abe5e2020-08-19 23:06:08 +0000839 /*
840 * lone --git-completion-helper and --git-completion-helper-all
841 * are asked by git-completion.bash
842 */
843 if (ctx->total == 1 && !strcmp(arg, "--git-completion-helper"))
844 return show_gitcomp(options, 0);
845 if (ctx->total == 1 && !strcmp(arg, "--git-completion-helper-all"))
846 return show_gitcomp(options, 1);
Nguyễn Thái Ngọc Duyb9d7f4b2018-02-09 18:01:40 +0700847
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200848 if (arg[1] != '-') {
849 ctx->opt = arg + 1;
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200850 switch (parse_short_opt(ctx, options)) {
Nguyễn Thái Ngọc Duyf41179f2019-01-27 07:35:27 +0700851 case PARSE_OPT_ERROR:
Paul-Sebastian Ungureanu3bb09232018-03-22 20:43:51 +0200852 return PARSE_OPT_ERROR;
Nguyễn Thái Ngọc Duyf41179f2019-01-27 07:35:27 +0700853 case PARSE_OPT_UNKNOWN:
René Scharfe38916c52012-03-03 12:00:29 +0100854 if (ctx->opt)
855 check_typos(arg + 1, options);
René Scharfe5ad0d3d2015-11-17 11:25:38 +0100856 if (internal_help && *ctx->opt == 'h')
857 goto show_usage;
René Scharfeb5ce3a52009-03-08 19:12:47 +0100858 goto unknown;
Nguyễn Thái Ngọc Duyf41179f2019-01-27 07:35:27 +0700859 case PARSE_OPT_NON_OPTION:
SZEDER Gáborfa83cc82022-08-19 18:04:00 +0200860 case PARSE_OPT_SUBCOMMAND:
Nguyễn Thái Ngọc Duyf41179f2019-01-27 07:35:27 +0700861 case PARSE_OPT_HELP:
862 case PARSE_OPT_COMPLETE:
863 BUG("parse_short_opt() cannot return these");
864 case PARSE_OPT_DONE:
865 break;
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200866 }
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200867 if (ctx->opt)
868 check_typos(arg + 1, options);
869 while (ctx->opt) {
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200870 switch (parse_short_opt(ctx, options)) {
Nguyễn Thái Ngọc Duyf41179f2019-01-27 07:35:27 +0700871 case PARSE_OPT_ERROR:
Paul-Sebastian Ungureanu3bb09232018-03-22 20:43:51 +0200872 return PARSE_OPT_ERROR;
Nguyễn Thái Ngọc Duyf41179f2019-01-27 07:35:27 +0700873 case PARSE_OPT_UNKNOWN:
René Scharfe5ad0d3d2015-11-17 11:25:38 +0100874 if (internal_help && *ctx->opt == 'h')
875 goto show_usage;
876
Pierre Habouzit26141b52008-06-23 22:55:11 +0200877 /* fake a short option thing to hide the fact that we may have
878 * started to parse aggregated stuff
879 *
880 * This is leaky, too bad.
881 */
882 ctx->argv[0] = xstrdup(ctx->opt - 1);
883 *(char *)ctx->argv[0] = '-';
René Scharfeb5ce3a52009-03-08 19:12:47 +0100884 goto unknown;
Nguyễn Thái Ngọc Duyf41179f2019-01-27 07:35:27 +0700885 case PARSE_OPT_NON_OPTION:
SZEDER Gáborfa83cc82022-08-19 18:04:00 +0200886 case PARSE_OPT_SUBCOMMAND:
Nguyễn Thái Ngọc Duyf41179f2019-01-27 07:35:27 +0700887 case PARSE_OPT_COMPLETE:
888 case PARSE_OPT_HELP:
889 BUG("parse_short_opt() cannot return these");
890 case PARSE_OPT_DONE:
891 break;
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200892 }
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200893 }
894 continue;
895 }
896
Jeff King51b45942019-08-06 10:40:16 -0400897 if (!arg[2] /* "--" */ ||
898 !strcmp(arg + 2, "end-of-options")) {
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200899 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
900 ctx->argc--;
901 ctx->argv++;
902 }
903 break;
904 }
905
René Scharfeb92891f2009-03-08 19:15:08 +0100906 if (internal_help && !strcmp(arg + 2, "help-all"))
Thomas Rast47e9cd22010-06-12 14:57:39 +0200907 return usage_with_options_internal(ctx, usagestr, options, 1, 0);
René Scharfeb92891f2009-03-08 19:15:08 +0100908 if (internal_help && !strcmp(arg + 2, "help"))
René Scharfeac20ff62015-11-17 11:25:14 +0100909 goto show_usage;
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200910 switch (parse_long_opt(ctx, arg + 2, options)) {
Nguyễn Thái Ngọc Duyf41179f2019-01-27 07:35:27 +0700911 case PARSE_OPT_ERROR:
Paul-Sebastian Ungureanu3bb09232018-03-22 20:43:51 +0200912 return PARSE_OPT_ERROR;
Nguyễn Thái Ngọc Duyf41179f2019-01-27 07:35:27 +0700913 case PARSE_OPT_UNKNOWN:
René Scharfeb5ce3a52009-03-08 19:12:47 +0100914 goto unknown;
Nguyễn Thái Ngọc Duyf41179f2019-01-27 07:35:27 +0700915 case PARSE_OPT_HELP:
Paul-Sebastian Ungureanu3bb09232018-03-22 20:43:51 +0200916 goto show_usage;
Nguyễn Thái Ngọc Duyf41179f2019-01-27 07:35:27 +0700917 case PARSE_OPT_NON_OPTION:
SZEDER Gáborfa83cc82022-08-19 18:04:00 +0200918 case PARSE_OPT_SUBCOMMAND:
Nguyễn Thái Ngọc Duyf41179f2019-01-27 07:35:27 +0700919 case PARSE_OPT_COMPLETE:
920 BUG("parse_long_opt() cannot return these");
921 case PARSE_OPT_DONE:
922 break;
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200923 }
René Scharfeb5ce3a52009-03-08 19:12:47 +0100924 continue;
925unknown:
Nguyễn Thái Ngọc Duy202fbb32019-01-27 07:35:23 +0700926 if (ctx->flags & PARSE_OPT_ONE_SHOT)
927 break;
SZEDER Gáborfa83cc82022-08-19 18:04:00 +0200928 if (ctx->has_subcommands &&
929 (ctx->flags & PARSE_OPT_SUBCOMMAND_OPTIONAL) &&
930 (ctx->flags & PARSE_OPT_KEEP_UNKNOWN_OPT)) {
931 /*
932 * Found an unknown option given to a command with
933 * subcommands that has a default operation mode:
934 * we treat this option and all remaining args as
935 * arguments meant to that default operation mode.
936 * So we are done parsing.
937 */
938 return PARSE_OPT_DONE;
939 }
SZEDER Gábor99d86d62022-08-19 18:03:57 +0200940 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN_OPT))
René Scharfeb5ce3a52009-03-08 19:12:47 +0100941 return PARSE_OPT_UNKNOWN;
942 ctx->out[ctx->cpidx++] = ctx->argv[0];
943 ctx->opt = NULL;
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200944 }
945 return PARSE_OPT_DONE;
René Scharfeac20ff62015-11-17 11:25:14 +0100946
René Scharfeac20ff62015-11-17 11:25:14 +0100947 show_usage:
Paul-Sebastian Ungureanu3bb09232018-03-22 20:43:51 +0200948 return usage_with_options_internal(ctx, usagestr, options, 0, 0);
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200949}
950
Pierre Habouzit7e7bbcb2008-06-23 21:59:37 +0200951int parse_options_end(struct parse_opt_ctx_t *ctx)
952{
Nguyễn Thái Ngọc Duy202fbb32019-01-27 07:35:23 +0700953 if (ctx->flags & PARSE_OPT_ONE_SHOT)
954 return ctx->total - ctx->argc;
955
SZEDER Gáborf919ffe2018-01-22 18:50:09 +0100956 MOVE_ARRAY(ctx->out + ctx->cpidx, ctx->argv, ctx->argc);
Pierre Habouzit7e7bbcb2008-06-23 21:59:37 +0200957 ctx->out[ctx->cpidx + ctx->argc] = NULL;
958 return ctx->cpidx + ctx->argc;
959}
960
Ævar Arnfjörð Bjarmason06a199f2021-11-09 12:04:43 +0100961int parse_options(int argc, const char **argv,
962 const char *prefix,
963 const struct option *options,
964 const char * const usagestr[],
965 enum parse_opt_flags flags)
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200966{
Pierre Habouzit7e7bbcb2008-06-23 21:59:37 +0200967 struct parse_opt_ctx_t ctx;
Nguyễn Thái Ngọc Duy5c387422019-04-29 17:05:25 +0700968 struct option *real_options;
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200969
Johannes Schindelinb02e7d52019-04-12 02:37:24 -0700970 disallow_abbreviated_options =
971 git_env_bool("GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS", 0);
972
Nguyễn Thái Ngọc Duy5c387422019-04-29 17:05:25 +0700973 memset(&ctx, 0, sizeof(ctx));
974 real_options = preprocess_options(&ctx, options);
975 if (real_options)
976 options = real_options;
977 parse_options_start_1(&ctx, argc, argv, prefix, options, flags);
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200978 switch (parse_options_step(&ctx, options, usagestr)) {
979 case PARSE_OPT_HELP:
Paul-Sebastian Ungureanu3bb09232018-03-22 20:43:51 +0200980 case PARSE_OPT_ERROR:
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200981 exit(129);
Nguyễn Thái Ngọc Duya92ec7e2018-12-11 16:35:01 +0100982 case PARSE_OPT_COMPLETE:
983 exit(0);
Jonathan Nieder979240f2010-12-01 17:32:55 -0600984 case PARSE_OPT_NON_OPTION:
SZEDER Gáborfa83cc82022-08-19 18:04:00 +0200985 case PARSE_OPT_SUBCOMMAND:
986 break;
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200987 case PARSE_OPT_DONE:
SZEDER Gáborfa83cc82022-08-19 18:04:00 +0200988 if (ctx.has_subcommands &&
989 !(flags & PARSE_OPT_SUBCOMMAND_OPTIONAL)) {
990 error(_("need a subcommand"));
991 usage_with_options(usagestr, options);
992 }
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200993 break;
Ævar Arnfjörð Bjarmason1b887352021-10-08 21:07:40 +0200994 case PARSE_OPT_UNKNOWN:
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200995 if (ctx.argv[0][1] == '-') {
Nguyễn Thái Ngọc Duy89003422018-11-10 06:16:13 +0100996 error(_("unknown option `%s'"), ctx.argv[0] + 2);
Erik Faye-Lundb141a472013-02-12 00:13:48 +0100997 } else if (isascii(*ctx.opt)) {
Nguyễn Thái Ngọc Duy89003422018-11-10 06:16:13 +0100998 error(_("unknown switch `%c'"), *ctx.opt);
Erik Faye-Lundb141a472013-02-12 00:13:48 +0100999 } else {
Nguyễn Thái Ngọc Duy89003422018-11-10 06:16:13 +01001000 error(_("unknown non-ascii option in string: `%s'"),
Erik Faye-Lundb141a472013-02-12 00:13:48 +01001001 ctx.argv[0]);
Pierre Habouzit07fe54d2008-06-23 22:46:36 +02001002 }
1003 usage_with_options(usagestr, options);
Pierre Habouzit4a59fd12007-10-15 01:35:37 +02001004 }
1005
Torsten Bögershausen5c327502021-02-03 17:28:23 +01001006 precompose_argv_prefix(argc, argv, NULL);
Andrzej Hunt64cc5392021-03-21 16:58:36 +00001007 free_preprocessed_options(real_options);
Nguyễn Thái Ngọc Duy5c387422019-04-29 17:05:25 +07001008 free(ctx.alias_groups);
Pierre Habouzit7e7bbcb2008-06-23 21:59:37 +02001009 return parse_options_end(&ctx);
Pierre Habouzit4a59fd12007-10-15 01:35:37 +02001010}
Pierre Habouzitd7a38c52007-10-15 01:38:30 +02001011
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +02001012static int usage_argh(const struct option *opts, FILE *outfile)
Stephen Boyd29f25d42009-05-21 00:33:17 -07001013{
1014 const char *s;
René Scharfe5f0df442018-08-02 21:18:14 +02001015 int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
1016 !opts->argh || !!strpbrk(opts->argh, "()<>[]|");
Stephen Boyd29f25d42009-05-21 00:33:17 -07001017 if (opts->flags & PARSE_OPT_OPTARG)
1018 if (opts->long_name)
1019 s = literal ? "[=%s]" : "[=<%s>]";
1020 else
1021 s = literal ? "[%s]" : "[<%s>]";
1022 else
1023 s = literal ? " %s" : " <%s>";
Jiang Xinc0821962013-02-09 14:31:09 +08001024 return utf8_fprintf(outfile, s, opts->argh ? _(opts->argh) : _("..."));
Stephen Boyd29f25d42009-05-21 00:33:17 -07001025}
1026
René Scharfe652a6b12023-08-05 16:43:04 +02001027static int usage_indent(FILE *outfile)
1028{
1029 return fprintf(outfile, " ");
1030}
1031
René Scharfe311c8ff2023-08-05 16:52:42 +02001032#define USAGE_OPTS_WIDTH 26
Pierre Habouzitd7a38c52007-10-15 01:38:30 +02001033
René Scharfe652a6b12023-08-05 16:43:04 +02001034static void usage_padding(FILE *outfile, size_t pos)
1035{
René Scharfe311c8ff2023-08-05 16:52:42 +02001036 if (pos < USAGE_OPTS_WIDTH)
1037 fprintf(outfile, "%*s", USAGE_OPTS_WIDTH - (int)pos, "");
1038 else
1039 fprintf(outfile, "\n%*s", USAGE_OPTS_WIDTH, "");
René Scharfe652a6b12023-08-05 16:43:04 +02001040}
1041
René Scharfe2a409a12023-08-05 16:44:45 +02001042static const struct option *find_option_by_long_name(const struct option *opts,
1043 const char *long_name)
1044{
1045 for (; opts->type != OPTION_END; opts++) {
1046 if (opts->long_name && !strcmp(opts->long_name, long_name))
1047 return opts;
1048 }
1049 return NULL;
1050}
Pierre Habouzitd7a38c52007-10-15 01:38:30 +02001051
Ævar Arnfjörð Bjarmason352e7612021-10-08 21:07:39 +02001052static enum parse_opt_result usage_with_options_internal(struct parse_opt_ctx_t *ctx,
1053 const char * const *usagestr,
1054 const struct option *opts,
1055 int full, int err)
Pierre Habouzitd7a38c52007-10-15 01:38:30 +02001056{
René Scharfe2a409a12023-08-05 16:44:45 +02001057 const struct option *all_opts = opts;
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +02001058 FILE *outfile = err ? stderr : stdout;
Brandon Caseya6304fa2017-09-24 21:08:05 -07001059 int need_newline;
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +02001060
Ævar Arnfjörð Bjarmason4631cfc2021-09-21 15:30:11 +02001061 const char *usage_prefix = _("usage: %s");
1062 /*
1063 * The translation could be anything, but we can count on
1064 * msgfmt(1)'s --check option to have asserted that "%s" is in
1065 * the translation. So compute the length of the "usage: "
1066 * part. We are assuming that the translator wasn't overly
1067 * clever and used e.g. "%1$s" instead of "%s", there's only
1068 * one "%s" in "usage_prefix" above, so there's no reason to
1069 * do so even with a RTL language.
1070 */
1071 size_t usage_len = strlen(usage_prefix) - strlen("%s");
1072 /*
1073 * TRANSLATORS: the colon here should align with the
1074 * one in "usage: %s" translation.
1075 */
1076 const char *or_prefix = _(" or: %s");
1077 /*
1078 * TRANSLATORS: You should only need to translate this format
1079 * string if your language is a RTL language (e.g. Arabic,
1080 * Hebrew etc.), not if it's a LTR language (e.g. German,
1081 * Russian, Chinese etc.).
1082 *
1083 * When a translated usage string has an embedded "\n" it's
1084 * because options have wrapped to the next line. The line
1085 * after the "\n" will then be padded to align with the
1086 * command name, such as N_("git cmd [opt]\n<8
1087 * spaces>[opt2]"), where the 8 spaces are the same length as
1088 * "git cmd ".
1089 *
1090 * This format string prints out that already-translated
1091 * line. The "%*s" is whitespace padding to account for the
1092 * padding at the start of the line that we add in this
1093 * function. The "%s" is a line in the (hopefully already
1094 * translated) N_() usage string, which contained embedded
1095 * newlines before we split it up.
1096 */
1097 const char *usage_continued = _("%*s%s");
1098 const char *prefix = usage_prefix;
1099 int saw_empty_line = 0;
1100
René Scharfe49b61802009-03-08 19:16:58 +01001101 if (!usagestr)
1102 return PARSE_OPT_HELP;
1103
Thomas Rast47e9cd22010-06-12 14:57:39 +02001104 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
1105 fprintf(outfile, "cat <<\\EOF\n");
1106
Jeff King44d86e92008-06-14 03:27:21 -04001107 while (*usagestr) {
Ævar Arnfjörð Bjarmason4631cfc2021-09-21 15:30:11 +02001108 const char *str = _(*usagestr++);
1109 struct string_list list = STRING_LIST_INIT_DUP;
1110 unsigned int j;
1111
1112 if (!saw_empty_line && !*str)
1113 saw_empty_line = 1;
1114
1115 string_list_split(&list, str, '\n', -1);
1116 for (j = 0; j < list.nr; j++) {
1117 const char *line = list.items[j].string;
1118
1119 if (saw_empty_line && *line)
1120 fprintf_ln(outfile, _(" %s"), line);
1121 else if (saw_empty_line)
1122 fputc('\n', outfile);
1123 else if (!j)
1124 fprintf_ln(outfile, prefix, line);
1125 else
1126 fprintf_ln(outfile, usage_continued,
1127 (int)usage_len, "", line);
1128 }
1129 string_list_clear(&list, 0);
1130
1131 prefix = or_prefix;
Jeff King44d86e92008-06-14 03:27:21 -04001132 }
Pierre Habouzitd7a38c52007-10-15 01:38:30 +02001133
Brandon Caseya6304fa2017-09-24 21:08:05 -07001134 need_newline = 1;
Pierre Habouzitd7a38c52007-10-15 01:38:30 +02001135
1136 for (; opts->type != OPTION_END; opts++) {
1137 size_t pos;
Junio C Hamano448abbb2023-07-18 15:54:04 -07001138 const char *cp, *np;
René Scharfe2a409a12023-08-05 16:44:45 +02001139 const char *positive_name = NULL;
Pierre Habouzitd7a38c52007-10-15 01:38:30 +02001140
SZEDER Gáborfa83cc82022-08-19 18:04:00 +02001141 if (opts->type == OPTION_SUBCOMMAND)
1142 continue;
Pierre Habouzitd7a38c52007-10-15 01:38:30 +02001143 if (opts->type == OPTION_GROUP) {
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +02001144 fputc('\n', outfile);
Brandon Caseya6304fa2017-09-24 21:08:05 -07001145 need_newline = 0;
Pierre Habouzitd7a38c52007-10-15 01:38:30 +02001146 if (*opts->help)
Nguyễn Thái Ngọc Duy54e6dc72012-05-06 21:23:51 +07001147 fprintf(outfile, "%s\n", _(opts->help));
Pierre Habouzitd7a38c52007-10-15 01:38:30 +02001148 continue;
1149 }
Pierre Habouzitdd3bf0f2007-11-19 10:21:44 +01001150 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
1151 continue;
Pierre Habouzitd7a38c52007-10-15 01:38:30 +02001152
Brandon Caseya6304fa2017-09-24 21:08:05 -07001153 if (need_newline) {
1154 fputc('\n', outfile);
1155 need_newline = 0;
1156 }
1157
René Scharfe652a6b12023-08-05 16:43:04 +02001158 pos = usage_indent(outfile);
René Scharfecbb08c22012-02-28 20:06:09 +01001159 if (opts->short_name) {
René Scharfe51a99492009-05-07 21:45:42 +02001160 if (opts->flags & PARSE_OPT_NODASH)
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +02001161 pos += fprintf(outfile, "%c", opts->short_name);
René Scharfe51a99492009-05-07 21:45:42 +02001162 else
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +02001163 pos += fprintf(outfile, "-%c", opts->short_name);
René Scharfe51a99492009-05-07 21:45:42 +02001164 }
Pierre Habouzitd7a38c52007-10-15 01:38:30 +02001165 if (opts->long_name && opts->short_name)
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +02001166 pos += fprintf(outfile, ", ");
René Scharfee8e5d292023-08-05 16:40:59 +02001167 if (opts->long_name) {
1168 const char *long_name = opts->long_name;
René Scharfe2a409a12023-08-05 16:44:45 +02001169 if ((opts->flags & PARSE_OPT_NONEG) ||
1170 skip_prefix(long_name, "no-", &positive_name))
René Scharfee8e5d292023-08-05 16:40:59 +02001171 pos += fprintf(outfile, "--%s", long_name);
1172 else
1173 pos += fprintf(outfile, "--[no-]%s", long_name);
1174 }
1175
René Scharfee0319ff2009-05-07 21:45:08 +02001176 if (opts->type == OPTION_NUMBER)
Jiang Xinc0821962013-02-09 14:31:09 +08001177 pos += utf8_fprintf(outfile, _("-NUM"));
Pierre Habouzitd7a38c52007-10-15 01:38:30 +02001178
Jonathan Niederb57c68a2010-12-01 17:31:36 -06001179 if ((opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
1180 !(opts->flags & PARSE_OPT_NOARG))
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +02001181 pos += usage_argh(opts, outfile);
Pierre Habouzitd7a38c52007-10-15 01:38:30 +02001182
Nguyễn Thái Ngọc Duy5c387422019-04-29 17:05:25 +07001183 if (opts->type == OPTION_ALIAS) {
René Scharfe652a6b12023-08-05 16:43:04 +02001184 usage_padding(outfile, pos);
Nguyễn Thái Ngọc Duy5c387422019-04-29 17:05:25 +07001185 fprintf_ln(outfile, _("alias of --%s"),
1186 (const char *)opts->value);
1187 continue;
1188 }
Junio C Hamano448abbb2023-07-18 15:54:04 -07001189
René Scharfecd52d9e2023-08-26 10:06:00 +02001190 for (cp = opts->help ? _(opts->help) : ""; *cp; cp = np) {
Junio C Hamano448abbb2023-07-18 15:54:04 -07001191 np = strchrnul(cp, '\n');
Junio C Hamano448abbb2023-07-18 15:54:04 -07001192 if (*np)
1193 np++;
René Scharfecd52d9e2023-08-26 10:06:00 +02001194 usage_padding(outfile, pos);
1195 fwrite(cp, 1, np - cp, outfile);
René Scharfe652a6b12023-08-05 16:43:04 +02001196 pos = 0;
Junio C Hamano448abbb2023-07-18 15:54:04 -07001197 }
René Scharfecd52d9e2023-08-26 10:06:00 +02001198 fputc('\n', outfile);
René Scharfe2a409a12023-08-05 16:44:45 +02001199
1200 if (positive_name) {
1201 if (find_option_by_long_name(all_opts, positive_name))
1202 continue;
1203 pos = usage_indent(outfile);
1204 pos += fprintf(outfile, "--%s", positive_name);
1205 usage_padding(outfile, pos);
1206 fprintf_ln(outfile, _("opposite of --no-%s"),
1207 positive_name);
Stephen Boydc2e86ad2011-03-22 00:51:05 -07001208 }
Pierre Habouzitff43ec32008-06-23 22:38:58 +02001209 }
Pierre Habouzitdd3bf0f2007-11-19 10:21:44 +01001210 fputc('\n', outfile);
Thomas Rast47e9cd22010-06-12 14:57:39 +02001211
Pierre Habouzitff43ec32008-06-23 22:38:58 +02001212 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
Pierre Habouzitdd3bf0f2007-11-19 10:21:44 +01001213 fputs("EOF\n", outfile);
1214
Stephen Boydc2e86ad2011-03-22 00:51:05 -07001215 return PARSE_OPT_HELP;
Christian Couder451bb212009-02-02 06:12:58 +01001216}
1217
1218void NORETURN usage_with_options(const char * const *usagestr,
Ævar Arnfjörð Bjarmasone081a7c2021-12-07 19:26:30 +01001219 const struct option *opts)
Christian Couder451bb212009-02-02 06:12:58 +01001220{
1221 usage_with_options_internal(NULL, usagestr, opts, 0, 1);
Ævar Arnfjörð Bjarmasonfa476be2021-12-28 14:28:43 +01001222 exit(129);
1223}
1224
1225void NORETURN usage_msg_opt(const char *msg,
1226 const char * const *usagestr,
1227 const struct option *options)
1228{
1229 die_message("%s\n", msg); /* The extra \n is intentional */
1230 usage_with_options(usagestr, options);
1231}
1232
1233void NORETURN usage_msg_optf(const char * const fmt,
1234 const char * const *usagestr,
Junio C Hamanod21d5dd2022-02-25 15:47:35 -08001235 const struct option *options, ...)
Jean-Noël Avilaa6993672022-01-31 22:07:46 +00001236{
1237 struct strbuf msg = STRBUF_INIT;
1238 va_list ap;
1239 va_start(ap, options);
1240 strbuf_vaddf(&msg, fmt, ap);
1241 va_end(ap);
1242
1243 usage_msg_opt(msg.buf, usagestr, options);
1244}
1245
1246void die_for_incompatible_opt4(int opt1, const char *opt1_name,
1247 int opt2, const char *opt2_name,
1248 int opt3, const char *opt3_name,
1249 int opt4, const char *opt4_name)
1250{
1251 int count = 0;
1252 const char *options[4];
1253
1254 if (opt1)
1255 options[count++] = opt1_name;
1256 if (opt2)
1257 options[count++] = opt2_name;
1258 if (opt3)
1259 options[count++] = opt3_name;
1260 if (opt4)
1261 options[count++] = opt4_name;
1262 switch (count) {
1263 case 4:
1264 die(_("options '%s', '%s', '%s', and '%s' cannot be used together"),
1265 opt1_name, opt2_name, opt3_name, opt4_name);
1266 break;
1267 case 3:
1268 die(_("options '%s', '%s', and '%s' cannot be used together"),
Pierre Habouzit4a59fd12007-10-15 01:35:37 +02001269 options[0], options[1], options[2]);
1270 break;
1271 case 2:
1272 die(_("options '%s' and '%s' cannot be used together"),
1273 options[0], options[1]);
1274 break;
1275 default:
1276 break;
1277 }
1278}