]> git.ipfire.org Git - thirdparty/git.git/blob - parse-options.c
parse-options: clearer reporting of API misuse
[thirdparty/git.git] / parse-options.c
1 #include "git-compat-util.h"
2 #include "parse-options.h"
3 #include "cache.h"
4 #include "commit.h"
5 #include "color.h"
6
7 static int parse_options_usage(struct parse_opt_ctx_t *ctx,
8 const char * const *usagestr,
9 const struct option *opts, int err);
10
11 #define OPT_SHORT 1
12 #define OPT_UNSET 2
13
14 static 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
21 static 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
30 static 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;
38 } else if (p->argc > 1) {
39 p->argc--;
40 *arg = *++p->argv;
41 } else
42 return opterror(opt, "requires a value", flags);
43 return 0;
44 }
45
46 static 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
54 static int get_value(struct parse_opt_ctx_t *p,
55 const struct option *opt, int flags)
56 {
57 const char *s, *arg;
58 const int unset = flags & OPT_UNSET;
59 int err;
60
61 if (unset && p->opt)
62 return opterror(opt, "takes no value", flags);
63 if (unset && (opt->flags & PARSE_OPT_NONEG))
64 return opterror(opt, "isn't available", flags);
65
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:
74 case OPTION_NEGBIT:
75 case OPTION_SET_INT:
76 case OPTION_SET_PTR:
77 return opterror(opt, "takes no value", flags);
78 default:
79 break;
80 }
81 }
82
83 switch (opt->type) {
84 case OPTION_BIT:
85 if (unset)
86 *(int *)opt->value &= ~opt->defval;
87 else
88 *(int *)opt->value |= opt->defval;
89 return 0;
90
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
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;
108 return 0;
109
110 case OPTION_STRING:
111 if (unset)
112 *(const char **)opt->value = NULL;
113 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
114 *(const char **)opt->value = (const char *)opt->defval;
115 else
116 return get_arg(p, opt, flags, (const char **)opt->value);
117 return 0;
118
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
132 case OPTION_CALLBACK:
133 if (unset)
134 return (*opt->callback)(opt, NULL, 1) ? (-1) : 0;
135 if (opt->flags & PARSE_OPT_NOARG)
136 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
137 if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
138 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
139 if (get_arg(p, opt, flags, &arg))
140 return -1;
141 return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
142
143 case OPTION_INTEGER:
144 if (unset) {
145 *(int *)opt->value = 0;
146 return 0;
147 }
148 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
149 *(int *)opt->value = opt->defval;
150 return 0;
151 }
152 if (get_arg(p, opt, flags, &arg))
153 return -1;
154 *(int *)opt->value = strtol(arg, (char **)&s, 10);
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
164 static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)
165 {
166 const struct option *numopt = NULL;
167
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 }
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;
193 }
194 return -2;
195 }
196
197 static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
198 const struct option *options)
199 {
200 const char *arg_end = strchr(arg, '=');
201 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
202 int abbrev_flags = 0, ambiguous_flags = 0;
203
204 if (!arg_end)
205 arg_end = arg + strlen(arg);
206
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);
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 }
225 if (!rest) {
226 /* abbreviated? */
227 if (!strncmp(options->long_name, arg, arg_end - arg)) {
228 is_abbreviated:
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 }
239 if (!(flags & OPT_UNSET) && *arg_end)
240 p->opt = arg_end + 1;
241 abbrev_option = options;
242 abbrev_flags = flags;
243 continue;
244 }
245 /* negation allowed? */
246 if (options->flags & PARSE_OPT_NONEG)
247 continue;
248 /* negated and abbreviated very much? */
249 if (!prefixcmp("no-", arg)) {
250 flags |= OPT_UNSET;
251 goto is_abbreviated;
252 }
253 /* negated? */
254 if (strncmp(arg, "no-", 3))
255 continue;
256 flags |= OPT_UNSET;
257 rest = skip_prefix(arg + 3, options->long_name);
258 /* abbreviated and negated? */
259 if (!rest && !prefixcmp(options->long_name, arg + 3))
260 goto is_abbreviated;
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 }
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);
280 if (abbrev_option)
281 return get_value(p, abbrev_option, abbrev_flags);
282 return -2;
283 }
284
285 static 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
304 static void check_typos(const char *arg, const struct option *options)
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
324 static 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) &&
330 (opts->flags & PARSE_OPT_OPTARG))
331 err |= optbug(opts, "uses incompatible flags "
332 "LASTARG_DEFAULT and OPTARG");
333 }
334 if (err)
335 exit(128);
336 }
337
338 void parse_options_start(struct parse_opt_ctx_t *ctx,
339 int argc, const char **argv, const char *prefix,
340 const struct option *options, int flags)
341 {
342 memset(ctx, 0, sizeof(*ctx));
343 ctx->argc = argc - 1;
344 ctx->argv = argv + 1;
345 ctx->out = argv;
346 ctx->prefix = prefix;
347 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
348 ctx->flags = flags;
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");
352 parse_options_check(options);
353 }
354
355 static int usage_with_options_internal(struct parse_opt_ctx_t *,
356 const char * const *,
357 const struct option *, int, int);
358
359 int parse_options_step(struct parse_opt_ctx_t *ctx,
360 const struct option *options,
361 const char * const usagestr[])
362 {
363 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
364
365 /* we must reset ->opt, unknown short option leave it dangling */
366 ctx->opt = NULL;
367
368 for (; ctx->argc; ctx->argc--, ctx->argv++) {
369 const char *arg = ctx->argv[0];
370
371 if (*arg != '-' || !arg[1]) {
372 if (parse_nodash_opt(ctx, arg, options) == 0)
373 continue;
374 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
375 break;
376 ctx->out[ctx->cpidx++] = ctx->argv[0];
377 continue;
378 }
379
380 if (arg[1] != '-') {
381 ctx->opt = arg + 1;
382 if (internal_help && *ctx->opt == 'h')
383 return parse_options_usage(ctx, usagestr, options, 0);
384 switch (parse_short_opt(ctx, options)) {
385 case -1:
386 return parse_options_usage(ctx, usagestr, options, 1);
387 case -2:
388 goto unknown;
389 }
390 if (ctx->opt)
391 check_typos(arg + 1, options);
392 while (ctx->opt) {
393 if (internal_help && *ctx->opt == 'h')
394 return parse_options_usage(ctx, usagestr, options, 0);
395 switch (parse_short_opt(ctx, options)) {
396 case -1:
397 return parse_options_usage(ctx, usagestr, options, 1);
398 case -2:
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] = '-';
406 goto unknown;
407 }
408 }
409 continue;
410 }
411
412 if (!arg[2]) { /* "--" */
413 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
414 ctx->argc--;
415 ctx->argv++;
416 }
417 break;
418 }
419
420 if (internal_help && !strcmp(arg + 2, "help-all"))
421 return usage_with_options_internal(ctx, usagestr, options, 1, 0);
422 if (internal_help && !strcmp(arg + 2, "help"))
423 return parse_options_usage(ctx, usagestr, options, 0);
424 switch (parse_long_opt(ctx, arg + 2, options)) {
425 case -1:
426 return parse_options_usage(ctx, usagestr, options, 1);
427 case -2:
428 goto unknown;
429 }
430 continue;
431 unknown:
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;
436 }
437 return PARSE_OPT_DONE;
438 }
439
440 int 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
447 int parse_options(int argc, const char **argv, const char *prefix,
448 const struct option *options, const char * const usagestr[],
449 int flags)
450 {
451 struct parse_opt_ctx_t ctx;
452
453 parse_options_start(&ctx, argc, argv, prefix, options, flags);
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 */
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);
466 }
467
468 return parse_options_end(&ctx);
469 }
470
471 static int usage_argh(const struct option *opts, FILE *outfile)
472 {
473 const char *s;
474 int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) || !opts->argh;
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>";
482 return fprintf(outfile, s, opts->argh ? opts->argh : "...");
483 }
484
485 #define USAGE_OPTS_WIDTH 24
486 #define USAGE_GAP 2
487
488 static 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)
491 {
492 FILE *outfile = err ? stderr : stdout;
493
494 if (!usagestr)
495 return PARSE_OPT_HELP;
496
497 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
498 fprintf(outfile, "cat <<\\EOF\n");
499
500 fprintf(outfile, "usage: %s\n", *usagestr++);
501 while (*usagestr && **usagestr)
502 fprintf(outfile, " or: %s\n", *usagestr++);
503 while (*usagestr) {
504 fprintf(outfile, "%s%s\n",
505 **usagestr ? " " : "",
506 *usagestr);
507 usagestr++;
508 }
509
510 if (opts->type != OPTION_GROUP)
511 fputc('\n', outfile);
512
513 for (; opts->type != OPTION_END; opts++) {
514 size_t pos;
515 int pad;
516
517 if (opts->type == OPTION_GROUP) {
518 fputc('\n', outfile);
519 if (*opts->help)
520 fprintf(outfile, "%s\n", opts->help);
521 continue;
522 }
523 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
524 continue;
525
526 pos = fprintf(outfile, " ");
527 if (opts->short_name && !(opts->flags & PARSE_OPT_NEGHELP)) {
528 if (opts->flags & PARSE_OPT_NODASH)
529 pos += fprintf(outfile, "%c", opts->short_name);
530 else
531 pos += fprintf(outfile, "-%c", opts->short_name);
532 }
533 if (opts->long_name && opts->short_name)
534 pos += fprintf(outfile, ", ");
535 if (opts->long_name)
536 pos += fprintf(outfile, "--%s%s",
537 (opts->flags & PARSE_OPT_NEGHELP) ? "no-" : "",
538 opts->long_name);
539 if (opts->type == OPTION_NUMBER)
540 pos += fprintf(outfile, "-NUM");
541
542 if (!(opts->flags & PARSE_OPT_NOARG))
543 pos += usage_argh(opts, outfile);
544
545 if (pos <= USAGE_OPTS_WIDTH)
546 pad = USAGE_OPTS_WIDTH - pos;
547 else {
548 fputc('\n', outfile);
549 pad = USAGE_OPTS_WIDTH;
550 }
551 fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", opts->help);
552 }
553 fputc('\n', outfile);
554
555 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
556 fputs("EOF\n", outfile);
557
558 return PARSE_OPT_HELP;
559 }
560
561 void usage_with_options(const char * const *usagestr,
562 const struct option *opts)
563 {
564 usage_with_options_internal(NULL, usagestr, opts, 0, 1);
565 exit(129);
566 }
567
568 void 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
576 static int parse_options_usage(struct parse_opt_ctx_t *ctx,
577 const char * const *usagestr,
578 const struct option *opts, int err)
579 {
580 return usage_with_options_internal(ctx, usagestr, opts, 0, err);
581 }
582
583
584 /*----- some often used options -----*/
585 #include "cache.h"
586
587 int 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 }
605
606 int 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 }
612
613 int 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
628 int 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
650 int 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 }
665
666 int 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 }
672
673 int 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 }