parseopt: add OPT_NEGBIT
[git/git.git] / parse-options.c
CommitLineData
4a59fd13
PH
1#include "git-compat-util.h"
2#include "parse-options.h"
26141b5b 3#include "cache.h"
269defdf 4#include "commit.h"
4a59fd13
PH
5
6#define OPT_SHORT 1
7#define OPT_UNSET 2
8
4a59fd13
PH
9static int opterror(const struct option *opt, const char *reason, int flags)
10{
11 if (flags & OPT_SHORT)
12 return error("switch `%c' %s", opt->short_name, reason);
13 if (flags & OPT_UNSET)
14 return error("option `no-%s' %s", opt->long_name, reason);
15 return error("option `%s' %s", opt->long_name, reason);
16}
17
1cc6985c
PH
18static int get_arg(struct parse_opt_ctx_t *p, const struct option *opt,
19 int flags, const char **arg)
20{
21 if (p->opt) {
22 *arg = p->opt;
23 p->opt = NULL;
24 } else if (p->argc == 1 && (opt->flags & PARSE_OPT_LASTARG_DEFAULT)) {
25 *arg = (const char *)opt->defval;
d5d745f9 26 } else if (p->argc > 1) {
1cc6985c
PH
27 p->argc--;
28 *arg = *++p->argv;
29 } else
30 return opterror(opt, "requires a value", flags);
31 return 0;
32}
33
7e7bbcb4 34static int get_value(struct parse_opt_ctx_t *p,
1cc6985c 35 const struct option *opt, int flags)
4a59fd13 36{
ffe659f9 37 const char *s, *arg;
db7244bd 38 const int unset = flags & OPT_UNSET;
4a59fd13 39
db7244bd 40 if (unset && p->opt)
4a59fd13 41 return opterror(opt, "takes no value", flags);
db7244bd
PH
42 if (unset && (opt->flags & PARSE_OPT_NONEG))
43 return opterror(opt, "isn't available", flags);
4a59fd13 44
db7244bd
PH
45 if (!(flags & OPT_SHORT) && p->opt) {
46 switch (opt->type) {
47 case OPTION_CALLBACK:
48 if (!(opt->flags & PARSE_OPT_NOARG))
49 break;
50 /* FALLTHROUGH */
51 case OPTION_BOOLEAN:
52 case OPTION_BIT:
2f4b97f9 53 case OPTION_NEGBIT:
db7244bd
PH
54 case OPTION_SET_INT:
55 case OPTION_SET_PTR:
4a59fd13 56 return opterror(opt, "takes no value", flags);
db7244bd
PH
57 default:
58 break;
59 }
60 }
61
db7244bd
PH
62 switch (opt->type) {
63 case OPTION_BIT:
64 if (unset)
65 *(int *)opt->value &= ~opt->defval;
4a59fd13 66 else
db7244bd
PH
67 *(int *)opt->value |= opt->defval;
68 return 0;
69
2f4b97f9
RS
70 case OPTION_NEGBIT:
71 if (unset)
72 *(int *)opt->value |= opt->defval;
73 else
74 *(int *)opt->value &= ~opt->defval;
75 return 0;
76
db7244bd
PH
77 case OPTION_BOOLEAN:
78 *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
79 return 0;
80
81 case OPTION_SET_INT:
82 *(int *)opt->value = unset ? 0 : opt->defval;
83 return 0;
84
85 case OPTION_SET_PTR:
86 *(void **)opt->value = unset ? NULL : (void *)opt->defval;
4a59fd13
PH
87 return 0;
88
89 case OPTION_STRING:
1cc6985c 90 if (unset)
db7244bd 91 *(const char **)opt->value = NULL;
1cc6985c 92 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
ffe659f9 93 *(const char **)opt->value = (const char *)opt->defval;
1cc6985c
PH
94 else
95 return get_arg(p, opt, flags, (const char **)opt->value);
4a59fd13
PH
96 return 0;
97
ffe659f9 98 case OPTION_CALLBACK:
db7244bd 99 if (unset)
07fe54db 100 return (*opt->callback)(opt, NULL, 1) ? (-1) : 0;
db7244bd 101 if (opt->flags & PARSE_OPT_NOARG)
07fe54db 102 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
c43a2483 103 if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
07fe54db 104 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
1cc6985c
PH
105 if (get_arg(p, opt, flags, &arg))
106 return -1;
107 return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
ffe659f9 108
4a59fd13 109 case OPTION_INTEGER:
db7244bd 110 if (unset) {
4a59fd13
PH
111 *(int *)opt->value = 0;
112 return 0;
113 }
c43a2483 114 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
ffe659f9
PH
115 *(int *)opt->value = opt->defval;
116 return 0;
117 }
1cc6985c
PH
118 if (get_arg(p, opt, flags, &arg))
119 return -1;
120 *(int *)opt->value = strtol(arg, (char **)&s, 10);
4a59fd13
PH
121 if (*s)
122 return opterror(opt, "expects a numerical value", flags);
123 return 0;
124
125 default:
126 die("should not happen, someone must be hit on the forehead");
127 }
128}
129
7e7bbcb4 130static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)
4a59fd13
PH
131{
132 for (; options->type != OPTION_END; options++) {
133 if (options->short_name == *p->opt) {
134 p->opt = p->opt[1] ? p->opt + 1 : NULL;
135 return get_value(p, options, OPT_SHORT);
136 }
137 }
07fe54db 138 return -2;
4a59fd13
PH
139}
140
7e7bbcb4 141static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
4a59fd13
PH
142 const struct option *options)
143{
7f275b91 144 const char *arg_end = strchr(arg, '=');
243e0614
JS
145 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
146 int abbrev_flags = 0, ambiguous_flags = 0;
7f275b91
JS
147
148 if (!arg_end)
149 arg_end = arg + strlen(arg);
150
4a59fd13
PH
151 for (; options->type != OPTION_END; options++) {
152 const char *rest;
153 int flags = 0;
154
155 if (!options->long_name)
156 continue;
157
158 rest = skip_prefix(arg, options->long_name);
580d5bff
PH
159 if (options->type == OPTION_ARGUMENT) {
160 if (!rest)
161 continue;
162 if (*rest == '=')
163 return opterror(options, "takes no value", flags);
164 if (*rest)
165 continue;
166 p->out[p->cpidx++] = arg - 2;
167 return 0;
168 }
4a59fd13 169 if (!rest) {
7f275b91
JS
170 /* abbreviated? */
171 if (!strncmp(options->long_name, arg, arg_end - arg)) {
172is_abbreviated:
243e0614
JS
173 if (abbrev_option) {
174 /*
175 * If this is abbreviated, it is
176 * ambiguous. So when there is no
177 * exact match later, we need to
178 * error out.
179 */
180 ambiguous_option = abbrev_option;
181 ambiguous_flags = abbrev_flags;
182 }
7f275b91
JS
183 if (!(flags & OPT_UNSET) && *arg_end)
184 p->opt = arg_end + 1;
185 abbrev_option = options;
186 abbrev_flags = flags;
187 continue;
188 }
189 /* negated and abbreviated very much? */
190 if (!prefixcmp("no-", arg)) {
191 flags |= OPT_UNSET;
192 goto is_abbreviated;
193 }
194 /* negated? */
4a59fd13
PH
195 if (strncmp(arg, "no-", 3))
196 continue;
197 flags |= OPT_UNSET;
198 rest = skip_prefix(arg + 3, options->long_name);
7f275b91
JS
199 /* abbreviated and negated? */
200 if (!rest && !prefixcmp(options->long_name, arg + 3))
201 goto is_abbreviated;
4a59fd13
PH
202 if (!rest)
203 continue;
204 }
205 if (*rest) {
206 if (*rest != '=')
207 continue;
208 p->opt = rest + 1;
209 }
210 return get_value(p, options, flags);
211 }
243e0614
JS
212
213 if (ambiguous_option)
214 return error("Ambiguous option: %s "
215 "(could be --%s%s or --%s%s)",
216 arg,
217 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
218 ambiguous_option->long_name,
219 (abbrev_flags & OPT_UNSET) ? "no-" : "",
220 abbrev_option->long_name);
7f275b91
JS
221 if (abbrev_option)
222 return get_value(p, abbrev_option, abbrev_flags);
07fe54db 223 return -2;
4a59fd13
PH
224}
225
1121a878 226static void check_typos(const char *arg, const struct option *options)
3a9f0f41
PH
227{
228 if (strlen(arg) < 3)
229 return;
230
231 if (!prefixcmp(arg, "no-")) {
232 error ("did you mean `--%s` (with two dashes ?)", arg);
233 exit(129);
234 }
235
236 for (; options->type != OPTION_END; options++) {
237 if (!options->long_name)
238 continue;
239 if (!prefixcmp(options->long_name, arg)) {
240 error ("did you mean `--%s` (with two dashes ?)", arg);
241 exit(129);
242 }
243 }
244}
245
7e7bbcb4
PH
246void parse_options_start(struct parse_opt_ctx_t *ctx,
247 int argc, const char **argv, int flags)
248{
249 memset(ctx, 0, sizeof(*ctx));
250 ctx->argc = argc - 1;
251 ctx->argv = argv + 1;
252 ctx->out = argv;
a32a4eaa 253 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
7e7bbcb4 254 ctx->flags = flags;
0d260f9a
RS
255 if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
256 (flags & PARSE_OPT_STOP_AT_NON_OPTION))
257 die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
7e7bbcb4
PH
258}
259
ee68b87a 260static int usage_with_options_internal(const char * const *,
ff43ec3e 261 const struct option *, int);
dd3bf0f4 262
ff43ec3e
PH
263int parse_options_step(struct parse_opt_ctx_t *ctx,
264 const struct option *options,
265 const char * const usagestr[])
4a59fd13 266{
b92891f9
RS
267 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
268
26141b5b
PH
269 /* we must reset ->opt, unknown short option leave it dangling */
270 ctx->opt = NULL;
271
ff43ec3e
PH
272 for (; ctx->argc; ctx->argc--, ctx->argv++) {
273 const char *arg = ctx->argv[0];
4a59fd13
PH
274
275 if (*arg != '-' || !arg[1]) {
ff43ec3e 276 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
a0ec9d25 277 break;
ff43ec3e 278 ctx->out[ctx->cpidx++] = ctx->argv[0];
4a59fd13
PH
279 continue;
280 }
281
282 if (arg[1] != '-') {
ff43ec3e 283 ctx->opt = arg + 1;
b92891f9 284 if (internal_help && *ctx->opt == 'h')
ff43ec3e 285 return parse_options_usage(usagestr, options);
07fe54db
PH
286 switch (parse_short_opt(ctx, options)) {
287 case -1:
288 return parse_options_usage(usagestr, options);
289 case -2:
b5ce3a54 290 goto unknown;
07fe54db 291 }
ff43ec3e 292 if (ctx->opt)
3a9f0f41 293 check_typos(arg + 1, options);
ff43ec3e 294 while (ctx->opt) {
b92891f9 295 if (internal_help && *ctx->opt == 'h')
ff43ec3e 296 return parse_options_usage(usagestr, options);
07fe54db
PH
297 switch (parse_short_opt(ctx, options)) {
298 case -1:
299 return parse_options_usage(usagestr, options);
300 case -2:
26141b5b
PH
301 /* fake a short option thing to hide the fact that we may have
302 * started to parse aggregated stuff
303 *
304 * This is leaky, too bad.
305 */
306 ctx->argv[0] = xstrdup(ctx->opt - 1);
307 *(char *)ctx->argv[0] = '-';
b5ce3a54 308 goto unknown;
07fe54db 309 }
3a9f0f41 310 }
4a59fd13
PH
311 continue;
312 }
313
314 if (!arg[2]) { /* "--" */
ff43ec3e
PH
315 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
316 ctx->argc--;
317 ctx->argv++;
4a59fd13
PH
318 }
319 break;
320 }
321
b92891f9 322 if (internal_help && !strcmp(arg + 2, "help-all"))
ff43ec3e 323 return usage_with_options_internal(usagestr, options, 1);
b92891f9 324 if (internal_help && !strcmp(arg + 2, "help"))
ff43ec3e 325 return parse_options_usage(usagestr, options);
07fe54db
PH
326 switch (parse_long_opt(ctx, arg + 2, options)) {
327 case -1:
328 return parse_options_usage(usagestr, options);
329 case -2:
b5ce3a54 330 goto unknown;
07fe54db 331 }
b5ce3a54
RS
332 continue;
333unknown:
334 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
335 return PARSE_OPT_UNKNOWN;
336 ctx->out[ctx->cpidx++] = ctx->argv[0];
337 ctx->opt = NULL;
ff43ec3e
PH
338 }
339 return PARSE_OPT_DONE;
340}
341
342int parse_options_end(struct parse_opt_ctx_t *ctx)
343{
344 memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out));
345 ctx->out[ctx->cpidx + ctx->argc] = NULL;
346 return ctx->cpidx + ctx->argc;
347}
348
349int parse_options(int argc, const char **argv, const struct option *options,
350 const char * const usagestr[], int flags)
351{
352 struct parse_opt_ctx_t ctx;
353
354 parse_options_start(&ctx, argc, argv, flags);
355 switch (parse_options_step(&ctx, options, usagestr)) {
356 case PARSE_OPT_HELP:
357 exit(129);
358 case PARSE_OPT_DONE:
359 break;
360 default: /* PARSE_OPT_UNKNOWN */
07fe54db
PH
361 if (ctx.argv[0][1] == '-') {
362 error("unknown option `%s'", ctx.argv[0] + 2);
363 } else {
364 error("unknown switch `%c'", *ctx.opt);
365 }
366 usage_with_options(usagestr, options);
4a59fd13
PH
367 }
368
7e7bbcb4 369 return parse_options_end(&ctx);
4a59fd13 370}
d7a38c54
PH
371
372#define USAGE_OPTS_WIDTH 24
373#define USAGE_GAP 2
374
ee68b87a 375int usage_with_options_internal(const char * const *usagestr,
ff43ec3e 376 const struct option *opts, int full)
d7a38c54 377{
49b61802
RS
378 if (!usagestr)
379 return PARSE_OPT_HELP;
380
f389c808
AR
381 fprintf(stderr, "usage: %s\n", *usagestr++);
382 while (*usagestr && **usagestr)
383 fprintf(stderr, " or: %s\n", *usagestr++);
44d86e91
JK
384 while (*usagestr) {
385 fprintf(stderr, "%s%s\n",
386 **usagestr ? " " : "",
387 *usagestr);
388 usagestr++;
389 }
d7a38c54
PH
390
391 if (opts->type != OPTION_GROUP)
f389c808 392 fputc('\n', stderr);
d7a38c54
PH
393
394 for (; opts->type != OPTION_END; opts++) {
395 size_t pos;
396 int pad;
397
398 if (opts->type == OPTION_GROUP) {
f389c808 399 fputc('\n', stderr);
d7a38c54 400 if (*opts->help)
f389c808 401 fprintf(stderr, "%s\n", opts->help);
d7a38c54
PH
402 continue;
403 }
dd3bf0f4
PH
404 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
405 continue;
d7a38c54 406
f389c808 407 pos = fprintf(stderr, " ");
d7a38c54 408 if (opts->short_name)
f389c808 409 pos += fprintf(stderr, "-%c", opts->short_name);
d7a38c54 410 if (opts->long_name && opts->short_name)
f389c808 411 pos += fprintf(stderr, ", ");
d7a38c54 412 if (opts->long_name)
f389c808 413 pos += fprintf(stderr, "--%s", opts->long_name);
d7a38c54
PH
414
415 switch (opts->type) {
580d5bff
PH
416 case OPTION_ARGUMENT:
417 break;
d7a38c54 418 case OPTION_INTEGER:
ffe659f9 419 if (opts->flags & PARSE_OPT_OPTARG)
6422f633
MB
420 if (opts->long_name)
421 pos += fprintf(stderr, "[=<n>]");
422 else
423 pos += fprintf(stderr, "[<n>]");
ffe659f9
PH
424 else
425 pos += fprintf(stderr, " <n>");
d7a38c54 426 break;
ffe659f9 427 case OPTION_CALLBACK:
f481e22a
PH
428 if (opts->flags & PARSE_OPT_NOARG)
429 break;
430 /* FALLTHROUGH */
431 case OPTION_STRING:
ffe659f9
PH
432 if (opts->argh) {
433 if (opts->flags & PARSE_OPT_OPTARG)
6422f633
MB
434 if (opts->long_name)
435 pos += fprintf(stderr, "[=<%s>]", opts->argh);
436 else
437 pos += fprintf(stderr, "[<%s>]", opts->argh);
ffe659f9
PH
438 else
439 pos += fprintf(stderr, " <%s>", opts->argh);
440 } else {
441 if (opts->flags & PARSE_OPT_OPTARG)
6422f633
MB
442 if (opts->long_name)
443 pos += fprintf(stderr, "[=...]");
444 else
445 pos += fprintf(stderr, "[...]");
ffe659f9
PH
446 else
447 pos += fprintf(stderr, " ...");
448 }
d7a38c54 449 break;
db7244bd 450 default: /* OPTION_{BIT,BOOLEAN,SET_INT,SET_PTR} */
d7a38c54
PH
451 break;
452 }
453
f389c808
AR
454 if (pos <= USAGE_OPTS_WIDTH)
455 pad = USAGE_OPTS_WIDTH - pos;
d7a38c54 456 else {
f389c808 457 fputc('\n', stderr);
d7a38c54
PH
458 pad = USAGE_OPTS_WIDTH;
459 }
f389c808 460 fprintf(stderr, "%*s%s\n", pad + USAGE_GAP, "", opts->help);
d7a38c54 461 }
f389c808
AR
462 fputc('\n', stderr);
463
ee68b87a 464 return PARSE_OPT_HELP;
d7a38c54 465}
0ce865b1 466
dd3bf0f4 467void usage_with_options(const char * const *usagestr,
ff43ec3e 468 const struct option *opts)
dd3bf0f4 469{
ff43ec3e
PH
470 usage_with_options_internal(usagestr, opts, 0);
471 exit(129);
dd3bf0f4
PH
472}
473
ee68b87a
PH
474int parse_options_usage(const char * const *usagestr,
475 const struct option *opts)
476{
ff43ec3e 477 return usage_with_options_internal(usagestr, opts, 0);
ee68b87a
PH
478}
479
480
0ce865b1
PH
481/*----- some often used options -----*/
482#include "cache.h"
db7244bd 483
0ce865b1
PH
484int parse_opt_abbrev_cb(const struct option *opt, const char *arg, int unset)
485{
486 int v;
487
488 if (!arg) {
489 v = unset ? 0 : DEFAULT_ABBREV;
490 } else {
491 v = strtol(arg, (char **)&arg, 10);
492 if (*arg)
493 return opterror(opt, "expects a numerical value", 0);
494 if (v && v < MINIMUM_ABBREV)
495 v = MINIMUM_ABBREV;
496 else if (v > 40)
497 v = 40;
498 }
499 *(int *)(opt->value) = v;
500 return 0;
501}
1f4a711a
MB
502
503int parse_opt_approxidate_cb(const struct option *opt, const char *arg,
504 int unset)
505{
506 *(unsigned long *)(opt->value) = approxidate(arg);
507 return 0;
508}
dbd0f5c7 509
7f87aff2
TA
510int parse_opt_verbosity_cb(const struct option *opt, const char *arg,
511 int unset)
512{
513 int *target = opt->value;
514
515 if (unset)
516 /* --no-quiet, --no-verbose */
517 *target = 0;
518 else if (opt->short_name == 'v') {
519 if (*target >= 0)
520 (*target)++;
521 else
522 *target = 1;
523 } else {
524 if (*target <= 0)
525 (*target)--;
526 else
527 *target = -1;
528 }
529 return 0;
530}
531
269defdf
JG
532int parse_opt_with_commit(const struct option *opt, const char *arg, int unset)
533{
534 unsigned char sha1[20];
535 struct commit *commit;
536
537 if (!arg)
538 return -1;
539 if (get_sha1(arg, sha1))
540 return error("malformed object name %s", arg);
541 commit = lookup_commit_reference(sha1);
542 if (!commit)
543 return error("no such commit %s", arg);
544 commit_list_insert(commit, opt->value);
545 return 0;
546}
547
dbd0f5c7
JH
548/*
549 * This should really be OPTION_FILENAME type as a part of
550 * parse_options that take prefix to do this while parsing.
551 */
552extern const char *parse_options_fix_filename(const char *prefix, const char *file)
553{
554 if (!file || !prefix || is_absolute_path(file) || !strcmp("-", file))
555 return file;
556 return prefix_filename(prefix, strlen(prefix), file);
557}
558