blob: cec74522e56b084fbb0c8eeec47456d0bb2f7d3e [file] [log] [blame]
Pierre Habouzit4a59fd12007-10-15 01:35:37 +02001#include "git-compat-util.h"
2#include "parse-options.h"
Pierre Habouzit26141b52008-06-23 22:55:11 +02003#include "cache.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07004#include "config.h"
Jake Goulding269defd2009-01-26 09:13:23 -05005#include "commit.h"
Mark Lodato73e9da02010-02-16 23:55:58 -05006#include "color.h"
Jiang Xinc0821962013-02-09 14:31:09 +08007#include "utf8.h"
Pierre Habouzit4a59fd12007-10-15 01:35:37 +02008
9#define OPT_SHORT 1
10#define OPT_UNSET 2
11
Dmitry Ivankov1f275b72011-08-11 15:15:37 +060012int optbug(const struct option *opt, const char *reason)
Jonathan Nieder1e5ce572010-12-02 00:01:18 -060013{
Junio C Hamanoaf465af2014-09-03 12:42:37 -070014 if (opt->long_name) {
15 if (opt->short_name)
16 return error("BUG: switch '%c' (--%s) %s",
17 opt->short_name, opt->long_name, reason);
Jonathan Nieder1e5ce572010-12-02 00:01:18 -060018 return error("BUG: option '%s' %s", opt->long_name, reason);
Junio C Hamanoaf465af2014-09-03 12:42:37 -070019 }
Jonathan Nieder1e5ce572010-12-02 00:01:18 -060020 return error("BUG: switch '%c' %s", opt->short_name, reason);
21}
22
Nguyễn Thái Ngọc Duyf41179f2019-01-27 07:35:27 +070023static enum parse_opt_result get_arg(struct parse_opt_ctx_t *p,
24 const struct option *opt,
25 int flags, const char **arg)
Pierre Habouzit1cc69852008-07-08 12:34:08 +020026{
27 if (p->opt) {
28 *arg = p->opt;
29 p->opt = NULL;
30 } else if (p->argc == 1 && (opt->flags & PARSE_OPT_LASTARG_DEFAULT)) {
31 *arg = (const char *)opt->defval;
Olivier Marind5d745f2008-07-21 20:30:36 +020032 } else if (p->argc > 1) {
Pierre Habouzit1cc69852008-07-08 12:34:08 +020033 p->argc--;
34 *arg = *++p->argv;
35 } else
Nguyễn Thái Ngọc Duy9440b832018-11-10 06:16:11 +010036 return error(_("%s requires a value"), optname(opt, flags));
Pierre Habouzit1cc69852008-07-08 12:34:08 +020037 return 0;
38}
39
Stephen Boyddf217ed2009-05-23 11:53:13 -070040static void fix_filename(const char *prefix, const char **file)
41{
42 if (!file || !*file || !prefix || is_absolute_path(*file)
43 || !strcmp("-", *file))
44 return;
Jeff Kinge4da43b2017-03-20 21:28:49 -040045 *file = prefix_filename(prefix, *file);
Stephen Boyddf217ed2009-05-23 11:53:13 -070046}
47
Nguyễn Thái Ngọc Duyf41179f2019-01-27 07:35:27 +070048static enum parse_opt_result opt_command_mode_error(
49 const struct option *opt,
50 const struct option *all_opts,
51 int flags)
Junio C Hamano11588262013-07-30 12:06:01 -070052{
53 const struct option *that;
Junio C Hamano11588262013-07-30 12:06:01 -070054 struct strbuf that_name = STRBUF_INIT;
55
56 /*
57 * Find the other option that was used to set the variable
58 * already, and report that this is not compatible with it.
59 */
60 for (that = all_opts; that->type != OPTION_END; that++) {
61 if (that == opt ||
62 that->type != OPTION_CMDMODE ||
63 that->value != opt->value ||
64 that->defval != *(int *)opt->value)
65 continue;
66
67 if (that->long_name)
68 strbuf_addf(&that_name, "--%s", that->long_name);
69 else
70 strbuf_addf(&that_name, "-%c", that->short_name);
Nguyễn Thái Ngọc Duy9440b832018-11-10 06:16:11 +010071 error(_("%s is incompatible with %s"),
72 optname(opt, flags), that_name.buf);
Junio C Hamano11588262013-07-30 12:06:01 -070073 strbuf_release(&that_name);
Nguyễn Thái Ngọc Duyf41179f2019-01-27 07:35:27 +070074 return PARSE_OPT_ERROR;
Junio C Hamano11588262013-07-30 12:06:01 -070075 }
Nguyễn Thái Ngọc Duy9440b832018-11-10 06:16:11 +010076 return error(_("%s : incompatible with something else"),
77 optname(opt, flags));
Junio C Hamano11588262013-07-30 12:06:01 -070078}
79
Nguyễn Thái Ngọc Duyf41179f2019-01-27 07:35:27 +070080static enum parse_opt_result get_value(struct parse_opt_ctx_t *p,
81 const struct option *opt,
82 const struct option *all_opts,
83 int flags)
Pierre Habouzit4a59fd12007-10-15 01:35:37 +020084{
Pierre Habouzitffe659f2007-10-15 01:45:45 +020085 const char *s, *arg;
Pierre Habouzitdb7244b2007-11-07 11:20:27 +010086 const int unset = flags & OPT_UNSET;
Stephen Boyddf217ed2009-05-23 11:53:13 -070087 int err;
Pierre Habouzit4a59fd12007-10-15 01:35:37 +020088
Pierre Habouzitdb7244b2007-11-07 11:20:27 +010089 if (unset && p->opt)
Nguyễn Thái Ngọc Duy9440b832018-11-10 06:16:11 +010090 return error(_("%s takes no value"), optname(opt, flags));
Pierre Habouzitdb7244b2007-11-07 11:20:27 +010091 if (unset && (opt->flags & PARSE_OPT_NONEG))
Nguyễn Thái Ngọc Duy9440b832018-11-10 06:16:11 +010092 return error(_("%s isn't available"), optname(opt, flags));
Stephen Boydc1f4ec92010-12-01 17:30:40 -060093 if (!(flags & OPT_SHORT) && p->opt && (opt->flags & PARSE_OPT_NOARG))
Nguyễn Thái Ngọc Duy9440b832018-11-10 06:16:11 +010094 return error(_("%s takes no value"), optname(opt, flags));
Pierre Habouzitdb7244b2007-11-07 11:20:27 +010095
Pierre Habouzitdb7244b2007-11-07 11:20:27 +010096 switch (opt->type) {
Jonathan Niederb0b3a8b2010-12-01 17:32:16 -060097 case OPTION_LOWLEVEL_CALLBACK:
Nguyễn Thái Ngọc Duy3ebbe282019-01-27 07:35:28 +070098 return opt->ll_callback(p, opt, NULL, unset);
Jonathan Niederb0b3a8b2010-12-01 17:32:16 -060099
Pierre Habouzitdb7244b2007-11-07 11:20:27 +0100100 case OPTION_BIT:
101 if (unset)
102 *(int *)opt->value &= ~opt->defval;
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200103 else
Pierre Habouzitdb7244b2007-11-07 11:20:27 +0100104 *(int *)opt->value |= opt->defval;
105 return 0;
106
René Scharfe2f4b97f2009-05-07 21:44:17 +0200107 case OPTION_NEGBIT:
108 if (unset)
109 *(int *)opt->value |= opt->defval;
110 else
111 *(int *)opt->value &= ~opt->defval;
112 return 0;
113
Nguyễn Thái Ngọc Duyf62470c2019-01-27 07:35:25 +0700114 case OPTION_BITOP:
115 if (unset)
116 BUG("BITOP can't have unset form");
117 *(int *)opt->value &= ~opt->extra;
118 *(int *)opt->value |= opt->defval;
119 return 0;
120
Junio C Hamanob04ba2b2011-09-27 16:56:49 -0700121 case OPTION_COUNTUP:
Pranit Bauvae0070e82016-05-05 15:20:00 +0530122 if (*(int *)opt->value < 0)
123 *(int *)opt->value = 0;
Pierre Habouzitdb7244b2007-11-07 11:20:27 +0100124 *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
125 return 0;
126
127 case OPTION_SET_INT:
128 *(int *)opt->value = unset ? 0 : opt->defval;
129 return 0;
130
Junio C Hamano11588262013-07-30 12:06:01 -0700131 case OPTION_CMDMODE:
132 /*
133 * Giving the same mode option twice, although is unnecessary,
134 * is not a grave error, so let it pass.
135 */
136 if (*(int *)opt->value && *(int *)opt->value != opt->defval)
137 return opt_command_mode_error(opt, all_opts, flags);
138 *(int *)opt->value = opt->defval;
139 return 0;
140
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200141 case OPTION_STRING:
Pierre Habouzit1cc69852008-07-08 12:34:08 +0200142 if (unset)
Pierre Habouzitdb7244b2007-11-07 11:20:27 +0100143 *(const char **)opt->value = NULL;
Pierre Habouzit1cc69852008-07-08 12:34:08 +0200144 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
Pierre Habouzitffe659f2007-10-15 01:45:45 +0200145 *(const char **)opt->value = (const char *)opt->defval;
Pierre Habouzit1cc69852008-07-08 12:34:08 +0200146 else
147 return get_arg(p, opt, flags, (const char **)opt->value);
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200148 return 0;
149
Stephen Boyddf217ed2009-05-23 11:53:13 -0700150 case OPTION_FILENAME:
151 err = 0;
152 if (unset)
153 *(const char **)opt->value = NULL;
154 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
155 *(const char **)opt->value = (const char *)opt->defval;
156 else
157 err = get_arg(p, opt, flags, (const char **)opt->value);
158
159 if (!err)
160 fix_filename(p->prefix, (const char **)opt->value);
161 return err;
162
Pierre Habouzitffe659f2007-10-15 01:45:45 +0200163 case OPTION_CALLBACK:
Nguyễn Thái Ngọc Duy3ebbe282019-01-27 07:35:28 +0700164 {
165 const char *p_arg = NULL;
166 int p_unset;
Pierre Habouzitffe659f2007-10-15 01:45:45 +0200167
Nguyễn Thái Ngọc Duy3ebbe282019-01-27 07:35:28 +0700168 if (unset)
169 p_unset = 1;
170 else if (opt->flags & PARSE_OPT_NOARG)
171 p_unset = 0;
172 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
173 p_unset = 0;
174 else if (get_arg(p, opt, flags, &arg))
175 return -1;
176 else {
177 p_unset = 0;
178 p_arg = arg;
179 }
180 if (opt->callback)
181 return (*opt->callback)(opt, p_arg, p_unset) ? (-1) : 0;
182 else
183 return (*opt->ll_callback)(p, opt, p_arg, p_unset);
184 }
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200185 case OPTION_INTEGER:
Pierre Habouzitdb7244b2007-11-07 11:20:27 +0100186 if (unset) {
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200187 *(int *)opt->value = 0;
188 return 0;
189 }
Pierre Habouzitc43a2482007-12-21 11:41:41 +0100190 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
Pierre Habouzitffe659f2007-10-15 01:45:45 +0200191 *(int *)opt->value = opt->defval;
192 return 0;
193 }
Pierre Habouzit1cc69852008-07-08 12:34:08 +0200194 if (get_arg(p, opt, flags, &arg))
195 return -1;
196 *(int *)opt->value = strtol(arg, (char **)&s, 10);
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200197 if (*s)
Nguyễn Thái Ngọc Duy9440b832018-11-10 06:16:11 +0100198 return error(_("%s expects a numerical value"),
199 optname(opt, flags));
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200200 return 0;
201
Charles Bailey2a514ed2015-06-21 19:25:44 +0100202 case OPTION_MAGNITUDE:
203 if (unset) {
204 *(unsigned long *)opt->value = 0;
205 return 0;
206 }
207 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
208 *(unsigned long *)opt->value = opt->defval;
209 return 0;
210 }
211 if (get_arg(p, opt, flags, &arg))
212 return -1;
213 if (!git_parse_ulong(arg, opt->value))
Nguyễn Thái Ngọc Duy9440b832018-11-10 06:16:11 +0100214 return error(_("%s expects a non-negative integer value"
215 " with an optional k/m/g suffix"),
216 optname(opt, flags));
Charles Bailey2a514ed2015-06-21 19:25:44 +0100217 return 0;
218
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200219 default:
Nguyễn Thái Ngọc Duy48a54992018-11-10 06:16:12 +0100220 BUG("opt->type %d should not happen", opt->type);
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200221 }
222}
223
Nguyễn Thái Ngọc Duyf41179f2019-01-27 07:35:27 +0700224static enum parse_opt_result parse_short_opt(struct parse_opt_ctx_t *p,
225 const struct option *options)
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200226{
Junio C Hamano11588262013-07-30 12:06:01 -0700227 const struct option *all_opts = options;
René Scharfee0319ff2009-05-07 21:45:08 +0200228 const struct option *numopt = NULL;
229
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200230 for (; options->type != OPTION_END; options++) {
231 if (options->short_name == *p->opt) {
232 p->opt = p->opt[1] ? p->opt + 1 : NULL;
Junio C Hamano11588262013-07-30 12:06:01 -0700233 return get_value(p, options, all_opts, OPT_SHORT);
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200234 }
René Scharfee0319ff2009-05-07 21:45:08 +0200235
236 /*
237 * Handle the numerical option later, explicit one-digit
238 * options take precedence over it.
239 */
240 if (options->type == OPTION_NUMBER)
241 numopt = options;
242 }
243 if (numopt && isdigit(*p->opt)) {
244 size_t len = 1;
245 char *arg;
246 int rc;
247
248 while (isdigit(p->opt[len]))
249 len++;
250 arg = xmemdupz(p->opt, len);
251 p->opt = p->opt[len] ? p->opt + len : NULL;
Nguyễn Thái Ngọc Duy3ebbe282019-01-27 07:35:28 +0700252 if (numopt->callback)
253 rc = (*numopt->callback)(numopt, arg, 0) ? (-1) : 0;
254 else
255 rc = (*numopt->ll_callback)(p, numopt, arg, 0);
René Scharfee0319ff2009-05-07 21:45:08 +0200256 free(arg);
257 return rc;
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200258 }
Nguyễn Thái Ngọc Duyf41179f2019-01-27 07:35:27 +0700259 return PARSE_OPT_UNKNOWN;
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200260}
261
Nguyễn Thái Ngọc Duyf41179f2019-01-27 07:35:27 +0700262static enum parse_opt_result parse_long_opt(
263 struct parse_opt_ctx_t *p, const char *arg,
264 const struct option *options)
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200265{
Junio C Hamano11588262013-07-30 12:06:01 -0700266 const struct option *all_opts = options;
Rohit Mani2c5495f2014-03-07 22:48:31 -0800267 const char *arg_end = strchrnul(arg, '=');
Johannes Schindelin243e0612007-11-05 13:15:21 +0000268 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
269 int abbrev_flags = 0, ambiguous_flags = 0;
Johannes Schindelin7f275b92007-10-14 17:54:06 +0100270
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200271 for (; options->type != OPTION_END; options++) {
René Scharfe0f1930c52012-02-25 20:14:54 +0100272 const char *rest, *long_name = options->long_name;
273 int flags = 0, opt_flags = 0;
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200274
René Scharfe0f1930c52012-02-25 20:14:54 +0100275 if (!long_name)
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200276 continue;
277
René Scharfe0f1930c52012-02-25 20:14:54 +0100278again:
Jeff Kingcf4fff52014-06-18 15:44:19 -0400279 if (!skip_prefix(arg, long_name, &rest))
280 rest = NULL;
Pierre Habouzit580d5bf2008-03-02 11:35:56 +0100281 if (options->type == OPTION_ARGUMENT) {
282 if (!rest)
283 continue;
284 if (*rest == '=')
Nguyễn Thái Ngọc Duy9440b832018-11-10 06:16:11 +0100285 return error(_("%s takes no value"),
286 optname(options, flags));
Pierre Habouzit580d5bf2008-03-02 11:35:56 +0100287 if (*rest)
288 continue;
289 p->out[p->cpidx++] = arg - 2;
Nguyễn Thái Ngọc Duyf41179f2019-01-27 07:35:27 +0700290 return PARSE_OPT_DONE;
Pierre Habouzit580d5bf2008-03-02 11:35:56 +0100291 }
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200292 if (!rest) {
Johannes Schindelin7f275b92007-10-14 17:54:06 +0100293 /* abbreviated? */
Nguyễn Thái Ngọc Duybaa4adc2019-01-27 07:35:24 +0700294 if (!(p->flags & PARSE_OPT_KEEP_UNKNOWN) &&
295 !strncmp(long_name, arg, arg_end - arg)) {
Johannes Schindelin7f275b92007-10-14 17:54:06 +0100296is_abbreviated:
Johannes Schindelin243e0612007-11-05 13:15:21 +0000297 if (abbrev_option) {
298 /*
299 * If this is abbreviated, it is
300 * ambiguous. So when there is no
301 * exact match later, we need to
302 * error out.
303 */
304 ambiguous_option = abbrev_option;
305 ambiguous_flags = abbrev_flags;
306 }
Johannes Schindelin7f275b92007-10-14 17:54:06 +0100307 if (!(flags & OPT_UNSET) && *arg_end)
308 p->opt = arg_end + 1;
309 abbrev_option = options;
René Scharfe0f1930c52012-02-25 20:14:54 +0100310 abbrev_flags = flags ^ opt_flags;
Johannes Schindelin7f275b92007-10-14 17:54:06 +0100311 continue;
312 }
Andreas Schwab6bbfd1f2009-09-25 20:44:44 +0200313 /* negation allowed? */
314 if (options->flags & PARSE_OPT_NONEG)
315 continue;
Johannes Schindelin7f275b92007-10-14 17:54:06 +0100316 /* negated and abbreviated very much? */
Christian Couder59556542013-11-30 21:55:40 +0100317 if (starts_with("no-", arg)) {
Johannes Schindelin7f275b92007-10-14 17:54:06 +0100318 flags |= OPT_UNSET;
319 goto is_abbreviated;
320 }
321 /* negated? */
Christian Couder59556542013-11-30 21:55:40 +0100322 if (!starts_with(arg, "no-")) {
323 if (starts_with(long_name, "no-")) {
René Scharfe0f1930c52012-02-25 20:14:54 +0100324 long_name += 3;
325 opt_flags |= OPT_UNSET;
326 goto again;
327 }
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200328 continue;
René Scharfe0f1930c52012-02-25 20:14:54 +0100329 }
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200330 flags |= OPT_UNSET;
Jeff Kingcf4fff52014-06-18 15:44:19 -0400331 if (!skip_prefix(arg + 3, long_name, &rest)) {
332 /* abbreviated and negated? */
333 if (starts_with(long_name, arg + 3))
334 goto is_abbreviated;
335 else
336 continue;
337 }
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200338 }
339 if (*rest) {
340 if (*rest != '=')
341 continue;
342 p->opt = rest + 1;
343 }
Junio C Hamano11588262013-07-30 12:06:01 -0700344 return get_value(p, options, all_opts, flags ^ opt_flags);
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200345 }
Johannes Schindelin243e0612007-11-05 13:15:21 +0000346
Paul-Sebastian Ungureanu3bb09232018-03-22 20:43:51 +0200347 if (ambiguous_option) {
Nguyễn Thái Ngọc Duy89003422018-11-10 06:16:13 +0100348 error(_("ambiguous option: %s "
349 "(could be --%s%s or --%s%s)"),
Johannes Schindelin243e0612007-11-05 13:15:21 +0000350 arg,
351 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
352 ambiguous_option->long_name,
353 (abbrev_flags & OPT_UNSET) ? "no-" : "",
354 abbrev_option->long_name);
Nguyễn Thái Ngọc Duyf41179f2019-01-27 07:35:27 +0700355 return PARSE_OPT_HELP;
Paul-Sebastian Ungureanu3bb09232018-03-22 20:43:51 +0200356 }
Johannes Schindelin7f275b92007-10-14 17:54:06 +0100357 if (abbrev_option)
Junio C Hamano11588262013-07-30 12:06:01 -0700358 return get_value(p, abbrev_option, all_opts, abbrev_flags);
Nguyễn Thái Ngọc Duyf41179f2019-01-27 07:35:27 +0700359 return PARSE_OPT_UNKNOWN;
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200360}
361
René Scharfe51a99492009-05-07 21:45:42 +0200362static int parse_nodash_opt(struct parse_opt_ctx_t *p, const char *arg,
363 const struct option *options)
364{
Junio C Hamano11588262013-07-30 12:06:01 -0700365 const struct option *all_opts = options;
366
René Scharfe51a99492009-05-07 21:45:42 +0200367 for (; options->type != OPTION_END; options++) {
368 if (!(options->flags & PARSE_OPT_NODASH))
369 continue;
René Scharfe51a99492009-05-07 21:45:42 +0200370 if (options->short_name == arg[0] && arg[1] == '\0')
Junio C Hamano11588262013-07-30 12:06:01 -0700371 return get_value(p, options, all_opts, OPT_SHORT);
René Scharfe51a99492009-05-07 21:45:42 +0200372 }
373 return -2;
374}
375
Nanako Shiraishi1121a872008-07-16 19:42:18 +0900376static void check_typos(const char *arg, const struct option *options)
Pierre Habouzit3a9f0f42008-01-26 12:26:57 +0100377{
378 if (strlen(arg) < 3)
379 return;
380
Christian Couder59556542013-11-30 21:55:40 +0100381 if (starts_with(arg, "no-")) {
Nguyễn Thái Ngọc Duy89003422018-11-10 06:16:13 +0100382 error(_("did you mean `--%s` (with two dashes ?)"), arg);
Pierre Habouzit3a9f0f42008-01-26 12:26:57 +0100383 exit(129);
384 }
385
386 for (; options->type != OPTION_END; options++) {
387 if (!options->long_name)
388 continue;
Christian Couder59556542013-11-30 21:55:40 +0100389 if (starts_with(options->long_name, arg)) {
Nguyễn Thái Ngọc Duy89003422018-11-10 06:16:13 +0100390 error(_("did you mean `--%s` (with two dashes ?)"), arg);
Pierre Habouzit3a9f0f42008-01-26 12:26:57 +0100391 exit(129);
392 }
393 }
394}
395
Pierre Habouzitcb9d3982009-06-09 10:23:44 +0200396static void parse_options_check(const struct option *opts)
397{
398 int err = 0;
Junio C Hamanoaf465af2014-09-03 12:42:37 -0700399 char short_opts[128];
Pierre Habouzitcb9d3982009-06-09 10:23:44 +0200400
Junio C Hamanoaf465af2014-09-03 12:42:37 -0700401 memset(short_opts, '\0', sizeof(short_opts));
Pierre Habouzitcb9d3982009-06-09 10:23:44 +0200402 for (; opts->type != OPTION_END; opts++) {
403 if ((opts->flags & PARSE_OPT_LASTARG_DEFAULT) &&
Jonathan Nieder1e5ce572010-12-02 00:01:18 -0600404 (opts->flags & PARSE_OPT_OPTARG))
405 err |= optbug(opts, "uses incompatible flags "
406 "LASTARG_DEFAULT and OPTARG");
Junio C Hamanoaf465af2014-09-03 12:42:37 -0700407 if (opts->short_name) {
408 if (0x7F <= opts->short_name)
409 err |= optbug(opts, "invalid short name");
410 else if (short_opts[opts->short_name]++)
411 err |= optbug(opts, "short name already used");
412 }
Jonathan Niedera02dd4f2010-12-02 00:05:05 -0600413 if (opts->flags & PARSE_OPT_NODASH &&
414 ((opts->flags & PARSE_OPT_OPTARG) ||
415 !(opts->flags & PARSE_OPT_NOARG) ||
416 !(opts->flags & PARSE_OPT_NONEG) ||
417 opts->long_name))
418 err |= optbug(opts, "uses feature "
419 "not supported for dashless options");
Jonathan Nieder5c400ed2010-12-02 00:08:57 -0600420 switch (opts->type) {
Junio C Hamanob04ba2b2011-09-27 16:56:49 -0700421 case OPTION_COUNTUP:
Jonathan Nieder5c400ed2010-12-02 00:08:57 -0600422 case OPTION_BIT:
423 case OPTION_NEGBIT:
424 case OPTION_SET_INT:
Jonathan Nieder5c400ed2010-12-02 00:08:57 -0600425 case OPTION_NUMBER:
426 if ((opts->flags & PARSE_OPT_OPTARG) ||
427 !(opts->flags & PARSE_OPT_NOARG))
428 err |= optbug(opts, "should not accept an argument");
Nguyễn Thái Ngọc Duybf3ff332019-01-27 07:35:26 +0700429 break;
430 case OPTION_CALLBACK:
Nguyễn Thái Ngọc Duy3ebbe282019-01-27 07:35:28 +0700431 if (!opts->callback && !opts->ll_callback)
432 BUG("OPTION_CALLBACK needs one callback");
433 if (opts->callback && opts->ll_callback)
434 BUG("OPTION_CALLBACK can't have two callbacks");
Nguyễn Thái Ngọc Duybf3ff332019-01-27 07:35:26 +0700435 break;
436 case OPTION_LOWLEVEL_CALLBACK:
437 if (!opts->ll_callback)
438 BUG("OPTION_LOWLEVEL_CALLBACK needs a callback");
439 if (opts->callback)
440 BUG("OPTION_LOWLEVEL_CALLBACK needs no high level callback");
441 break;
Jonathan Nieder5c400ed2010-12-02 00:08:57 -0600442 default:
443 ; /* ok. (usually accepts an argument) */
444 }
Junio C Hamanob6c2a0d2014-03-23 16:04:36 -0700445 if (opts->argh &&
446 strcspn(opts->argh, " _") != strlen(opts->argh))
447 err |= optbug(opts, "multi-word argh should use dash to separate words");
Pierre Habouzitcb9d3982009-06-09 10:23:44 +0200448 }
Pierre Habouzitcb9d3982009-06-09 10:23:44 +0200449 if (err)
Jonathan Nieder1e5ce572010-12-02 00:01:18 -0600450 exit(128);
Pierre Habouzitcb9d3982009-06-09 10:23:44 +0200451}
452
Pierre Habouzit7e7bbcb2008-06-23 21:59:37 +0200453void parse_options_start(struct parse_opt_ctx_t *ctx,
Stephen Boyd37782922009-05-23 11:53:12 -0700454 int argc, const char **argv, const char *prefix,
Stephen Boyd9ca11692010-12-05 23:57:42 -0800455 const struct option *options, int flags)
Pierre Habouzit7e7bbcb2008-06-23 21:59:37 +0200456{
457 memset(ctx, 0, sizeof(*ctx));
Nguyễn Thái Ngọc Duy202fbb32019-01-27 07:35:23 +0700458 ctx->argc = argc;
459 ctx->argv = argv;
460 if (!(flags & PARSE_OPT_ONE_SHOT)) {
461 ctx->argc--;
462 ctx->argv++;
463 }
464 ctx->total = ctx->argc;
465 ctx->out = argv;
Stephen Boyd37782922009-05-23 11:53:12 -0700466 ctx->prefix = prefix;
Pierre Habouzita32a4ea2008-06-24 00:31:31 +0200467 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
Pierre Habouzit7e7bbcb2008-06-23 21:59:37 +0200468 ctx->flags = flags;
René Scharfe0d260f92009-03-09 21:57:38 +0100469 if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
Nguyễn Thái Ngọc Duy202fbb32019-01-27 07:35:23 +0700470 (flags & PARSE_OPT_STOP_AT_NON_OPTION) &&
471 !(flags & PARSE_OPT_ONE_SHOT))
Nguyễn Thái Ngọc Duy48a54992018-11-10 06:16:12 +0100472 BUG("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
Nguyễn Thái Ngọc Duy202fbb32019-01-27 07:35:23 +0700473 if ((flags & PARSE_OPT_ONE_SHOT) &&
474 (flags & PARSE_OPT_KEEP_ARGV0))
475 BUG("Can't keep argv0 if you don't have it");
Stephen Boyd9ca11692010-12-05 23:57:42 -0800476 parse_options_check(options);
Pierre Habouzit7e7bbcb2008-06-23 21:59:37 +0200477}
478
Nguyễn Thái Ngọc Duyb221b5a2018-06-06 11:41:39 +0200479static void show_negated_gitcomp(const struct option *opts, int nr_noopts)
480{
481 int printed_dashdash = 0;
482
483 for (; opts->type != OPTION_END; opts++) {
484 int has_unset_form = 0;
485 const char *name;
486
487 if (!opts->long_name)
488 continue;
489 if (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE))
490 continue;
491 if (opts->flags & PARSE_OPT_NONEG)
492 continue;
493
494 switch (opts->type) {
495 case OPTION_STRING:
496 case OPTION_FILENAME:
497 case OPTION_INTEGER:
498 case OPTION_MAGNITUDE:
499 case OPTION_CALLBACK:
500 case OPTION_BIT:
501 case OPTION_NEGBIT:
502 case OPTION_COUNTUP:
503 case OPTION_SET_INT:
504 has_unset_form = 1;
505 break;
506 default:
507 break;
508 }
509 if (!has_unset_form)
510 continue;
511
512 if (skip_prefix(opts->long_name, "no-", &name)) {
513 if (nr_noopts < 0)
514 printf(" --%s", name);
515 } else if (nr_noopts >= 0) {
516 if (nr_noopts && !printed_dashdash) {
517 printf(" --");
518 printed_dashdash = 1;
519 }
520 printf(" --no-%s", opts->long_name);
521 nr_noopts++;
522 }
523 }
524}
525
Nguyễn Thái Ngọc Duyb9d7f4b2018-02-09 18:01:40 +0700526static int show_gitcomp(struct parse_opt_ctx_t *ctx,
527 const struct option *opts)
528{
Nguyễn Thái Ngọc Duyb221b5a2018-06-06 11:41:39 +0200529 const struct option *original_opts = opts;
530 int nr_noopts = 0;
531
Nguyễn Thái Ngọc Duyb9d7f4b2018-02-09 18:01:40 +0700532 for (; opts->type != OPTION_END; opts++) {
533 const char *suffix = "";
534
535 if (!opts->long_name)
536 continue;
537 if (opts->flags & (PARSE_OPT_HIDDEN | PARSE_OPT_NOCOMPLETE))
538 continue;
539
540 switch (opts->type) {
541 case OPTION_GROUP:
542 continue;
543 case OPTION_STRING:
544 case OPTION_FILENAME:
545 case OPTION_INTEGER:
546 case OPTION_MAGNITUDE:
547 case OPTION_CALLBACK:
548 if (opts->flags & PARSE_OPT_NOARG)
549 break;
550 if (opts->flags & PARSE_OPT_OPTARG)
551 break;
552 if (opts->flags & PARSE_OPT_LASTARG_DEFAULT)
553 break;
554 suffix = "=";
555 break;
556 default:
557 break;
558 }
Nguyễn Thái Ngọc Duyebc4a042018-02-09 18:02:12 +0700559 if (opts->flags & PARSE_OPT_COMP_ARG)
560 suffix = "=";
Nguyễn Thái Ngọc Duyb221b5a2018-06-06 11:41:39 +0200561 if (starts_with(opts->long_name, "no-"))
562 nr_noopts++;
Nguyễn Thái Ngọc Duyb9d7f4b2018-02-09 18:01:40 +0700563 printf(" --%s%s", opts->long_name, suffix);
564 }
Nguyễn Thái Ngọc Duyb221b5a2018-06-06 11:41:39 +0200565 show_negated_gitcomp(original_opts, -1);
566 show_negated_gitcomp(original_opts, nr_noopts);
Nguyễn Thái Ngọc Duyb9d7f4b2018-02-09 18:01:40 +0700567 fputc('\n', stdout);
Nguyễn Thái Ngọc Duya92ec7e2018-12-11 16:35:01 +0100568 return PARSE_OPT_COMPLETE;
Nguyễn Thái Ngọc Duyb9d7f4b2018-02-09 18:01:40 +0700569}
570
Thomas Rast47e9cd22010-06-12 14:57:39 +0200571static int usage_with_options_internal(struct parse_opt_ctx_t *,
572 const char * const *,
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200573 const struct option *, int, int);
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200574
575int parse_options_step(struct parse_opt_ctx_t *ctx,
576 const struct option *options,
577 const char * const usagestr[])
578{
René Scharfeb92891f2009-03-08 19:15:08 +0100579 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
580
Pierre Habouzit26141b52008-06-23 22:55:11 +0200581 /* we must reset ->opt, unknown short option leave it dangling */
582 ctx->opt = NULL;
583
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200584 for (; ctx->argc; ctx->argc--, ctx->argv++) {
585 const char *arg = ctx->argv[0];
586
Nguyễn Thái Ngọc Duy202fbb32019-01-27 07:35:23 +0700587 if (ctx->flags & PARSE_OPT_ONE_SHOT &&
588 ctx->argc != ctx->total)
589 break;
590
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200591 if (*arg != '-' || !arg[1]) {
René Scharfe51a99492009-05-07 21:45:42 +0200592 if (parse_nodash_opt(ctx, arg, options) == 0)
593 continue;
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200594 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
Jonathan Nieder979240f2010-12-01 17:32:55 -0600595 return PARSE_OPT_NON_OPTION;
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200596 ctx->out[ctx->cpidx++] = ctx->argv[0];
597 continue;
598 }
599
René Scharfe5ad0d3d2015-11-17 11:25:38 +0100600 /* lone -h asks for help */
601 if (internal_help && ctx->total == 1 && !strcmp(arg + 1, "h"))
602 goto show_usage;
603
Nguyễn Thái Ngọc Duyb9d7f4b2018-02-09 18:01:40 +0700604 /* lone --git-completion-helper is asked by git-completion.bash */
605 if (ctx->total == 1 && !strcmp(arg + 1, "-git-completion-helper"))
606 return show_gitcomp(ctx, options);
607
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200608 if (arg[1] != '-') {
609 ctx->opt = arg + 1;
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200610 switch (parse_short_opt(ctx, options)) {
Nguyễn Thái Ngọc Duyf41179f2019-01-27 07:35:27 +0700611 case PARSE_OPT_ERROR:
Paul-Sebastian Ungureanu3bb09232018-03-22 20:43:51 +0200612 return PARSE_OPT_ERROR;
Nguyễn Thái Ngọc Duyf41179f2019-01-27 07:35:27 +0700613 case PARSE_OPT_UNKNOWN:
René Scharfe38916c52012-03-03 12:00:29 +0100614 if (ctx->opt)
615 check_typos(arg + 1, options);
René Scharfe5ad0d3d2015-11-17 11:25:38 +0100616 if (internal_help && *ctx->opt == 'h')
617 goto show_usage;
René Scharfeb5ce3a52009-03-08 19:12:47 +0100618 goto unknown;
Nguyễn Thái Ngọc Duyf41179f2019-01-27 07:35:27 +0700619 case PARSE_OPT_NON_OPTION:
620 case PARSE_OPT_HELP:
621 case PARSE_OPT_COMPLETE:
622 BUG("parse_short_opt() cannot return these");
623 case PARSE_OPT_DONE:
624 break;
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200625 }
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200626 if (ctx->opt)
627 check_typos(arg + 1, options);
628 while (ctx->opt) {
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200629 switch (parse_short_opt(ctx, options)) {
Nguyễn Thái Ngọc Duyf41179f2019-01-27 07:35:27 +0700630 case PARSE_OPT_ERROR:
Paul-Sebastian Ungureanu3bb09232018-03-22 20:43:51 +0200631 return PARSE_OPT_ERROR;
Nguyễn Thái Ngọc Duyf41179f2019-01-27 07:35:27 +0700632 case PARSE_OPT_UNKNOWN:
René Scharfe5ad0d3d2015-11-17 11:25:38 +0100633 if (internal_help && *ctx->opt == 'h')
634 goto show_usage;
635
Pierre Habouzit26141b52008-06-23 22:55:11 +0200636 /* fake a short option thing to hide the fact that we may have
637 * started to parse aggregated stuff
638 *
639 * This is leaky, too bad.
640 */
641 ctx->argv[0] = xstrdup(ctx->opt - 1);
642 *(char *)ctx->argv[0] = '-';
René Scharfeb5ce3a52009-03-08 19:12:47 +0100643 goto unknown;
Nguyễn Thái Ngọc Duyf41179f2019-01-27 07:35:27 +0700644 case PARSE_OPT_NON_OPTION:
645 case PARSE_OPT_COMPLETE:
646 case PARSE_OPT_HELP:
647 BUG("parse_short_opt() cannot return these");
648 case PARSE_OPT_DONE:
649 break;
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200650 }
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200651 }
652 continue;
653 }
654
655 if (!arg[2]) { /* "--" */
656 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
657 ctx->argc--;
658 ctx->argv++;
659 }
660 break;
661 }
662
René Scharfeb92891f2009-03-08 19:15:08 +0100663 if (internal_help && !strcmp(arg + 2, "help-all"))
Thomas Rast47e9cd22010-06-12 14:57:39 +0200664 return usage_with_options_internal(ctx, usagestr, options, 1, 0);
René Scharfeb92891f2009-03-08 19:15:08 +0100665 if (internal_help && !strcmp(arg + 2, "help"))
René Scharfeac20ff62015-11-17 11:25:14 +0100666 goto show_usage;
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200667 switch (parse_long_opt(ctx, arg + 2, options)) {
Nguyễn Thái Ngọc Duyf41179f2019-01-27 07:35:27 +0700668 case PARSE_OPT_ERROR:
Paul-Sebastian Ungureanu3bb09232018-03-22 20:43:51 +0200669 return PARSE_OPT_ERROR;
Nguyễn Thái Ngọc Duyf41179f2019-01-27 07:35:27 +0700670 case PARSE_OPT_UNKNOWN:
René Scharfeb5ce3a52009-03-08 19:12:47 +0100671 goto unknown;
Nguyễn Thái Ngọc Duyf41179f2019-01-27 07:35:27 +0700672 case PARSE_OPT_HELP:
Paul-Sebastian Ungureanu3bb09232018-03-22 20:43:51 +0200673 goto show_usage;
Nguyễn Thái Ngọc Duyf41179f2019-01-27 07:35:27 +0700674 case PARSE_OPT_NON_OPTION:
675 case PARSE_OPT_COMPLETE:
676 BUG("parse_long_opt() cannot return these");
677 case PARSE_OPT_DONE:
678 break;
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200679 }
René Scharfeb5ce3a52009-03-08 19:12:47 +0100680 continue;
681unknown:
Nguyễn Thái Ngọc Duy202fbb32019-01-27 07:35:23 +0700682 if (ctx->flags & PARSE_OPT_ONE_SHOT)
683 break;
René Scharfeb5ce3a52009-03-08 19:12:47 +0100684 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
685 return PARSE_OPT_UNKNOWN;
686 ctx->out[ctx->cpidx++] = ctx->argv[0];
687 ctx->opt = NULL;
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200688 }
689 return PARSE_OPT_DONE;
René Scharfeac20ff62015-11-17 11:25:14 +0100690
René Scharfeac20ff62015-11-17 11:25:14 +0100691 show_usage:
Paul-Sebastian Ungureanu3bb09232018-03-22 20:43:51 +0200692 return usage_with_options_internal(ctx, usagestr, options, 0, 0);
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200693}
694
Pierre Habouzit7e7bbcb2008-06-23 21:59:37 +0200695int parse_options_end(struct parse_opt_ctx_t *ctx)
696{
Nguyễn Thái Ngọc Duy202fbb32019-01-27 07:35:23 +0700697 if (ctx->flags & PARSE_OPT_ONE_SHOT)
698 return ctx->total - ctx->argc;
699
SZEDER Gáborf919ffe2018-01-22 18:50:09 +0100700 MOVE_ARRAY(ctx->out + ctx->cpidx, ctx->argv, ctx->argc);
Pierre Habouzit7e7bbcb2008-06-23 21:59:37 +0200701 ctx->out[ctx->cpidx + ctx->argc] = NULL;
702 return ctx->cpidx + ctx->argc;
703}
704
Stephen Boyd37782922009-05-23 11:53:12 -0700705int parse_options(int argc, const char **argv, const char *prefix,
706 const struct option *options, const char * const usagestr[],
707 int flags)
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200708{
Pierre Habouzit7e7bbcb2008-06-23 21:59:37 +0200709 struct parse_opt_ctx_t ctx;
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200710
Stephen Boyd9ca11692010-12-05 23:57:42 -0800711 parse_options_start(&ctx, argc, argv, prefix, options, flags);
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200712 switch (parse_options_step(&ctx, options, usagestr)) {
713 case PARSE_OPT_HELP:
Paul-Sebastian Ungureanu3bb09232018-03-22 20:43:51 +0200714 case PARSE_OPT_ERROR:
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200715 exit(129);
Nguyễn Thái Ngọc Duya92ec7e2018-12-11 16:35:01 +0100716 case PARSE_OPT_COMPLETE:
717 exit(0);
Jonathan Nieder979240f2010-12-01 17:32:55 -0600718 case PARSE_OPT_NON_OPTION:
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200719 case PARSE_OPT_DONE:
720 break;
721 default: /* PARSE_OPT_UNKNOWN */
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200722 if (ctx.argv[0][1] == '-') {
Nguyễn Thái Ngọc Duy89003422018-11-10 06:16:13 +0100723 error(_("unknown option `%s'"), ctx.argv[0] + 2);
Erik Faye-Lundb141a472013-02-12 00:13:48 +0100724 } else if (isascii(*ctx.opt)) {
Nguyễn Thái Ngọc Duy89003422018-11-10 06:16:13 +0100725 error(_("unknown switch `%c'"), *ctx.opt);
Erik Faye-Lundb141a472013-02-12 00:13:48 +0100726 } else {
Nguyễn Thái Ngọc Duy89003422018-11-10 06:16:13 +0100727 error(_("unknown non-ascii option in string: `%s'"),
Erik Faye-Lundb141a472013-02-12 00:13:48 +0100728 ctx.argv[0]);
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200729 }
730 usage_with_options(usagestr, options);
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200731 }
732
Torsten Bögershausen76759c72012-07-08 15:50:25 +0200733 precompose_argv(argc, argv);
Pierre Habouzit7e7bbcb2008-06-23 21:59:37 +0200734 return parse_options_end(&ctx);
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200735}
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200736
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200737static int usage_argh(const struct option *opts, FILE *outfile)
Stephen Boyd29f25d42009-05-21 00:33:17 -0700738{
739 const char *s;
René Scharfe5f0df442018-08-02 21:18:14 +0200740 int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
741 !opts->argh || !!strpbrk(opts->argh, "()<>[]|");
Stephen Boyd29f25d42009-05-21 00:33:17 -0700742 if (opts->flags & PARSE_OPT_OPTARG)
743 if (opts->long_name)
744 s = literal ? "[=%s]" : "[=<%s>]";
745 else
746 s = literal ? "[%s]" : "[<%s>]";
747 else
748 s = literal ? " %s" : " <%s>";
Jiang Xinc0821962013-02-09 14:31:09 +0800749 return utf8_fprintf(outfile, s, opts->argh ? _(opts->argh) : _("..."));
Stephen Boyd29f25d42009-05-21 00:33:17 -0700750}
751
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200752#define USAGE_OPTS_WIDTH 24
753#define USAGE_GAP 2
754
Thomas Rast47e9cd22010-06-12 14:57:39 +0200755static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
756 const char * const *usagestr,
757 const struct option *opts, int full, int err)
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200758{
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200759 FILE *outfile = err ? stderr : stdout;
Brandon Caseya6304fa2017-09-24 21:08:05 -0700760 int need_newline;
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200761
René Scharfe49b61802009-03-08 19:16:58 +0100762 if (!usagestr)
763 return PARSE_OPT_HELP;
764
Thomas Rast47e9cd22010-06-12 14:57:39 +0200765 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
766 fprintf(outfile, "cat <<\\EOF\n");
767
Nguyễn Thái Ngọc Duy54e6dc72012-05-06 21:23:51 +0700768 fprintf_ln(outfile, _("usage: %s"), _(*usagestr++));
Alex Riesenf389c802007-10-14 00:10:51 +0200769 while (*usagestr && **usagestr)
Ævar Arnfjörð Bjarmason66f5f6d2017-05-11 21:20:12 +0000770 /*
771 * TRANSLATORS: the colon here should align with the
772 * one in "usage: %s" translation.
773 */
Nguyễn Thái Ngọc Duy54e6dc72012-05-06 21:23:51 +0700774 fprintf_ln(outfile, _(" or: %s"), _(*usagestr++));
Jeff King44d86e92008-06-14 03:27:21 -0400775 while (*usagestr) {
Nguyễn Thái Ngọc Duy54e6dc72012-05-06 21:23:51 +0700776 if (**usagestr)
777 fprintf_ln(outfile, _(" %s"), _(*usagestr));
778 else
Brandon Casey1a9bf1e2017-09-24 21:08:04 -0700779 fputc('\n', outfile);
Jeff King44d86e92008-06-14 03:27:21 -0400780 usagestr++;
781 }
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200782
Brandon Caseya6304fa2017-09-24 21:08:05 -0700783 need_newline = 1;
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200784
785 for (; opts->type != OPTION_END; opts++) {
786 size_t pos;
787 int pad;
788
789 if (opts->type == OPTION_GROUP) {
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200790 fputc('\n', outfile);
Brandon Caseya6304fa2017-09-24 21:08:05 -0700791 need_newline = 0;
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200792 if (*opts->help)
Nguyễn Thái Ngọc Duy54e6dc72012-05-06 21:23:51 +0700793 fprintf(outfile, "%s\n", _(opts->help));
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200794 continue;
795 }
Pierre Habouzitdd3bf0f2007-11-19 10:21:44 +0100796 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
797 continue;
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200798
Brandon Caseya6304fa2017-09-24 21:08:05 -0700799 if (need_newline) {
800 fputc('\n', outfile);
801 need_newline = 0;
802 }
803
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200804 pos = fprintf(outfile, " ");
René Scharfecbb08c22012-02-28 20:06:09 +0100805 if (opts->short_name) {
René Scharfe51a99492009-05-07 21:45:42 +0200806 if (opts->flags & PARSE_OPT_NODASH)
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200807 pos += fprintf(outfile, "%c", opts->short_name);
René Scharfe51a99492009-05-07 21:45:42 +0200808 else
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200809 pos += fprintf(outfile, "-%c", opts->short_name);
René Scharfe51a99492009-05-07 21:45:42 +0200810 }
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200811 if (opts->long_name && opts->short_name)
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200812 pos += fprintf(outfile, ", ");
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200813 if (opts->long_name)
René Scharfecbb08c22012-02-28 20:06:09 +0100814 pos += fprintf(outfile, "--%s", opts->long_name);
René Scharfee0319ff2009-05-07 21:45:08 +0200815 if (opts->type == OPTION_NUMBER)
Jiang Xinc0821962013-02-09 14:31:09 +0800816 pos += utf8_fprintf(outfile, _("-NUM"));
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200817
Jonathan Niederb57c68a2010-12-01 17:31:36 -0600818 if ((opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
819 !(opts->flags & PARSE_OPT_NOARG))
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200820 pos += usage_argh(opts, outfile);
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200821
Alex Riesenf389c802007-10-14 00:10:51 +0200822 if (pos <= USAGE_OPTS_WIDTH)
823 pad = USAGE_OPTS_WIDTH - pos;
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200824 else {
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200825 fputc('\n', outfile);
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200826 pad = USAGE_OPTS_WIDTH;
827 }
Nguyễn Thái Ngọc Duy54e6dc72012-05-06 21:23:51 +0700828 fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", _(opts->help));
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200829 }
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200830 fputc('\n', outfile);
Alex Riesenf389c802007-10-14 00:10:51 +0200831
Thomas Rast47e9cd22010-06-12 14:57:39 +0200832 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
833 fputs("EOF\n", outfile);
834
Pierre Habouzitee68b872008-06-23 22:28:04 +0200835 return PARSE_OPT_HELP;
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200836}
Pierre Habouzit0ce865b2007-10-14 11:05:12 +0200837
Stephen Boydc2e86ad2011-03-22 00:51:05 -0700838void NORETURN usage_with_options(const char * const *usagestr,
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200839 const struct option *opts)
Pierre Habouzitdd3bf0f2007-11-19 10:21:44 +0100840{
Thomas Rast47e9cd22010-06-12 14:57:39 +0200841 usage_with_options_internal(NULL, usagestr, opts, 0, 1);
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200842 exit(129);
Pierre Habouzitdd3bf0f2007-11-19 10:21:44 +0100843}
844
Stephen Boydc2e86ad2011-03-22 00:51:05 -0700845void NORETURN usage_msg_opt(const char *msg,
Christian Couder451bb212009-02-02 06:12:58 +0100846 const char * const *usagestr,
847 const struct option *options)
848{
Jeff King87433262016-12-14 10:10:10 -0500849 fprintf(stderr, "fatal: %s\n\n", msg);
Christian Couder451bb212009-02-02 06:12:58 +0100850 usage_with_options(usagestr, options);
851}
852
Nguyễn Thái Ngọc Duy9440b832018-11-10 06:16:11 +0100853const char *optname(const struct option *opt, int flags)
Jeff Kinga469a102012-12-15 12:42:10 -0500854{
Nguyễn Thái Ngọc Duy9440b832018-11-10 06:16:11 +0100855 static struct strbuf sb = STRBUF_INIT;
856
857 strbuf_reset(&sb);
Jeff Kinga469a102012-12-15 12:42:10 -0500858 if (flags & OPT_SHORT)
Nguyễn Thái Ngọc Duy9440b832018-11-10 06:16:11 +0100859 strbuf_addf(&sb, "switch `%c'", opt->short_name);
860 else if (flags & OPT_UNSET)
861 strbuf_addf(&sb, "option `no-%s'", opt->long_name);
862 else
863 strbuf_addf(&sb, "option `%s'", opt->long_name);
864
865 return sb.buf;
Jeff Kinga469a102012-12-15 12:42:10 -0500866}