Merge branch 'jk/diff-graph-cleanup'
[alt-git.git] / parse-options.c
bloba6ce9efb79df18ab433904024cce9cce2b1e56ce
1 #include "git-compat-util.h"
2 #include "parse-options.h"
3 #include "cache.h"
4 #include "commit.h"
5 #include "color.h"
6 #include "utf8.h"
8 static int parse_options_usage(struct parse_opt_ctx_t *ctx,
9 const char * const *usagestr,
10 const struct option *opts, int err);
12 #define OPT_SHORT 1
13 #define OPT_UNSET 2
15 int optbug(const struct option *opt, const char *reason)
17 if (opt->long_name)
18 return error("BUG: option '%s' %s", opt->long_name, reason);
19 return error("BUG: switch '%c' %s", opt->short_name, reason);
22 static int get_arg(struct parse_opt_ctx_t *p, const struct option *opt,
23 int flags, const char **arg)
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;
30 } else if (p->argc > 1) {
31 p->argc--;
32 *arg = *++p->argv;
33 } else
34 return opterror(opt, "requires a value", flags);
35 return 0;
38 static void fix_filename(const char *prefix, const char **file)
40 if (!file || !*file || !prefix || is_absolute_path(*file)
41 || !strcmp("-", *file))
42 return;
43 *file = xstrdup(prefix_filename(prefix, strlen(prefix), *file));
46 static int get_value(struct parse_opt_ctx_t *p,
47 const struct option *opt, int flags)
49 const char *s, *arg;
50 const int unset = flags & OPT_UNSET;
51 int err;
53 if (unset && p->opt)
54 return opterror(opt, "takes no value", flags);
55 if (unset && (opt->flags & PARSE_OPT_NONEG))
56 return opterror(opt, "isn't available", flags);
57 if (!(flags & OPT_SHORT) && p->opt && (opt->flags & PARSE_OPT_NOARG))
58 return opterror(opt, "takes no value", flags);
60 switch (opt->type) {
61 case OPTION_LOWLEVEL_CALLBACK:
62 return (*(parse_opt_ll_cb *)opt->callback)(p, opt, unset);
64 case OPTION_BIT:
65 if (unset)
66 *(int *)opt->value &= ~opt->defval;
67 else
68 *(int *)opt->value |= opt->defval;
69 return 0;
71 case OPTION_NEGBIT:
72 if (unset)
73 *(int *)opt->value |= opt->defval;
74 else
75 *(int *)opt->value &= ~opt->defval;
76 return 0;
78 case OPTION_COUNTUP:
79 *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
80 return 0;
82 case OPTION_SET_INT:
83 *(int *)opt->value = unset ? 0 : opt->defval;
84 return 0;
86 case OPTION_SET_PTR:
87 *(void **)opt->value = unset ? NULL : (void *)opt->defval;
88 return 0;
90 case OPTION_STRING:
91 if (unset)
92 *(const char **)opt->value = NULL;
93 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
94 *(const char **)opt->value = (const char *)opt->defval;
95 else
96 return get_arg(p, opt, flags, (const char **)opt->value);
97 return 0;
99 case OPTION_FILENAME:
100 err = 0;
101 if (unset)
102 *(const char **)opt->value = NULL;
103 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
104 *(const char **)opt->value = (const char *)opt->defval;
105 else
106 err = get_arg(p, opt, flags, (const char **)opt->value);
108 if (!err)
109 fix_filename(p->prefix, (const char **)opt->value);
110 return err;
112 case OPTION_CALLBACK:
113 if (unset)
114 return (*opt->callback)(opt, NULL, 1) ? (-1) : 0;
115 if (opt->flags & PARSE_OPT_NOARG)
116 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
117 if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
118 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
119 if (get_arg(p, opt, flags, &arg))
120 return -1;
121 return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
123 case OPTION_INTEGER:
124 if (unset) {
125 *(int *)opt->value = 0;
126 return 0;
128 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
129 *(int *)opt->value = opt->defval;
130 return 0;
132 if (get_arg(p, opt, flags, &arg))
133 return -1;
134 *(int *)opt->value = strtol(arg, (char **)&s, 10);
135 if (*s)
136 return opterror(opt, "expects a numerical value", flags);
137 return 0;
139 default:
140 die("should not happen, someone must be hit on the forehead");
144 static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)
146 const struct option *numopt = NULL;
148 for (; options->type != OPTION_END; options++) {
149 if (options->short_name == *p->opt) {
150 p->opt = p->opt[1] ? p->opt + 1 : NULL;
151 return get_value(p, options, OPT_SHORT);
155 * Handle the numerical option later, explicit one-digit
156 * options take precedence over it.
158 if (options->type == OPTION_NUMBER)
159 numopt = options;
161 if (numopt && isdigit(*p->opt)) {
162 size_t len = 1;
163 char *arg;
164 int rc;
166 while (isdigit(p->opt[len]))
167 len++;
168 arg = xmemdupz(p->opt, len);
169 p->opt = p->opt[len] ? p->opt + len : NULL;
170 rc = (*numopt->callback)(numopt, arg, 0) ? (-1) : 0;
171 free(arg);
172 return rc;
174 return -2;
177 static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
178 const struct option *options)
180 const char *arg_end = strchr(arg, '=');
181 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
182 int abbrev_flags = 0, ambiguous_flags = 0;
184 if (!arg_end)
185 arg_end = arg + strlen(arg);
187 for (; options->type != OPTION_END; options++) {
188 const char *rest, *long_name = options->long_name;
189 int flags = 0, opt_flags = 0;
191 if (!long_name)
192 continue;
194 again:
195 rest = skip_prefix(arg, long_name);
196 if (options->type == OPTION_ARGUMENT) {
197 if (!rest)
198 continue;
199 if (*rest == '=')
200 return opterror(options, "takes no value", flags);
201 if (*rest)
202 continue;
203 p->out[p->cpidx++] = arg - 2;
204 return 0;
206 if (!rest) {
207 /* abbreviated? */
208 if (!strncmp(long_name, arg, arg_end - arg)) {
209 is_abbreviated:
210 if (abbrev_option) {
212 * If this is abbreviated, it is
213 * ambiguous. So when there is no
214 * exact match later, we need to
215 * error out.
217 ambiguous_option = abbrev_option;
218 ambiguous_flags = abbrev_flags;
220 if (!(flags & OPT_UNSET) && *arg_end)
221 p->opt = arg_end + 1;
222 abbrev_option = options;
223 abbrev_flags = flags ^ opt_flags;
224 continue;
226 /* negation allowed? */
227 if (options->flags & PARSE_OPT_NONEG)
228 continue;
229 /* negated and abbreviated very much? */
230 if (!prefixcmp("no-", arg)) {
231 flags |= OPT_UNSET;
232 goto is_abbreviated;
234 /* negated? */
235 if (prefixcmp(arg, "no-")) {
236 if (!prefixcmp(long_name, "no-")) {
237 long_name += 3;
238 opt_flags |= OPT_UNSET;
239 goto again;
241 continue;
243 flags |= OPT_UNSET;
244 rest = skip_prefix(arg + 3, long_name);
245 /* abbreviated and negated? */
246 if (!rest && !prefixcmp(long_name, arg + 3))
247 goto is_abbreviated;
248 if (!rest)
249 continue;
251 if (*rest) {
252 if (*rest != '=')
253 continue;
254 p->opt = rest + 1;
256 return get_value(p, options, flags ^ opt_flags);
259 if (ambiguous_option)
260 return error("Ambiguous option: %s "
261 "(could be --%s%s or --%s%s)",
262 arg,
263 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
264 ambiguous_option->long_name,
265 (abbrev_flags & OPT_UNSET) ? "no-" : "",
266 abbrev_option->long_name);
267 if (abbrev_option)
268 return get_value(p, abbrev_option, abbrev_flags);
269 return -2;
272 static int parse_nodash_opt(struct parse_opt_ctx_t *p, const char *arg,
273 const struct option *options)
275 for (; options->type != OPTION_END; options++) {
276 if (!(options->flags & PARSE_OPT_NODASH))
277 continue;
278 if (options->short_name == arg[0] && arg[1] == '\0')
279 return get_value(p, options, OPT_SHORT);
281 return -2;
284 static void check_typos(const char *arg, const struct option *options)
286 if (strlen(arg) < 3)
287 return;
289 if (!prefixcmp(arg, "no-")) {
290 error ("did you mean `--%s` (with two dashes ?)", arg);
291 exit(129);
294 for (; options->type != OPTION_END; options++) {
295 if (!options->long_name)
296 continue;
297 if (!prefixcmp(options->long_name, arg)) {
298 error ("did you mean `--%s` (with two dashes ?)", arg);
299 exit(129);
304 static void parse_options_check(const struct option *opts)
306 int err = 0;
308 for (; opts->type != OPTION_END; opts++) {
309 if ((opts->flags & PARSE_OPT_LASTARG_DEFAULT) &&
310 (opts->flags & PARSE_OPT_OPTARG))
311 err |= optbug(opts, "uses incompatible flags "
312 "LASTARG_DEFAULT and OPTARG");
313 if (opts->flags & PARSE_OPT_NODASH &&
314 ((opts->flags & PARSE_OPT_OPTARG) ||
315 !(opts->flags & PARSE_OPT_NOARG) ||
316 !(opts->flags & PARSE_OPT_NONEG) ||
317 opts->long_name))
318 err |= optbug(opts, "uses feature "
319 "not supported for dashless options");
320 switch (opts->type) {
321 case OPTION_COUNTUP:
322 case OPTION_BIT:
323 case OPTION_NEGBIT:
324 case OPTION_SET_INT:
325 case OPTION_SET_PTR:
326 case OPTION_NUMBER:
327 if ((opts->flags & PARSE_OPT_OPTARG) ||
328 !(opts->flags & PARSE_OPT_NOARG))
329 err |= optbug(opts, "should not accept an argument");
330 default:
331 ; /* ok. (usually accepts an argument) */
334 if (err)
335 exit(128);
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)
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);
355 static int usage_with_options_internal(struct parse_opt_ctx_t *,
356 const char * const *,
357 const struct option *, int, int);
359 int parse_options_step(struct parse_opt_ctx_t *ctx,
360 const struct option *options,
361 const char * const usagestr[])
363 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
365 /* we must reset ->opt, unknown short option leave it dangling */
366 ctx->opt = NULL;
368 for (; ctx->argc; ctx->argc--, ctx->argv++) {
369 const char *arg = ctx->argv[0];
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 return PARSE_OPT_NON_OPTION;
376 ctx->out[ctx->cpidx++] = ctx->argv[0];
377 continue;
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 if (ctx->opt)
389 check_typos(arg + 1, options);
390 goto unknown;
392 if (ctx->opt)
393 check_typos(arg + 1, options);
394 while (ctx->opt) {
395 if (internal_help && *ctx->opt == 'h')
396 return parse_options_usage(ctx, usagestr, options, 0);
397 switch (parse_short_opt(ctx, options)) {
398 case -1:
399 return parse_options_usage(ctx, usagestr, options, 1);
400 case -2:
401 /* fake a short option thing to hide the fact that we may have
402 * started to parse aggregated stuff
404 * This is leaky, too bad.
406 ctx->argv[0] = xstrdup(ctx->opt - 1);
407 *(char *)ctx->argv[0] = '-';
408 goto unknown;
411 continue;
414 if (!arg[2]) { /* "--" */
415 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
416 ctx->argc--;
417 ctx->argv++;
419 break;
422 if (internal_help && !strcmp(arg + 2, "help-all"))
423 return usage_with_options_internal(ctx, usagestr, options, 1, 0);
424 if (internal_help && !strcmp(arg + 2, "help"))
425 return parse_options_usage(ctx, usagestr, options, 0);
426 switch (parse_long_opt(ctx, arg + 2, options)) {
427 case -1:
428 return parse_options_usage(ctx, usagestr, options, 1);
429 case -2:
430 goto unknown;
432 continue;
433 unknown:
434 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
435 return PARSE_OPT_UNKNOWN;
436 ctx->out[ctx->cpidx++] = ctx->argv[0];
437 ctx->opt = NULL;
439 return PARSE_OPT_DONE;
442 int parse_options_end(struct parse_opt_ctx_t *ctx)
444 memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out));
445 ctx->out[ctx->cpidx + ctx->argc] = NULL;
446 return ctx->cpidx + ctx->argc;
449 int parse_options(int argc, const char **argv, const char *prefix,
450 const struct option *options, const char * const usagestr[],
451 int flags)
453 struct parse_opt_ctx_t ctx;
455 parse_options_start(&ctx, argc, argv, prefix, options, flags);
456 switch (parse_options_step(&ctx, options, usagestr)) {
457 case PARSE_OPT_HELP:
458 exit(129);
459 case PARSE_OPT_NON_OPTION:
460 case PARSE_OPT_DONE:
461 break;
462 default: /* PARSE_OPT_UNKNOWN */
463 if (ctx.argv[0][1] == '-') {
464 error("unknown option `%s'", ctx.argv[0] + 2);
465 } else {
466 error("unknown switch `%c'", *ctx.opt);
468 usage_with_options(usagestr, options);
471 precompose_argv(argc, argv);
472 return parse_options_end(&ctx);
475 static int usage_argh(const struct option *opts, FILE *outfile)
477 const char *s;
478 int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) || !opts->argh;
479 if (opts->flags & PARSE_OPT_OPTARG)
480 if (opts->long_name)
481 s = literal ? "[=%s]" : "[=<%s>]";
482 else
483 s = literal ? "[%s]" : "[<%s>]";
484 else
485 s = literal ? " %s" : " <%s>";
486 return utf8_fprintf(outfile, s, opts->argh ? _(opts->argh) : _("..."));
489 #define USAGE_OPTS_WIDTH 24
490 #define USAGE_GAP 2
492 static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
493 const char * const *usagestr,
494 const struct option *opts, int full, int err)
496 FILE *outfile = err ? stderr : stdout;
498 if (!usagestr)
499 return PARSE_OPT_HELP;
501 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
502 fprintf(outfile, "cat <<\\EOF\n");
504 fprintf_ln(outfile, _("usage: %s"), _(*usagestr++));
505 while (*usagestr && **usagestr)
506 /* TRANSLATORS: the colon here should align with the
507 one in "usage: %s" translation */
508 fprintf_ln(outfile, _(" or: %s"), _(*usagestr++));
509 while (*usagestr) {
510 if (**usagestr)
511 fprintf_ln(outfile, _(" %s"), _(*usagestr));
512 else
513 putchar('\n');
514 usagestr++;
517 if (opts->type != OPTION_GROUP)
518 fputc('\n', outfile);
520 for (; opts->type != OPTION_END; opts++) {
521 size_t pos;
522 int pad;
524 if (opts->type == OPTION_GROUP) {
525 fputc('\n', outfile);
526 if (*opts->help)
527 fprintf(outfile, "%s\n", _(opts->help));
528 continue;
530 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
531 continue;
533 pos = fprintf(outfile, " ");
534 if (opts->short_name) {
535 if (opts->flags & PARSE_OPT_NODASH)
536 pos += fprintf(outfile, "%c", opts->short_name);
537 else
538 pos += fprintf(outfile, "-%c", opts->short_name);
540 if (opts->long_name && opts->short_name)
541 pos += fprintf(outfile, ", ");
542 if (opts->long_name)
543 pos += fprintf(outfile, "--%s", opts->long_name);
544 if (opts->type == OPTION_NUMBER)
545 pos += utf8_fprintf(outfile, _("-NUM"));
547 if ((opts->flags & PARSE_OPT_LITERAL_ARGHELP) ||
548 !(opts->flags & PARSE_OPT_NOARG))
549 pos += usage_argh(opts, outfile);
551 if (pos <= USAGE_OPTS_WIDTH)
552 pad = USAGE_OPTS_WIDTH - pos;
553 else {
554 fputc('\n', outfile);
555 pad = USAGE_OPTS_WIDTH;
557 fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", _(opts->help));
559 fputc('\n', outfile);
561 if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
562 fputs("EOF\n", outfile);
564 return PARSE_OPT_HELP;
567 void NORETURN usage_with_options(const char * const *usagestr,
568 const struct option *opts)
570 usage_with_options_internal(NULL, usagestr, opts, 0, 1);
571 exit(129);
574 void NORETURN usage_msg_opt(const char *msg,
575 const char * const *usagestr,
576 const struct option *options)
578 fprintf(stderr, "%s\n\n", msg);
579 usage_with_options(usagestr, options);
582 static int parse_options_usage(struct parse_opt_ctx_t *ctx,
583 const char * const *usagestr,
584 const struct option *opts, int err)
586 return usage_with_options_internal(ctx, usagestr, opts, 0, err);
589 #undef opterror
590 int opterror(const struct option *opt, const char *reason, int flags)
592 if (flags & OPT_SHORT)
593 return error("switch `%c' %s", opt->short_name, reason);
594 if (flags & OPT_UNSET)
595 return error("option `no-%s' %s", opt->long_name, reason);
596 return error("option `%s' %s", opt->long_name, reason);