parse-options: clearer reporting of API misuse
[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"
73e9da01 5#include "color.h"
4a59fd13 6
47e9cd28
TR
7static int parse_options_usage(struct parse_opt_ctx_t *ctx,
8 const char * const *usagestr,
9c7304e3 9 const struct option *opts, int err);
41064ebc 10
4a59fd13
PH
11#define OPT_SHORT 1
12#define OPT_UNSET 2
13
1e5ce570
JN
14static 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
4a59fd13
PH
21static 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
1cc6985c
PH
30static 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;
d5d745f9 38 } else if (p->argc > 1) {
1cc6985c
PH
39 p->argc--;
40 *arg = *++p->argv;
41 } else
42 return opterror(opt, "requires a value", flags);
43 return 0;
44}
45
df217ed6
SB
46static 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
7e7bbcb4 54static int get_value(struct parse_opt_ctx_t *p,
1cc6985c 55 const struct option *opt, int flags)
4a59fd13 56{
ffe659f9 57 const char *s, *arg;
db7244bd 58 const int unset = flags & OPT_UNSET;
df217ed6 59 int err;
4a59fd13 60
db7244bd 61 if (unset && p->opt)
4a59fd13 62 return opterror(opt, "takes no value", flags);
db7244bd
PH
63 if (unset && (opt->flags & PARSE_OPT_NONEG))
64 return opterror(opt, "isn't available", flags);
4a59fd13 65
db7244bd
PH
66 if (!(flags & OPT_SHORT) && p->opt) {
67 switch (opt->type) {
68 case OPTION_CALLBACK:
69 if (!(opt->flags & PARSE_OPT_NOARG))
70 break;
71 /* FALLTHROUGH */
72 case OPTION_BOOLEAN:
73 case OPTION_BIT:
2f4b97f9 74 case OPTION_NEGBIT:
db7244bd
PH
75 case OPTION_SET_INT:
76 case OPTION_SET_PTR:
4a59fd13 77 return opterror(opt, "takes no value", flags);
db7244bd
PH
78 default:
79 break;
80 }
81 }
82
db7244bd
PH
83 switch (opt->type) {
84 case OPTION_BIT:
85 if (unset)
86 *(int *)opt->value &= ~opt->defval;
4a59fd13 87 else
db7244bd
PH
88 *(int *)opt->value |= opt->defval;
89 return 0;
90
2f4b97f9
RS
91 case OPTION_NEGBIT:
92 if (unset)
93 *(int *)opt->value |= opt->defval;
94 else
95 *(int *)opt->value &= ~opt->defval;
96 return 0;
97
db7244bd
PH
98 case OPTION_BOOLEAN:
99 *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
100 return 0;
101
102 case OPTION_SET_INT:
103 *(int *)opt->value = unset ? 0 : opt->defval;
104 return 0;
105
106 case OPTION_SET_PTR:
107 *(void **)opt->value = unset ? NULL : (void *)opt->defval;
4a59fd13
PH
108 return 0;
109
110 case OPTION_STRING:
1cc6985c 111 if (unset)
db7244bd 112 *(const char **)opt->value = NULL;
1cc6985c 113 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
ffe659f9 114 *(const char **)opt->value = (const char *)opt->defval;
1cc6985c
PH
115 else
116 return get_arg(p, opt, flags, (const char **)opt->value);
4a59fd13
PH
117 return 0;
118
df217ed6
SB
119 case OPTION_FILENAME:
120 err = 0;
121 if (unset)
122 *(const char **)opt->value = NULL;
123 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
124 *(const char **)opt->value = (const char *)opt->defval;
125 else
126 err = get_arg(p, opt, flags, (const char **)opt->value);
127
128 if (!err)
129 fix_filename(p->prefix, (const char **)opt->value);
130 return err;
131
ffe659f9 132 case OPTION_CALLBACK:
db7244bd 133 if (unset)
07fe54db 134 return (*opt->callback)(opt, NULL, 1) ? (-1) : 0;
db7244bd 135 if (opt->flags & PARSE_OPT_NOARG)
07fe54db 136 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
c43a2483 137 if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
07fe54db 138 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
1cc6985c
PH
139 if (get_arg(p, opt, flags, &arg))
140 return -1;
141 return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
ffe659f9 142
4a59fd13 143 case OPTION_INTEGER:
db7244bd 144 if (unset) {
4a59fd13
PH
145 *(int *)opt->value = 0;
146 return 0;
147 }
c43a2483 148 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
ffe659f9
PH
149 *(int *)opt->value = opt->defval;
150 return 0;
151 }
1cc6985c
PH
152 if (get_arg(p, opt, flags, &arg))
153 return -1;
154 *(int *)opt->value = strtol(arg, (char **)&s, 10);
4a59fd13
PH
155 if (*s)
156 return opterror(opt, "expects a numerical value", flags);
157 return 0;
158
159 default:
160 die("should not happen, someone must be hit on the forehead");
161 }
162}
163
7e7bbcb4 164static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)
4a59fd13 165{
e0319ff5
RS
166 const struct option *numopt = NULL;
167
4a59fd13
PH
168 for (; options->type != OPTION_END; options++) {
169 if (options->short_name == *p->opt) {
170 p->opt = p->opt[1] ? p->opt + 1 : NULL;
171 return get_value(p, options, OPT_SHORT);
172 }
e0319ff5
RS
173
174 /*
175 * Handle the numerical option later, explicit one-digit
176 * options take precedence over it.
177 */
178 if (options->type == OPTION_NUMBER)
179 numopt = options;
180 }
181 if (numopt && isdigit(*p->opt)) {
182 size_t len = 1;
183 char *arg;
184 int rc;
185
186 while (isdigit(p->opt[len]))
187 len++;
188 arg = xmemdupz(p->opt, len);
189 p->opt = p->opt[len] ? p->opt + len : NULL;
190 rc = (*numopt->callback)(numopt, arg, 0) ? (-1) : 0;
191 free(arg);
192 return rc;
4a59fd13 193 }
07fe54db 194 return -2;
4a59fd13
PH
195}
196
7e7bbcb4 197static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
4a59fd13
PH
198 const struct option *options)
199{
7f275b91 200 const char *arg_end = strchr(arg, '=');
243e0614
JS
201 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
202 int abbrev_flags = 0, ambiguous_flags = 0;
7f275b91
JS
203
204 if (!arg_end)
205 arg_end = arg + strlen(arg);
206
4a59fd13
PH
207 for (; options->type != OPTION_END; options++) {
208 const char *rest;
209 int flags = 0;
210
211 if (!options->long_name)
212 continue;
213
214 rest = skip_prefix(arg, options->long_name);
580d5bff
PH
215 if (options->type == OPTION_ARGUMENT) {
216 if (!rest)
217 continue;
218 if (*rest == '=')
219 return opterror(options, "takes no value", flags);
220 if (*rest)
221 continue;
222 p->out[p->cpidx++] = arg - 2;
223 return 0;
224 }
4a59fd13 225 if (!rest) {
7f275b91
JS
226 /* abbreviated? */
227 if (!strncmp(options->long_name, arg, arg_end - arg)) {
228is_abbreviated:
243e0614
JS
229 if (abbrev_option) {
230 /*
231 * If this is abbreviated, it is
232 * ambiguous. So when there is no
233 * exact match later, we need to
234 * error out.
235 */
236 ambiguous_option = abbrev_option;
237 ambiguous_flags = abbrev_flags;
238 }
7f275b91
JS
239 if (!(flags & OPT_UNSET) && *arg_end)
240 p->opt = arg_end + 1;
241 abbrev_option = options;
242 abbrev_flags = flags;
243 continue;
244 }
6bbfd1fa
AS
245 /* negation allowed? */
246 if (options->flags & PARSE_OPT_NONEG)
247 continue;
7f275b91
JS
248 /* negated and abbreviated very much? */
249 if (!prefixcmp("no-", arg)) {
250 flags |= OPT_UNSET;
251 goto is_abbreviated;
252 }
253 /* negated? */
4a59fd13
PH
254 if (strncmp(arg, "no-", 3))
255 continue;
256 flags |= OPT_UNSET;
257 rest = skip_prefix(arg + 3, options->long_name);
7f275b91
JS
258 /* abbreviated and negated? */
259 if (!rest && !prefixcmp(options->long_name, arg + 3))
260 goto is_abbreviated;
4a59fd13
PH
261 if (!rest)
262 continue;
263 }
264 if (*rest) {
265 if (*rest != '=')
266 continue;
267 p->opt = rest + 1;
268 }
269 return get_value(p, options, flags);
270 }
243e0614
JS
271
272 if (ambiguous_option)
273 return error("Ambiguous option: %s "
274 "(could be --%s%s or --%s%s)",
275 arg,
276 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
277 ambiguous_option->long_name,
278 (abbrev_flags & OPT_UNSET) ? "no-" : "",
279 abbrev_option->long_name);
7f275b91
JS
280 if (abbrev_option)
281 return get_value(p, abbrev_option, abbrev_flags);
07fe54db 282 return -2;
4a59fd13
PH
283}
284
51a9949e
RS
285static int parse_nodash_opt(struct parse_opt_ctx_t *p, const char *arg,
286 const struct option *options)
287{
288 for (; options->type != OPTION_END; options++) {
289 if (!(options->flags & PARSE_OPT_NODASH))
290 continue;
291 if ((options->flags & PARSE_OPT_OPTARG) ||
292 !(options->flags & PARSE_OPT_NOARG))
293 die("BUG: dashless options don't support arguments");
294 if (!(options->flags & PARSE_OPT_NONEG))
295 die("BUG: dashless options don't support negation");
296 if (options->long_name)
297 die("BUG: dashless options can't be long");
298 if (options->short_name == arg[0] && arg[1] == '\0')
299 return get_value(p, options, OPT_SHORT);
300 }
301 return -2;
302}
303
1121a878 304static void check_typos(const char *arg, const struct option *options)
3a9f0f41
PH
305{
306 if (strlen(arg) < 3)
307 return;
308
309 if (!prefixcmp(arg, "no-")) {
310 error ("did you mean `--%s` (with two dashes ?)", arg);
311 exit(129);
312 }
313
314 for (; options->type != OPTION_END; options++) {
315 if (!options->long_name)
316 continue;
317 if (!prefixcmp(options->long_name, arg)) {
318 error ("did you mean `--%s` (with two dashes ?)", arg);
319 exit(129);
320 }
321 }
322}
323
cb9d398c
PH
324static void parse_options_check(const struct option *opts)
325{
326 int err = 0;
327
328 for (; opts->type != OPTION_END; opts++) {
329 if ((opts->flags & PARSE_OPT_LASTARG_DEFAULT) &&
1e5ce570
JN
330 (opts->flags & PARSE_OPT_OPTARG))
331 err |= optbug(opts, "uses incompatible flags "
332 "LASTARG_DEFAULT and OPTARG");
cb9d398c 333 }
cb9d398c 334 if (err)
1e5ce570 335 exit(128);
cb9d398c
PH
336}
337
7e7bbcb4 338void parse_options_start(struct parse_opt_ctx_t *ctx,
37782920 339 int argc, const char **argv, const char *prefix,
9ca1169f 340 const struct option *options, int flags)
7e7bbcb4
PH
341{
342 memset(ctx, 0, sizeof(*ctx));
343 ctx->argc = argc - 1;
344 ctx->argv = argv + 1;
345 ctx->out = argv;
37782920 346 ctx->prefix = prefix;
a32a4eaa 347 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
7e7bbcb4 348 ctx->flags = flags;
0d260f9a
RS
349 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");
9ca1169f 352 parse_options_check(options);
7e7bbcb4
PH
353}
354
47e9cd28
TR
355static int usage_with_options_internal(struct parse_opt_ctx_t *,
356 const char * const *,
9c7304e3 357 const struct option *, int, int);
dd3bf0f4 358
ff43ec3e
PH
359int parse_options_step(struct parse_opt_ctx_t *ctx,
360 const struct option *options,
361 const char * const usagestr[])
4a59fd13 362{
b92891f9
RS
363 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
364
26141b5b
PH
365 /* we must reset ->opt, unknown short option leave it dangling */
366 ctx->opt = NULL;
367
ff43ec3e
PH
368 for (; ctx->argc; ctx->argc--, ctx->argv++) {
369 const char *arg = ctx->argv[0];
4a59fd13
PH
370
371 if (*arg != '-' || !arg[1]) {
51a9949e
RS
372 if (parse_nodash_opt(ctx, arg, options) == 0)
373 continue;
ff43ec3e 374 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
a0ec9d25 375 break;
ff43ec3e 376 ctx->out[ctx->cpidx++] = ctx->argv[0];
4a59fd13
PH
377 continue;
378 }
379
380 if (arg[1] != '-') {
ff43ec3e 381 ctx->opt = arg + 1;
b92891f9 382 if (internal_help && *ctx->opt == 'h')
47e9cd28 383 return parse_options_usage(ctx, usagestr, options, 0);
07fe54db
PH
384 switch (parse_short_opt(ctx, options)) {
385 case -1:
47e9cd28 386 return parse_options_usage(ctx, usagestr, options, 1);
07fe54db 387 case -2:
b5ce3a54 388 goto unknown;
07fe54db 389 }
ff43ec3e 390 if (ctx->opt)
3a9f0f41 391 check_typos(arg + 1, options);
ff43ec3e 392 while (ctx->opt) {
b92891f9 393 if (internal_help && *ctx->opt == 'h')
47e9cd28 394 return parse_options_usage(ctx, usagestr, options, 0);
07fe54db
PH
395 switch (parse_short_opt(ctx, options)) {
396 case -1:
47e9cd28 397 return parse_options_usage(ctx, usagestr, options, 1);
07fe54db 398 case -2:
26141b5b
PH
399 /* fake a short option thing to hide the fact that we may have
400 * started to parse aggregated stuff
401 *
402 * This is leaky, too bad.
403 */
404 ctx->argv[0] = xstrdup(ctx->opt - 1);
405 *(char *)ctx->argv[0] = '-';
b5ce3a54 406 goto unknown;
07fe54db 407 }
3a9f0f41 408 }
4a59fd13
PH
409 continue;
410 }
411
412 if (!arg[2]) { /* "--" */
ff43ec3e
PH
413 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
414 ctx->argc--;
415 ctx->argv++;
4a59fd13
PH
416 }
417 break;
418 }
419
b92891f9 420 if (internal_help && !strcmp(arg + 2, "help-all"))
47e9cd28 421 return usage_with_options_internal(ctx, usagestr, options, 1, 0);
b92891f9 422 if (internal_help && !strcmp(arg + 2, "help"))
47e9cd28 423 return parse_options_usage(ctx, usagestr, options, 0);
07fe54db
PH
424 switch (parse_long_opt(ctx, arg + 2, options)) {
425 case -1:
47e9cd28 426 return parse_options_usage(ctx, usagestr, options, 1);
07fe54db 427 case -2:
b5ce3a54 428 goto unknown;
07fe54db 429 }
b5ce3a54
RS
430 continue;
431unknown:
432 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
433 return PARSE_OPT_UNKNOWN;
434 ctx->out[ctx->cpidx++] = ctx->argv[0];
435 ctx->opt = NULL;
ff43ec3e
PH
436 }
437 return PARSE_OPT_DONE;
438}
439
440int parse_options_end(struct parse_opt_ctx_t *ctx)
441{
442 memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out));
443 ctx->out[ctx->cpidx + ctx->argc] = NULL;
444 return ctx->cpidx + ctx->argc;
445}
446
37782920
SB
447int parse_options(int argc, const char **argv, const char *prefix,
448 const struct option *options, const char * const usagestr[],
449 int flags)
ff43ec3e
PH
450{
451 struct parse_opt_ctx_t ctx;
452
9ca1169f 453 parse_options_start(&ctx, argc, argv, prefix, options, flags);
ff43ec3e
PH
454 switch (parse_options_step(&ctx, options, usagestr)) {
455 case PARSE_OPT_HELP:
456 exit(129);
457 case PARSE_OPT_DONE:
458 break;
459 default: /* PARSE_OPT_UNKNOWN */
07fe54db
PH
460 if (ctx.argv[0][1] == '-') {
461 error("unknown option `%s'", ctx.argv[0] + 2);
462 } else {
463 error("unknown switch `%c'", *ctx.opt);
464 }
465 usage_with_options(usagestr, options);
4a59fd13
PH
466 }
467
7e7bbcb4 468 return parse_options_end(&ctx);
4a59fd13 469}
d7a38c54 470
9c7304e3 471static int usage_argh(const struct option *opts, FILE *outfile)
29f25d49
SB
472{
473 const char *s;
34aec9f5 474 int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) || !opts->argh;
29f25d49
SB
475 if (opts->flags & PARSE_OPT_OPTARG)
476 if (opts->long_name)
477 s = literal ? "[=%s]" : "[=<%s>]";
478 else
479 s = literal ? "[%s]" : "[<%s>]";
480 else
481 s = literal ? " %s" : " <%s>";
9c7304e3 482 return fprintf(outfile, s, opts->argh ? opts->argh : "...");
29f25d49
SB
483}
484
d7a38c54
PH
485#define USAGE_OPTS_WIDTH 24
486#define USAGE_GAP 2
487
47e9cd28
TR
488static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
489 const char * const *usagestr,
490 const struct option *opts, int full, int err)
d7a38c54 491{
9c7304e3
GS
492 FILE *outfile = err ? stderr : stdout;
493
49b61802
RS
494 if (!usagestr)
495 return PARSE_OPT_HELP;
496
47e9cd28
TR
497 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
498 fprintf(outfile, "cat <<\\EOF\n");
499
9c7304e3 500 fprintf(outfile, "usage: %s\n", *usagestr++);
f389c808 501 while (*usagestr && **usagestr)
9c7304e3 502 fprintf(outfile, " or: %s\n", *usagestr++);
44d86e91 503 while (*usagestr) {
9c7304e3 504 fprintf(outfile, "%s%s\n",
44d86e91
JK
505 **usagestr ? " " : "",
506 *usagestr);
507 usagestr++;
508 }
d7a38c54
PH
509
510 if (opts->type != OPTION_GROUP)
9c7304e3 511 fputc('\n', outfile);
d7a38c54
PH
512
513 for (; opts->type != OPTION_END; opts++) {
514 size_t pos;
515 int pad;
516
517 if (opts->type == OPTION_GROUP) {
9c7304e3 518 fputc('\n', outfile);
d7a38c54 519 if (*opts->help)
9c7304e3 520 fprintf(outfile, "%s\n", opts->help);
d7a38c54
PH
521 continue;
522 }
dd3bf0f4
PH
523 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
524 continue;
d7a38c54 525
9c7304e3 526 pos = fprintf(outfile, " ");
86b5efb2 527 if (opts->short_name && !(opts->flags & PARSE_OPT_NEGHELP)) {
51a9949e 528 if (opts->flags & PARSE_OPT_NODASH)
9c7304e3 529 pos += fprintf(outfile, "%c", opts->short_name);
51a9949e 530 else
9c7304e3 531 pos += fprintf(outfile, "-%c", opts->short_name);
51a9949e 532 }
d7a38c54 533 if (opts->long_name && opts->short_name)
9c7304e3 534 pos += fprintf(outfile, ", ");
d7a38c54 535 if (opts->long_name)
9c7304e3 536 pos += fprintf(outfile, "--%s%s",
86b5efb2
JS
537 (opts->flags & PARSE_OPT_NEGHELP) ? "no-" : "",
538 opts->long_name);
e0319ff5 539 if (opts->type == OPTION_NUMBER)
9c7304e3 540 pos += fprintf(outfile, "-NUM");
d7a38c54 541
34aec9f5 542 if (!(opts->flags & PARSE_OPT_NOARG))
9c7304e3 543 pos += usage_argh(opts, outfile);
d7a38c54 544
f389c808
AR
545 if (pos <= USAGE_OPTS_WIDTH)
546 pad = USAGE_OPTS_WIDTH - pos;
d7a38c54 547 else {
9c7304e3 548 fputc('\n', outfile);
d7a38c54
PH
549 pad = USAGE_OPTS_WIDTH;
550 }
9c7304e3 551 fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", opts->help);
d7a38c54 552 }
9c7304e3 553 fputc('\n', outfile);
f389c808 554
47e9cd28
TR
555 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
556 fputs("EOF\n", outfile);
557
ee68b87a 558 return PARSE_OPT_HELP;
d7a38c54 559}
0ce865b1 560
dd3bf0f4 561void usage_with_options(const char * const *usagestr,
ff43ec3e 562 const struct option *opts)
dd3bf0f4 563{
47e9cd28 564 usage_with_options_internal(NULL, usagestr, opts, 0, 1);
ff43ec3e 565 exit(129);
dd3bf0f4
PH
566}
567
451bb210
CC
568void usage_msg_opt(const char *msg,
569 const char * const *usagestr,
570 const struct option *options)
571{
572 fprintf(stderr, "%s\n\n", msg);
573 usage_with_options(usagestr, options);
574}
575
47e9cd28
TR
576static int parse_options_usage(struct parse_opt_ctx_t *ctx,
577 const char * const *usagestr,
9c7304e3 578 const struct option *opts, int err)
ee68b87a 579{
47e9cd28 580 return usage_with_options_internal(ctx, usagestr, opts, 0, err);
ee68b87a
PH
581}
582
583
0ce865b1
PH
584/*----- some often used options -----*/
585#include "cache.h"
db7244bd 586
0ce865b1
PH
587int parse_opt_abbrev_cb(const struct option *opt, const char *arg, int unset)
588{
589 int v;
590
591 if (!arg) {
592 v = unset ? 0 : DEFAULT_ABBREV;
593 } else {
594 v = strtol(arg, (char **)&arg, 10);
595 if (*arg)
596 return opterror(opt, "expects a numerical value", 0);
597 if (v && v < MINIMUM_ABBREV)
598 v = MINIMUM_ABBREV;
599 else if (v > 40)
600 v = 40;
601 }
602 *(int *)(opt->value) = v;
603 return 0;
604}
1f4a711a
MB
605
606int parse_opt_approxidate_cb(const struct option *opt, const char *arg,
607 int unset)
608{
609 *(unsigned long *)(opt->value) = approxidate(arg);
610 return 0;
611}
dbd0f5c7 612
73e9da01
ML
613int parse_opt_color_flag_cb(const struct option *opt, const char *arg,
614 int unset)
615{
616 int value;
617
618 if (!arg)
619 arg = unset ? "never" : (const char *)opt->defval;
620 value = git_config_colorbool(NULL, arg, -1);
621 if (value < 0)
622 return opterror(opt,
623 "expects \"always\", \"auto\", or \"never\"", 0);
624 *(int *)opt->value = value;
625 return 0;
626}
627
7f87aff2
TA
628int parse_opt_verbosity_cb(const struct option *opt, const char *arg,
629 int unset)
630{
631 int *target = opt->value;
632
633 if (unset)
634 /* --no-quiet, --no-verbose */
635 *target = 0;
636 else if (opt->short_name == 'v') {
637 if (*target >= 0)
638 (*target)++;
639 else
640 *target = 1;
641 } else {
642 if (*target <= 0)
643 (*target)--;
644 else
645 *target = -1;
646 }
647 return 0;
648}
649
269defdf
JG
650int parse_opt_with_commit(const struct option *opt, const char *arg, int unset)
651{
652 unsigned char sha1[20];
653 struct commit *commit;
654
655 if (!arg)
656 return -1;
657 if (get_sha1(arg, sha1))
658 return error("malformed object name %s", arg);
659 commit = lookup_commit_reference(sha1);
660 if (!commit)
661 return error("no such commit %s", arg);
662 commit_list_insert(commit, opt->value);
663 return 0;
664}
cb6020bb
JH
665
666int parse_opt_tertiary(const struct option *opt, const char *arg, int unset)
667{
668 int *target = opt->value;
669 *target = unset ? 2 : 1;
670 return 0;
671}
8b74d75c
JH
672
673int parse_options_concat(struct option *dst, size_t dst_size, struct option *src)
674{
675 int i, j;
676
677 for (i = 0; i < dst_size; i++)
678 if (dst[i].type == OPTION_END)
679 break;
680 for (j = 0; i < dst_size; i++, j++) {
681 dst[i] = src[j];
682 if (src[j].type == OPTION_END)
683 return 0;
684 }
685 return -1;
686}