blob: b536896f2689dee23afae7d3a169c065592aaabe [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 Habouzit4a59fd12007-10-15 01:35:37 +0200130 case OPTION_STRING:
Pierre Habouzit1cc69852008-07-08 12:34:08 +0200131 if (unset)
Pierre Habouzitdb7244b2007-11-07 11:20:27 +0100132 *(const char **)opt->value = NULL;
Pierre Habouzit1cc69852008-07-08 12:34:08 +0200133 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
Pierre Habouzitffe659f2007-10-15 01:45:45 +0200134 *(const char **)opt->value = (const char *)opt->defval;
Pierre Habouzit1cc69852008-07-08 12:34:08 +0200135 else
136 return get_arg(p, opt, flags, (const char **)opt->value);
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200137 return 0;
138
Stephen Boyddf217ed2009-05-23 11:53:13 -0700139 case OPTION_FILENAME:
140 err = 0;
141 if (unset)
142 *(const char **)opt->value = NULL;
143 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
144 *(const char **)opt->value = (const char *)opt->defval;
145 else
146 err = get_arg(p, opt, flags, (const char **)opt->value);
147
148 if (!err)
149 fix_filename(p->prefix, (const char **)opt->value);
150 return err;
151
Pierre Habouzitffe659f2007-10-15 01:45:45 +0200152 case OPTION_CALLBACK:
Pierre Habouzitdb7244b2007-11-07 11:20:27 +0100153 if (unset)
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200154 return (*opt->callback)(opt, NULL, 1) ? (-1) : 0;
Pierre Habouzitdb7244b2007-11-07 11:20:27 +0100155 if (opt->flags & PARSE_OPT_NOARG)
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200156 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
Pierre Habouzitc43a2482007-12-21 11:41:41 +0100157 if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200158 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
Pierre Habouzit1cc69852008-07-08 12:34:08 +0200159 if (get_arg(p, opt, flags, &arg))
160 return -1;
161 return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
Pierre Habouzitffe659f2007-10-15 01:45:45 +0200162
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200163 case OPTION_INTEGER:
Pierre Habouzitdb7244b2007-11-07 11:20:27 +0100164 if (unset) {
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200165 *(int *)opt->value = 0;
166 return 0;
167 }
Pierre Habouzitc43a2482007-12-21 11:41:41 +0100168 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
Pierre Habouzitffe659f2007-10-15 01:45:45 +0200169 *(int *)opt->value = opt->defval;
170 return 0;
171 }
Pierre Habouzit1cc69852008-07-08 12:34:08 +0200172 if (get_arg(p, opt, flags, &arg))
173 return -1;
174 *(int *)opt->value = strtol(arg, (char **)&s, 10);
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200175 if (*s)
176 return opterror(opt, "expects a numerical value", flags);
177 return 0;
178
179 default:
180 die("should not happen, someone must be hit on the forehead");
181 }
182}
183
Pierre Habouzit7e7bbcb2008-06-23 21:59:37 +0200184static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200185{
Junio C Hamano11588262013-07-30 12:06:01 -0700186 const struct option *all_opts = options;
René Scharfee0319ff2009-05-07 21:45:08 +0200187 const struct option *numopt = NULL;
188
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200189 for (; options->type != OPTION_END; options++) {
190 if (options->short_name == *p->opt) {
191 p->opt = p->opt[1] ? p->opt + 1 : NULL;
Junio C Hamano11588262013-07-30 12:06:01 -0700192 return get_value(p, options, all_opts, OPT_SHORT);
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200193 }
René Scharfee0319ff2009-05-07 21:45:08 +0200194
195 /*
196 * Handle the numerical option later, explicit one-digit
197 * options take precedence over it.
198 */
199 if (options->type == OPTION_NUMBER)
200 numopt = options;
201 }
202 if (numopt && isdigit(*p->opt)) {
203 size_t len = 1;
204 char *arg;
205 int rc;
206
207 while (isdigit(p->opt[len]))
208 len++;
209 arg = xmemdupz(p->opt, len);
210 p->opt = p->opt[len] ? p->opt + len : NULL;
211 rc = (*numopt->callback)(numopt, arg, 0) ? (-1) : 0;
212 free(arg);
213 return rc;
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200214 }
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200215 return -2;
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200216}
217
Pierre Habouzit7e7bbcb2008-06-23 21:59:37 +0200218static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200219 const struct option *options)
220{
Junio C Hamano11588262013-07-30 12:06:01 -0700221 const struct option *all_opts = options;
Rohit Mani2c5495f2014-03-07 22:48:31 -0800222 const char *arg_end = strchrnul(arg, '=');
Johannes Schindelin243e0612007-11-05 13:15:21 +0000223 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
224 int abbrev_flags = 0, ambiguous_flags = 0;
Johannes Schindelin7f275b92007-10-14 17:54:06 +0100225
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200226 for (; options->type != OPTION_END; options++) {
René Scharfe0f1930c52012-02-25 20:14:54 +0100227 const char *rest, *long_name = options->long_name;
228 int flags = 0, opt_flags = 0;
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200229
René Scharfe0f1930c52012-02-25 20:14:54 +0100230 if (!long_name)
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200231 continue;
232
René Scharfe0f1930c52012-02-25 20:14:54 +0100233again:
234 rest = skip_prefix(arg, long_name);
Pierre Habouzit580d5bf2008-03-02 11:35:56 +0100235 if (options->type == OPTION_ARGUMENT) {
236 if (!rest)
237 continue;
238 if (*rest == '=')
239 return opterror(options, "takes no value", flags);
240 if (*rest)
241 continue;
242 p->out[p->cpidx++] = arg - 2;
243 return 0;
244 }
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200245 if (!rest) {
Johannes Schindelin7f275b92007-10-14 17:54:06 +0100246 /* abbreviated? */
René Scharfe0f1930c52012-02-25 20:14:54 +0100247 if (!strncmp(long_name, arg, arg_end - arg)) {
Johannes Schindelin7f275b92007-10-14 17:54:06 +0100248is_abbreviated:
Johannes Schindelin243e0612007-11-05 13:15:21 +0000249 if (abbrev_option) {
250 /*
251 * If this is abbreviated, it is
252 * ambiguous. So when there is no
253 * exact match later, we need to
254 * error out.
255 */
256 ambiguous_option = abbrev_option;
257 ambiguous_flags = abbrev_flags;
258 }
Johannes Schindelin7f275b92007-10-14 17:54:06 +0100259 if (!(flags & OPT_UNSET) && *arg_end)
260 p->opt = arg_end + 1;
261 abbrev_option = options;
René Scharfe0f1930c52012-02-25 20:14:54 +0100262 abbrev_flags = flags ^ opt_flags;
Johannes Schindelin7f275b92007-10-14 17:54:06 +0100263 continue;
264 }
Andreas Schwab6bbfd1f2009-09-25 20:44:44 +0200265 /* negation allowed? */
266 if (options->flags & PARSE_OPT_NONEG)
267 continue;
Johannes Schindelin7f275b92007-10-14 17:54:06 +0100268 /* negated and abbreviated very much? */
Christian Couder59556542013-11-30 21:55:40 +0100269 if (starts_with("no-", arg)) {
Johannes Schindelin7f275b92007-10-14 17:54:06 +0100270 flags |= OPT_UNSET;
271 goto is_abbreviated;
272 }
273 /* negated? */
Christian Couder59556542013-11-30 21:55:40 +0100274 if (!starts_with(arg, "no-")) {
275 if (starts_with(long_name, "no-")) {
René Scharfe0f1930c52012-02-25 20:14:54 +0100276 long_name += 3;
277 opt_flags |= OPT_UNSET;
278 goto again;
279 }
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200280 continue;
René Scharfe0f1930c52012-02-25 20:14:54 +0100281 }
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200282 flags |= OPT_UNSET;
René Scharfe0f1930c52012-02-25 20:14:54 +0100283 rest = skip_prefix(arg + 3, long_name);
Johannes Schindelin7f275b92007-10-14 17:54:06 +0100284 /* abbreviated and negated? */
Christian Couder59556542013-11-30 21:55:40 +0100285 if (!rest && starts_with(long_name, arg + 3))
Johannes Schindelin7f275b92007-10-14 17:54:06 +0100286 goto is_abbreviated;
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200287 if (!rest)
288 continue;
289 }
290 if (*rest) {
291 if (*rest != '=')
292 continue;
293 p->opt = rest + 1;
294 }
Junio C Hamano11588262013-07-30 12:06:01 -0700295 return get_value(p, options, all_opts, flags ^ opt_flags);
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200296 }
Johannes Schindelin243e0612007-11-05 13:15:21 +0000297
298 if (ambiguous_option)
299 return error("Ambiguous option: %s "
300 "(could be --%s%s or --%s%s)",
301 arg,
302 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
303 ambiguous_option->long_name,
304 (abbrev_flags & OPT_UNSET) ? "no-" : "",
305 abbrev_option->long_name);
Johannes Schindelin7f275b92007-10-14 17:54:06 +0100306 if (abbrev_option)
Junio C Hamano11588262013-07-30 12:06:01 -0700307 return get_value(p, abbrev_option, all_opts, abbrev_flags);
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200308 return -2;
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200309}
310
René Scharfe51a99492009-05-07 21:45:42 +0200311static int parse_nodash_opt(struct parse_opt_ctx_t *p, const char *arg,
312 const struct option *options)
313{
Junio C Hamano11588262013-07-30 12:06:01 -0700314 const struct option *all_opts = options;
315
René Scharfe51a99492009-05-07 21:45:42 +0200316 for (; options->type != OPTION_END; options++) {
317 if (!(options->flags & PARSE_OPT_NODASH))
318 continue;
René Scharfe51a99492009-05-07 21:45:42 +0200319 if (options->short_name == arg[0] && arg[1] == '\0')
Junio C Hamano11588262013-07-30 12:06:01 -0700320 return get_value(p, options, all_opts, OPT_SHORT);
René Scharfe51a99492009-05-07 21:45:42 +0200321 }
322 return -2;
323}
324
Nanako Shiraishi1121a872008-07-16 19:42:18 +0900325static void check_typos(const char *arg, const struct option *options)
Pierre Habouzit3a9f0f42008-01-26 12:26:57 +0100326{
327 if (strlen(arg) < 3)
328 return;
329
Christian Couder59556542013-11-30 21:55:40 +0100330 if (starts_with(arg, "no-")) {
Pierre Habouzit3a9f0f42008-01-26 12:26:57 +0100331 error ("did you mean `--%s` (with two dashes ?)", arg);
332 exit(129);
333 }
334
335 for (; options->type != OPTION_END; options++) {
336 if (!options->long_name)
337 continue;
Christian Couder59556542013-11-30 21:55:40 +0100338 if (starts_with(options->long_name, arg)) {
Pierre Habouzit3a9f0f42008-01-26 12:26:57 +0100339 error ("did you mean `--%s` (with two dashes ?)", arg);
340 exit(129);
341 }
342 }
343}
344
Pierre Habouzitcb9d3982009-06-09 10:23:44 +0200345static void parse_options_check(const struct option *opts)
346{
347 int err = 0;
348
349 for (; opts->type != OPTION_END; opts++) {
350 if ((opts->flags & PARSE_OPT_LASTARG_DEFAULT) &&
Jonathan Nieder1e5ce572010-12-02 00:01:18 -0600351 (opts->flags & PARSE_OPT_OPTARG))
352 err |= optbug(opts, "uses incompatible flags "
353 "LASTARG_DEFAULT and OPTARG");
Jonathan Niedera02dd4f2010-12-02 00:05:05 -0600354 if (opts->flags & PARSE_OPT_NODASH &&
355 ((opts->flags & PARSE_OPT_OPTARG) ||
356 !(opts->flags & PARSE_OPT_NOARG) ||
357 !(opts->flags & PARSE_OPT_NONEG) ||
358 opts->long_name))
359 err |= optbug(opts, "uses feature "
360 "not supported for dashless options");
Jonathan Nieder5c400ed2010-12-02 00:08:57 -0600361 switch (opts->type) {
Junio C Hamanob04ba2b2011-09-27 16:56:49 -0700362 case OPTION_COUNTUP:
Jonathan Nieder5c400ed2010-12-02 00:08:57 -0600363 case OPTION_BIT:
364 case OPTION_NEGBIT:
365 case OPTION_SET_INT:
Jonathan Nieder5c400ed2010-12-02 00:08:57 -0600366 case OPTION_NUMBER:
367 if ((opts->flags & PARSE_OPT_OPTARG) ||
368 !(opts->flags & PARSE_OPT_NOARG))
369 err |= optbug(opts, "should not accept an argument");
370 default:
371 ; /* ok. (usually accepts an argument) */
372 }
Junio C Hamanob6c2a0d2014-03-23 16:04:36 -0700373 if (opts->argh &&
374 strcspn(opts->argh, " _") != strlen(opts->argh))
375 err |= optbug(opts, "multi-word argh should use dash to separate words");
Pierre Habouzitcb9d3982009-06-09 10:23:44 +0200376 }
Pierre Habouzitcb9d3982009-06-09 10:23:44 +0200377 if (err)
Jonathan Nieder1e5ce572010-12-02 00:01:18 -0600378 exit(128);
Pierre Habouzitcb9d3982009-06-09 10:23:44 +0200379}
380
Pierre Habouzit7e7bbcb2008-06-23 21:59:37 +0200381void parse_options_start(struct parse_opt_ctx_t *ctx,
Stephen Boyd37782922009-05-23 11:53:12 -0700382 int argc, const char **argv, const char *prefix,
Stephen Boyd9ca11692010-12-05 23:57:42 -0800383 const struct option *options, int flags)
Pierre Habouzit7e7bbcb2008-06-23 21:59:37 +0200384{
385 memset(ctx, 0, sizeof(*ctx));
386 ctx->argc = argc - 1;
387 ctx->argv = argv + 1;
388 ctx->out = argv;
Stephen Boyd37782922009-05-23 11:53:12 -0700389 ctx->prefix = prefix;
Pierre Habouzita32a4ea2008-06-24 00:31:31 +0200390 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
Pierre Habouzit7e7bbcb2008-06-23 21:59:37 +0200391 ctx->flags = flags;
René Scharfe0d260f92009-03-09 21:57:38 +0100392 if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
393 (flags & PARSE_OPT_STOP_AT_NON_OPTION))
394 die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
Stephen Boyd9ca11692010-12-05 23:57:42 -0800395 parse_options_check(options);
Pierre Habouzit7e7bbcb2008-06-23 21:59:37 +0200396}
397
Thomas Rast47e9cd22010-06-12 14:57:39 +0200398static int usage_with_options_internal(struct parse_opt_ctx_t *,
399 const char * const *,
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200400 const struct option *, int, int);
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200401
402int parse_options_step(struct parse_opt_ctx_t *ctx,
403 const struct option *options,
404 const char * const usagestr[])
405{
René Scharfeb92891f2009-03-08 19:15:08 +0100406 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
407
Pierre Habouzit26141b52008-06-23 22:55:11 +0200408 /* we must reset ->opt, unknown short option leave it dangling */
409 ctx->opt = NULL;
410
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200411 for (; ctx->argc; ctx->argc--, ctx->argv++) {
412 const char *arg = ctx->argv[0];
413
414 if (*arg != '-' || !arg[1]) {
René Scharfe51a99492009-05-07 21:45:42 +0200415 if (parse_nodash_opt(ctx, arg, options) == 0)
416 continue;
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200417 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
Jonathan Nieder979240f2010-12-01 17:32:55 -0600418 return PARSE_OPT_NON_OPTION;
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200419 ctx->out[ctx->cpidx++] = ctx->argv[0];
420 continue;
421 }
422
423 if (arg[1] != '-') {
424 ctx->opt = arg + 1;
René Scharfeb92891f2009-03-08 19:15:08 +0100425 if (internal_help && *ctx->opt == 'h')
Thomas Rast47e9cd22010-06-12 14:57:39 +0200426 return parse_options_usage(ctx, usagestr, options, 0);
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200427 switch (parse_short_opt(ctx, options)) {
428 case -1:
Thomas Rast47e9cd22010-06-12 14:57:39 +0200429 return parse_options_usage(ctx, usagestr, options, 1);
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200430 case -2:
René Scharfe38916c52012-03-03 12:00:29 +0100431 if (ctx->opt)
432 check_typos(arg + 1, options);
René Scharfeb5ce3a52009-03-08 19:12:47 +0100433 goto unknown;
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200434 }
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200435 if (ctx->opt)
436 check_typos(arg + 1, options);
437 while (ctx->opt) {
René Scharfeb92891f2009-03-08 19:15:08 +0100438 if (internal_help && *ctx->opt == 'h')
Thomas Rast47e9cd22010-06-12 14:57:39 +0200439 return parse_options_usage(ctx, usagestr, options, 0);
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200440 switch (parse_short_opt(ctx, options)) {
441 case -1:
Thomas Rast47e9cd22010-06-12 14:57:39 +0200442 return parse_options_usage(ctx, usagestr, options, 1);
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200443 case -2:
Pierre Habouzit26141b52008-06-23 22:55:11 +0200444 /* fake a short option thing to hide the fact that we may have
445 * started to parse aggregated stuff
446 *
447 * This is leaky, too bad.
448 */
449 ctx->argv[0] = xstrdup(ctx->opt - 1);
450 *(char *)ctx->argv[0] = '-';
René Scharfeb5ce3a52009-03-08 19:12:47 +0100451 goto unknown;
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200452 }
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200453 }
454 continue;
455 }
456
457 if (!arg[2]) { /* "--" */
458 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
459 ctx->argc--;
460 ctx->argv++;
461 }
462 break;
463 }
464
René Scharfeb92891f2009-03-08 19:15:08 +0100465 if (internal_help && !strcmp(arg + 2, "help-all"))
Thomas Rast47e9cd22010-06-12 14:57:39 +0200466 return usage_with_options_internal(ctx, usagestr, options, 1, 0);
René Scharfeb92891f2009-03-08 19:15:08 +0100467 if (internal_help && !strcmp(arg + 2, "help"))
Thomas Rast47e9cd22010-06-12 14:57:39 +0200468 return parse_options_usage(ctx, usagestr, options, 0);
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200469 switch (parse_long_opt(ctx, arg + 2, options)) {
470 case -1:
Thomas Rast47e9cd22010-06-12 14:57:39 +0200471 return parse_options_usage(ctx, usagestr, options, 1);
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200472 case -2:
René Scharfeb5ce3a52009-03-08 19:12:47 +0100473 goto unknown;
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200474 }
René Scharfeb5ce3a52009-03-08 19:12:47 +0100475 continue;
476unknown:
477 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
478 return PARSE_OPT_UNKNOWN;
479 ctx->out[ctx->cpidx++] = ctx->argv[0];
480 ctx->opt = NULL;
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200481 }
482 return PARSE_OPT_DONE;
483}
484
Pierre Habouzit7e7bbcb2008-06-23 21:59:37 +0200485int parse_options_end(struct parse_opt_ctx_t *ctx)
486{
487 memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out));
488 ctx->out[ctx->cpidx + ctx->argc] = NULL;
489 return ctx->cpidx + ctx->argc;
490}
491
Stephen Boyd37782922009-05-23 11:53:12 -0700492int parse_options(int argc, const char **argv, const char *prefix,
493 const struct option *options, const char * const usagestr[],
494 int flags)
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200495{
Pierre Habouzit7e7bbcb2008-06-23 21:59:37 +0200496 struct parse_opt_ctx_t ctx;
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200497
Stephen Boyd9ca11692010-12-05 23:57:42 -0800498 parse_options_start(&ctx, argc, argv, prefix, options, flags);
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200499 switch (parse_options_step(&ctx, options, usagestr)) {
500 case PARSE_OPT_HELP:
501 exit(129);
Jonathan Nieder979240f2010-12-01 17:32:55 -0600502 case PARSE_OPT_NON_OPTION:
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200503 case PARSE_OPT_DONE:
504 break;
505 default: /* PARSE_OPT_UNKNOWN */
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200506 if (ctx.argv[0][1] == '-') {
507 error("unknown option `%s'", ctx.argv[0] + 2);
Erik Faye-Lundb141a472013-02-12 00:13:48 +0100508 } else if (isascii(*ctx.opt)) {
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200509 error("unknown switch `%c'", *ctx.opt);
Erik Faye-Lundb141a472013-02-12 00:13:48 +0100510 } else {
511 error("unknown non-ascii option in string: `%s'",
512 ctx.argv[0]);
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200513 }
514 usage_with_options(usagestr, options);
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200515 }
516
Torsten Bögershausen76759c72012-07-08 15:50:25 +0200517 precompose_argv(argc, argv);
Pierre Habouzit7e7bbcb2008-06-23 21:59:37 +0200518 return parse_options_end(&ctx);
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200519}
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200520
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200521static int usage_argh(const struct option *opts, FILE *outfile)
Stephen Boyd29f25d42009-05-21 00:33:17 -0700522{
523 const char *s;
Stephen Boyd34aec9f2009-06-04 16:43:57 -0700524 int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) || !opts->argh;
Stephen Boyd29f25d42009-05-21 00:33:17 -0700525 if (opts->flags & PARSE_OPT_OPTARG)
526 if (opts->long_name)
527 s = literal ? "[=%s]" : "[=<%s>]";
528 else
529 s = literal ? "[%s]" : "[<%s>]";
530 else
531 s = literal ? " %s" : " <%s>";
Jiang Xinc0821962013-02-09 14:31:09 +0800532 return utf8_fprintf(outfile, s, opts->argh ? _(opts->argh) : _("..."));
Stephen Boyd29f25d42009-05-21 00:33:17 -0700533}
534
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200535#define USAGE_OPTS_WIDTH 24
536#define USAGE_GAP 2
537
Thomas Rast47e9cd22010-06-12 14:57:39 +0200538static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
539 const char * const *usagestr,
540 const struct option *opts, int full, int err)
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200541{
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200542 FILE *outfile = err ? stderr : stdout;
543
René Scharfe49b61802009-03-08 19:16:58 +0100544 if (!usagestr)
545 return PARSE_OPT_HELP;
546
Thomas Rast47e9cd22010-06-12 14:57:39 +0200547 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
548 fprintf(outfile, "cat <<\\EOF\n");
549
Nguyễn Thái Ngọc Duy54e6dc72012-05-06 21:23:51 +0700550 fprintf_ln(outfile, _("usage: %s"), _(*usagestr++));
Alex Riesenf389c802007-10-14 00:10:51 +0200551 while (*usagestr && **usagestr)
Nguyễn Thái Ngọc Duy54e6dc72012-05-06 21:23:51 +0700552 /* TRANSLATORS: the colon here should align with the
553 one in "usage: %s" translation */
554 fprintf_ln(outfile, _(" or: %s"), _(*usagestr++));
Jeff King44d86e92008-06-14 03:27:21 -0400555 while (*usagestr) {
Nguyễn Thái Ngọc Duy54e6dc72012-05-06 21:23:51 +0700556 if (**usagestr)
557 fprintf_ln(outfile, _(" %s"), _(*usagestr));
558 else
559 putchar('\n');
Jeff King44d86e92008-06-14 03:27:21 -0400560 usagestr++;
561 }
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200562
563 if (opts->type != OPTION_GROUP)
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200564 fputc('\n', outfile);
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200565
566 for (; opts->type != OPTION_END; opts++) {
567 size_t pos;
568 int pad;
569
570 if (opts->type == OPTION_GROUP) {
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200571 fputc('\n', outfile);
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200572 if (*opts->help)
Nguyễn Thái Ngọc Duy54e6dc72012-05-06 21:23:51 +0700573 fprintf(outfile, "%s\n", _(opts->help));
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200574 continue;
575 }
Pierre Habouzitdd3bf0f2007-11-19 10:21:44 +0100576 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
577 continue;
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200578
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200579 pos = fprintf(outfile, " ");
René Scharfecbb08c22012-02-28 20:06:09 +0100580 if (opts->short_name) {
René Scharfe51a99492009-05-07 21:45:42 +0200581 if (opts->flags & PARSE_OPT_NODASH)
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200582 pos += fprintf(outfile, "%c", opts->short_name);
René Scharfe51a99492009-05-07 21:45:42 +0200583 else
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200584 pos += fprintf(outfile, "-%c", opts->short_name);
René Scharfe51a99492009-05-07 21:45:42 +0200585 }
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200586 if (opts->long_name && opts->short_name)
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200587 pos += fprintf(outfile, ", ");
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200588 if (opts->long_name)
René Scharfecbb08c22012-02-28 20:06:09 +0100589 pos += fprintf(outfile, "--%s", opts->long_name);
René Scharfee0319ff2009-05-07 21:45:08 +0200590 if (opts->type == OPTION_NUMBER)
Jiang Xinc0821962013-02-09 14:31:09 +0800591 pos += utf8_fprintf(outfile, _("-NUM"));
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200592
Jonathan Niederb57c68a2010-12-01 17:31:36 -0600593 if ((opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
594 !(opts->flags & PARSE_OPT_NOARG))
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200595 pos += usage_argh(opts, outfile);
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200596
Alex Riesenf389c802007-10-14 00:10:51 +0200597 if (pos <= USAGE_OPTS_WIDTH)
598 pad = USAGE_OPTS_WIDTH - pos;
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200599 else {
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200600 fputc('\n', outfile);
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200601 pad = USAGE_OPTS_WIDTH;
602 }
Nguyễn Thái Ngọc Duy54e6dc72012-05-06 21:23:51 +0700603 fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", _(opts->help));
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200604 }
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200605 fputc('\n', outfile);
Alex Riesenf389c802007-10-14 00:10:51 +0200606
Thomas Rast47e9cd22010-06-12 14:57:39 +0200607 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
608 fputs("EOF\n", outfile);
609
Pierre Habouzitee68b872008-06-23 22:28:04 +0200610 return PARSE_OPT_HELP;
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200611}
Pierre Habouzit0ce865b2007-10-14 11:05:12 +0200612
Stephen Boydc2e86ad2011-03-22 00:51:05 -0700613void NORETURN usage_with_options(const char * const *usagestr,
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200614 const struct option *opts)
Pierre Habouzitdd3bf0f2007-11-19 10:21:44 +0100615{
Thomas Rast47e9cd22010-06-12 14:57:39 +0200616 usage_with_options_internal(NULL, usagestr, opts, 0, 1);
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200617 exit(129);
Pierre Habouzitdd3bf0f2007-11-19 10:21:44 +0100618}
619
Stephen Boydc2e86ad2011-03-22 00:51:05 -0700620void NORETURN usage_msg_opt(const char *msg,
Christian Couder451bb212009-02-02 06:12:58 +0100621 const char * const *usagestr,
622 const struct option *options)
623{
624 fprintf(stderr, "%s\n\n", msg);
625 usage_with_options(usagestr, options);
626}
627
Thomas Rast47e9cd22010-06-12 14:57:39 +0200628static int parse_options_usage(struct parse_opt_ctx_t *ctx,
629 const char * const *usagestr,
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200630 const struct option *opts, int err)
Pierre Habouzitee68b872008-06-23 22:28:04 +0200631{
Thomas Rast47e9cd22010-06-12 14:57:39 +0200632 return usage_with_options_internal(ctx, usagestr, opts, 0, err);
Pierre Habouzitee68b872008-06-23 22:28:04 +0200633}
634
Jeff Kinga469a102012-12-15 12:42:10 -0500635#undef opterror
636int opterror(const struct option *opt, const char *reason, int flags)
637{
638 if (flags & OPT_SHORT)
639 return error("switch `%c' %s", opt->short_name, reason);
640 if (flags & OPT_UNSET)
641 return error("option `no-%s' %s", opt->long_name, reason);
642 return error("option `%s' %s", opt->long_name, reason);
643}