]> git.ipfire.org Git - thirdparty/git.git/blame - parse-options.c
Documentation/SubmittingPatches: clarify GMail section and SMTP
[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
41064ebc
JH
7static int parse_options_usage(const char * const *usagestr,
8 const struct option *opts);
9
4a59fd13
PH
10#define OPT_SHORT 1
11#define OPT_UNSET 2
12
4a59fd13
PH
13static int opterror(const struct option *opt, const char *reason, int flags)
14{
15 if (flags & OPT_SHORT)
16 return error("switch `%c' %s", opt->short_name, reason);
17 if (flags & OPT_UNSET)
18 return error("option `no-%s' %s", opt->long_name, reason);
19 return error("option `%s' %s", opt->long_name, reason);
20}
21
1cc6985c
PH
22static 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;
d5d745f9 30 } else if (p->argc > 1) {
1cc6985c
PH
31 p->argc--;
32 *arg = *++p->argv;
33 } else
34 return opterror(opt, "requires a value", flags);
35 return 0;
36}
37
df217ed6
SB
38static 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
7e7bbcb4 46static int get_value(struct parse_opt_ctx_t *p,
1cc6985c 47 const struct option *opt, int flags)
4a59fd13 48{
ffe659f9 49 const char *s, *arg;
db7244bd 50 const int unset = flags & OPT_UNSET;
df217ed6 51 int err;
4a59fd13 52
db7244bd 53 if (unset && p->opt)
4a59fd13 54 return opterror(opt, "takes no value", flags);
db7244bd
PH
55 if (unset && (opt->flags & PARSE_OPT_NONEG))
56 return opterror(opt, "isn't available", flags);
4a59fd13 57
db7244bd
PH
58 if (!(flags & OPT_SHORT) && p->opt) {
59 switch (opt->type) {
60 case OPTION_CALLBACK:
61 if (!(opt->flags & PARSE_OPT_NOARG))
62 break;
63 /* FALLTHROUGH */
64 case OPTION_BOOLEAN:
65 case OPTION_BIT:
2f4b97f9 66 case OPTION_NEGBIT:
db7244bd
PH
67 case OPTION_SET_INT:
68 case OPTION_SET_PTR:
4a59fd13 69 return opterror(opt, "takes no value", flags);
db7244bd
PH
70 default:
71 break;
72 }
73 }
74
db7244bd
PH
75 switch (opt->type) {
76 case OPTION_BIT:
77 if (unset)
78 *(int *)opt->value &= ~opt->defval;
4a59fd13 79 else
db7244bd
PH
80 *(int *)opt->value |= opt->defval;
81 return 0;
82
2f4b97f9
RS
83 case OPTION_NEGBIT:
84 if (unset)
85 *(int *)opt->value |= opt->defval;
86 else
87 *(int *)opt->value &= ~opt->defval;
88 return 0;
89
db7244bd
PH
90 case OPTION_BOOLEAN:
91 *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
92 return 0;
93
94 case OPTION_SET_INT:
95 *(int *)opt->value = unset ? 0 : opt->defval;
96 return 0;
97
98 case OPTION_SET_PTR:
99 *(void **)opt->value = unset ? NULL : (void *)opt->defval;
4a59fd13
PH
100 return 0;
101
102 case OPTION_STRING:
1cc6985c 103 if (unset)
db7244bd 104 *(const char **)opt->value = NULL;
1cc6985c 105 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
ffe659f9 106 *(const char **)opt->value = (const char *)opt->defval;
1cc6985c
PH
107 else
108 return get_arg(p, opt, flags, (const char **)opt->value);
4a59fd13
PH
109 return 0;
110
df217ed6
SB
111 case OPTION_FILENAME:
112 err = 0;
113 if (unset)
114 *(const char **)opt->value = NULL;
115 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
116 *(const char **)opt->value = (const char *)opt->defval;
117 else
118 err = get_arg(p, opt, flags, (const char **)opt->value);
119
120 if (!err)
121 fix_filename(p->prefix, (const char **)opt->value);
122 return err;
123
ffe659f9 124 case OPTION_CALLBACK:
db7244bd 125 if (unset)
07fe54db 126 return (*opt->callback)(opt, NULL, 1) ? (-1) : 0;
db7244bd 127 if (opt->flags & PARSE_OPT_NOARG)
07fe54db 128 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
c43a2483 129 if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
07fe54db 130 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
1cc6985c
PH
131 if (get_arg(p, opt, flags, &arg))
132 return -1;
133 return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
ffe659f9 134
4a59fd13 135 case OPTION_INTEGER:
db7244bd 136 if (unset) {
4a59fd13
PH
137 *(int *)opt->value = 0;
138 return 0;
139 }
c43a2483 140 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
ffe659f9
PH
141 *(int *)opt->value = opt->defval;
142 return 0;
143 }
1cc6985c
PH
144 if (get_arg(p, opt, flags, &arg))
145 return -1;
146 *(int *)opt->value = strtol(arg, (char **)&s, 10);
4a59fd13
PH
147 if (*s)
148 return opterror(opt, "expects a numerical value", flags);
149 return 0;
150
151 default:
152 die("should not happen, someone must be hit on the forehead");
153 }
154}
155
7e7bbcb4 156static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)
4a59fd13 157{
e0319ff5
RS
158 const struct option *numopt = NULL;
159
4a59fd13
PH
160 for (; options->type != OPTION_END; options++) {
161 if (options->short_name == *p->opt) {
162 p->opt = p->opt[1] ? p->opt + 1 : NULL;
163 return get_value(p, options, OPT_SHORT);
164 }
e0319ff5
RS
165
166 /*
167 * Handle the numerical option later, explicit one-digit
168 * options take precedence over it.
169 */
170 if (options->type == OPTION_NUMBER)
171 numopt = options;
172 }
173 if (numopt && isdigit(*p->opt)) {
174 size_t len = 1;
175 char *arg;
176 int rc;
177
178 while (isdigit(p->opt[len]))
179 len++;
180 arg = xmemdupz(p->opt, len);
181 p->opt = p->opt[len] ? p->opt + len : NULL;
182 rc = (*numopt->callback)(numopt, arg, 0) ? (-1) : 0;
183 free(arg);
184 return rc;
4a59fd13 185 }
07fe54db 186 return -2;
4a59fd13
PH
187}
188
7e7bbcb4 189static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
4a59fd13
PH
190 const struct option *options)
191{
7f275b91 192 const char *arg_end = strchr(arg, '=');
243e0614
JS
193 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
194 int abbrev_flags = 0, ambiguous_flags = 0;
7f275b91
JS
195
196 if (!arg_end)
197 arg_end = arg + strlen(arg);
198
4a59fd13
PH
199 for (; options->type != OPTION_END; options++) {
200 const char *rest;
201 int flags = 0;
202
203 if (!options->long_name)
204 continue;
205
206 rest = skip_prefix(arg, options->long_name);
580d5bff
PH
207 if (options->type == OPTION_ARGUMENT) {
208 if (!rest)
209 continue;
210 if (*rest == '=')
211 return opterror(options, "takes no value", flags);
212 if (*rest)
213 continue;
214 p->out[p->cpidx++] = arg - 2;
215 return 0;
216 }
4a59fd13 217 if (!rest) {
7f275b91
JS
218 /* abbreviated? */
219 if (!strncmp(options->long_name, arg, arg_end - arg)) {
220is_abbreviated:
243e0614
JS
221 if (abbrev_option) {
222 /*
223 * If this is abbreviated, it is
224 * ambiguous. So when there is no
225 * exact match later, we need to
226 * error out.
227 */
228 ambiguous_option = abbrev_option;
229 ambiguous_flags = abbrev_flags;
230 }
7f275b91
JS
231 if (!(flags & OPT_UNSET) && *arg_end)
232 p->opt = arg_end + 1;
233 abbrev_option = options;
234 abbrev_flags = flags;
235 continue;
236 }
6bbfd1fa
AS
237 /* negation allowed? */
238 if (options->flags & PARSE_OPT_NONEG)
239 continue;
7f275b91
JS
240 /* negated and abbreviated very much? */
241 if (!prefixcmp("no-", arg)) {
242 flags |= OPT_UNSET;
243 goto is_abbreviated;
244 }
245 /* negated? */
4a59fd13
PH
246 if (strncmp(arg, "no-", 3))
247 continue;
248 flags |= OPT_UNSET;
249 rest = skip_prefix(arg + 3, options->long_name);
7f275b91
JS
250 /* abbreviated and negated? */
251 if (!rest && !prefixcmp(options->long_name, arg + 3))
252 goto is_abbreviated;
4a59fd13
PH
253 if (!rest)
254 continue;
255 }
256 if (*rest) {
257 if (*rest != '=')
258 continue;
259 p->opt = rest + 1;
260 }
261 return get_value(p, options, flags);
262 }
243e0614
JS
263
264 if (ambiguous_option)
265 return error("Ambiguous option: %s "
266 "(could be --%s%s or --%s%s)",
267 arg,
268 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
269 ambiguous_option->long_name,
270 (abbrev_flags & OPT_UNSET) ? "no-" : "",
271 abbrev_option->long_name);
7f275b91
JS
272 if (abbrev_option)
273 return get_value(p, abbrev_option, abbrev_flags);
07fe54db 274 return -2;
4a59fd13
PH
275}
276
51a9949e
RS
277static int parse_nodash_opt(struct parse_opt_ctx_t *p, const char *arg,
278 const struct option *options)
279{
280 for (; options->type != OPTION_END; options++) {
281 if (!(options->flags & PARSE_OPT_NODASH))
282 continue;
283 if ((options->flags & PARSE_OPT_OPTARG) ||
284 !(options->flags & PARSE_OPT_NOARG))
285 die("BUG: dashless options don't support arguments");
286 if (!(options->flags & PARSE_OPT_NONEG))
287 die("BUG: dashless options don't support negation");
288 if (options->long_name)
289 die("BUG: dashless options can't be long");
290 if (options->short_name == arg[0] && arg[1] == '\0')
291 return get_value(p, options, OPT_SHORT);
292 }
293 return -2;
294}
295
1121a878 296static void check_typos(const char *arg, const struct option *options)
3a9f0f41
PH
297{
298 if (strlen(arg) < 3)
299 return;
300
301 if (!prefixcmp(arg, "no-")) {
302 error ("did you mean `--%s` (with two dashes ?)", arg);
303 exit(129);
304 }
305
306 for (; options->type != OPTION_END; options++) {
307 if (!options->long_name)
308 continue;
309 if (!prefixcmp(options->long_name, arg)) {
310 error ("did you mean `--%s` (with two dashes ?)", arg);
311 exit(129);
312 }
313 }
314}
315
cb9d398c
PH
316static void parse_options_check(const struct option *opts)
317{
318 int err = 0;
319
320 for (; opts->type != OPTION_END; opts++) {
321 if ((opts->flags & PARSE_OPT_LASTARG_DEFAULT) &&
322 (opts->flags & PARSE_OPT_OPTARG)) {
323 if (opts->long_name) {
324 error("`--%s` uses incompatible flags "
325 "LASTARG_DEFAULT and OPTARG", opts->long_name);
326 } else {
327 error("`-%c` uses incompatible flags "
328 "LASTARG_DEFAULT and OPTARG", opts->short_name);
329 }
330 err |= 1;
331 }
332 }
333
334 if (err)
335 exit(129);
336}
337
7e7bbcb4 338void parse_options_start(struct parse_opt_ctx_t *ctx,
37782920
SB
339 int argc, const char **argv, const char *prefix,
340 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");
7e7bbcb4
PH
352}
353
ee68b87a 354static int usage_with_options_internal(const char * const *,
ff43ec3e 355 const struct option *, 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
cb9d398c
PH
363 parse_options_check(options);
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')
ff43ec3e 383 return parse_options_usage(usagestr, options);
07fe54db
PH
384 switch (parse_short_opt(ctx, options)) {
385 case -1:
386 return parse_options_usage(usagestr, options);
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')
ff43ec3e 394 return parse_options_usage(usagestr, options);
07fe54db
PH
395 switch (parse_short_opt(ctx, options)) {
396 case -1:
397 return parse_options_usage(usagestr, options);
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"))
ff43ec3e 421 return usage_with_options_internal(usagestr, options, 1);
b92891f9 422 if (internal_help && !strcmp(arg + 2, "help"))
ff43ec3e 423 return parse_options_usage(usagestr, options);
07fe54db
PH
424 switch (parse_long_opt(ctx, arg + 2, options)) {
425 case -1:
426 return parse_options_usage(usagestr, options);
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
37782920 453 parse_options_start(&ctx, argc, argv, prefix, 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
29f25d49
SB
471static int usage_argh(const struct option *opts)
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>";
34aec9f5 482 return fprintf(stderr, s, opts->argh ? opts->argh : "...");
29f25d49
SB
483}
484
d7a38c54
PH
485#define USAGE_OPTS_WIDTH 24
486#define USAGE_GAP 2
487
2af202be 488static int usage_with_options_internal(const char * const *usagestr,
ff43ec3e 489 const struct option *opts, int full)
d7a38c54 490{
49b61802
RS
491 if (!usagestr)
492 return PARSE_OPT_HELP;
493
f389c808
AR
494 fprintf(stderr, "usage: %s\n", *usagestr++);
495 while (*usagestr && **usagestr)
496 fprintf(stderr, " or: %s\n", *usagestr++);
44d86e91
JK
497 while (*usagestr) {
498 fprintf(stderr, "%s%s\n",
499 **usagestr ? " " : "",
500 *usagestr);
501 usagestr++;
502 }
d7a38c54
PH
503
504 if (opts->type != OPTION_GROUP)
f389c808 505 fputc('\n', stderr);
d7a38c54
PH
506
507 for (; opts->type != OPTION_END; opts++) {
508 size_t pos;
509 int pad;
510
511 if (opts->type == OPTION_GROUP) {
f389c808 512 fputc('\n', stderr);
d7a38c54 513 if (*opts->help)
f389c808 514 fprintf(stderr, "%s\n", opts->help);
d7a38c54
PH
515 continue;
516 }
dd3bf0f4
PH
517 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
518 continue;
d7a38c54 519
f389c808 520 pos = fprintf(stderr, " ");
86b5efb2 521 if (opts->short_name && !(opts->flags & PARSE_OPT_NEGHELP)) {
51a9949e
RS
522 if (opts->flags & PARSE_OPT_NODASH)
523 pos += fprintf(stderr, "%c", opts->short_name);
524 else
525 pos += fprintf(stderr, "-%c", opts->short_name);
526 }
d7a38c54 527 if (opts->long_name && opts->short_name)
f389c808 528 pos += fprintf(stderr, ", ");
d7a38c54 529 if (opts->long_name)
86b5efb2
JS
530 pos += fprintf(stderr, "--%s%s",
531 (opts->flags & PARSE_OPT_NEGHELP) ? "no-" : "",
532 opts->long_name);
e0319ff5
RS
533 if (opts->type == OPTION_NUMBER)
534 pos += fprintf(stderr, "-NUM");
d7a38c54 535
34aec9f5
SB
536 if (!(opts->flags & PARSE_OPT_NOARG))
537 pos += usage_argh(opts);
d7a38c54 538
f389c808
AR
539 if (pos <= USAGE_OPTS_WIDTH)
540 pad = USAGE_OPTS_WIDTH - pos;
d7a38c54 541 else {
f389c808 542 fputc('\n', stderr);
d7a38c54
PH
543 pad = USAGE_OPTS_WIDTH;
544 }
f389c808 545 fprintf(stderr, "%*s%s\n", pad + USAGE_GAP, "", opts->help);
d7a38c54 546 }
f389c808
AR
547 fputc('\n', stderr);
548
ee68b87a 549 return PARSE_OPT_HELP;
d7a38c54 550}
0ce865b1 551
dd3bf0f4 552void usage_with_options(const char * const *usagestr,
ff43ec3e 553 const struct option *opts)
dd3bf0f4 554{
ff43ec3e
PH
555 usage_with_options_internal(usagestr, opts, 0);
556 exit(129);
dd3bf0f4
PH
557}
558
451bb210
CC
559void usage_msg_opt(const char *msg,
560 const char * const *usagestr,
561 const struct option *options)
562{
563 fprintf(stderr, "%s\n\n", msg);
564 usage_with_options(usagestr, options);
565}
566
41064ebc
JH
567static int parse_options_usage(const char * const *usagestr,
568 const struct option *opts)
ee68b87a 569{
ff43ec3e 570 return usage_with_options_internal(usagestr, opts, 0);
ee68b87a
PH
571}
572
573
0ce865b1
PH
574/*----- some often used options -----*/
575#include "cache.h"
db7244bd 576
0ce865b1
PH
577int parse_opt_abbrev_cb(const struct option *opt, const char *arg, int unset)
578{
579 int v;
580
581 if (!arg) {
582 v = unset ? 0 : DEFAULT_ABBREV;
583 } else {
584 v = strtol(arg, (char **)&arg, 10);
585 if (*arg)
586 return opterror(opt, "expects a numerical value", 0);
587 if (v && v < MINIMUM_ABBREV)
588 v = MINIMUM_ABBREV;
589 else if (v > 40)
590 v = 40;
591 }
592 *(int *)(opt->value) = v;
593 return 0;
594}
1f4a711a
MB
595
596int parse_opt_approxidate_cb(const struct option *opt, const char *arg,
597 int unset)
598{
599 *(unsigned long *)(opt->value) = approxidate(arg);
600 return 0;
601}
dbd0f5c7 602
73e9da01
ML
603int parse_opt_color_flag_cb(const struct option *opt, const char *arg,
604 int unset)
605{
606 int value;
607
608 if (!arg)
609 arg = unset ? "never" : (const char *)opt->defval;
610 value = git_config_colorbool(NULL, arg, -1);
611 if (value < 0)
612 return opterror(opt,
613 "expects \"always\", \"auto\", or \"never\"", 0);
614 *(int *)opt->value = value;
615 return 0;
616}
617
7f87aff2
TA
618int parse_opt_verbosity_cb(const struct option *opt, const char *arg,
619 int unset)
620{
621 int *target = opt->value;
622
623 if (unset)
624 /* --no-quiet, --no-verbose */
625 *target = 0;
626 else if (opt->short_name == 'v') {
627 if (*target >= 0)
628 (*target)++;
629 else
630 *target = 1;
631 } else {
632 if (*target <= 0)
633 (*target)--;
634 else
635 *target = -1;
636 }
637 return 0;
638}
639
269defdf
JG
640int parse_opt_with_commit(const struct option *opt, const char *arg, int unset)
641{
642 unsigned char sha1[20];
643 struct commit *commit;
644
645 if (!arg)
646 return -1;
647 if (get_sha1(arg, sha1))
648 return error("malformed object name %s", arg);
649 commit = lookup_commit_reference(sha1);
650 if (!commit)
651 return error("no such commit %s", arg);
652 commit_list_insert(commit, opt->value);
653 return 0;
654}
cb6020bb
JH
655
656int parse_opt_tertiary(const struct option *opt, const char *arg, int unset)
657{
658 int *target = opt->value;
659 *target = unset ? 2 : 1;
660 return 0;
661}
8b74d75c
JH
662
663int parse_options_concat(struct option *dst, size_t dst_size, struct option *src)
664{
665 int i, j;
666
667 for (i = 0; i < dst_size; i++)
668 if (dst[i].type == OPTION_END)
669 break;
670 for (j = 0; i < dst_size; i++, j++) {
671 dst[i] = src[j];
672 if (src[j].type == OPTION_END)
673 return 0;
674 }
675 return -1;
676}