Merge branch 'sb/format-patch-parseopt' into sb/opt-filename
[git/spearce.git] / parse-options.c
blob1d25b94c72b3fc52eabbdc31a61e43e404ddb303
1 #include "git-compat-util.h"
2 #include "parse-options.h"
3 #include "cache.h"
4 #include "commit.h"
6 #define OPT_SHORT 1
7 #define OPT_UNSET 2
9 static int opterror(const struct option *opt, const char *reason, int flags)
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);
18 static int get_arg(struct parse_opt_ctx_t *p, const struct option *opt,
19 int flags, const char **arg)
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;
26 } else if (p->argc > 1) {
27 p->argc--;
28 *arg = *++p->argv;
29 } else
30 return opterror(opt, "requires a value", flags);
31 return 0;
34 static int get_value(struct parse_opt_ctx_t *p,
35 const struct option *opt, int flags)
37 const char *s, *arg;
38 const int unset = flags & OPT_UNSET;
40 if (unset && p->opt)
41 return opterror(opt, "takes no value", flags);
42 if (unset && (opt->flags & PARSE_OPT_NONEG))
43 return opterror(opt, "isn't available", flags);
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:
53 case OPTION_NEGBIT:
54 case OPTION_SET_INT:
55 case OPTION_SET_PTR:
56 return opterror(opt, "takes no value", flags);
57 default:
58 break;
62 switch (opt->type) {
63 case OPTION_BIT:
64 if (unset)
65 *(int *)opt->value &= ~opt->defval;
66 else
67 *(int *)opt->value |= opt->defval;
68 return 0;
70 case OPTION_NEGBIT:
71 if (unset)
72 *(int *)opt->value |= opt->defval;
73 else
74 *(int *)opt->value &= ~opt->defval;
75 return 0;
77 case OPTION_BOOLEAN:
78 *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
79 return 0;
81 case OPTION_SET_INT:
82 *(int *)opt->value = unset ? 0 : opt->defval;
83 return 0;
85 case OPTION_SET_PTR:
86 *(void **)opt->value = unset ? NULL : (void *)opt->defval;
87 return 0;
89 case OPTION_STRING:
90 if (unset)
91 *(const char **)opt->value = NULL;
92 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
93 *(const char **)opt->value = (const char *)opt->defval;
94 else
95 return get_arg(p, opt, flags, (const char **)opt->value);
96 return 0;
98 case OPTION_CALLBACK:
99 if (unset)
100 return (*opt->callback)(opt, NULL, 1) ? (-1) : 0;
101 if (opt->flags & PARSE_OPT_NOARG)
102 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
103 if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
104 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
105 if (get_arg(p, opt, flags, &arg))
106 return -1;
107 return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
109 case OPTION_INTEGER:
110 if (unset) {
111 *(int *)opt->value = 0;
112 return 0;
114 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
115 *(int *)opt->value = opt->defval;
116 return 0;
118 if (get_arg(p, opt, flags, &arg))
119 return -1;
120 *(int *)opt->value = strtol(arg, (char **)&s, 10);
121 if (*s)
122 return opterror(opt, "expects a numerical value", flags);
123 return 0;
125 default:
126 die("should not happen, someone must be hit on the forehead");
130 static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)
132 const struct option *numopt = NULL;
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);
141 * Handle the numerical option later, explicit one-digit
142 * options take precedence over it.
144 if (options->type == OPTION_NUMBER)
145 numopt = options;
147 if (numopt && isdigit(*p->opt)) {
148 size_t len = 1;
149 char *arg;
150 int rc;
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;
160 return -2;
163 static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
164 const struct option *options)
166 const char *arg_end = strchr(arg, '=');
167 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
168 int abbrev_flags = 0, ambiguous_flags = 0;
170 if (!arg_end)
171 arg_end = arg + strlen(arg);
173 for (; options->type != OPTION_END; options++) {
174 const char *rest;
175 int flags = 0;
177 if (!options->long_name)
178 continue;
180 rest = skip_prefix(arg, options->long_name);
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;
191 if (!rest) {
192 /* abbreviated? */
193 if (!strncmp(options->long_name, arg, arg_end - arg)) {
194 is_abbreviated:
195 if (abbrev_option) {
197 * If this is abbreviated, it is
198 * ambiguous. So when there is no
199 * exact match later, we need to
200 * error out.
202 ambiguous_option = abbrev_option;
203 ambiguous_flags = abbrev_flags;
205 if (!(flags & OPT_UNSET) && *arg_end)
206 p->opt = arg_end + 1;
207 abbrev_option = options;
208 abbrev_flags = flags;
209 continue;
211 /* negated and abbreviated very much? */
212 if (!prefixcmp("no-", arg)) {
213 flags |= OPT_UNSET;
214 goto is_abbreviated;
216 /* negated? */
217 if (strncmp(arg, "no-", 3))
218 continue;
219 flags |= OPT_UNSET;
220 rest = skip_prefix(arg + 3, options->long_name);
221 /* abbreviated and negated? */
222 if (!rest && !prefixcmp(options->long_name, arg + 3))
223 goto is_abbreviated;
224 if (!rest)
225 continue;
227 if (*rest) {
228 if (*rest != '=')
229 continue;
230 p->opt = rest + 1;
232 return get_value(p, options, flags);
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);
243 if (abbrev_option)
244 return get_value(p, abbrev_option, abbrev_flags);
245 return -2;
248 static int parse_nodash_opt(struct parse_opt_ctx_t *p, const char *arg,
249 const struct option *options)
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);
264 return -2;
267 static void check_typos(const char *arg, const struct option *options)
269 if (strlen(arg) < 3)
270 return;
272 if (!prefixcmp(arg, "no-")) {
273 error ("did you mean `--%s` (with two dashes ?)", arg);
274 exit(129);
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);
287 void parse_options_start(struct parse_opt_ctx_t *ctx,
288 int argc, const char **argv, int flags)
290 memset(ctx, 0, sizeof(*ctx));
291 ctx->argc = argc - 1;
292 ctx->argv = argv + 1;
293 ctx->out = argv;
294 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
295 ctx->flags = flags;
296 if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
297 (flags & PARSE_OPT_STOP_AT_NON_OPTION))
298 die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
301 static int usage_with_options_internal(const char * const *,
302 const struct option *, int);
304 int parse_options_step(struct parse_opt_ctx_t *ctx,
305 const struct option *options,
306 const char * const usagestr[])
308 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
310 /* we must reset ->opt, unknown short option leave it dangling */
311 ctx->opt = NULL;
313 for (; ctx->argc; ctx->argc--, ctx->argv++) {
314 const char *arg = ctx->argv[0];
316 if (*arg != '-' || !arg[1]) {
317 if (parse_nodash_opt(ctx, arg, options) == 0)
318 continue;
319 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
320 break;
321 ctx->out[ctx->cpidx++] = ctx->argv[0];
322 continue;
325 if (arg[1] != '-') {
326 ctx->opt = arg + 1;
327 if (internal_help && *ctx->opt == 'h')
328 return parse_options_usage(usagestr, options);
329 switch (parse_short_opt(ctx, options)) {
330 case -1:
331 return parse_options_usage(usagestr, options);
332 case -2:
333 goto unknown;
335 if (ctx->opt)
336 check_typos(arg + 1, options);
337 while (ctx->opt) {
338 if (internal_help && *ctx->opt == 'h')
339 return parse_options_usage(usagestr, options);
340 switch (parse_short_opt(ctx, options)) {
341 case -1:
342 return parse_options_usage(usagestr, options);
343 case -2:
344 /* fake a short option thing to hide the fact that we may have
345 * started to parse aggregated stuff
347 * This is leaky, too bad.
349 ctx->argv[0] = xstrdup(ctx->opt - 1);
350 *(char *)ctx->argv[0] = '-';
351 goto unknown;
354 continue;
357 if (!arg[2]) { /* "--" */
358 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
359 ctx->argc--;
360 ctx->argv++;
362 break;
365 if (internal_help && !strcmp(arg + 2, "help-all"))
366 return usage_with_options_internal(usagestr, options, 1);
367 if (internal_help && !strcmp(arg + 2, "help"))
368 return parse_options_usage(usagestr, options);
369 switch (parse_long_opt(ctx, arg + 2, options)) {
370 case -1:
371 return parse_options_usage(usagestr, options);
372 case -2:
373 goto unknown;
375 continue;
376 unknown:
377 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
378 return PARSE_OPT_UNKNOWN;
379 ctx->out[ctx->cpidx++] = ctx->argv[0];
380 ctx->opt = NULL;
382 return PARSE_OPT_DONE;
385 int parse_options_end(struct parse_opt_ctx_t *ctx)
387 memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out));
388 ctx->out[ctx->cpidx + ctx->argc] = NULL;
389 return ctx->cpidx + ctx->argc;
392 int parse_options(int argc, const char **argv, const struct option *options,
393 const char * const usagestr[], int flags)
395 struct parse_opt_ctx_t ctx;
397 parse_options_start(&ctx, argc, argv, flags);
398 switch (parse_options_step(&ctx, options, usagestr)) {
399 case PARSE_OPT_HELP:
400 exit(129);
401 case PARSE_OPT_DONE:
402 break;
403 default: /* PARSE_OPT_UNKNOWN */
404 if (ctx.argv[0][1] == '-') {
405 error("unknown option `%s'", ctx.argv[0] + 2);
406 } else {
407 error("unknown switch `%c'", *ctx.opt);
409 usage_with_options(usagestr, options);
412 return parse_options_end(&ctx);
415 static int usage_argh(const struct option *opts)
417 const char *s;
418 int literal = opts->flags & PARSE_OPT_LITERAL_ARGHELP;
419 if (opts->flags & PARSE_OPT_OPTARG)
420 if (opts->long_name)
421 s = literal ? "[=%s]" : "[=<%s>]";
422 else
423 s = literal ? "[%s]" : "[<%s>]";
424 else
425 s = literal ? " %s" : " <%s>";
426 return fprintf(stderr, s, opts->argh);
429 #define USAGE_OPTS_WIDTH 24
430 #define USAGE_GAP 2
432 int usage_with_options_internal(const char * const *usagestr,
433 const struct option *opts, int full)
435 if (!usagestr)
436 return PARSE_OPT_HELP;
438 fprintf(stderr, "usage: %s\n", *usagestr++);
439 while (*usagestr && **usagestr)
440 fprintf(stderr, " or: %s\n", *usagestr++);
441 while (*usagestr) {
442 fprintf(stderr, "%s%s\n",
443 **usagestr ? " " : "",
444 *usagestr);
445 usagestr++;
448 if (opts->type != OPTION_GROUP)
449 fputc('\n', stderr);
451 for (; opts->type != OPTION_END; opts++) {
452 size_t pos;
453 int pad;
455 if (opts->type == OPTION_GROUP) {
456 fputc('\n', stderr);
457 if (*opts->help)
458 fprintf(stderr, "%s\n", opts->help);
459 continue;
461 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
462 continue;
464 pos = fprintf(stderr, " ");
465 if (opts->short_name) {
466 if (opts->flags & PARSE_OPT_NODASH)
467 pos += fprintf(stderr, "%c", opts->short_name);
468 else
469 pos += fprintf(stderr, "-%c", opts->short_name);
471 if (opts->long_name && opts->short_name)
472 pos += fprintf(stderr, ", ");
473 if (opts->long_name)
474 pos += fprintf(stderr, "--%s", opts->long_name);
475 if (opts->type == OPTION_NUMBER)
476 pos += fprintf(stderr, "-NUM");
478 switch (opts->type) {
479 case OPTION_ARGUMENT:
480 break;
481 case OPTION_INTEGER:
482 if (opts->flags & PARSE_OPT_OPTARG)
483 if (opts->long_name)
484 pos += fprintf(stderr, "[=<n>]");
485 else
486 pos += fprintf(stderr, "[<n>]");
487 else
488 pos += fprintf(stderr, " <n>");
489 break;
490 case OPTION_CALLBACK:
491 if (opts->flags & PARSE_OPT_NOARG)
492 break;
493 /* FALLTHROUGH */
494 case OPTION_STRING:
495 if (opts->argh)
496 pos += usage_argh(opts);
497 else {
498 if (opts->flags & PARSE_OPT_OPTARG)
499 if (opts->long_name)
500 pos += fprintf(stderr, "[=...]");
501 else
502 pos += fprintf(stderr, "[...]");
503 else
504 pos += fprintf(stderr, " ...");
506 break;
507 default: /* OPTION_{BIT,BOOLEAN,NUMBER,SET_INT,SET_PTR} */
508 break;
511 if (pos <= USAGE_OPTS_WIDTH)
512 pad = USAGE_OPTS_WIDTH - pos;
513 else {
514 fputc('\n', stderr);
515 pad = USAGE_OPTS_WIDTH;
517 fprintf(stderr, "%*s%s\n", pad + USAGE_GAP, "", opts->help);
519 fputc('\n', stderr);
521 return PARSE_OPT_HELP;
524 void usage_with_options(const char * const *usagestr,
525 const struct option *opts)
527 usage_with_options_internal(usagestr, opts, 0);
528 exit(129);
531 int parse_options_usage(const char * const *usagestr,
532 const struct option *opts)
534 return usage_with_options_internal(usagestr, opts, 0);
538 /*----- some often used options -----*/
539 #include "cache.h"
541 int parse_opt_abbrev_cb(const struct option *opt, const char *arg, int unset)
543 int v;
545 if (!arg) {
546 v = unset ? 0 : DEFAULT_ABBREV;
547 } else {
548 v = strtol(arg, (char **)&arg, 10);
549 if (*arg)
550 return opterror(opt, "expects a numerical value", 0);
551 if (v && v < MINIMUM_ABBREV)
552 v = MINIMUM_ABBREV;
553 else if (v > 40)
554 v = 40;
556 *(int *)(opt->value) = v;
557 return 0;
560 int parse_opt_approxidate_cb(const struct option *opt, const char *arg,
561 int unset)
563 *(unsigned long *)(opt->value) = approxidate(arg);
564 return 0;
567 int parse_opt_verbosity_cb(const struct option *opt, const char *arg,
568 int unset)
570 int *target = opt->value;
572 if (unset)
573 /* --no-quiet, --no-verbose */
574 *target = 0;
575 else if (opt->short_name == 'v') {
576 if (*target >= 0)
577 (*target)++;
578 else
579 *target = 1;
580 } else {
581 if (*target <= 0)
582 (*target)--;
583 else
584 *target = -1;
586 return 0;
589 int parse_opt_with_commit(const struct option *opt, const char *arg, int unset)
591 unsigned char sha1[20];
592 struct commit *commit;
594 if (!arg)
595 return -1;
596 if (get_sha1(arg, sha1))
597 return error("malformed object name %s", arg);
598 commit = lookup_commit_reference(sha1);
599 if (!commit)
600 return error("no such commit %s", arg);
601 commit_list_insert(commit, opt->value);
602 return 0;
606 * This should really be OPTION_FILENAME type as a part of
607 * parse_options that take prefix to do this while parsing.
609 extern const char *parse_options_fix_filename(const char *prefix, const char *file)
611 if (!file || !prefix || is_absolute_path(file) || !strcmp("-", file))
612 return file;
613 return prefix_filename(prefix, strlen(prefix), file);