blob: 73bd28ad90986af1618d82176076133186d045de [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"
Pierre Habouzit4a59fd12007-10-15 01:35:37 +02006
Thomas Rast47e9cd22010-06-12 14:57:39 +02007static int parse_options_usage(struct parse_opt_ctx_t *ctx,
8 const char * const *usagestr,
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +02009 const struct option *opts, int err);
Junio C Hamano41064eb2010-01-11 22:28:45 -080010
Pierre Habouzit4a59fd12007-10-15 01:35:37 +020011#define OPT_SHORT 1
12#define OPT_UNSET 2
13
Jonathan Nieder1e5ce572010-12-02 00:01:18 -060014static int optbug(const struct option *opt, const char *reason)
15{
16 if (opt->long_name)
17 return error("BUG: option '%s' %s", opt->long_name, reason);
18 return error("BUG: switch '%c' %s", opt->short_name, reason);
19}
20
Pierre Habouzit4a59fd12007-10-15 01:35:37 +020021static int opterror(const struct option *opt, const char *reason, int flags)
22{
23 if (flags & OPT_SHORT)
24 return error("switch `%c' %s", opt->short_name, reason);
25 if (flags & OPT_UNSET)
26 return error("option `no-%s' %s", opt->long_name, reason);
27 return error("option `%s' %s", opt->long_name, reason);
28}
29
Pierre Habouzit1cc69852008-07-08 12:34:08 +020030static int get_arg(struct parse_opt_ctx_t *p, const struct option *opt,
31 int flags, const char **arg)
32{
33 if (p->opt) {
34 *arg = p->opt;
35 p->opt = NULL;
36 } else if (p->argc == 1 && (opt->flags & PARSE_OPT_LASTARG_DEFAULT)) {
37 *arg = (const char *)opt->defval;
Olivier Marind5d745f2008-07-21 20:30:36 +020038 } else if (p->argc > 1) {
Pierre Habouzit1cc69852008-07-08 12:34:08 +020039 p->argc--;
40 *arg = *++p->argv;
41 } else
42 return opterror(opt, "requires a value", flags);
43 return 0;
44}
45
Stephen Boyddf217ed2009-05-23 11:53:13 -070046static void fix_filename(const char *prefix, const char **file)
47{
48 if (!file || !*file || !prefix || is_absolute_path(*file)
49 || !strcmp("-", *file))
50 return;
51 *file = xstrdup(prefix_filename(prefix, strlen(prefix), *file));
52}
53
Pierre Habouzit7e7bbcb2008-06-23 21:59:37 +020054static int get_value(struct parse_opt_ctx_t *p,
Pierre Habouzit1cc69852008-07-08 12:34:08 +020055 const struct option *opt, int flags)
Pierre Habouzit4a59fd12007-10-15 01:35:37 +020056{
Pierre Habouzitffe659f2007-10-15 01:45:45 +020057 const char *s, *arg;
Pierre Habouzitdb7244b2007-11-07 11:20:27 +010058 const int unset = flags & OPT_UNSET;
Stephen Boyddf217ed2009-05-23 11:53:13 -070059 int err;
Pierre Habouzit4a59fd12007-10-15 01:35:37 +020060
Pierre Habouzitdb7244b2007-11-07 11:20:27 +010061 if (unset && p->opt)
Pierre Habouzit4a59fd12007-10-15 01:35:37 +020062 return opterror(opt, "takes no value", flags);
Pierre Habouzitdb7244b2007-11-07 11:20:27 +010063 if (unset && (opt->flags & PARSE_OPT_NONEG))
64 return opterror(opt, "isn't available", flags);
Stephen Boydc1f4ec92010-12-01 17:30:40 -060065 if (!(flags & OPT_SHORT) && p->opt && (opt->flags & PARSE_OPT_NOARG))
66 return opterror(opt, "takes no value", flags);
Pierre Habouzitdb7244b2007-11-07 11:20:27 +010067
Pierre Habouzitdb7244b2007-11-07 11:20:27 +010068 switch (opt->type) {
Jonathan Niederb0b3a8b2010-12-01 17:32:16 -060069 case OPTION_LOWLEVEL_CALLBACK:
70 return (*(parse_opt_ll_cb *)opt->callback)(p, opt, unset);
71
Pierre Habouzitdb7244b2007-11-07 11:20:27 +010072 case OPTION_BIT:
73 if (unset)
74 *(int *)opt->value &= ~opt->defval;
Pierre Habouzit4a59fd12007-10-15 01:35:37 +020075 else
Pierre Habouzitdb7244b2007-11-07 11:20:27 +010076 *(int *)opt->value |= opt->defval;
77 return 0;
78
René Scharfe2f4b97f2009-05-07 21:44:17 +020079 case OPTION_NEGBIT:
80 if (unset)
81 *(int *)opt->value |= opt->defval;
82 else
83 *(int *)opt->value &= ~opt->defval;
84 return 0;
85
Pierre Habouzitdb7244b2007-11-07 11:20:27 +010086 case OPTION_BOOLEAN:
87 *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
88 return 0;
89
90 case OPTION_SET_INT:
91 *(int *)opt->value = unset ? 0 : opt->defval;
92 return 0;
93
94 case OPTION_SET_PTR:
95 *(void **)opt->value = unset ? NULL : (void *)opt->defval;
Pierre Habouzit4a59fd12007-10-15 01:35:37 +020096 return 0;
97
98 case OPTION_STRING:
Pierre Habouzit1cc69852008-07-08 12:34:08 +020099 if (unset)
Pierre Habouzitdb7244b2007-11-07 11:20:27 +0100100 *(const char **)opt->value = NULL;
Pierre Habouzit1cc69852008-07-08 12:34:08 +0200101 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
Pierre Habouzitffe659f2007-10-15 01:45:45 +0200102 *(const char **)opt->value = (const char *)opt->defval;
Pierre Habouzit1cc69852008-07-08 12:34:08 +0200103 else
104 return get_arg(p, opt, flags, (const char **)opt->value);
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200105 return 0;
106
Stephen Boyddf217ed2009-05-23 11:53:13 -0700107 case OPTION_FILENAME:
108 err = 0;
109 if (unset)
110 *(const char **)opt->value = NULL;
111 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
112 *(const char **)opt->value = (const char *)opt->defval;
113 else
114 err = get_arg(p, opt, flags, (const char **)opt->value);
115
116 if (!err)
117 fix_filename(p->prefix, (const char **)opt->value);
118 return err;
119
Pierre Habouzitffe659f2007-10-15 01:45:45 +0200120 case OPTION_CALLBACK:
Pierre Habouzitdb7244b2007-11-07 11:20:27 +0100121 if (unset)
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200122 return (*opt->callback)(opt, NULL, 1) ? (-1) : 0;
Pierre Habouzitdb7244b2007-11-07 11:20:27 +0100123 if (opt->flags & PARSE_OPT_NOARG)
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200124 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
Pierre Habouzitc43a2482007-12-21 11:41:41 +0100125 if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200126 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
Pierre Habouzit1cc69852008-07-08 12:34:08 +0200127 if (get_arg(p, opt, flags, &arg))
128 return -1;
129 return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
Pierre Habouzitffe659f2007-10-15 01:45:45 +0200130
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200131 case OPTION_INTEGER:
Pierre Habouzitdb7244b2007-11-07 11:20:27 +0100132 if (unset) {
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200133 *(int *)opt->value = 0;
134 return 0;
135 }
Pierre Habouzitc43a2482007-12-21 11:41:41 +0100136 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
Pierre Habouzitffe659f2007-10-15 01:45:45 +0200137 *(int *)opt->value = opt->defval;
138 return 0;
139 }
Pierre Habouzit1cc69852008-07-08 12:34:08 +0200140 if (get_arg(p, opt, flags, &arg))
141 return -1;
142 *(int *)opt->value = strtol(arg, (char **)&s, 10);
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200143 if (*s)
144 return opterror(opt, "expects a numerical value", flags);
145 return 0;
146
147 default:
148 die("should not happen, someone must be hit on the forehead");
149 }
150}
151
Pierre Habouzit7e7bbcb2008-06-23 21:59:37 +0200152static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200153{
René Scharfee0319ff2009-05-07 21:45:08 +0200154 const struct option *numopt = NULL;
155
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200156 for (; options->type != OPTION_END; options++) {
157 if (options->short_name == *p->opt) {
158 p->opt = p->opt[1] ? p->opt + 1 : NULL;
159 return get_value(p, options, OPT_SHORT);
160 }
René Scharfee0319ff2009-05-07 21:45:08 +0200161
162 /*
163 * Handle the numerical option later, explicit one-digit
164 * options take precedence over it.
165 */
166 if (options->type == OPTION_NUMBER)
167 numopt = options;
168 }
169 if (numopt && isdigit(*p->opt)) {
170 size_t len = 1;
171 char *arg;
172 int rc;
173
174 while (isdigit(p->opt[len]))
175 len++;
176 arg = xmemdupz(p->opt, len);
177 p->opt = p->opt[len] ? p->opt + len : NULL;
178 rc = (*numopt->callback)(numopt, arg, 0) ? (-1) : 0;
179 free(arg);
180 return rc;
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200181 }
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200182 return -2;
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200183}
184
Pierre Habouzit7e7bbcb2008-06-23 21:59:37 +0200185static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200186 const struct option *options)
187{
Johannes Schindelin7f275b92007-10-14 17:54:06 +0100188 const char *arg_end = strchr(arg, '=');
Johannes Schindelin243e0612007-11-05 13:15:21 +0000189 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
190 int abbrev_flags = 0, ambiguous_flags = 0;
Johannes Schindelin7f275b92007-10-14 17:54:06 +0100191
192 if (!arg_end)
193 arg_end = arg + strlen(arg);
194
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200195 for (; options->type != OPTION_END; options++) {
196 const char *rest;
197 int flags = 0;
198
199 if (!options->long_name)
200 continue;
201
202 rest = skip_prefix(arg, options->long_name);
Pierre Habouzit580d5bf2008-03-02 11:35:56 +0100203 if (options->type == OPTION_ARGUMENT) {
204 if (!rest)
205 continue;
206 if (*rest == '=')
207 return opterror(options, "takes no value", flags);
208 if (*rest)
209 continue;
210 p->out[p->cpidx++] = arg - 2;
211 return 0;
212 }
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200213 if (!rest) {
Johannes Schindelin7f275b92007-10-14 17:54:06 +0100214 /* abbreviated? */
215 if (!strncmp(options->long_name, arg, arg_end - arg)) {
216is_abbreviated:
Johannes Schindelin243e0612007-11-05 13:15:21 +0000217 if (abbrev_option) {
218 /*
219 * If this is abbreviated, it is
220 * ambiguous. So when there is no
221 * exact match later, we need to
222 * error out.
223 */
224 ambiguous_option = abbrev_option;
225 ambiguous_flags = abbrev_flags;
226 }
Johannes Schindelin7f275b92007-10-14 17:54:06 +0100227 if (!(flags & OPT_UNSET) && *arg_end)
228 p->opt = arg_end + 1;
229 abbrev_option = options;
230 abbrev_flags = flags;
231 continue;
232 }
Andreas Schwab6bbfd1f2009-09-25 20:44:44 +0200233 /* negation allowed? */
234 if (options->flags & PARSE_OPT_NONEG)
235 continue;
Johannes Schindelin7f275b92007-10-14 17:54:06 +0100236 /* negated and abbreviated very much? */
237 if (!prefixcmp("no-", arg)) {
238 flags |= OPT_UNSET;
239 goto is_abbreviated;
240 }
241 /* negated? */
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200242 if (strncmp(arg, "no-", 3))
243 continue;
244 flags |= OPT_UNSET;
245 rest = skip_prefix(arg + 3, options->long_name);
Johannes Schindelin7f275b92007-10-14 17:54:06 +0100246 /* abbreviated and negated? */
247 if (!rest && !prefixcmp(options->long_name, arg + 3))
248 goto is_abbreviated;
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200249 if (!rest)
250 continue;
251 }
252 if (*rest) {
253 if (*rest != '=')
254 continue;
255 p->opt = rest + 1;
256 }
257 return get_value(p, options, flags);
258 }
Johannes Schindelin243e0612007-11-05 13:15:21 +0000259
260 if (ambiguous_option)
261 return error("Ambiguous option: %s "
262 "(could be --%s%s or --%s%s)",
263 arg,
264 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
265 ambiguous_option->long_name,
266 (abbrev_flags & OPT_UNSET) ? "no-" : "",
267 abbrev_option->long_name);
Johannes Schindelin7f275b92007-10-14 17:54:06 +0100268 if (abbrev_option)
269 return get_value(p, abbrev_option, abbrev_flags);
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200270 return -2;
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200271}
272
René Scharfe51a99492009-05-07 21:45:42 +0200273static int parse_nodash_opt(struct parse_opt_ctx_t *p, const char *arg,
274 const struct option *options)
275{
276 for (; options->type != OPTION_END; options++) {
277 if (!(options->flags & PARSE_OPT_NODASH))
278 continue;
René Scharfe51a99492009-05-07 21:45:42 +0200279 if (options->short_name == arg[0] && arg[1] == '\0')
280 return get_value(p, options, OPT_SHORT);
281 }
282 return -2;
283}
284
Nanako Shiraishi1121a872008-07-16 19:42:18 +0900285static void check_typos(const char *arg, const struct option *options)
Pierre Habouzit3a9f0f42008-01-26 12:26:57 +0100286{
287 if (strlen(arg) < 3)
288 return;
289
290 if (!prefixcmp(arg, "no-")) {
291 error ("did you mean `--%s` (with two dashes ?)", arg);
292 exit(129);
293 }
294
295 for (; options->type != OPTION_END; options++) {
296 if (!options->long_name)
297 continue;
298 if (!prefixcmp(options->long_name, arg)) {
299 error ("did you mean `--%s` (with two dashes ?)", arg);
300 exit(129);
301 }
302 }
303}
304
Pierre Habouzitcb9d3982009-06-09 10:23:44 +0200305static void parse_options_check(const struct option *opts)
306{
307 int err = 0;
308
309 for (; opts->type != OPTION_END; opts++) {
310 if ((opts->flags & PARSE_OPT_LASTARG_DEFAULT) &&
Jonathan Nieder1e5ce572010-12-02 00:01:18 -0600311 (opts->flags & PARSE_OPT_OPTARG))
312 err |= optbug(opts, "uses incompatible flags "
313 "LASTARG_DEFAULT and OPTARG");
Jonathan Niedera02dd4f2010-12-02 00:05:05 -0600314 if (opts->flags & PARSE_OPT_NODASH &&
315 ((opts->flags & PARSE_OPT_OPTARG) ||
316 !(opts->flags & PARSE_OPT_NOARG) ||
317 !(opts->flags & PARSE_OPT_NONEG) ||
318 opts->long_name))
319 err |= optbug(opts, "uses feature "
320 "not supported for dashless options");
Jonathan Nieder5c400ed2010-12-02 00:08:57 -0600321 switch (opts->type) {
322 case OPTION_BOOLEAN:
323 case OPTION_BIT:
324 case OPTION_NEGBIT:
325 case OPTION_SET_INT:
326 case OPTION_SET_PTR:
327 case OPTION_NUMBER:
328 if ((opts->flags & PARSE_OPT_OPTARG) ||
329 !(opts->flags & PARSE_OPT_NOARG))
330 err |= optbug(opts, "should not accept an argument");
331 default:
332 ; /* ok. (usually accepts an argument) */
333 }
Pierre Habouzitcb9d3982009-06-09 10:23:44 +0200334 }
Pierre Habouzitcb9d3982009-06-09 10:23:44 +0200335 if (err)
Jonathan Nieder1e5ce572010-12-02 00:01:18 -0600336 exit(128);
Pierre Habouzitcb9d3982009-06-09 10:23:44 +0200337}
338
Pierre Habouzit7e7bbcb2008-06-23 21:59:37 +0200339void parse_options_start(struct parse_opt_ctx_t *ctx,
Stephen Boyd37782922009-05-23 11:53:12 -0700340 int argc, const char **argv, const char *prefix,
Stephen Boyd9ca11692010-12-05 23:57:42 -0800341 const struct option *options, int flags)
Pierre Habouzit7e7bbcb2008-06-23 21:59:37 +0200342{
343 memset(ctx, 0, sizeof(*ctx));
344 ctx->argc = argc - 1;
345 ctx->argv = argv + 1;
346 ctx->out = argv;
Stephen Boyd37782922009-05-23 11:53:12 -0700347 ctx->prefix = prefix;
Pierre Habouzita32a4ea2008-06-24 00:31:31 +0200348 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
Pierre Habouzit7e7bbcb2008-06-23 21:59:37 +0200349 ctx->flags = flags;
René Scharfe0d260f92009-03-09 21:57:38 +0100350 if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
351 (flags & PARSE_OPT_STOP_AT_NON_OPTION))
352 die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
Stephen Boyd9ca11692010-12-05 23:57:42 -0800353 parse_options_check(options);
Pierre Habouzit7e7bbcb2008-06-23 21:59:37 +0200354}
355
Thomas Rast47e9cd22010-06-12 14:57:39 +0200356static int usage_with_options_internal(struct parse_opt_ctx_t *,
357 const char * const *,
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200358 const struct option *, int, int);
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200359
360int parse_options_step(struct parse_opt_ctx_t *ctx,
361 const struct option *options,
362 const char * const usagestr[])
363{
René Scharfeb92891f2009-03-08 19:15:08 +0100364 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
365
Pierre Habouzit26141b52008-06-23 22:55:11 +0200366 /* we must reset ->opt, unknown short option leave it dangling */
367 ctx->opt = NULL;
368
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200369 for (; ctx->argc; ctx->argc--, ctx->argv++) {
370 const char *arg = ctx->argv[0];
371
372 if (*arg != '-' || !arg[1]) {
René Scharfe51a99492009-05-07 21:45:42 +0200373 if (parse_nodash_opt(ctx, arg, options) == 0)
374 continue;
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200375 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
Jonathan Nieder979240f2010-12-01 17:32:55 -0600376 return PARSE_OPT_NON_OPTION;
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200377 ctx->out[ctx->cpidx++] = ctx->argv[0];
378 continue;
379 }
380
381 if (arg[1] != '-') {
382 ctx->opt = arg + 1;
René Scharfeb92891f2009-03-08 19:15:08 +0100383 if (internal_help && *ctx->opt == 'h')
Thomas Rast47e9cd22010-06-12 14:57:39 +0200384 return parse_options_usage(ctx, usagestr, options, 0);
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200385 switch (parse_short_opt(ctx, options)) {
386 case -1:
Thomas Rast47e9cd22010-06-12 14:57:39 +0200387 return parse_options_usage(ctx, usagestr, options, 1);
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200388 case -2:
René Scharfeb5ce3a52009-03-08 19:12:47 +0100389 goto unknown;
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200390 }
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200391 if (ctx->opt)
392 check_typos(arg + 1, options);
393 while (ctx->opt) {
René Scharfeb92891f2009-03-08 19:15:08 +0100394 if (internal_help && *ctx->opt == 'h')
Thomas Rast47e9cd22010-06-12 14:57:39 +0200395 return parse_options_usage(ctx, usagestr, options, 0);
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200396 switch (parse_short_opt(ctx, options)) {
397 case -1:
Thomas Rast47e9cd22010-06-12 14:57:39 +0200398 return parse_options_usage(ctx, usagestr, options, 1);
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200399 case -2:
Pierre Habouzit26141b52008-06-23 22:55:11 +0200400 /* fake a short option thing to hide the fact that we may have
401 * started to parse aggregated stuff
402 *
403 * This is leaky, too bad.
404 */
405 ctx->argv[0] = xstrdup(ctx->opt - 1);
406 *(char *)ctx->argv[0] = '-';
René Scharfeb5ce3a52009-03-08 19:12:47 +0100407 goto unknown;
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200408 }
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200409 }
410 continue;
411 }
412
413 if (!arg[2]) { /* "--" */
414 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
415 ctx->argc--;
416 ctx->argv++;
417 }
418 break;
419 }
420
René Scharfeb92891f2009-03-08 19:15:08 +0100421 if (internal_help && !strcmp(arg + 2, "help-all"))
Thomas Rast47e9cd22010-06-12 14:57:39 +0200422 return usage_with_options_internal(ctx, usagestr, options, 1, 0);
René Scharfeb92891f2009-03-08 19:15:08 +0100423 if (internal_help && !strcmp(arg + 2, "help"))
Thomas Rast47e9cd22010-06-12 14:57:39 +0200424 return parse_options_usage(ctx, usagestr, options, 0);
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200425 switch (parse_long_opt(ctx, arg + 2, options)) {
426 case -1:
Thomas Rast47e9cd22010-06-12 14:57:39 +0200427 return parse_options_usage(ctx, usagestr, options, 1);
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200428 case -2:
René Scharfeb5ce3a52009-03-08 19:12:47 +0100429 goto unknown;
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200430 }
René Scharfeb5ce3a52009-03-08 19:12:47 +0100431 continue;
432unknown:
433 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
434 return PARSE_OPT_UNKNOWN;
435 ctx->out[ctx->cpidx++] = ctx->argv[0];
436 ctx->opt = NULL;
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200437 }
438 return PARSE_OPT_DONE;
439}
440
Pierre Habouzit7e7bbcb2008-06-23 21:59:37 +0200441int parse_options_end(struct parse_opt_ctx_t *ctx)
442{
443 memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out));
444 ctx->out[ctx->cpidx + ctx->argc] = NULL;
445 return ctx->cpidx + ctx->argc;
446}
447
Stephen Boyd37782922009-05-23 11:53:12 -0700448int parse_options(int argc, const char **argv, const char *prefix,
449 const struct option *options, const char * const usagestr[],
450 int flags)
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200451{
Pierre Habouzit7e7bbcb2008-06-23 21:59:37 +0200452 struct parse_opt_ctx_t ctx;
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200453
Stephen Boyd9ca11692010-12-05 23:57:42 -0800454 parse_options_start(&ctx, argc, argv, prefix, options, flags);
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200455 switch (parse_options_step(&ctx, options, usagestr)) {
456 case PARSE_OPT_HELP:
457 exit(129);
Jonathan Nieder979240f2010-12-01 17:32:55 -0600458 case PARSE_OPT_NON_OPTION:
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200459 case PARSE_OPT_DONE:
460 break;
461 default: /* PARSE_OPT_UNKNOWN */
Pierre Habouzit07fe54d2008-06-23 22:46:36 +0200462 if (ctx.argv[0][1] == '-') {
463 error("unknown option `%s'", ctx.argv[0] + 2);
464 } else {
465 error("unknown switch `%c'", *ctx.opt);
466 }
467 usage_with_options(usagestr, options);
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200468 }
469
Pierre Habouzit7e7bbcb2008-06-23 21:59:37 +0200470 return parse_options_end(&ctx);
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200471}
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200472
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200473static int usage_argh(const struct option *opts, FILE *outfile)
Stephen Boyd29f25d42009-05-21 00:33:17 -0700474{
475 const char *s;
Stephen Boyd34aec9f2009-06-04 16:43:57 -0700476 int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) || !opts->argh;
Stephen Boyd29f25d42009-05-21 00:33:17 -0700477 if (opts->flags & PARSE_OPT_OPTARG)
478 if (opts->long_name)
479 s = literal ? "[=%s]" : "[=<%s>]";
480 else
481 s = literal ? "[%s]" : "[<%s>]";
482 else
483 s = literal ? " %s" : " <%s>";
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200484 return fprintf(outfile, s, opts->argh ? opts->argh : "...");
Stephen Boyd29f25d42009-05-21 00:33:17 -0700485}
486
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200487#define USAGE_OPTS_WIDTH 24
488#define USAGE_GAP 2
489
Thomas Rast47e9cd22010-06-12 14:57:39 +0200490static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
491 const char * const *usagestr,
492 const struct option *opts, int full, int err)
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200493{
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200494 FILE *outfile = err ? stderr : stdout;
495
René Scharfe49b61802009-03-08 19:16:58 +0100496 if (!usagestr)
497 return PARSE_OPT_HELP;
498
Thomas Rast47e9cd22010-06-12 14:57:39 +0200499 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
500 fprintf(outfile, "cat <<\\EOF\n");
501
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200502 fprintf(outfile, "usage: %s\n", *usagestr++);
Alex Riesenf389c802007-10-14 00:10:51 +0200503 while (*usagestr && **usagestr)
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200504 fprintf(outfile, " or: %s\n", *usagestr++);
Jeff King44d86e92008-06-14 03:27:21 -0400505 while (*usagestr) {
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200506 fprintf(outfile, "%s%s\n",
Jeff King44d86e92008-06-14 03:27:21 -0400507 **usagestr ? " " : "",
508 *usagestr);
509 usagestr++;
510 }
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200511
512 if (opts->type != OPTION_GROUP)
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200513 fputc('\n', outfile);
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200514
515 for (; opts->type != OPTION_END; opts++) {
516 size_t pos;
517 int pad;
518
519 if (opts->type == OPTION_GROUP) {
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200520 fputc('\n', outfile);
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200521 if (*opts->help)
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200522 fprintf(outfile, "%s\n", opts->help);
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200523 continue;
524 }
Pierre Habouzitdd3bf0f2007-11-19 10:21:44 +0100525 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
526 continue;
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200527
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200528 pos = fprintf(outfile, " ");
Johannes Schindelin86b5efb2009-07-27 20:49:56 +0200529 if (opts->short_name && !(opts->flags & PARSE_OPT_NEGHELP)) {
René Scharfe51a99492009-05-07 21:45:42 +0200530 if (opts->flags & PARSE_OPT_NODASH)
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200531 pos += fprintf(outfile, "%c", opts->short_name);
René Scharfe51a99492009-05-07 21:45:42 +0200532 else
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200533 pos += fprintf(outfile, "-%c", opts->short_name);
René Scharfe51a99492009-05-07 21:45:42 +0200534 }
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200535 if (opts->long_name && opts->short_name)
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200536 pos += fprintf(outfile, ", ");
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200537 if (opts->long_name)
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200538 pos += fprintf(outfile, "--%s%s",
Johannes Schindelin86b5efb2009-07-27 20:49:56 +0200539 (opts->flags & PARSE_OPT_NEGHELP) ? "no-" : "",
540 opts->long_name);
René Scharfee0319ff2009-05-07 21:45:08 +0200541 if (opts->type == OPTION_NUMBER)
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200542 pos += fprintf(outfile, "-NUM");
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200543
Jonathan Niederb57c68a2010-12-01 17:31:36 -0600544 if ((opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
545 !(opts->flags & PARSE_OPT_NOARG))
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200546 pos += usage_argh(opts, outfile);
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200547
Alex Riesenf389c802007-10-14 00:10:51 +0200548 if (pos <= USAGE_OPTS_WIDTH)
549 pad = USAGE_OPTS_WIDTH - pos;
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200550 else {
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200551 fputc('\n', outfile);
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200552 pad = USAGE_OPTS_WIDTH;
553 }
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200554 fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", opts->help);
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200555 }
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200556 fputc('\n', outfile);
Alex Riesenf389c802007-10-14 00:10:51 +0200557
Thomas Rast47e9cd22010-06-12 14:57:39 +0200558 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
559 fputs("EOF\n", outfile);
560
Pierre Habouzitee68b872008-06-23 22:28:04 +0200561 return PARSE_OPT_HELP;
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200562}
Pierre Habouzit0ce865b2007-10-14 11:05:12 +0200563
Stephen Boydc2e86ad2011-03-22 00:51:05 -0700564void NORETURN usage_with_options(const char * const *usagestr,
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200565 const struct option *opts)
Pierre Habouzitdd3bf0f2007-11-19 10:21:44 +0100566{
Thomas Rast47e9cd22010-06-12 14:57:39 +0200567 usage_with_options_internal(NULL, usagestr, opts, 0, 1);
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200568 exit(129);
Pierre Habouzitdd3bf0f2007-11-19 10:21:44 +0100569}
570
Stephen Boydc2e86ad2011-03-22 00:51:05 -0700571void NORETURN usage_msg_opt(const char *msg,
Christian Couder451bb212009-02-02 06:12:58 +0100572 const char * const *usagestr,
573 const struct option *options)
574{
575 fprintf(stderr, "%s\n\n", msg);
576 usage_with_options(usagestr, options);
577}
578
Thomas Rast47e9cd22010-06-12 14:57:39 +0200579static int parse_options_usage(struct parse_opt_ctx_t *ctx,
580 const char * const *usagestr,
Giuseppe Scrivano9c7304e2010-05-17 17:34:41 +0200581 const struct option *opts, int err)
Pierre Habouzitee68b872008-06-23 22:28:04 +0200582{
Thomas Rast47e9cd22010-06-12 14:57:39 +0200583 return usage_with_options_internal(ctx, usagestr, opts, 0, err);
Pierre Habouzitee68b872008-06-23 22:28:04 +0200584}
585
586
Pierre Habouzit0ce865b2007-10-14 11:05:12 +0200587/*----- some often used options -----*/
588#include "cache.h"
Pierre Habouzitdb7244b2007-11-07 11:20:27 +0100589
Pierre Habouzit0ce865b2007-10-14 11:05:12 +0200590int parse_opt_abbrev_cb(const struct option *opt, const char *arg, int unset)
591{
592 int v;
593
594 if (!arg) {
595 v = unset ? 0 : DEFAULT_ABBREV;
596 } else {
597 v = strtol(arg, (char **)&arg, 10);
598 if (*arg)
599 return opterror(opt, "expects a numerical value", 0);
600 if (v && v < MINIMUM_ABBREV)
601 v = MINIMUM_ABBREV;
602 else if (v > 40)
603 v = 40;
604 }
605 *(int *)(opt->value) = v;
606 return 0;
607}
Michele Ballabio1f4a7112008-03-24 15:02:21 +0100608
609int parse_opt_approxidate_cb(const struct option *opt, const char *arg,
610 int unset)
611{
612 *(unsigned long *)(opt->value) = approxidate(arg);
613 return 0;
614}
Junio C Hamanodbd0f5c2008-08-06 11:43:47 -0700615
Mark Lodato73e9da02010-02-16 23:55:58 -0500616int parse_opt_color_flag_cb(const struct option *opt, const char *arg,
617 int unset)
618{
619 int value;
620
621 if (!arg)
622 arg = unset ? "never" : (const char *)opt->defval;
623 value = git_config_colorbool(NULL, arg, -1);
624 if (value < 0)
625 return opterror(opt,
626 "expects \"always\", \"auto\", or \"never\"", 0);
627 *(int *)opt->value = value;
628 return 0;
629}
630
Tuncer Ayaz7f87aff2008-11-15 01:14:24 +0100631int parse_opt_verbosity_cb(const struct option *opt, const char *arg,
632 int unset)
633{
634 int *target = opt->value;
635
636 if (unset)
637 /* --no-quiet, --no-verbose */
638 *target = 0;
639 else if (opt->short_name == 'v') {
640 if (*target >= 0)
641 (*target)++;
642 else
643 *target = 1;
644 } else {
645 if (*target <= 0)
646 (*target)--;
647 else
648 *target = -1;
649 }
650 return 0;
651}
652
Jake Goulding269defd2009-01-26 09:13:23 -0500653int parse_opt_with_commit(const struct option *opt, const char *arg, int unset)
654{
655 unsigned char sha1[20];
656 struct commit *commit;
657
658 if (!arg)
659 return -1;
660 if (get_sha1(arg, sha1))
661 return error("malformed object name %s", arg);
662 commit = lookup_commit_reference(sha1);
663 if (!commit)
664 return error("no such commit %s", arg);
665 commit_list_insert(commit, opt->value);
666 return 0;
667}
Junio C Hamanocb6020b2009-12-04 00:20:48 -0800668
669int parse_opt_tertiary(const struct option *opt, const char *arg, int unset)
670{
671 int *target = opt->value;
672 *target = unset ? 2 : 1;
673 return 0;
674}
Junio C Hamano8b74d752010-03-06 21:34:40 +0100675
676int parse_options_concat(struct option *dst, size_t dst_size, struct option *src)
677{
678 int i, j;
679
680 for (i = 0; i < dst_size; i++)
681 if (dst[i].type == OPTION_END)
682 break;
683 for (j = 0; i < dst_size; i++, j++) {
684 dst[i] = src[j];
685 if (src[j].type == OPTION_END)
686 return 0;
687 }
688 return -1;
689}