]> git.ipfire.org Git - thirdparty/git.git/blob - parse-options.c
parse-options: do not infer PARSE_OPT_NOARG from option type
[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 if (!(flags & OPT_SHORT) && p->opt && (opt->flags & PARSE_OPT_NOARG))
66 return opterror(opt, "takes no value", flags);
67
68 switch (opt->type) {
69 case OPTION_BIT:
70 if (unset)
71 *(int *)opt->value &= ~opt->defval;
72 else
73 *(int *)opt->value |= opt->defval;
74 return 0;
75
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
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;
93 return 0;
94
95 case OPTION_STRING:
96 if (unset)
97 *(const char **)opt->value = NULL;
98 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
99 *(const char **)opt->value = (const char *)opt->defval;
100 else
101 return get_arg(p, opt, flags, (const char **)opt->value);
102 return 0;
103
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
117 case OPTION_CALLBACK:
118 if (unset)
119 return (*opt->callback)(opt, NULL, 1) ? (-1) : 0;
120 if (opt->flags & PARSE_OPT_NOARG)
121 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
122 if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
123 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
124 if (get_arg(p, opt, flags, &arg))
125 return -1;
126 return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
127
128 case OPTION_INTEGER:
129 if (unset) {
130 *(int *)opt->value = 0;
131 return 0;
132 }
133 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
134 *(int *)opt->value = opt->defval;
135 return 0;
136 }
137 if (get_arg(p, opt, flags, &arg))
138 return -1;
139 *(int *)opt->value = strtol(arg, (char **)&s, 10);
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
149 static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)
150 {
151 const struct option *numopt = NULL;
152
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 }
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;
178 }
179 return -2;
180 }
181
182 static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
183 const struct option *options)
184 {
185 const char *arg_end = strchr(arg, '=');
186 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
187 int abbrev_flags = 0, ambiguous_flags = 0;
188
189 if (!arg_end)
190 arg_end = arg + strlen(arg);
191
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);
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 }
210 if (!rest) {
211 /* abbreviated? */
212 if (!strncmp(options->long_name, arg, arg_end - arg)) {
213 is_abbreviated:
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 }
224 if (!(flags & OPT_UNSET) && *arg_end)
225 p->opt = arg_end + 1;
226 abbrev_option = options;
227 abbrev_flags = flags;
228 continue;
229 }
230 /* negation allowed? */
231 if (options->flags & PARSE_OPT_NONEG)
232 continue;
233 /* negated and abbreviated very much? */
234 if (!prefixcmp("no-", arg)) {
235 flags |= OPT_UNSET;
236 goto is_abbreviated;
237 }
238 /* negated? */
239 if (strncmp(arg, "no-", 3))
240 continue;
241 flags |= OPT_UNSET;
242 rest = skip_prefix(arg + 3, options->long_name);
243 /* abbreviated and negated? */
244 if (!rest && !prefixcmp(options->long_name, arg + 3))
245 goto is_abbreviated;
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 }
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);
265 if (abbrev_option)
266 return get_value(p, abbrev_option, abbrev_flags);
267 return -2;
268 }
269
270 static 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;
276 if (options->short_name == arg[0] && arg[1] == '\0')
277 return get_value(p, options, OPT_SHORT);
278 }
279 return -2;
280 }
281
282 static void check_typos(const char *arg, const struct option *options)
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
302 static 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) &&
308 (opts->flags & PARSE_OPT_OPTARG))
309 err |= optbug(opts, "uses incompatible flags "
310 "LASTARG_DEFAULT and OPTARG");
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");
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 }
331 }
332 if (err)
333 exit(128);
334 }
335
336 void parse_options_start(struct parse_opt_ctx_t *ctx,
337 int argc, const char **argv, const char *prefix,
338 const struct option *options, int flags)
339 {
340 memset(ctx, 0, sizeof(*ctx));
341 ctx->argc = argc - 1;
342 ctx->argv = argv + 1;
343 ctx->out = argv;
344 ctx->prefix = prefix;
345 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
346 ctx->flags = flags;
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");
350 parse_options_check(options);
351 }
352
353 static int usage_with_options_internal(struct parse_opt_ctx_t *,
354 const char * const *,
355 const struct option *, int, int);
356
357 int parse_options_step(struct parse_opt_ctx_t *ctx,
358 const struct option *options,
359 const char * const usagestr[])
360 {
361 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
362
363 /* we must reset ->opt, unknown short option leave it dangling */
364 ctx->opt = NULL;
365
366 for (; ctx->argc; ctx->argc--, ctx->argv++) {
367 const char *arg = ctx->argv[0];
368
369 if (*arg != '-' || !arg[1]) {
370 if (parse_nodash_opt(ctx, arg, options) == 0)
371 continue;
372 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
373 break;
374 ctx->out[ctx->cpidx++] = ctx->argv[0];
375 continue;
376 }
377
378 if (arg[1] != '-') {
379 ctx->opt = arg + 1;
380 if (internal_help && *ctx->opt == 'h')
381 return parse_options_usage(ctx, usagestr, options, 0);
382 switch (parse_short_opt(ctx, options)) {
383 case -1:
384 return parse_options_usage(ctx, usagestr, options, 1);
385 case -2:
386 goto unknown;
387 }
388 if (ctx->opt)
389 check_typos(arg + 1, options);
390 while (ctx->opt) {
391 if (internal_help && *ctx->opt == 'h')
392 return parse_options_usage(ctx, usagestr, options, 0);
393 switch (parse_short_opt(ctx, options)) {
394 case -1:
395 return parse_options_usage(ctx, usagestr, options, 1);
396 case -2:
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] = '-';
404 goto unknown;
405 }
406 }
407 continue;
408 }
409
410 if (!arg[2]) { /* "--" */
411 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
412 ctx->argc--;
413 ctx->argv++;
414 }
415 break;
416 }
417
418 if (internal_help && !strcmp(arg + 2, "help-all"))
419 return usage_with_options_internal(ctx, usagestr, options, 1, 0);
420 if (internal_help && !strcmp(arg + 2, "help"))
421 return parse_options_usage(ctx, usagestr, options, 0);
422 switch (parse_long_opt(ctx, arg + 2, options)) {
423 case -1:
424 return parse_options_usage(ctx, usagestr, options, 1);
425 case -2:
426 goto unknown;
427 }
428 continue;
429 unknown:
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;
434 }
435 return PARSE_OPT_DONE;
436 }
437
438 int 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
445 int parse_options(int argc, const char **argv, const char *prefix,
446 const struct option *options, const char * const usagestr[],
447 int flags)
448 {
449 struct parse_opt_ctx_t ctx;
450
451 parse_options_start(&ctx, argc, argv, prefix, options, flags);
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 */
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);
464 }
465
466 return parse_options_end(&ctx);
467 }
468
469 static int usage_argh(const struct option *opts, FILE *outfile)
470 {
471 const char *s;
472 int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) || !opts->argh;
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>";
480 return fprintf(outfile, s, opts->argh ? opts->argh : "...");
481 }
482
483 #define USAGE_OPTS_WIDTH 24
484 #define USAGE_GAP 2
485
486 static 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)
489 {
490 FILE *outfile = err ? stderr : stdout;
491
492 if (!usagestr)
493 return PARSE_OPT_HELP;
494
495 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
496 fprintf(outfile, "cat <<\\EOF\n");
497
498 fprintf(outfile, "usage: %s\n", *usagestr++);
499 while (*usagestr && **usagestr)
500 fprintf(outfile, " or: %s\n", *usagestr++);
501 while (*usagestr) {
502 fprintf(outfile, "%s%s\n",
503 **usagestr ? " " : "",
504 *usagestr);
505 usagestr++;
506 }
507
508 if (opts->type != OPTION_GROUP)
509 fputc('\n', outfile);
510
511 for (; opts->type != OPTION_END; opts++) {
512 size_t pos;
513 int pad;
514
515 if (opts->type == OPTION_GROUP) {
516 fputc('\n', outfile);
517 if (*opts->help)
518 fprintf(outfile, "%s\n", opts->help);
519 continue;
520 }
521 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
522 continue;
523
524 pos = fprintf(outfile, " ");
525 if (opts->short_name && !(opts->flags & PARSE_OPT_NEGHELP)) {
526 if (opts->flags & PARSE_OPT_NODASH)
527 pos += fprintf(outfile, "%c", opts->short_name);
528 else
529 pos += fprintf(outfile, "-%c", opts->short_name);
530 }
531 if (opts->long_name && opts->short_name)
532 pos += fprintf(outfile, ", ");
533 if (opts->long_name)
534 pos += fprintf(outfile, "--%s%s",
535 (opts->flags & PARSE_OPT_NEGHELP) ? "no-" : "",
536 opts->long_name);
537 if (opts->type == OPTION_NUMBER)
538 pos += fprintf(outfile, "-NUM");
539
540 if (!(opts->flags & PARSE_OPT_NOARG))
541 pos += usage_argh(opts, outfile);
542
543 if (pos <= USAGE_OPTS_WIDTH)
544 pad = USAGE_OPTS_WIDTH - pos;
545 else {
546 fputc('\n', outfile);
547 pad = USAGE_OPTS_WIDTH;
548 }
549 fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", opts->help);
550 }
551 fputc('\n', outfile);
552
553 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
554 fputs("EOF\n", outfile);
555
556 return PARSE_OPT_HELP;
557 }
558
559 void usage_with_options(const char * const *usagestr,
560 const struct option *opts)
561 {
562 usage_with_options_internal(NULL, usagestr, opts, 0, 1);
563 exit(129);
564 }
565
566 void usage_msg_opt(const char *msg,
567 const char * const *usagestr,
568 const struct option *options)
569 {
570 fprintf(stderr, "%s\n\n", msg);
571 usage_with_options(usagestr, options);
572 }
573
574 static int parse_options_usage(struct parse_opt_ctx_t *ctx,
575 const char * const *usagestr,
576 const struct option *opts, int err)
577 {
578 return usage_with_options_internal(ctx, usagestr, opts, 0, err);
579 }
580
581
582 /*----- some often used options -----*/
583 #include "cache.h"
584
585 int parse_opt_abbrev_cb(const struct option *opt, const char *arg, int unset)
586 {
587 int v;
588
589 if (!arg) {
590 v = unset ? 0 : DEFAULT_ABBREV;
591 } else {
592 v = strtol(arg, (char **)&arg, 10);
593 if (*arg)
594 return opterror(opt, "expects a numerical value", 0);
595 if (v && v < MINIMUM_ABBREV)
596 v = MINIMUM_ABBREV;
597 else if (v > 40)
598 v = 40;
599 }
600 *(int *)(opt->value) = v;
601 return 0;
602 }
603
604 int parse_opt_approxidate_cb(const struct option *opt, const char *arg,
605 int unset)
606 {
607 *(unsigned long *)(opt->value) = approxidate(arg);
608 return 0;
609 }
610
611 int parse_opt_color_flag_cb(const struct option *opt, const char *arg,
612 int unset)
613 {
614 int value;
615
616 if (!arg)
617 arg = unset ? "never" : (const char *)opt->defval;
618 value = git_config_colorbool(NULL, arg, -1);
619 if (value < 0)
620 return opterror(opt,
621 "expects \"always\", \"auto\", or \"never\"", 0);
622 *(int *)opt->value = value;
623 return 0;
624 }
625
626 int parse_opt_verbosity_cb(const struct option *opt, const char *arg,
627 int unset)
628 {
629 int *target = opt->value;
630
631 if (unset)
632 /* --no-quiet, --no-verbose */
633 *target = 0;
634 else if (opt->short_name == 'v') {
635 if (*target >= 0)
636 (*target)++;
637 else
638 *target = 1;
639 } else {
640 if (*target <= 0)
641 (*target)--;
642 else
643 *target = -1;
644 }
645 return 0;
646 }
647
648 int parse_opt_with_commit(const struct option *opt, const char *arg, int unset)
649 {
650 unsigned char sha1[20];
651 struct commit *commit;
652
653 if (!arg)
654 return -1;
655 if (get_sha1(arg, sha1))
656 return error("malformed object name %s", arg);
657 commit = lookup_commit_reference(sha1);
658 if (!commit)
659 return error("no such commit %s", arg);
660 commit_list_insert(commit, opt->value);
661 return 0;
662 }
663
664 int parse_opt_tertiary(const struct option *opt, const char *arg, int unset)
665 {
666 int *target = opt->value;
667 *target = unset ? 2 : 1;
668 return 0;
669 }
670
671 int parse_options_concat(struct option *dst, size_t dst_size, struct option *src)
672 {
673 int i, j;
674
675 for (i = 0; i < dst_size; i++)
676 if (dst[i].type == OPTION_END)
677 break;
678 for (j = 0; i < dst_size; i++, j++) {
679 dst[i] = src[j];
680 if (src[j].type == OPTION_END)
681 return 0;
682 }
683 return -1;
684 }