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