parse-opts: prepare for OPT_FILENAME
[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 131{
e0319ff5
RS
132 const struct option *numopt = NULL;
133
4a59fd13
PH
134 for (; options->type != OPTION_END; options++) {
135 if (options->short_name == *p->opt) {
136 p->opt = p->opt[1] ? p->opt + 1 : NULL;
137 return get_value(p, options, OPT_SHORT);
138 }
e0319ff5
RS
139
140 /*
141 * Handle the numerical option later, explicit one-digit
142 * options take precedence over it.
143 */
144 if (options->type == OPTION_NUMBER)
145 numopt = options;
146 }
147 if (numopt && isdigit(*p->opt)) {
148 size_t len = 1;
149 char *arg;
150 int rc;
151
152 while (isdigit(p->opt[len]))
153 len++;
154 arg = xmemdupz(p->opt, len);
155 p->opt = p->opt[len] ? p->opt + len : NULL;
156 rc = (*numopt->callback)(numopt, arg, 0) ? (-1) : 0;
157 free(arg);
158 return rc;
4a59fd13 159 }
07fe54db 160 return -2;
4a59fd13
PH
161}
162
7e7bbcb4 163static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
4a59fd13
PH
164 const struct option *options)
165{
7f275b91 166 const char *arg_end = strchr(arg, '=');
243e0614
JS
167 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
168 int abbrev_flags = 0, ambiguous_flags = 0;
7f275b91
JS
169
170 if (!arg_end)
171 arg_end = arg + strlen(arg);
172
4a59fd13
PH
173 for (; options->type != OPTION_END; options++) {
174 const char *rest;
175 int flags = 0;
176
177 if (!options->long_name)
178 continue;
179
180 rest = skip_prefix(arg, options->long_name);
580d5bff
PH
181 if (options->type == OPTION_ARGUMENT) {
182 if (!rest)
183 continue;
184 if (*rest == '=')
185 return opterror(options, "takes no value", flags);
186 if (*rest)
187 continue;
188 p->out[p->cpidx++] = arg - 2;
189 return 0;
190 }
4a59fd13 191 if (!rest) {
7f275b91
JS
192 /* abbreviated? */
193 if (!strncmp(options->long_name, arg, arg_end - arg)) {
194is_abbreviated:
243e0614
JS
195 if (abbrev_option) {
196 /*
197 * If this is abbreviated, it is
198 * ambiguous. So when there is no
199 * exact match later, we need to
200 * error out.
201 */
202 ambiguous_option = abbrev_option;
203 ambiguous_flags = abbrev_flags;
204 }
7f275b91
JS
205 if (!(flags & OPT_UNSET) && *arg_end)
206 p->opt = arg_end + 1;
207 abbrev_option = options;
208 abbrev_flags = flags;
209 continue;
210 }
211 /* negated and abbreviated very much? */
212 if (!prefixcmp("no-", arg)) {
213 flags |= OPT_UNSET;
214 goto is_abbreviated;
215 }
216 /* negated? */
4a59fd13
PH
217 if (strncmp(arg, "no-", 3))
218 continue;
219 flags |= OPT_UNSET;
220 rest = skip_prefix(arg + 3, options->long_name);
7f275b91
JS
221 /* abbreviated and negated? */
222 if (!rest && !prefixcmp(options->long_name, arg + 3))
223 goto is_abbreviated;
4a59fd13
PH
224 if (!rest)
225 continue;
226 }
227 if (*rest) {
228 if (*rest != '=')
229 continue;
230 p->opt = rest + 1;
231 }
232 return get_value(p, options, flags);
233 }
243e0614
JS
234
235 if (ambiguous_option)
236 return error("Ambiguous option: %s "
237 "(could be --%s%s or --%s%s)",
238 arg,
239 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
240 ambiguous_option->long_name,
241 (abbrev_flags & OPT_UNSET) ? "no-" : "",
242 abbrev_option->long_name);
7f275b91
JS
243 if (abbrev_option)
244 return get_value(p, abbrev_option, abbrev_flags);
07fe54db 245 return -2;
4a59fd13
PH
246}
247
51a9949e
RS
248static int parse_nodash_opt(struct parse_opt_ctx_t *p, const char *arg,
249 const struct option *options)
250{
251 for (; options->type != OPTION_END; options++) {
252 if (!(options->flags & PARSE_OPT_NODASH))
253 continue;
254 if ((options->flags & PARSE_OPT_OPTARG) ||
255 !(options->flags & PARSE_OPT_NOARG))
256 die("BUG: dashless options don't support arguments");
257 if (!(options->flags & PARSE_OPT_NONEG))
258 die("BUG: dashless options don't support negation");
259 if (options->long_name)
260 die("BUG: dashless options can't be long");
261 if (options->short_name == arg[0] && arg[1] == '\0')
262 return get_value(p, options, OPT_SHORT);
263 }
264 return -2;
265}
266
1121a878 267static void check_typos(const char *arg, const struct option *options)
3a9f0f41
PH
268{
269 if (strlen(arg) < 3)
270 return;
271
272 if (!prefixcmp(arg, "no-")) {
273 error ("did you mean `--%s` (with two dashes ?)", arg);
274 exit(129);
275 }
276
277 for (; options->type != OPTION_END; options++) {
278 if (!options->long_name)
279 continue;
280 if (!prefixcmp(options->long_name, arg)) {
281 error ("did you mean `--%s` (with two dashes ?)", arg);
282 exit(129);
283 }
284 }
285}
286
7e7bbcb4 287void parse_options_start(struct parse_opt_ctx_t *ctx,
37782920
SB
288 int argc, const char **argv, const char *prefix,
289 int flags)
7e7bbcb4
PH
290{
291 memset(ctx, 0, sizeof(*ctx));
292 ctx->argc = argc - 1;
293 ctx->argv = argv + 1;
294 ctx->out = argv;
37782920 295 ctx->prefix = prefix;
a32a4eaa 296 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
7e7bbcb4 297 ctx->flags = flags;
0d260f9a
RS
298 if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
299 (flags & PARSE_OPT_STOP_AT_NON_OPTION))
300 die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
7e7bbcb4
PH
301}
302
ee68b87a 303static int usage_with_options_internal(const char * const *,
ff43ec3e 304 const struct option *, int);
dd3bf0f4 305
ff43ec3e
PH
306int parse_options_step(struct parse_opt_ctx_t *ctx,
307 const struct option *options,
308 const char * const usagestr[])
4a59fd13 309{
b92891f9
RS
310 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
311
26141b5b
PH
312 /* we must reset ->opt, unknown short option leave it dangling */
313 ctx->opt = NULL;
314
ff43ec3e
PH
315 for (; ctx->argc; ctx->argc--, ctx->argv++) {
316 const char *arg = ctx->argv[0];
4a59fd13
PH
317
318 if (*arg != '-' || !arg[1]) {
51a9949e
RS
319 if (parse_nodash_opt(ctx, arg, options) == 0)
320 continue;
ff43ec3e 321 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
a0ec9d25 322 break;
ff43ec3e 323 ctx->out[ctx->cpidx++] = ctx->argv[0];
4a59fd13
PH
324 continue;
325 }
326
327 if (arg[1] != '-') {
ff43ec3e 328 ctx->opt = arg + 1;
b92891f9 329 if (internal_help && *ctx->opt == 'h')
ff43ec3e 330 return parse_options_usage(usagestr, options);
07fe54db
PH
331 switch (parse_short_opt(ctx, options)) {
332 case -1:
333 return parse_options_usage(usagestr, options);
334 case -2:
b5ce3a54 335 goto unknown;
07fe54db 336 }
ff43ec3e 337 if (ctx->opt)
3a9f0f41 338 check_typos(arg + 1, options);
ff43ec3e 339 while (ctx->opt) {
b92891f9 340 if (internal_help && *ctx->opt == 'h')
ff43ec3e 341 return parse_options_usage(usagestr, options);
07fe54db
PH
342 switch (parse_short_opt(ctx, options)) {
343 case -1:
344 return parse_options_usage(usagestr, options);
345 case -2:
26141b5b
PH
346 /* fake a short option thing to hide the fact that we may have
347 * started to parse aggregated stuff
348 *
349 * This is leaky, too bad.
350 */
351 ctx->argv[0] = xstrdup(ctx->opt - 1);
352 *(char *)ctx->argv[0] = '-';
b5ce3a54 353 goto unknown;
07fe54db 354 }
3a9f0f41 355 }
4a59fd13
PH
356 continue;
357 }
358
359 if (!arg[2]) { /* "--" */
ff43ec3e
PH
360 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
361 ctx->argc--;
362 ctx->argv++;
4a59fd13
PH
363 }
364 break;
365 }
366
b92891f9 367 if (internal_help && !strcmp(arg + 2, "help-all"))
ff43ec3e 368 return usage_with_options_internal(usagestr, options, 1);
b92891f9 369 if (internal_help && !strcmp(arg + 2, "help"))
ff43ec3e 370 return parse_options_usage(usagestr, options);
07fe54db
PH
371 switch (parse_long_opt(ctx, arg + 2, options)) {
372 case -1:
373 return parse_options_usage(usagestr, options);
374 case -2:
b5ce3a54 375 goto unknown;
07fe54db 376 }
b5ce3a54
RS
377 continue;
378unknown:
379 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
380 return PARSE_OPT_UNKNOWN;
381 ctx->out[ctx->cpidx++] = ctx->argv[0];
382 ctx->opt = NULL;
ff43ec3e
PH
383 }
384 return PARSE_OPT_DONE;
385}
386
387int parse_options_end(struct parse_opt_ctx_t *ctx)
388{
389 memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out));
390 ctx->out[ctx->cpidx + ctx->argc] = NULL;
391 return ctx->cpidx + ctx->argc;
392}
393
37782920
SB
394int parse_options(int argc, const char **argv, const char *prefix,
395 const struct option *options, const char * const usagestr[],
396 int flags)
ff43ec3e
PH
397{
398 struct parse_opt_ctx_t ctx;
399
37782920 400 parse_options_start(&ctx, argc, argv, prefix, flags);
ff43ec3e
PH
401 switch (parse_options_step(&ctx, options, usagestr)) {
402 case PARSE_OPT_HELP:
403 exit(129);
404 case PARSE_OPT_DONE:
405 break;
406 default: /* PARSE_OPT_UNKNOWN */
07fe54db
PH
407 if (ctx.argv[0][1] == '-') {
408 error("unknown option `%s'", ctx.argv[0] + 2);
409 } else {
410 error("unknown switch `%c'", *ctx.opt);
411 }
412 usage_with_options(usagestr, options);
4a59fd13
PH
413 }
414
7e7bbcb4 415 return parse_options_end(&ctx);
4a59fd13 416}
d7a38c54 417
29f25d49
SB
418static int usage_argh(const struct option *opts)
419{
420 const char *s;
421 int literal = opts->flags & PARSE_OPT_LITERAL_ARGHELP;
422 if (opts->flags & PARSE_OPT_OPTARG)
423 if (opts->long_name)
424 s = literal ? "[=%s]" : "[=<%s>]";
425 else
426 s = literal ? "[%s]" : "[<%s>]";
427 else
428 s = literal ? " %s" : " <%s>";
429 return fprintf(stderr, s, opts->argh);
430}
431
d7a38c54
PH
432#define USAGE_OPTS_WIDTH 24
433#define USAGE_GAP 2
434
ee68b87a 435int usage_with_options_internal(const char * const *usagestr,
ff43ec3e 436 const struct option *opts, int full)
d7a38c54 437{
49b61802
RS
438 if (!usagestr)
439 return PARSE_OPT_HELP;
440
f389c808
AR
441 fprintf(stderr, "usage: %s\n", *usagestr++);
442 while (*usagestr && **usagestr)
443 fprintf(stderr, " or: %s\n", *usagestr++);
44d86e91
JK
444 while (*usagestr) {
445 fprintf(stderr, "%s%s\n",
446 **usagestr ? " " : "",
447 *usagestr);
448 usagestr++;
449 }
d7a38c54
PH
450
451 if (opts->type != OPTION_GROUP)
f389c808 452 fputc('\n', stderr);
d7a38c54
PH
453
454 for (; opts->type != OPTION_END; opts++) {
455 size_t pos;
456 int pad;
457
458 if (opts->type == OPTION_GROUP) {
f389c808 459 fputc('\n', stderr);
d7a38c54 460 if (*opts->help)
f389c808 461 fprintf(stderr, "%s\n", opts->help);
d7a38c54
PH
462 continue;
463 }
dd3bf0f4
PH
464 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
465 continue;
d7a38c54 466
f389c808 467 pos = fprintf(stderr, " ");
51a9949e
RS
468 if (opts->short_name) {
469 if (opts->flags & PARSE_OPT_NODASH)
470 pos += fprintf(stderr, "%c", opts->short_name);
471 else
472 pos += fprintf(stderr, "-%c", opts->short_name);
473 }
d7a38c54 474 if (opts->long_name && opts->short_name)
f389c808 475 pos += fprintf(stderr, ", ");
d7a38c54 476 if (opts->long_name)
f389c808 477 pos += fprintf(stderr, "--%s", opts->long_name);
e0319ff5
RS
478 if (opts->type == OPTION_NUMBER)
479 pos += fprintf(stderr, "-NUM");
d7a38c54
PH
480
481 switch (opts->type) {
580d5bff
PH
482 case OPTION_ARGUMENT:
483 break;
d7a38c54 484 case OPTION_INTEGER:
ffe659f9 485 if (opts->flags & PARSE_OPT_OPTARG)
6422f633
MB
486 if (opts->long_name)
487 pos += fprintf(stderr, "[=<n>]");
488 else
489 pos += fprintf(stderr, "[<n>]");
ffe659f9
PH
490 else
491 pos += fprintf(stderr, " <n>");
d7a38c54 492 break;
ffe659f9 493 case OPTION_CALLBACK:
f481e22a
PH
494 if (opts->flags & PARSE_OPT_NOARG)
495 break;
496 /* FALLTHROUGH */
497 case OPTION_STRING:
29f25d49
SB
498 if (opts->argh)
499 pos += usage_argh(opts);
500 else {
ffe659f9 501 if (opts->flags & PARSE_OPT_OPTARG)
6422f633
MB
502 if (opts->long_name)
503 pos += fprintf(stderr, "[=...]");
504 else
505 pos += fprintf(stderr, "[...]");
ffe659f9
PH
506 else
507 pos += fprintf(stderr, " ...");
508 }
d7a38c54 509 break;
e0319ff5 510 default: /* OPTION_{BIT,BOOLEAN,NUMBER,SET_INT,SET_PTR} */
d7a38c54
PH
511 break;
512 }
513
f389c808
AR
514 if (pos <= USAGE_OPTS_WIDTH)
515 pad = USAGE_OPTS_WIDTH - pos;
d7a38c54 516 else {
f389c808 517 fputc('\n', stderr);
d7a38c54
PH
518 pad = USAGE_OPTS_WIDTH;
519 }
f389c808 520 fprintf(stderr, "%*s%s\n", pad + USAGE_GAP, "", opts->help);
d7a38c54 521 }
f389c808
AR
522 fputc('\n', stderr);
523
ee68b87a 524 return PARSE_OPT_HELP;
d7a38c54 525}
0ce865b1 526
dd3bf0f4 527void usage_with_options(const char * const *usagestr,
ff43ec3e 528 const struct option *opts)
dd3bf0f4 529{
ff43ec3e
PH
530 usage_with_options_internal(usagestr, opts, 0);
531 exit(129);
dd3bf0f4
PH
532}
533
ee68b87a
PH
534int parse_options_usage(const char * const *usagestr,
535 const struct option *opts)
536{
ff43ec3e 537 return usage_with_options_internal(usagestr, opts, 0);
ee68b87a
PH
538}
539
540
0ce865b1
PH
541/*----- some often used options -----*/
542#include "cache.h"
db7244bd 543
0ce865b1
PH
544int parse_opt_abbrev_cb(const struct option *opt, const char *arg, int unset)
545{
546 int v;
547
548 if (!arg) {
549 v = unset ? 0 : DEFAULT_ABBREV;
550 } else {
551 v = strtol(arg, (char **)&arg, 10);
552 if (*arg)
553 return opterror(opt, "expects a numerical value", 0);
554 if (v && v < MINIMUM_ABBREV)
555 v = MINIMUM_ABBREV;
556 else if (v > 40)
557 v = 40;
558 }
559 *(int *)(opt->value) = v;
560 return 0;
561}
1f4a711a
MB
562
563int parse_opt_approxidate_cb(const struct option *opt, const char *arg,
564 int unset)
565{
566 *(unsigned long *)(opt->value) = approxidate(arg);
567 return 0;
568}
dbd0f5c7 569
7f87aff2
TA
570int parse_opt_verbosity_cb(const struct option *opt, const char *arg,
571 int unset)
572{
573 int *target = opt->value;
574
575 if (unset)
576 /* --no-quiet, --no-verbose */
577 *target = 0;
578 else if (opt->short_name == 'v') {
579 if (*target >= 0)
580 (*target)++;
581 else
582 *target = 1;
583 } else {
584 if (*target <= 0)
585 (*target)--;
586 else
587 *target = -1;
588 }
589 return 0;
590}
591
269defdf
JG
592int parse_opt_with_commit(const struct option *opt, const char *arg, int unset)
593{
594 unsigned char sha1[20];
595 struct commit *commit;
596
597 if (!arg)
598 return -1;
599 if (get_sha1(arg, sha1))
600 return error("malformed object name %s", arg);
601 commit = lookup_commit_reference(sha1);
602 if (!commit)
603 return error("no such commit %s", arg);
604 commit_list_insert(commit, opt->value);
605 return 0;
606}
607
dbd0f5c7
JH
608/*
609 * This should really be OPTION_FILENAME type as a part of
610 * parse_options that take prefix to do this while parsing.
611 */
612extern const char *parse_options_fix_filename(const char *prefix, const char *file)
613{
614 if (!file || !prefix || is_absolute_path(file) || !strcmp("-", file))
615 return file;
616 return prefix_filename(prefix, strlen(prefix), file);
617}
618