blob: c2cbca25cc6bb010996063c88c9af3e7b38aad5a [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
Pierre Habouzit7e7bbcb2008-06-23 21:59:37 +020046static int get_value(struct parse_opt_ctx_t *p,
Pierre Habouzit1cc69852008-07-08 12:34:08 +020047 const struct option *opt, int flags)
Pierre Habouzit4a59fd12007-10-15 01:35:37 +020048{
Pierre Habouzitffe659f2007-10-15 01:45:45 +020049 const char *s, *arg;
Pierre Habouzitdb7244b2007-11-07 11:20:27 +010050 const int unset = flags & OPT_UNSET;
Stephen Boyddf217ed2009-05-23 11:53:13 -070051 int err;
Pierre Habouzit4a59fd12007-10-15 01:35:37 +020052
Pierre Habouzitdb7244b2007-11-07 11:20:27 +010053 if (unset && p->opt)
Pierre Habouzit4a59fd12007-10-15 01:35:37 +020054 return opterror(opt, "takes no value", flags);
Pierre Habouzitdb7244b2007-11-07 11:20:27 +010055 if (unset && (opt->flags & PARSE_OPT_NONEG))
56 return opterror(opt, "isn't available", flags);
Stephen Boydc1f4ec92010-12-01 17:30:40 -060057 if (!(flags & OPT_SHORT) && p->opt && (opt->flags & PARSE_OPT_NOARG))
58 return opterror(opt, "takes no value", flags);
Pierre Habouzitdb7244b2007-11-07 11:20:27 +010059
Pierre Habouzitdb7244b2007-11-07 11:20:27 +010060 switch (opt->type) {
Jonathan Niederb0b3a8b2010-12-01 17:32:16 -060061 case OPTION_LOWLEVEL_CALLBACK:
62 return (*(parse_opt_ll_cb *)opt->callback)(p, opt, unset);
63
Pierre Habouzitdb7244b2007-11-07 11:20:27 +010064 case OPTION_BIT:
65 if (unset)
66 *(int *)opt->value &= ~opt->defval;
Pierre Habouzit4a59fd12007-10-15 01:35:37 +020067 else
Pierre Habouzitdb7244b2007-11-07 11:20:27 +010068 *(int *)opt->value |= opt->defval;
69 return 0;
70
René Scharfe2f4b97f2009-05-07 21:44:17 +020071 case OPTION_NEGBIT:
72 if (unset)
73 *(int *)opt->value |= opt->defval;
74 else
75 *(int *)opt->value &= ~opt->defval;
76 return 0;
77
Junio C Hamanob04ba2b2011-09-27 16:56:49 -070078 case OPTION_COUNTUP:
Pierre Habouzitdb7244b2007-11-07 11:20:27 +010079 *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
80 return 0;
81
82 case OPTION_SET_INT:
83 *(int *)opt->value = unset ? 0 : opt->defval;
84 return 0;
85
86 case OPTION_SET_PTR:
87 *(void **)opt->value = unset ? NULL : (void *)opt->defval;
Pierre Habouzit4a59fd12007-10-15 01:35:37 +020088 return 0;
89
90 case OPTION_STRING:
Pierre Habouzit1cc69852008-07-08 12:34:08 +020091 if (unset)
Pierre Habouzitdb7244b2007-11-07 11:20:27 +010092 *(const char **)opt->value = NULL;
Pierre Habouzit1cc69852008-07-08 12:34:08 +020093 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
Pierre Habouzitffe659f2007-10-15 01:45:45 +020094 *(const char **)opt->value = (const char *)opt->defval;
Pierre Habouzit1cc69852008-07-08 12:34:08 +020095 else
96 return get_arg(p, opt, flags, (const char **)opt->value);
Pierre Habouzit4a59fd12007-10-15 01:35:37 +020097 return 0;
98
Stephen Boyddf217ed2009-05-23 11:53:13 -070099 case OPTION_FILENAME:
100 err = 0;
101 if (unset)
102 *(const char **)opt->value = NULL;
103 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
104 *(const char **)opt->value = (const char *)opt->defval;
105 else
106 err = get_arg(p, opt, flags, (const char **)opt->value);
107
108 if (!err)
109 fix_filename(p->prefix, (const char **)opt->value);
110 return err;
111
Pierre Habouzitffe659f2007-10-15 01:45:45 +0200112 case OPTION_CALLBACK:
Pierre Habouzitdb7244b2007-11-07 11:20:27 +0100113 if (unset)
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200114 return (*opt->callback)(opt, NULL, 1) ? (-1) : 0;
Pierre Habouzitdb7244b2007-11-07 11:20:27 +0100115 if (opt->flags & PARSE_OPT_NOARG)
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200116 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
Pierre Habouzitc43a2482007-12-21 11:41:41 +0100117 if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200118 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
Pierre Habouzit1cc69852008-07-08 12:34:08 +0200119 if (get_arg(p, opt, flags, &arg))
120 return -1;
121 return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
Pierre Habouzitffe659f2007-10-15 01:45:45 +0200122
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200123 case OPTION_INTEGER:
Pierre Habouzitdb7244b2007-11-07 11:20:27 +0100124 if (unset) {
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200125 *(int *)opt->value = 0;
126 return 0;
127 }
Pierre Habouzitc43a2482007-12-21 11:41:41 +0100128 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
Pierre Habouzitffe659f2007-10-15 01:45:45 +0200129 *(int *)opt->value = opt->defval;
130 return 0;
131 }
Pierre Habouzit1cc69852008-07-08 12:34:08 +0200132 if (get_arg(p, opt, flags, &arg))
133 return -1;
134 *(int *)opt->value = strtol(arg, (char **)&s, 10);
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200135 if (*s)
136 return opterror(opt, "expects a numerical value", flags);
137 return 0;
138
139 default:
140 die("should not happen, someone must be hit on the forehead");
141 }
142}
143
Pierre Habouzit7e7bbcb2008-06-23 21:59:37 +0200144static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200145{
René Scharfee0319ff2009-05-07 21:45:08 +0200146 const struct option *numopt = NULL;
147
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200148 for (; options->type != OPTION_END; options++) {
149 if (options->short_name == *p->opt) {
150 p->opt = p->opt[1] ? p->opt + 1 : NULL;
151 return get_value(p, options, OPT_SHORT);
152 }
René Scharfee0319ff2009-05-07 21:45:08 +0200153
154 /*
155 * Handle the numerical option later, explicit one-digit
156 * options take precedence over it.
157 */
158 if (options->type == OPTION_NUMBER)
159 numopt = options;
160 }
161 if (numopt && isdigit(*p->opt)) {
162 size_t len = 1;
163 char *arg;
164 int rc;
165
166 while (isdigit(p->opt[len]))
167 len++;
168 arg = xmemdupz(p->opt, len);
169 p->opt = p->opt[len] ? p->opt + len : NULL;
170 rc = (*numopt->callback)(numopt, arg, 0) ? (-1) : 0;
171 free(arg);
172 return rc;
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200173 }
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200174 return -2;
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200175}
176
Pierre Habouzit7e7bbcb2008-06-23 21:59:37 +0200177static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200178 const struct option *options)
179{
Johannes Schindelin7f275b92007-10-14 17:54:06 +0100180 const char *arg_end = strchr(arg, '=');
Johannes Schindelin243e0612007-11-05 13:15:21 +0000181 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
182 int abbrev_flags = 0, ambiguous_flags = 0;
Johannes Schindelin7f275b92007-10-14 17:54:06 +0100183
184 if (!arg_end)
185 arg_end = arg + strlen(arg);
186
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200187 for (; options->type != OPTION_END; options++) {
René Scharfe0f1930c52012-02-25 20:14:54 +0100188 const char *rest, *long_name = options->long_name;
189 int flags = 0, opt_flags = 0;
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200190
René Scharfe0f1930c52012-02-25 20:14:54 +0100191 if (!long_name)
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200192 continue;
193
René Scharfe0f1930c52012-02-25 20:14:54 +0100194again:
195 rest = skip_prefix(arg, long_name);
Pierre Habouzit580d5bf2008-03-02 11:35:56 +0100196 if (options->type == OPTION_ARGUMENT) {
197 if (!rest)
198 continue;
199 if (*rest == '=')
200 return opterror(options, "takes no value", flags);
201 if (*rest)
202 continue;
203 p->out[p->cpidx++] = arg - 2;
204 return 0;
205 }
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200206 if (!rest) {
Johannes Schindelin7f275b92007-10-14 17:54:06 +0100207 /* abbreviated? */
René Scharfe0f1930c52012-02-25 20:14:54 +0100208 if (!strncmp(long_name, arg, arg_end - arg)) {
Johannes Schindelin7f275b92007-10-14 17:54:06 +0100209is_abbreviated:
Johannes Schindelin243e0612007-11-05 13:15:21 +0000210 if (abbrev_option) {
211 /*
212 * If this is abbreviated, it is
213 * ambiguous. So when there is no
214 * exact match later, we need to
215 * error out.
216 */
217 ambiguous_option = abbrev_option;
218 ambiguous_flags = abbrev_flags;
219 }
Johannes Schindelin7f275b92007-10-14 17:54:06 +0100220 if (!(flags & OPT_UNSET) && *arg_end)
221 p->opt = arg_end + 1;
222 abbrev_option = options;
René Scharfe0f1930c52012-02-25 20:14:54 +0100223 abbrev_flags = flags ^ opt_flags;
Johannes Schindelin7f275b92007-10-14 17:54:06 +0100224 continue;
225 }
Andreas Schwab6bbfd1f2009-09-25 20:44:44 +0200226 /* negation allowed? */
227 if (options->flags & PARSE_OPT_NONEG)
228 continue;
Johannes Schindelin7f275b92007-10-14 17:54:06 +0100229 /* negated and abbreviated very much? */
230 if (!prefixcmp("no-", arg)) {
231 flags |= OPT_UNSET;
232 goto is_abbreviated;
233 }
234 /* negated? */
René Scharfe0f1930c52012-02-25 20:14:54 +0100235 if (prefixcmp(arg, "no-")) {
236 if (!prefixcmp(long_name, "no-")) {
237 long_name += 3;
238 opt_flags |= OPT_UNSET;
239 goto again;
240 }
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200241 continue;
René Scharfe0f1930c52012-02-25 20:14:54 +0100242 }
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200243 flags |= OPT_UNSET;
René Scharfe0f1930c52012-02-25 20:14:54 +0100244 rest = skip_prefix(arg + 3, long_name);
Johannes Schindelin7f275b92007-10-14 17:54:06 +0100245 /* abbreviated and negated? */
René Scharfe0f1930c52012-02-25 20:14:54 +0100246 if (!rest && !prefixcmp(long_name, arg + 3))
Johannes Schindelin7f275b92007-10-14 17:54:06 +0100247 goto is_abbreviated;
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200248 if (!rest)
249 continue;
250 }
251 if (*rest) {
252 if (*rest != '=')
253 continue;
254 p->opt = rest + 1;
255 }
René Scharfe0f1930c52012-02-25 20:14:54 +0100256 return get_value(p, options, flags ^ opt_flags);
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200257 }
Johannes Schindelin243e0612007-11-05 13:15:21 +0000258
259 if (ambiguous_option)
260 return error("Ambiguous option: %s "
261 "(could be --%s%s or --%s%s)",
262 arg,
263 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
264 ambiguous_option->long_name,
265 (abbrev_flags & OPT_UNSET) ? "no-" : "",
266 abbrev_option->long_name);
Johannes Schindelin7f275b92007-10-14 17:54:06 +0100267 if (abbrev_option)
268 return get_value(p, abbrev_option, abbrev_flags);
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200269 return -2;
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200270}
271
René Scharfe51a99492009-05-07 21:45:42 +0200272static int parse_nodash_opt(struct parse_opt_ctx_t *p, const char *arg,
273 const struct option *options)
274{
275 for (; options->type != OPTION_END; options++) {
276 if (!(options->flags & PARSE_OPT_NODASH))
277 continue;
René Scharfe51a99492009-05-07 21:45:42 +0200278 if (options->short_name == arg[0] && arg[1] == '\0')
279 return get_value(p, options, OPT_SHORT);
280 }
281 return -2;
282}
283
Nanako Shiraishi1121a872008-07-16 19:42:18 +0900284static void check_typos(const char *arg, const struct option *options)
Pierre Habouzit3a9f0f42008-01-26 12:26:57 +0100285{
286 if (strlen(arg) < 3)
287 return;
288
289 if (!prefixcmp(arg, "no-")) {
290 error ("did you mean `--%s` (with two dashes ?)", arg);
291 exit(129);
292 }
293
294 for (; options->type != OPTION_END; options++) {
295 if (!options->long_name)
296 continue;
297 if (!prefixcmp(options->long_name, arg)) {
298 error ("did you mean `--%s` (with two dashes ?)", arg);
299 exit(129);
300 }
301 }
302}
303
Pierre Habouzitcb9d3982009-06-09 10:23:44 +0200304static void parse_options_check(const struct option *opts)
305{
306 int err = 0;
307
308 for (; opts->type != OPTION_END; opts++) {
309 if ((opts->flags & PARSE_OPT_LASTARG_DEFAULT) &&
Jonathan Nieder1e5ce572010-12-02 00:01:18 -0600310 (opts->flags & PARSE_OPT_OPTARG))
311 err |= optbug(opts, "uses incompatible flags "
312 "LASTARG_DEFAULT and OPTARG");
Jonathan Niedera02dd4f2010-12-02 00:05:05 -0600313 if (opts->flags & PARSE_OPT_NODASH &&
314 ((opts->flags & PARSE_OPT_OPTARG) ||
315 !(opts->flags & PARSE_OPT_NOARG) ||
316 !(opts->flags & PARSE_OPT_NONEG) ||
317 opts->long_name))
318 err |= optbug(opts, "uses feature "
319 "not supported for dashless options");
Jonathan Nieder5c400ed2010-12-02 00:08:57 -0600320 switch (opts->type) {
Junio C Hamanob04ba2b2011-09-27 16:56:49 -0700321 case OPTION_COUNTUP:
Jonathan Nieder5c400ed2010-12-02 00:08:57 -0600322 case OPTION_BIT:
323 case OPTION_NEGBIT:
324 case OPTION_SET_INT:
325 case OPTION_SET_PTR:
326 case OPTION_NUMBER:
327 if ((opts->flags & PARSE_OPT_OPTARG) ||
328 !(opts->flags & PARSE_OPT_NOARG))
329 err |= optbug(opts, "should not accept an argument");
330 default:
331 ; /* ok. (usually accepts an argument) */
332 }
Pierre Habouzitcb9d3982009-06-09 10:23:44 +0200333 }
Pierre Habouzitcb9d3982009-06-09 10:23:44 +0200334 if (err)
Jonathan Nieder1e5ce572010-12-02 00:01:18 -0600335 exit(128);
Pierre Habouzitcb9d3982009-06-09 10:23:44 +0200336}
337
Pierre Habouzit7e7bbcb2008-06-23 21:59:37 +0200338void parse_options_start(struct parse_opt_ctx_t *ctx,
Stephen Boyd37782922009-05-23 11:53:12 -0700339 int argc, const char **argv, const char *prefix,
Stephen Boyd9ca11692010-12-05 23:57:42 -0800340 const struct option *options, int flags)
Pierre Habouzit7e7bbcb2008-06-23 21:59:37 +0200341{
342 memset(ctx, 0, sizeof(*ctx));
343 ctx->argc = argc - 1;
344 ctx->argv = argv + 1;
345 ctx->out = argv;
Stephen Boyd37782922009-05-23 11:53:12 -0700346 ctx->prefix = prefix;
Pierre Habouzita32a4ea2008-06-24 00:31:31 +0200347 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
Pierre Habouzit7e7bbcb2008-06-23 21:59:37 +0200348 ctx->flags = flags;
René Scharfe0d260f92009-03-09 21:57:38 +0100349 if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
350 (flags & PARSE_OPT_STOP_AT_NON_OPTION))
351 die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
Stephen Boyd9ca11692010-12-05 23:57:42 -0800352 parse_options_check(options);
Pierre Habouzit7e7bbcb2008-06-23 21:59:37 +0200353}
354
Thomas Rast47e9cd22010-06-12 14:57:39 +0200355static int usage_with_options_internal(struct parse_opt_ctx_t *,
356 const char * const *,
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200357 const struct option *, int, int);
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200358
359int parse_options_step(struct parse_opt_ctx_t *ctx,
360 const struct option *options,
361 const char * const usagestr[])
362{
René Scharfeb92891f2009-03-08 19:15:08 +0100363 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
364
Pierre Habouzit26141b52008-06-23 22:55:11 +0200365 /* we must reset ->opt, unknown short option leave it dangling */
366 ctx->opt = NULL;
367
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200368 for (; ctx->argc; ctx->argc--, ctx->argv++) {
369 const char *arg = ctx->argv[0];
370
371 if (*arg != '-' || !arg[1]) {
René Scharfe51a99492009-05-07 21:45:42 +0200372 if (parse_nodash_opt(ctx, arg, options) == 0)
373 continue;
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200374 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
Jonathan Nieder979240f2010-12-01 17:32:55 -0600375 return PARSE_OPT_NON_OPTION;
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200376 ctx->out[ctx->cpidx++] = ctx->argv[0];
377 continue;
378 }
379
380 if (arg[1] != '-') {
381 ctx->opt = arg + 1;
René Scharfeb92891f2009-03-08 19:15:08 +0100382 if (internal_help && *ctx->opt == 'h')
Thomas Rast47e9cd22010-06-12 14:57:39 +0200383 return parse_options_usage(ctx, usagestr, options, 0);
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200384 switch (parse_short_opt(ctx, options)) {
385 case -1:
Thomas Rast47e9cd22010-06-12 14:57:39 +0200386 return parse_options_usage(ctx, usagestr, options, 1);
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200387 case -2:
René Scharfe38916c52012-03-03 12:00:29 +0100388 if (ctx->opt)
389 check_typos(arg + 1, options);
René Scharfeb5ce3a52009-03-08 19:12:47 +0100390 goto unknown;
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200391 }
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200392 if (ctx->opt)
393 check_typos(arg + 1, options);
394 while (ctx->opt) {
René Scharfeb92891f2009-03-08 19:15:08 +0100395 if (internal_help && *ctx->opt == 'h')
Thomas Rast47e9cd22010-06-12 14:57:39 +0200396 return parse_options_usage(ctx, usagestr, options, 0);
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200397 switch (parse_short_opt(ctx, options)) {
398 case -1:
Thomas Rast47e9cd22010-06-12 14:57:39 +0200399 return parse_options_usage(ctx, usagestr, options, 1);
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200400 case -2:
Pierre Habouzit26141b52008-06-23 22:55:11 +0200401 /* fake a short option thing to hide the fact that we may have
402 * started to parse aggregated stuff
403 *
404 * This is leaky, too bad.
405 */
406 ctx->argv[0] = xstrdup(ctx->opt - 1);
407 *(char *)ctx->argv[0] = '-';
René Scharfeb5ce3a52009-03-08 19:12:47 +0100408 goto unknown;
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200409 }
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200410 }
411 continue;
412 }
413
414 if (!arg[2]) { /* "--" */
415 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
416 ctx->argc--;
417 ctx->argv++;
418 }
419 break;
420 }
421
René Scharfeb92891f2009-03-08 19:15:08 +0100422 if (internal_help && !strcmp(arg + 2, "help-all"))
Thomas Rast47e9cd22010-06-12 14:57:39 +0200423 return usage_with_options_internal(ctx, usagestr, options, 1, 0);
René Scharfeb92891f2009-03-08 19:15:08 +0100424 if (internal_help && !strcmp(arg + 2, "help"))
Thomas Rast47e9cd22010-06-12 14:57:39 +0200425 return parse_options_usage(ctx, usagestr, options, 0);
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200426 switch (parse_long_opt(ctx, arg + 2, options)) {
427 case -1:
Thomas Rast47e9cd22010-06-12 14:57:39 +0200428 return parse_options_usage(ctx, usagestr, options, 1);
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200429 case -2:
René Scharfeb5ce3a52009-03-08 19:12:47 +0100430 goto unknown;
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200431 }
René Scharfeb5ce3a52009-03-08 19:12:47 +0100432 continue;
433unknown:
434 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
435 return PARSE_OPT_UNKNOWN;
436 ctx->out[ctx->cpidx++] = ctx->argv[0];
437 ctx->opt = NULL;
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200438 }
439 return PARSE_OPT_DONE;
440}
441
Pierre Habouzit7e7bbcb2008-06-23 21:59:37 +0200442int parse_options_end(struct parse_opt_ctx_t *ctx)
443{
444 memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out));
445 ctx->out[ctx->cpidx + ctx->argc] = NULL;
446 return ctx->cpidx + ctx->argc;
447}
448
Stephen Boyd37782922009-05-23 11:53:12 -0700449int parse_options(int argc, const char **argv, const char *prefix,
450 const struct option *options, const char * const usagestr[],
451 int flags)
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200452{
Pierre Habouzit7e7bbcb2008-06-23 21:59:37 +0200453 struct parse_opt_ctx_t ctx;
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200454
Stephen Boyd9ca11692010-12-05 23:57:42 -0800455 parse_options_start(&ctx, argc, argv, prefix, options, flags);
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200456 switch (parse_options_step(&ctx, options, usagestr)) {
457 case PARSE_OPT_HELP:
458 exit(129);
Jonathan Nieder979240f2010-12-01 17:32:55 -0600459 case PARSE_OPT_NON_OPTION:
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200460 case PARSE_OPT_DONE:
461 break;
462 default: /* PARSE_OPT_UNKNOWN */
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200463 if (ctx.argv[0][1] == '-') {
464 error("unknown option `%s'", ctx.argv[0] + 2);
Erik Faye-Lundb141a472013-02-12 00:13:48 +0100465 } else if (isascii(*ctx.opt)) {
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200466 error("unknown switch `%c'", *ctx.opt);
Erik Faye-Lundb141a472013-02-12 00:13:48 +0100467 } else {
468 error("unknown non-ascii option in string: `%s'",
469 ctx.argv[0]);
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200470 }
471 usage_with_options(usagestr, options);
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200472 }
473
Torsten Bögershausen76759c72012-07-08 15:50:25 +0200474 precompose_argv(argc, argv);
Pierre Habouzit7e7bbcb2008-06-23 21:59:37 +0200475 return parse_options_end(&ctx);
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200476}
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200477
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200478static int usage_argh(const struct option *opts, FILE *outfile)
Stephen Boyd29f25d42009-05-21 00:33:17 -0700479{
480 const char *s;
Stephen Boyd34aec9f2009-06-04 16:43:57 -0700481 int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) || !opts->argh;
Stephen Boyd29f25d42009-05-21 00:33:17 -0700482 if (opts->flags & PARSE_OPT_OPTARG)
483 if (opts->long_name)
484 s = literal ? "[=%s]" : "[=<%s>]";
485 else
486 s = literal ? "[%s]" : "[<%s>]";
487 else
488 s = literal ? " %s" : " <%s>";
Jiang Xinc0821962013-02-09 14:31:09 +0800489 return utf8_fprintf(outfile, s, opts->argh ? _(opts->argh) : _("..."));
Stephen Boyd29f25d42009-05-21 00:33:17 -0700490}
491
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200492#define USAGE_OPTS_WIDTH 24
493#define USAGE_GAP 2
494
Thomas Rast47e9cd22010-06-12 14:57:39 +0200495static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
496 const char * const *usagestr,
497 const struct option *opts, int full, int err)
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200498{
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200499 FILE *outfile = err ? stderr : stdout;
500
René Scharfe49b61802009-03-08 19:16:58 +0100501 if (!usagestr)
502 return PARSE_OPT_HELP;
503
Thomas Rast47e9cd22010-06-12 14:57:39 +0200504 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
505 fprintf(outfile, "cat <<\\EOF\n");
506
Nguyễn Thái Ngọc Duy54e6dc72012-05-06 21:23:51 +0700507 fprintf_ln(outfile, _("usage: %s"), _(*usagestr++));
Alex Riesenf389c802007-10-14 00:10:51 +0200508 while (*usagestr && **usagestr)
Nguyễn Thái Ngọc Duy54e6dc72012-05-06 21:23:51 +0700509 /* TRANSLATORS: the colon here should align with the
510 one in "usage: %s" translation */
511 fprintf_ln(outfile, _(" or: %s"), _(*usagestr++));
Jeff King44d86e92008-06-14 03:27:21 -0400512 while (*usagestr) {
Nguyễn Thái Ngọc Duy54e6dc72012-05-06 21:23:51 +0700513 if (**usagestr)
514 fprintf_ln(outfile, _(" %s"), _(*usagestr));
515 else
516 putchar('\n');
Jeff King44d86e92008-06-14 03:27:21 -0400517 usagestr++;
518 }
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200519
520 if (opts->type != OPTION_GROUP)
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200521 fputc('\n', outfile);
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200522
523 for (; opts->type != OPTION_END; opts++) {
524 size_t pos;
525 int pad;
526
527 if (opts->type == OPTION_GROUP) {
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200528 fputc('\n', outfile);
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200529 if (*opts->help)
Nguyễn Thái Ngọc Duy54e6dc72012-05-06 21:23:51 +0700530 fprintf(outfile, "%s\n", _(opts->help));
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200531 continue;
532 }
Pierre Habouzitdd3bf0f2007-11-19 10:21:44 +0100533 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
534 continue;
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200535
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200536 pos = fprintf(outfile, " ");
René Scharfecbb08c22012-02-28 20:06:09 +0100537 if (opts->short_name) {
René Scharfe51a99492009-05-07 21:45:42 +0200538 if (opts->flags & PARSE_OPT_NODASH)
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200539 pos += fprintf(outfile, "%c", opts->short_name);
René Scharfe51a99492009-05-07 21:45:42 +0200540 else
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200541 pos += fprintf(outfile, "-%c", opts->short_name);
René Scharfe51a99492009-05-07 21:45:42 +0200542 }
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200543 if (opts->long_name && opts->short_name)
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200544 pos += fprintf(outfile, ", ");
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200545 if (opts->long_name)
René Scharfecbb08c22012-02-28 20:06:09 +0100546 pos += fprintf(outfile, "--%s", opts->long_name);
René Scharfee0319ff2009-05-07 21:45:08 +0200547 if (opts->type == OPTION_NUMBER)
Jiang Xinc0821962013-02-09 14:31:09 +0800548 pos += utf8_fprintf(outfile, _("-NUM"));
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200549
Jonathan Niederb57c68a2010-12-01 17:31:36 -0600550 if ((opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
551 !(opts->flags & PARSE_OPT_NOARG))
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200552 pos += usage_argh(opts, outfile);
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200553
Alex Riesenf389c802007-10-14 00:10:51 +0200554 if (pos <= USAGE_OPTS_WIDTH)
555 pad = USAGE_OPTS_WIDTH - pos;
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200556 else {
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200557 fputc('\n', outfile);
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200558 pad = USAGE_OPTS_WIDTH;
559 }
Nguyễn Thái Ngọc Duy54e6dc72012-05-06 21:23:51 +0700560 fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", _(opts->help));
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200561 }
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200562 fputc('\n', outfile);
Alex Riesenf389c802007-10-14 00:10:51 +0200563
Thomas Rast47e9cd22010-06-12 14:57:39 +0200564 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
565 fputs("EOF\n", outfile);
566
Pierre Habouzitee68b872008-06-23 22:28:04 +0200567 return PARSE_OPT_HELP;
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200568}
Pierre Habouzit0ce865b2007-10-14 11:05:12 +0200569
Stephen Boydc2e86ad2011-03-22 00:51:05 -0700570void NORETURN usage_with_options(const char * const *usagestr,
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200571 const struct option *opts)
Pierre Habouzitdd3bf0f2007-11-19 10:21:44 +0100572{
Thomas Rast47e9cd22010-06-12 14:57:39 +0200573 usage_with_options_internal(NULL, usagestr, opts, 0, 1);
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200574 exit(129);
Pierre Habouzitdd3bf0f2007-11-19 10:21:44 +0100575}
576
Stephen Boydc2e86ad2011-03-22 00:51:05 -0700577void NORETURN usage_msg_opt(const char *msg,
Christian Couder451bb212009-02-02 06:12:58 +0100578 const char * const *usagestr,
579 const struct option *options)
580{
581 fprintf(stderr, "%s\n\n", msg);
582 usage_with_options(usagestr, options);
583}
584
Thomas Rast47e9cd22010-06-12 14:57:39 +0200585static int parse_options_usage(struct parse_opt_ctx_t *ctx,
586 const char * const *usagestr,
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200587 const struct option *opts, int err)
Pierre Habouzitee68b872008-06-23 22:28:04 +0200588{
Thomas Rast47e9cd22010-06-12 14:57:39 +0200589 return usage_with_options_internal(ctx, usagestr, opts, 0, err);
Pierre Habouzitee68b872008-06-23 22:28:04 +0200590}
591
Jeff Kinga469a102012-12-15 12:42:10 -0500592#undef opterror
593int opterror(const struct option *opt, const char *reason, int flags)
594{
595 if (flags & OPT_SHORT)
596 return error("switch `%c' %s", opt->short_name, reason);
597 if (flags & OPT_UNSET)
598 return error("option `no-%s' %s", opt->long_name, reason);
599 return error("option `%s' %s", opt->long_name, reason);
600}