blob: 7b8d3faa171309e84bff23c70206a9c8bd99cc71 [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"
Jake Goulding269defd2009-01-26 09:13:23 -05004#include "commit.h"
Mark Lodato73e9da02010-02-16 23:55:58 -05005#include "color.h"
Jiang Xinc0821962013-02-09 14:31:09 +08006#include "utf8.h"
Pierre Habouzit4a59fd12007-10-15 01:35:37 +02007
Thomas Rast47e9cd22010-06-12 14:57:39 +02008static int parse_options_usage(struct parse_opt_ctx_t *ctx,
9 const char * const *usagestr,
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +020010 const struct option *opts, int err);
Junio C Hamano41064eb2010-01-11 22:28:45 -080011
Pierre Habouzit4a59fd12007-10-15 01:35:37 +020012#define OPT_SHORT 1
13#define OPT_UNSET 2
14
Dmitry Ivankov1f275b72011-08-11 15:15:37 +060015int optbug(const struct option *opt, const char *reason)
Jonathan Nieder1e5ce572010-12-02 00:01:18 -060016{
17 if (opt->long_name)
18 return error("BUG: option '%s' %s", opt->long_name, reason);
19 return error("BUG: switch '%c' %s", opt->short_name, reason);
20}
21
Pierre Habouzit1cc69852008-07-08 12:34:08 +020022static int get_arg(struct parse_opt_ctx_t *p, const struct option *opt,
23 int flags, const char **arg)
24{
25 if (p->opt) {
26 *arg = p->opt;
27 p->opt = NULL;
28 } else if (p->argc == 1 && (opt->flags & PARSE_OPT_LASTARG_DEFAULT)) {
29 *arg = (const char *)opt->defval;
Olivier Marind5d745f2008-07-21 20:30:36 +020030 } else if (p->argc > 1) {
Pierre Habouzit1cc69852008-07-08 12:34:08 +020031 p->argc--;
32 *arg = *++p->argv;
33 } else
34 return opterror(opt, "requires a value", flags);
35 return 0;
36}
37
Stephen Boyddf217ed2009-05-23 11:53:13 -070038static void fix_filename(const char *prefix, const char **file)
39{
40 if (!file || !*file || !prefix || is_absolute_path(*file)
41 || !strcmp("-", *file))
42 return;
43 *file = xstrdup(prefix_filename(prefix, strlen(prefix), *file));
44}
45
Junio C Hamano11588262013-07-30 12:06:01 -070046static int opt_command_mode_error(const struct option *opt,
47 const struct option *all_opts,
48 int flags)
49{
50 const struct option *that;
51 struct strbuf message = STRBUF_INIT;
52 struct strbuf that_name = STRBUF_INIT;
53
54 /*
55 * Find the other option that was used to set the variable
56 * already, and report that this is not compatible with it.
57 */
58 for (that = all_opts; that->type != OPTION_END; that++) {
59 if (that == opt ||
60 that->type != OPTION_CMDMODE ||
61 that->value != opt->value ||
62 that->defval != *(int *)opt->value)
63 continue;
64
65 if (that->long_name)
66 strbuf_addf(&that_name, "--%s", that->long_name);
67 else
68 strbuf_addf(&that_name, "-%c", that->short_name);
69 strbuf_addf(&message, ": incompatible with %s", that_name.buf);
70 strbuf_release(&that_name);
71 opterror(opt, message.buf, flags);
72 strbuf_release(&message);
73 return -1;
74 }
75 return opterror(opt, ": incompatible with something else", flags);
76}
77
Pierre Habouzit7e7bbcb2008-06-23 21:59:37 +020078static int get_value(struct parse_opt_ctx_t *p,
Junio C Hamano11588262013-07-30 12:06:01 -070079 const struct option *opt,
80 const struct option *all_opts,
81 int flags)
Pierre Habouzit4a59fd12007-10-15 01:35:37 +020082{
Pierre Habouzitffe659f2007-10-15 01:45:45 +020083 const char *s, *arg;
Pierre Habouzitdb7244b2007-11-07 11:20:27 +010084 const int unset = flags & OPT_UNSET;
Stephen Boyddf217ed2009-05-23 11:53:13 -070085 int err;
Pierre Habouzit4a59fd12007-10-15 01:35:37 +020086
Pierre Habouzitdb7244b2007-11-07 11:20:27 +010087 if (unset && p->opt)
Pierre Habouzit4a59fd12007-10-15 01:35:37 +020088 return opterror(opt, "takes no value", flags);
Pierre Habouzitdb7244b2007-11-07 11:20:27 +010089 if (unset && (opt->flags & PARSE_OPT_NONEG))
90 return opterror(opt, "isn't available", flags);
Stephen Boydc1f4ec92010-12-01 17:30:40 -060091 if (!(flags & OPT_SHORT) && p->opt && (opt->flags & PARSE_OPT_NOARG))
92 return opterror(opt, "takes no value", flags);
Pierre Habouzitdb7244b2007-11-07 11:20:27 +010093
Pierre Habouzitdb7244b2007-11-07 11:20:27 +010094 switch (opt->type) {
Jonathan Niederb0b3a8b2010-12-01 17:32:16 -060095 case OPTION_LOWLEVEL_CALLBACK:
96 return (*(parse_opt_ll_cb *)opt->callback)(p, opt, unset);
97
Pierre Habouzitdb7244b2007-11-07 11:20:27 +010098 case OPTION_BIT:
99 if (unset)
100 *(int *)opt->value &= ~opt->defval;
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200101 else
Pierre Habouzitdb7244b2007-11-07 11:20:27 +0100102 *(int *)opt->value |= opt->defval;
103 return 0;
104
René Scharfe2f4b97f2009-05-07 21:44:17 +0200105 case OPTION_NEGBIT:
106 if (unset)
107 *(int *)opt->value |= opt->defval;
108 else
109 *(int *)opt->value &= ~opt->defval;
110 return 0;
111
Junio C Hamanob04ba2b2011-09-27 16:56:49 -0700112 case OPTION_COUNTUP:
Pierre Habouzitdb7244b2007-11-07 11:20:27 +0100113 *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
114 return 0;
115
116 case OPTION_SET_INT:
117 *(int *)opt->value = unset ? 0 : opt->defval;
118 return 0;
119
Junio C Hamano11588262013-07-30 12:06:01 -0700120 case OPTION_CMDMODE:
121 /*
122 * Giving the same mode option twice, although is unnecessary,
123 * is not a grave error, so let it pass.
124 */
125 if (*(int *)opt->value && *(int *)opt->value != opt->defval)
126 return opt_command_mode_error(opt, all_opts, flags);
127 *(int *)opt->value = opt->defval;
128 return 0;
129
Pierre Habouzitdb7244b2007-11-07 11:20:27 +0100130 case OPTION_SET_PTR:
131 *(void **)opt->value = unset ? NULL : (void *)opt->defval;
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200132 return 0;
133
134 case OPTION_STRING:
Pierre Habouzit1cc69852008-07-08 12:34:08 +0200135 if (unset)
Pierre Habouzitdb7244b2007-11-07 11:20:27 +0100136 *(const char **)opt->value = NULL;
Pierre Habouzit1cc69852008-07-08 12:34:08 +0200137 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
Pierre Habouzitffe659f2007-10-15 01:45:45 +0200138 *(const char **)opt->value = (const char *)opt->defval;
Pierre Habouzit1cc69852008-07-08 12:34:08 +0200139 else
140 return get_arg(p, opt, flags, (const char **)opt->value);
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200141 return 0;
142
Stephen Boyddf217ed2009-05-23 11:53:13 -0700143 case OPTION_FILENAME:
144 err = 0;
145 if (unset)
146 *(const char **)opt->value = NULL;
147 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
148 *(const char **)opt->value = (const char *)opt->defval;
149 else
150 err = get_arg(p, opt, flags, (const char **)opt->value);
151
152 if (!err)
153 fix_filename(p->prefix, (const char **)opt->value);
154 return err;
155
Pierre Habouzitffe659f2007-10-15 01:45:45 +0200156 case OPTION_CALLBACK:
Pierre Habouzitdb7244b2007-11-07 11:20:27 +0100157 if (unset)
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200158 return (*opt->callback)(opt, NULL, 1) ? (-1) : 0;
Pierre Habouzitdb7244b2007-11-07 11:20:27 +0100159 if (opt->flags & PARSE_OPT_NOARG)
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200160 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
Pierre Habouzitc43a2482007-12-21 11:41:41 +0100161 if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200162 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
Pierre Habouzit1cc69852008-07-08 12:34:08 +0200163 if (get_arg(p, opt, flags, &arg))
164 return -1;
165 return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
Pierre Habouzitffe659f2007-10-15 01:45:45 +0200166
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200167 case OPTION_INTEGER:
Pierre Habouzitdb7244b2007-11-07 11:20:27 +0100168 if (unset) {
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200169 *(int *)opt->value = 0;
170 return 0;
171 }
Pierre Habouzitc43a2482007-12-21 11:41:41 +0100172 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
Pierre Habouzitffe659f2007-10-15 01:45:45 +0200173 *(int *)opt->value = opt->defval;
174 return 0;
175 }
Pierre Habouzit1cc69852008-07-08 12:34:08 +0200176 if (get_arg(p, opt, flags, &arg))
177 return -1;
178 *(int *)opt->value = strtol(arg, (char **)&s, 10);
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200179 if (*s)
180 return opterror(opt, "expects a numerical value", flags);
181 return 0;
182
183 default:
184 die("should not happen, someone must be hit on the forehead");
185 }
186}
187
Pierre Habouzit7e7bbcb2008-06-23 21:59:37 +0200188static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200189{
Junio C Hamano11588262013-07-30 12:06:01 -0700190 const struct option *all_opts = options;
René Scharfee0319ff2009-05-07 21:45:08 +0200191 const struct option *numopt = NULL;
192
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200193 for (; options->type != OPTION_END; options++) {
194 if (options->short_name == *p->opt) {
195 p->opt = p->opt[1] ? p->opt + 1 : NULL;
Junio C Hamano11588262013-07-30 12:06:01 -0700196 return get_value(p, options, all_opts, OPT_SHORT);
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200197 }
René Scharfee0319ff2009-05-07 21:45:08 +0200198
199 /*
200 * Handle the numerical option later, explicit one-digit
201 * options take precedence over it.
202 */
203 if (options->type == OPTION_NUMBER)
204 numopt = options;
205 }
206 if (numopt && isdigit(*p->opt)) {
207 size_t len = 1;
208 char *arg;
209 int rc;
210
211 while (isdigit(p->opt[len]))
212 len++;
213 arg = xmemdupz(p->opt, len);
214 p->opt = p->opt[len] ? p->opt + len : NULL;
215 rc = (*numopt->callback)(numopt, arg, 0) ? (-1) : 0;
216 free(arg);
217 return rc;
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200218 }
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200219 return -2;
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200220}
221
Pierre Habouzit7e7bbcb2008-06-23 21:59:37 +0200222static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200223 const struct option *options)
224{
Junio C Hamano11588262013-07-30 12:06:01 -0700225 const struct option *all_opts = options;
Johannes Schindelin7f275b92007-10-14 17:54:06 +0100226 const char *arg_end = strchr(arg, '=');
Johannes Schindelin243e0612007-11-05 13:15:21 +0000227 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
228 int abbrev_flags = 0, ambiguous_flags = 0;
Johannes Schindelin7f275b92007-10-14 17:54:06 +0100229
230 if (!arg_end)
231 arg_end = arg + strlen(arg);
232
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200233 for (; options->type != OPTION_END; options++) {
René Scharfe0f1930c52012-02-25 20:14:54 +0100234 const char *rest, *long_name = options->long_name;
235 int flags = 0, opt_flags = 0;
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200236
René Scharfe0f1930c52012-02-25 20:14:54 +0100237 if (!long_name)
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200238 continue;
239
René Scharfe0f1930c52012-02-25 20:14:54 +0100240again:
241 rest = skip_prefix(arg, long_name);
Pierre Habouzit580d5bf2008-03-02 11:35:56 +0100242 if (options->type == OPTION_ARGUMENT) {
243 if (!rest)
244 continue;
245 if (*rest == '=')
246 return opterror(options, "takes no value", flags);
247 if (*rest)
248 continue;
249 p->out[p->cpidx++] = arg - 2;
250 return 0;
251 }
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200252 if (!rest) {
Johannes Schindelin7f275b92007-10-14 17:54:06 +0100253 /* abbreviated? */
René Scharfe0f1930c52012-02-25 20:14:54 +0100254 if (!strncmp(long_name, arg, arg_end - arg)) {
Johannes Schindelin7f275b92007-10-14 17:54:06 +0100255is_abbreviated:
Johannes Schindelin243e0612007-11-05 13:15:21 +0000256 if (abbrev_option) {
257 /*
258 * If this is abbreviated, it is
259 * ambiguous. So when there is no
260 * exact match later, we need to
261 * error out.
262 */
263 ambiguous_option = abbrev_option;
264 ambiguous_flags = abbrev_flags;
265 }
Johannes Schindelin7f275b92007-10-14 17:54:06 +0100266 if (!(flags & OPT_UNSET) && *arg_end)
267 p->opt = arg_end + 1;
268 abbrev_option = options;
René Scharfe0f1930c52012-02-25 20:14:54 +0100269 abbrev_flags = flags ^ opt_flags;
Johannes Schindelin7f275b92007-10-14 17:54:06 +0100270 continue;
271 }
Andreas Schwab6bbfd1f2009-09-25 20:44:44 +0200272 /* negation allowed? */
273 if (options->flags & PARSE_OPT_NONEG)
274 continue;
Johannes Schindelin7f275b92007-10-14 17:54:06 +0100275 /* negated and abbreviated very much? */
Christian Couder59556542013-11-30 21:55:40 +0100276 if (starts_with("no-", arg)) {
Johannes Schindelin7f275b92007-10-14 17:54:06 +0100277 flags |= OPT_UNSET;
278 goto is_abbreviated;
279 }
280 /* negated? */
Christian Couder59556542013-11-30 21:55:40 +0100281 if (!starts_with(arg, "no-")) {
282 if (starts_with(long_name, "no-")) {
René Scharfe0f1930c52012-02-25 20:14:54 +0100283 long_name += 3;
284 opt_flags |= OPT_UNSET;
285 goto again;
286 }
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200287 continue;
René Scharfe0f1930c52012-02-25 20:14:54 +0100288 }
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200289 flags |= OPT_UNSET;
René Scharfe0f1930c52012-02-25 20:14:54 +0100290 rest = skip_prefix(arg + 3, long_name);
Johannes Schindelin7f275b92007-10-14 17:54:06 +0100291 /* abbreviated and negated? */
Christian Couder59556542013-11-30 21:55:40 +0100292 if (!rest && starts_with(long_name, arg + 3))
Johannes Schindelin7f275b92007-10-14 17:54:06 +0100293 goto is_abbreviated;
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200294 if (!rest)
295 continue;
296 }
297 if (*rest) {
298 if (*rest != '=')
299 continue;
300 p->opt = rest + 1;
301 }
Junio C Hamano11588262013-07-30 12:06:01 -0700302 return get_value(p, options, all_opts, flags ^ opt_flags);
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200303 }
Johannes Schindelin243e0612007-11-05 13:15:21 +0000304
305 if (ambiguous_option)
306 return error("Ambiguous option: %s "
307 "(could be --%s%s or --%s%s)",
308 arg,
309 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
310 ambiguous_option->long_name,
311 (abbrev_flags & OPT_UNSET) ? "no-" : "",
312 abbrev_option->long_name);
Johannes Schindelin7f275b92007-10-14 17:54:06 +0100313 if (abbrev_option)
Junio C Hamano11588262013-07-30 12:06:01 -0700314 return get_value(p, abbrev_option, all_opts, abbrev_flags);
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200315 return -2;
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200316}
317
René Scharfe51a99492009-05-07 21:45:42 +0200318static int parse_nodash_opt(struct parse_opt_ctx_t *p, const char *arg,
319 const struct option *options)
320{
Junio C Hamano11588262013-07-30 12:06:01 -0700321 const struct option *all_opts = options;
322
René Scharfe51a99492009-05-07 21:45:42 +0200323 for (; options->type != OPTION_END; options++) {
324 if (!(options->flags & PARSE_OPT_NODASH))
325 continue;
René Scharfe51a99492009-05-07 21:45:42 +0200326 if (options->short_name == arg[0] && arg[1] == '\0')
Junio C Hamano11588262013-07-30 12:06:01 -0700327 return get_value(p, options, all_opts, OPT_SHORT);
René Scharfe51a99492009-05-07 21:45:42 +0200328 }
329 return -2;
330}
331
Nanako Shiraishi1121a872008-07-16 19:42:18 +0900332static void check_typos(const char *arg, const struct option *options)
Pierre Habouzit3a9f0f42008-01-26 12:26:57 +0100333{
334 if (strlen(arg) < 3)
335 return;
336
Christian Couder59556542013-11-30 21:55:40 +0100337 if (starts_with(arg, "no-")) {
Pierre Habouzit3a9f0f42008-01-26 12:26:57 +0100338 error ("did you mean `--%s` (with two dashes ?)", arg);
339 exit(129);
340 }
341
342 for (; options->type != OPTION_END; options++) {
343 if (!options->long_name)
344 continue;
Christian Couder59556542013-11-30 21:55:40 +0100345 if (starts_with(options->long_name, arg)) {
Pierre Habouzit3a9f0f42008-01-26 12:26:57 +0100346 error ("did you mean `--%s` (with two dashes ?)", arg);
347 exit(129);
348 }
349 }
350}
351
Pierre Habouzitcb9d3982009-06-09 10:23:44 +0200352static void parse_options_check(const struct option *opts)
353{
354 int err = 0;
355
356 for (; opts->type != OPTION_END; opts++) {
357 if ((opts->flags & PARSE_OPT_LASTARG_DEFAULT) &&
Jonathan Nieder1e5ce572010-12-02 00:01:18 -0600358 (opts->flags & PARSE_OPT_OPTARG))
359 err |= optbug(opts, "uses incompatible flags "
360 "LASTARG_DEFAULT and OPTARG");
Jonathan Niedera02dd4f2010-12-02 00:05:05 -0600361 if (opts->flags & PARSE_OPT_NODASH &&
362 ((opts->flags & PARSE_OPT_OPTARG) ||
363 !(opts->flags & PARSE_OPT_NOARG) ||
364 !(opts->flags & PARSE_OPT_NONEG) ||
365 opts->long_name))
366 err |= optbug(opts, "uses feature "
367 "not supported for dashless options");
Jonathan Nieder5c400ed2010-12-02 00:08:57 -0600368 switch (opts->type) {
Junio C Hamanob04ba2b2011-09-27 16:56:49 -0700369 case OPTION_COUNTUP:
Jonathan Nieder5c400ed2010-12-02 00:08:57 -0600370 case OPTION_BIT:
371 case OPTION_NEGBIT:
372 case OPTION_SET_INT:
373 case OPTION_SET_PTR:
374 case OPTION_NUMBER:
375 if ((opts->flags & PARSE_OPT_OPTARG) ||
376 !(opts->flags & PARSE_OPT_NOARG))
377 err |= optbug(opts, "should not accept an argument");
378 default:
379 ; /* ok. (usually accepts an argument) */
380 }
Pierre Habouzitcb9d3982009-06-09 10:23:44 +0200381 }
Pierre Habouzitcb9d3982009-06-09 10:23:44 +0200382 if (err)
Jonathan Nieder1e5ce572010-12-02 00:01:18 -0600383 exit(128);
Pierre Habouzitcb9d3982009-06-09 10:23:44 +0200384}
385
Pierre Habouzit7e7bbcb2008-06-23 21:59:37 +0200386void parse_options_start(struct parse_opt_ctx_t *ctx,
Stephen Boyd37782922009-05-23 11:53:12 -0700387 int argc, const char **argv, const char *prefix,
Stephen Boyd9ca11692010-12-05 23:57:42 -0800388 const struct option *options, int flags)
Pierre Habouzit7e7bbcb2008-06-23 21:59:37 +0200389{
390 memset(ctx, 0, sizeof(*ctx));
391 ctx->argc = argc - 1;
392 ctx->argv = argv + 1;
393 ctx->out = argv;
Stephen Boyd37782922009-05-23 11:53:12 -0700394 ctx->prefix = prefix;
Pierre Habouzita32a4ea2008-06-24 00:31:31 +0200395 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
Pierre Habouzit7e7bbcb2008-06-23 21:59:37 +0200396 ctx->flags = flags;
René Scharfe0d260f92009-03-09 21:57:38 +0100397 if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
398 (flags & PARSE_OPT_STOP_AT_NON_OPTION))
399 die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
Stephen Boyd9ca11692010-12-05 23:57:42 -0800400 parse_options_check(options);
Pierre Habouzit7e7bbcb2008-06-23 21:59:37 +0200401}
402
Thomas Rast47e9cd22010-06-12 14:57:39 +0200403static int usage_with_options_internal(struct parse_opt_ctx_t *,
404 const char * const *,
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200405 const struct option *, int, int);
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200406
407int parse_options_step(struct parse_opt_ctx_t *ctx,
408 const struct option *options,
409 const char * const usagestr[])
410{
René Scharfeb92891f2009-03-08 19:15:08 +0100411 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
412
Pierre Habouzit26141b52008-06-23 22:55:11 +0200413 /* we must reset ->opt, unknown short option leave it dangling */
414 ctx->opt = NULL;
415
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200416 for (; ctx->argc; ctx->argc--, ctx->argv++) {
417 const char *arg = ctx->argv[0];
418
419 if (*arg != '-' || !arg[1]) {
René Scharfe51a99492009-05-07 21:45:42 +0200420 if (parse_nodash_opt(ctx, arg, options) == 0)
421 continue;
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200422 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
Jonathan Nieder979240f2010-12-01 17:32:55 -0600423 return PARSE_OPT_NON_OPTION;
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200424 ctx->out[ctx->cpidx++] = ctx->argv[0];
425 continue;
426 }
427
428 if (arg[1] != '-') {
429 ctx->opt = arg + 1;
René Scharfeb92891f2009-03-08 19:15:08 +0100430 if (internal_help && *ctx->opt == 'h')
Thomas Rast47e9cd22010-06-12 14:57:39 +0200431 return parse_options_usage(ctx, usagestr, options, 0);
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200432 switch (parse_short_opt(ctx, options)) {
433 case -1:
Thomas Rast47e9cd22010-06-12 14:57:39 +0200434 return parse_options_usage(ctx, usagestr, options, 1);
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200435 case -2:
René Scharfe38916c52012-03-03 12:00:29 +0100436 if (ctx->opt)
437 check_typos(arg + 1, options);
René Scharfeb5ce3a52009-03-08 19:12:47 +0100438 goto unknown;
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200439 }
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200440 if (ctx->opt)
441 check_typos(arg + 1, options);
442 while (ctx->opt) {
René Scharfeb92891f2009-03-08 19:15:08 +0100443 if (internal_help && *ctx->opt == 'h')
Thomas Rast47e9cd22010-06-12 14:57:39 +0200444 return parse_options_usage(ctx, usagestr, options, 0);
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200445 switch (parse_short_opt(ctx, options)) {
446 case -1:
Thomas Rast47e9cd22010-06-12 14:57:39 +0200447 return parse_options_usage(ctx, usagestr, options, 1);
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200448 case -2:
Pierre Habouzit26141b52008-06-23 22:55:11 +0200449 /* fake a short option thing to hide the fact that we may have
450 * started to parse aggregated stuff
451 *
452 * This is leaky, too bad.
453 */
454 ctx->argv[0] = xstrdup(ctx->opt - 1);
455 *(char *)ctx->argv[0] = '-';
René Scharfeb5ce3a52009-03-08 19:12:47 +0100456 goto unknown;
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200457 }
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200458 }
459 continue;
460 }
461
462 if (!arg[2]) { /* "--" */
463 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
464 ctx->argc--;
465 ctx->argv++;
466 }
467 break;
468 }
469
René Scharfeb92891f2009-03-08 19:15:08 +0100470 if (internal_help && !strcmp(arg + 2, "help-all"))
Thomas Rast47e9cd22010-06-12 14:57:39 +0200471 return usage_with_options_internal(ctx, usagestr, options, 1, 0);
René Scharfeb92891f2009-03-08 19:15:08 +0100472 if (internal_help && !strcmp(arg + 2, "help"))
Thomas Rast47e9cd22010-06-12 14:57:39 +0200473 return parse_options_usage(ctx, usagestr, options, 0);
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200474 switch (parse_long_opt(ctx, arg + 2, options)) {
475 case -1:
Thomas Rast47e9cd22010-06-12 14:57:39 +0200476 return parse_options_usage(ctx, usagestr, options, 1);
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200477 case -2:
René Scharfeb5ce3a52009-03-08 19:12:47 +0100478 goto unknown;
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200479 }
René Scharfeb5ce3a52009-03-08 19:12:47 +0100480 continue;
481unknown:
482 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
483 return PARSE_OPT_UNKNOWN;
484 ctx->out[ctx->cpidx++] = ctx->argv[0];
485 ctx->opt = NULL;
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200486 }
487 return PARSE_OPT_DONE;
488}
489
Pierre Habouzit7e7bbcb2008-06-23 21:59:37 +0200490int parse_options_end(struct parse_opt_ctx_t *ctx)
491{
492 memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out));
493 ctx->out[ctx->cpidx + ctx->argc] = NULL;
494 return ctx->cpidx + ctx->argc;
495}
496
Stephen Boyd37782922009-05-23 11:53:12 -0700497int parse_options(int argc, const char **argv, const char *prefix,
498 const struct option *options, const char * const usagestr[],
499 int flags)
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200500{
Pierre Habouzit7e7bbcb2008-06-23 21:59:37 +0200501 struct parse_opt_ctx_t ctx;
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200502
Stephen Boyd9ca11692010-12-05 23:57:42 -0800503 parse_options_start(&ctx, argc, argv, prefix, options, flags);
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200504 switch (parse_options_step(&ctx, options, usagestr)) {
505 case PARSE_OPT_HELP:
506 exit(129);
Jonathan Nieder979240f2010-12-01 17:32:55 -0600507 case PARSE_OPT_NON_OPTION:
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200508 case PARSE_OPT_DONE:
509 break;
510 default: /* PARSE_OPT_UNKNOWN */
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200511 if (ctx.argv[0][1] == '-') {
512 error("unknown option `%s'", ctx.argv[0] + 2);
Erik Faye-Lundb141a472013-02-12 00:13:48 +0100513 } else if (isascii(*ctx.opt)) {
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200514 error("unknown switch `%c'", *ctx.opt);
Erik Faye-Lundb141a472013-02-12 00:13:48 +0100515 } else {
516 error("unknown non-ascii option in string: `%s'",
517 ctx.argv[0]);
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200518 }
519 usage_with_options(usagestr, options);
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200520 }
521
Torsten Bögershausen76759c72012-07-08 15:50:25 +0200522 precompose_argv(argc, argv);
Pierre Habouzit7e7bbcb2008-06-23 21:59:37 +0200523 return parse_options_end(&ctx);
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200524}
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200525
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200526static int usage_argh(const struct option *opts, FILE *outfile)
Stephen Boyd29f25d42009-05-21 00:33:17 -0700527{
528 const char *s;
Stephen Boyd34aec9f2009-06-04 16:43:57 -0700529 int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) || !opts->argh;
Stephen Boyd29f25d42009-05-21 00:33:17 -0700530 if (opts->flags & PARSE_OPT_OPTARG)
531 if (opts->long_name)
532 s = literal ? "[=%s]" : "[=<%s>]";
533 else
534 s = literal ? "[%s]" : "[<%s>]";
535 else
536 s = literal ? " %s" : " <%s>";
Jiang Xinc0821962013-02-09 14:31:09 +0800537 return utf8_fprintf(outfile, s, opts->argh ? _(opts->argh) : _("..."));
Stephen Boyd29f25d42009-05-21 00:33:17 -0700538}
539
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200540#define USAGE_OPTS_WIDTH 24
541#define USAGE_GAP 2
542
Thomas Rast47e9cd22010-06-12 14:57:39 +0200543static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
544 const char * const *usagestr,
545 const struct option *opts, int full, int err)
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200546{
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200547 FILE *outfile = err ? stderr : stdout;
548
René Scharfe49b61802009-03-08 19:16:58 +0100549 if (!usagestr)
550 return PARSE_OPT_HELP;
551
Thomas Rast47e9cd22010-06-12 14:57:39 +0200552 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
553 fprintf(outfile, "cat <<\\EOF\n");
554
Nguyễn Thái Ngọc Duy54e6dc72012-05-06 21:23:51 +0700555 fprintf_ln(outfile, _("usage: %s"), _(*usagestr++));
Alex Riesenf389c802007-10-14 00:10:51 +0200556 while (*usagestr && **usagestr)
Nguyễn Thái Ngọc Duy54e6dc72012-05-06 21:23:51 +0700557 /* TRANSLATORS: the colon here should align with the
558 one in "usage: %s" translation */
559 fprintf_ln(outfile, _(" or: %s"), _(*usagestr++));
Jeff King44d86e92008-06-14 03:27:21 -0400560 while (*usagestr) {
Nguyễn Thái Ngọc Duy54e6dc72012-05-06 21:23:51 +0700561 if (**usagestr)
562 fprintf_ln(outfile, _(" %s"), _(*usagestr));
563 else
564 putchar('\n');
Jeff King44d86e92008-06-14 03:27:21 -0400565 usagestr++;
566 }
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200567
568 if (opts->type != OPTION_GROUP)
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200569 fputc('\n', outfile);
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200570
571 for (; opts->type != OPTION_END; opts++) {
572 size_t pos;
573 int pad;
574
575 if (opts->type == OPTION_GROUP) {
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200576 fputc('\n', outfile);
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200577 if (*opts->help)
Nguyễn Thái Ngọc Duy54e6dc72012-05-06 21:23:51 +0700578 fprintf(outfile, "%s\n", _(opts->help));
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200579 continue;
580 }
Pierre Habouzitdd3bf0f2007-11-19 10:21:44 +0100581 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
582 continue;
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200583
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200584 pos = fprintf(outfile, " ");
René Scharfecbb08c22012-02-28 20:06:09 +0100585 if (opts->short_name) {
René Scharfe51a99492009-05-07 21:45:42 +0200586 if (opts->flags & PARSE_OPT_NODASH)
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200587 pos += fprintf(outfile, "%c", opts->short_name);
René Scharfe51a99492009-05-07 21:45:42 +0200588 else
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200589 pos += fprintf(outfile, "-%c", opts->short_name);
René Scharfe51a99492009-05-07 21:45:42 +0200590 }
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200591 if (opts->long_name && opts->short_name)
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200592 pos += fprintf(outfile, ", ");
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200593 if (opts->long_name)
René Scharfecbb08c22012-02-28 20:06:09 +0100594 pos += fprintf(outfile, "--%s", opts->long_name);
René Scharfee0319ff2009-05-07 21:45:08 +0200595 if (opts->type == OPTION_NUMBER)
Jiang Xinc0821962013-02-09 14:31:09 +0800596 pos += utf8_fprintf(outfile, _("-NUM"));
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200597
Jonathan Niederb57c68a2010-12-01 17:31:36 -0600598 if ((opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
599 !(opts->flags & PARSE_OPT_NOARG))
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200600 pos += usage_argh(opts, outfile);
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200601
Alex Riesenf389c802007-10-14 00:10:51 +0200602 if (pos <= USAGE_OPTS_WIDTH)
603 pad = USAGE_OPTS_WIDTH - pos;
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200604 else {
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200605 fputc('\n', outfile);
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200606 pad = USAGE_OPTS_WIDTH;
607 }
Nguyễn Thái Ngọc Duy54e6dc72012-05-06 21:23:51 +0700608 fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", _(opts->help));
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200609 }
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200610 fputc('\n', outfile);
Alex Riesenf389c802007-10-14 00:10:51 +0200611
Thomas Rast47e9cd22010-06-12 14:57:39 +0200612 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
613 fputs("EOF\n", outfile);
614
Pierre Habouzitee68b872008-06-23 22:28:04 +0200615 return PARSE_OPT_HELP;
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200616}
Pierre Habouzit0ce865b2007-10-14 11:05:12 +0200617
Stephen Boydc2e86ad2011-03-22 00:51:05 -0700618void NORETURN usage_with_options(const char * const *usagestr,
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200619 const struct option *opts)
Pierre Habouzitdd3bf0f2007-11-19 10:21:44 +0100620{
Thomas Rast47e9cd22010-06-12 14:57:39 +0200621 usage_with_options_internal(NULL, usagestr, opts, 0, 1);
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200622 exit(129);
Pierre Habouzitdd3bf0f2007-11-19 10:21:44 +0100623}
624
Stephen Boydc2e86ad2011-03-22 00:51:05 -0700625void NORETURN usage_msg_opt(const char *msg,
Christian Couder451bb212009-02-02 06:12:58 +0100626 const char * const *usagestr,
627 const struct option *options)
628{
629 fprintf(stderr, "%s\n\n", msg);
630 usage_with_options(usagestr, options);
631}
632
Thomas Rast47e9cd22010-06-12 14:57:39 +0200633static int parse_options_usage(struct parse_opt_ctx_t *ctx,
634 const char * const *usagestr,
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200635 const struct option *opts, int err)
Pierre Habouzitee68b872008-06-23 22:28:04 +0200636{
Thomas Rast47e9cd22010-06-12 14:57:39 +0200637 return usage_with_options_internal(ctx, usagestr, opts, 0, err);
Pierre Habouzitee68b872008-06-23 22:28:04 +0200638}
639
Jeff Kinga469a102012-12-15 12:42:10 -0500640#undef opterror
641int opterror(const struct option *opt, const char *reason, int flags)
642{
643 if (flags & OPT_SHORT)
644 return error("switch `%c' %s", opt->short_name, reason);
645 if (flags & OPT_UNSET)
646 return error("option `no-%s' %s", opt->long_name, reason);
647 return error("option `%s' %s", opt->long_name, reason);
648}