2 #include "parse-options.h"
5 #include <linux/string.h>
10 static struct strbuf error_buf = STRBUF_INIT;
12 static int opterror(const struct option *opt, const char *reason, int flags)
14 if (flags & OPT_SHORT)
15 return error("switch `%c' %s", opt->short_name, reason);
16 if (flags & OPT_UNSET)
17 return error("option `no-%s' %s", opt->long_name, reason);
18 return error("option `%s' %s", opt->long_name, reason);
21 static int get_arg(struct parse_opt_ctx_t *p, const struct option *opt,
22 int flags, const char **arg)
27 } else if ((opt->flags & PARSE_OPT_LASTARG_DEFAULT) && (p->argc == 1 ||
28 **(p->argv + 1) == '-')) {
29 *arg = (const char *)opt->defval;
30 } else if (p->argc > 1) {
34 return opterror(opt, "requires a value", flags);
38 static int get_value(struct parse_opt_ctx_t *p,
39 const struct option *opt, int flags)
41 const char *s, *arg = NULL;
42 const int unset = flags & OPT_UNSET;
46 return opterror(opt, "takes no value", flags);
47 if (unset && (opt->flags & PARSE_OPT_NONEG))
48 return opterror(opt, "isn't available", flags);
49 if (opt->flags & PARSE_OPT_DISABLED)
50 return opterror(opt, "is not usable", flags);
52 if (opt->flags & PARSE_OPT_EXCLUSIVE) {
53 if (p->excl_opt && p->excl_opt != opt) {
56 if (((flags & OPT_SHORT) && p->excl_opt->short_name) ||
57 p->excl_opt->long_name == NULL) {
58 scnprintf(msg, sizeof(msg), "cannot be used with switch `%c'",
59 p->excl_opt->short_name);
61 scnprintf(msg, sizeof(msg), "cannot be used with %s",
62 p->excl_opt->long_name);
64 opterror(opt, msg, flags);
69 if (!(flags & OPT_SHORT) && p->opt) {
72 if (!(opt->flags & PARSE_OPT_NOARG))
80 return opterror(opt, "takes no value", flags);
97 *(int *)opt->value &= ~opt->defval;
99 *(int *)opt->value |= opt->defval;
103 *(bool *)opt->value = unset ? false : true;
105 *(bool *)opt->set = true;
109 *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
112 case OPTION_SET_UINT:
113 *(unsigned int *)opt->value = unset ? 0 : opt->defval;
117 *(void **)opt->value = unset ? NULL : (void *)opt->defval;
123 *(const char **)opt->value = NULL;
124 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
125 *(const char **)opt->value = (const char *)opt->defval;
127 err = get_arg(p, opt, flags, (const char **)opt->value);
129 /* PARSE_OPT_NOEMPTY: Allow NULL but disallow empty string. */
130 if (opt->flags & PARSE_OPT_NOEMPTY) {
131 const char *val = *(const char **)opt->value;
136 /* Similar to unset if we are given an empty string. */
137 if (val[0] == '\0') {
138 *(const char **)opt->value = NULL;
145 case OPTION_CALLBACK:
147 return (*opt->callback)(opt, NULL, 1) ? (-1) : 0;
148 if (opt->flags & PARSE_OPT_NOARG)
149 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
150 if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
151 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
152 if (get_arg(p, opt, flags, &arg))
154 return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
158 *(int *)opt->value = 0;
161 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
162 *(int *)opt->value = opt->defval;
165 if (get_arg(p, opt, flags, &arg))
167 *(int *)opt->value = strtol(arg, (char **)&s, 10);
169 return opterror(opt, "expects a numerical value", flags);
172 case OPTION_UINTEGER:
174 *(unsigned int *)opt->value = 0;
177 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
178 *(unsigned int *)opt->value = opt->defval;
181 if (get_arg(p, opt, flags, &arg))
183 *(unsigned int *)opt->value = strtol(arg, (char **)&s, 10);
185 return opterror(opt, "expects a numerical value", flags);
190 *(long *)opt->value = 0;
193 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
194 *(long *)opt->value = opt->defval;
197 if (get_arg(p, opt, flags, &arg))
199 *(long *)opt->value = strtol(arg, (char **)&s, 10);
201 return opterror(opt, "expects a numerical value", flags);
206 *(u64 *)opt->value = 0;
209 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
210 *(u64 *)opt->value = opt->defval;
213 if (get_arg(p, opt, flags, &arg))
215 *(u64 *)opt->value = strtoull(arg, (char **)&s, 10);
217 return opterror(opt, "expects a numerical value", flags);
221 case OPTION_ARGUMENT:
224 die("should not happen, someone must be hit on the forehead");
228 static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)
230 for (; options->type != OPTION_END; options++) {
231 if (options->short_name == *p->opt) {
232 p->opt = p->opt[1] ? p->opt + 1 : NULL;
233 return get_value(p, options, OPT_SHORT);
239 static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
240 const struct option *options)
242 const char *arg_end = strchr(arg, '=');
243 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
244 int abbrev_flags = 0, ambiguous_flags = 0;
247 arg_end = arg + strlen(arg);
249 for (; options->type != OPTION_END; options++) {
253 if (!options->long_name)
256 rest = skip_prefix(arg, options->long_name);
257 if (options->type == OPTION_ARGUMENT) {
261 return opterror(options, "takes no value", flags);
264 p->out[p->cpidx++] = arg - 2;
268 if (!prefixcmp(options->long_name, "no-")) {
270 * The long name itself starts with "no-", so
271 * accept the option without "no-" so that users
272 * do not have to enter "no-no-" to get the
275 rest = skip_prefix(arg, options->long_name + 3);
280 /* Abbreviated case */
281 if (!prefixcmp(options->long_name + 3, arg)) {
287 if (!strncmp(options->long_name, arg, arg_end - arg)) {
291 * If this is abbreviated, it is
292 * ambiguous. So when there is no
293 * exact match later, we need to
296 ambiguous_option = abbrev_option;
297 ambiguous_flags = abbrev_flags;
299 if (!(flags & OPT_UNSET) && *arg_end)
300 p->opt = arg_end + 1;
301 abbrev_option = options;
302 abbrev_flags = flags;
305 /* negated and abbreviated very much? */
306 if (!prefixcmp("no-", arg)) {
311 if (strncmp(arg, "no-", 3))
314 rest = skip_prefix(arg + 3, options->long_name);
315 /* abbreviated and negated? */
316 if (!rest && !prefixcmp(options->long_name, arg + 3))
327 return get_value(p, options, flags);
330 if (ambiguous_option)
331 return error("Ambiguous option: %s "
332 "(could be --%s%s or --%s%s)",
334 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
335 ambiguous_option->long_name,
336 (abbrev_flags & OPT_UNSET) ? "no-" : "",
337 abbrev_option->long_name);
339 return get_value(p, abbrev_option, abbrev_flags);
343 static void check_typos(const char *arg, const struct option *options)
348 if (!prefixcmp(arg, "no-")) {
349 error ("did you mean `--%s` (with two dashes ?)", arg);
353 for (; options->type != OPTION_END; options++) {
354 if (!options->long_name)
356 if (!prefixcmp(options->long_name, arg)) {
357 error ("did you mean `--%s` (with two dashes ?)", arg);
363 void parse_options_start(struct parse_opt_ctx_t *ctx,
364 int argc, const char **argv, int flags)
366 memset(ctx, 0, sizeof(*ctx));
367 ctx->argc = argc - 1;
368 ctx->argv = argv + 1;
370 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
372 if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
373 (flags & PARSE_OPT_STOP_AT_NON_OPTION))
374 die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
377 static int usage_with_options_internal(const char * const *,
378 const struct option *, int,
379 struct parse_opt_ctx_t *);
381 int parse_options_step(struct parse_opt_ctx_t *ctx,
382 const struct option *options,
383 const char * const usagestr[])
385 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
386 int excl_short_opt = 1;
389 /* we must reset ->opt, unknown short option leave it dangling */
392 for (; ctx->argc; ctx->argc--, ctx->argv++) {
394 if (*arg != '-' || !arg[1]) {
395 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
397 ctx->out[ctx->cpidx++] = ctx->argv[0];
403 if (internal_help && *ctx->opt == 'h') {
404 return usage_with_options_internal(usagestr, options, 0, ctx);
406 switch (parse_short_opt(ctx, options)) {
408 return parse_options_usage(usagestr, options, arg, 1);
417 check_typos(arg, options);
419 if (internal_help && *ctx->opt == 'h')
420 return usage_with_options_internal(usagestr, options, 0, ctx);
422 switch (parse_short_opt(ctx, options)) {
424 return parse_options_usage(usagestr, options, arg, 1);
426 /* fake a short option thing to hide the fact that we may have
427 * started to parse aggregated stuff
429 * This is leaky, too bad.
431 ctx->argv[0] = strdup(ctx->opt - 1);
432 *(char *)ctx->argv[0] = '-';
443 if (!arg[2]) { /* "--" */
444 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
452 if (internal_help && !strcmp(arg, "help-all"))
453 return usage_with_options_internal(usagestr, options, 1, ctx);
454 if (internal_help && !strcmp(arg, "help"))
455 return usage_with_options_internal(usagestr, options, 0, ctx);
456 if (!strcmp(arg, "list-opts"))
457 return PARSE_OPT_LIST_OPTS;
458 if (!strcmp(arg, "list-cmds"))
459 return PARSE_OPT_LIST_SUBCMDS;
460 switch (parse_long_opt(ctx, arg, options)) {
462 return parse_options_usage(usagestr, options, arg, 0);
473 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
474 return PARSE_OPT_UNKNOWN;
475 ctx->out[ctx->cpidx++] = ctx->argv[0];
478 return PARSE_OPT_DONE;
481 parse_options_usage(usagestr, options, arg, excl_short_opt);
482 if ((excl_short_opt && ctx->excl_opt->short_name) ||
483 ctx->excl_opt->long_name == NULL) {
484 char opt = ctx->excl_opt->short_name;
485 parse_options_usage(NULL, options, &opt, 1);
487 parse_options_usage(NULL, options, ctx->excl_opt->long_name, 0);
489 return PARSE_OPT_HELP;
492 int parse_options_end(struct parse_opt_ctx_t *ctx)
494 memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out));
495 ctx->out[ctx->cpidx + ctx->argc] = NULL;
496 return ctx->cpidx + ctx->argc;
499 int parse_options_subcommand(int argc, const char **argv, const struct option *options,
500 const char *const subcommands[], const char *usagestr[], int flags)
502 struct parse_opt_ctx_t ctx;
504 perf_env__set_cmdline(&perf_env, argc, argv);
506 /* build usage string if it's not provided */
507 if (subcommands && !usagestr[0]) {
508 struct strbuf buf = STRBUF_INIT;
510 strbuf_addf(&buf, "perf %s [<options>] {", argv[0]);
511 for (int i = 0; subcommands[i]; i++) {
513 strbuf_addstr(&buf, "|");
514 strbuf_addstr(&buf, subcommands[i]);
516 strbuf_addstr(&buf, "}");
518 usagestr[0] = strdup(buf.buf);
519 strbuf_release(&buf);
522 parse_options_start(&ctx, argc, argv, flags);
523 switch (parse_options_step(&ctx, options, usagestr)) {
528 case PARSE_OPT_LIST_OPTS:
529 while (options->type != OPTION_END) {
530 if (options->long_name)
531 printf("--%s ", options->long_name);
536 case PARSE_OPT_LIST_SUBCMDS:
538 for (int i = 0; subcommands[i]; i++)
539 printf("%s ", subcommands[i]);
543 default: /* PARSE_OPT_UNKNOWN */
544 if (ctx.argv[0][1] == '-') {
545 strbuf_addf(&error_buf, "unknown option `%s'",
548 strbuf_addf(&error_buf, "unknown switch `%c'",
551 usage_with_options(usagestr, options);
554 return parse_options_end(&ctx);
557 int parse_options(int argc, const char **argv, const struct option *options,
558 const char * const usagestr[], int flags)
560 return parse_options_subcommand(argc, argv, options, NULL,
561 (const char **) usagestr, flags);
564 #define USAGE_OPTS_WIDTH 24
567 static void print_option_help(const struct option *opts, int full)
572 if (opts->type == OPTION_GROUP) {
575 fprintf(stderr, "%s\n", opts->help);
578 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
580 if (opts->flags & PARSE_OPT_DISABLED)
583 pos = fprintf(stderr, " ");
584 if (opts->short_name)
585 pos += fprintf(stderr, "-%c", opts->short_name);
587 pos += fprintf(stderr, " ");
589 if (opts->long_name && opts->short_name)
590 pos += fprintf(stderr, ", ");
592 pos += fprintf(stderr, "--%s", opts->long_name);
594 switch (opts->type) {
595 case OPTION_ARGUMENT:
600 case OPTION_UINTEGER:
601 if (opts->flags & PARSE_OPT_OPTARG)
603 pos += fprintf(stderr, "[=<n>]");
605 pos += fprintf(stderr, "[<n>]");
607 pos += fprintf(stderr, " <n>");
609 case OPTION_CALLBACK:
610 if (opts->flags & PARSE_OPT_NOARG)
615 if (opts->flags & PARSE_OPT_OPTARG)
617 pos += fprintf(stderr, "[=<%s>]", opts->argh);
619 pos += fprintf(stderr, "[<%s>]", opts->argh);
621 pos += fprintf(stderr, " <%s>", opts->argh);
623 if (opts->flags & PARSE_OPT_OPTARG)
625 pos += fprintf(stderr, "[=...]");
627 pos += fprintf(stderr, "[...]");
629 pos += fprintf(stderr, " ...");
632 default: /* OPTION_{BIT,BOOLEAN,SET_UINT,SET_PTR} */
638 case OPTION_SET_UINT:
643 if (pos <= USAGE_OPTS_WIDTH)
644 pad = USAGE_OPTS_WIDTH - pos;
647 pad = USAGE_OPTS_WIDTH;
649 fprintf(stderr, "%*s%s\n", pad + USAGE_GAP, "", opts->help);
652 static int option__cmp(const void *va, const void *vb)
654 const struct option *a = va, *b = vb;
655 int sa = tolower(a->short_name), sb = tolower(b->short_name), ret;
665 const char *la = a->long_name ?: "",
666 *lb = b->long_name ?: "";
667 ret = strcmp(la, lb);
673 static struct option *options__order(const struct option *opts)
676 const struct option *o = opts;
677 struct option *ordered;
679 for (o = opts; o->type != OPTION_END; o++)
682 ordered = memdup(opts, sizeof(*o) * (nr_opts + 1));
686 qsort(ordered, nr_opts, sizeof(*o), option__cmp);
691 static bool option__in_argv(const struct option *opt, const struct parse_opt_ctx_t *ctx)
695 for (i = 1; i < ctx->argc; ++i) {
696 const char *arg = ctx->argv[i];
699 if (arg[1] == '\0') {
700 if (arg[0] == opt->short_name)
705 if (opt->long_name && strcmp(opt->long_name, arg) == 0)
708 if (opt->help && strcasestr(opt->help, arg) != NULL)
714 if (arg[1] == opt->short_name ||
715 (arg[1] == '-' && opt->long_name && strcmp(opt->long_name, arg + 2) == 0))
722 int usage_with_options_internal(const char * const *usagestr,
723 const struct option *opts, int full,
724 struct parse_opt_ctx_t *ctx)
726 struct option *ordered;
729 return PARSE_OPT_HELP;
733 if (strbuf_avail(&error_buf)) {
734 fprintf(stderr, " Error: %s\n", error_buf.buf);
735 strbuf_release(&error_buf);
738 fprintf(stderr, "\n Usage: %s\n", *usagestr++);
739 while (*usagestr && **usagestr)
740 fprintf(stderr, " or: %s\n", *usagestr++);
742 fprintf(stderr, "%s%s\n",
743 **usagestr ? " " : "",
748 if (opts->type != OPTION_GROUP)
751 ordered = options__order(opts);
755 for ( ; opts->type != OPTION_END; opts++) {
756 if (ctx && ctx->argc > 1 && !option__in_argv(opts, ctx))
758 print_option_help(opts, full);
765 return PARSE_OPT_HELP;
768 void usage_with_options(const char * const *usagestr,
769 const struct option *opts)
772 usage_with_options_internal(usagestr, opts, 0, NULL);
776 void usage_with_options_msg(const char * const *usagestr,
777 const struct option *opts, const char *fmt, ...)
784 strbuf_addv(&error_buf, fmt, ap);
787 usage_with_options_internal(usagestr, opts, 0, NULL);
791 int parse_options_usage(const char * const *usagestr,
792 const struct option *opts,
793 const char *optstr, bool short_opt)
798 fprintf(stderr, "\n Usage: %s\n", *usagestr++);
799 while (*usagestr && **usagestr)
800 fprintf(stderr, " or: %s\n", *usagestr++);
802 fprintf(stderr, "%s%s\n",
803 **usagestr ? " " : "",
810 for ( ; opts->type != OPTION_END; opts++) {
812 if (opts->short_name == *optstr) {
813 print_option_help(opts, 0);
819 if (opts->long_name == NULL)
822 if (!prefixcmp(opts->long_name, optstr))
823 print_option_help(opts, 0);
824 if (!prefixcmp("no-", optstr) &&
825 !prefixcmp(opts->long_name, optstr + 3))
826 print_option_help(opts, 0);
829 return PARSE_OPT_HELP;
833 int parse_opt_verbosity_cb(const struct option *opt,
834 const char *arg __maybe_unused,
837 int *target = opt->value;
840 /* --no-quiet, --no-verbose */
842 else if (opt->short_name == 'v') {
856 void set_option_flag(struct option *opts, int shortopt, const char *longopt,
859 for (; opts->type != OPTION_END; opts++) {
860 if ((shortopt && opts->short_name == shortopt) ||
861 (opts->long_name && longopt &&
862 !strcmp(opts->long_name, longopt))) {