2 #include "parse-options.h"
5 #include <linux/string.h>
10 static int opterror(const struct option *opt, const char *reason, int flags)
12 if (flags & OPT_SHORT)
13 return error("switch `%c' %s", opt->short_name, reason);
14 if (flags & OPT_UNSET)
15 return error("option `no-%s' %s", opt->long_name, reason);
16 return error("option `%s' %s", opt->long_name, reason);
19 static int get_arg(struct parse_opt_ctx_t *p, const struct option *opt,
20 int flags, const char **arg)
25 } else if ((opt->flags & PARSE_OPT_LASTARG_DEFAULT) && (p->argc == 1 ||
26 **(p->argv + 1) == '-')) {
27 *arg = (const char *)opt->defval;
28 } else if (p->argc > 1) {
32 return opterror(opt, "requires a value", flags);
36 static int get_value(struct parse_opt_ctx_t *p,
37 const struct option *opt, int flags)
39 const char *s, *arg = NULL;
40 const int unset = flags & OPT_UNSET;
44 return opterror(opt, "takes no value", flags);
45 if (unset && (opt->flags & PARSE_OPT_NONEG))
46 return opterror(opt, "isn't available", flags);
47 if (opt->flags & PARSE_OPT_DISABLED)
48 return opterror(opt, "is not usable", flags);
50 if (opt->flags & PARSE_OPT_EXCLUSIVE) {
51 if (p->excl_opt && p->excl_opt != opt) {
54 if (((flags & OPT_SHORT) && p->excl_opt->short_name) ||
55 p->excl_opt->long_name == NULL) {
56 scnprintf(msg, sizeof(msg), "cannot be used with switch `%c'",
57 p->excl_opt->short_name);
59 scnprintf(msg, sizeof(msg), "cannot be used with %s",
60 p->excl_opt->long_name);
62 opterror(opt, msg, flags);
67 if (!(flags & OPT_SHORT) && p->opt) {
70 if (!(opt->flags & PARSE_OPT_NOARG))
78 return opterror(opt, "takes no value", flags);
95 *(int *)opt->value &= ~opt->defval;
97 *(int *)opt->value |= opt->defval;
101 *(bool *)opt->value = unset ? false : true;
103 *(bool *)opt->set = true;
107 *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
110 case OPTION_SET_UINT:
111 *(unsigned int *)opt->value = unset ? 0 : opt->defval;
115 *(void **)opt->value = unset ? NULL : (void *)opt->defval;
121 *(const char **)opt->value = NULL;
122 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
123 *(const char **)opt->value = (const char *)opt->defval;
125 err = get_arg(p, opt, flags, (const char **)opt->value);
127 /* PARSE_OPT_NOEMPTY: Allow NULL but disallow empty string. */
128 if (opt->flags & PARSE_OPT_NOEMPTY) {
129 const char *val = *(const char **)opt->value;
134 /* Similar to unset if we are given an empty string. */
135 if (val[0] == '\0') {
136 *(const char **)opt->value = NULL;
143 case OPTION_CALLBACK:
145 return (*opt->callback)(opt, NULL, 1) ? (-1) : 0;
146 if (opt->flags & PARSE_OPT_NOARG)
147 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
148 if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
149 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
150 if (get_arg(p, opt, flags, &arg))
152 return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
156 *(int *)opt->value = 0;
159 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
160 *(int *)opt->value = opt->defval;
163 if (get_arg(p, opt, flags, &arg))
165 *(int *)opt->value = strtol(arg, (char **)&s, 10);
167 return opterror(opt, "expects a numerical value", flags);
170 case OPTION_UINTEGER:
172 *(unsigned int *)opt->value = 0;
175 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
176 *(unsigned int *)opt->value = opt->defval;
179 if (get_arg(p, opt, flags, &arg))
181 *(unsigned int *)opt->value = strtol(arg, (char **)&s, 10);
183 return opterror(opt, "expects a numerical value", flags);
188 *(long *)opt->value = 0;
191 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
192 *(long *)opt->value = opt->defval;
195 if (get_arg(p, opt, flags, &arg))
197 *(long *)opt->value = strtol(arg, (char **)&s, 10);
199 return opterror(opt, "expects a numerical value", flags);
204 *(u64 *)opt->value = 0;
207 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
208 *(u64 *)opt->value = opt->defval;
211 if (get_arg(p, opt, flags, &arg))
213 *(u64 *)opt->value = strtoull(arg, (char **)&s, 10);
215 return opterror(opt, "expects a numerical value", flags);
219 case OPTION_ARGUMENT:
222 die("should not happen, someone must be hit on the forehead");
226 static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)
228 for (; options->type != OPTION_END; options++) {
229 if (options->short_name == *p->opt) {
230 p->opt = p->opt[1] ? p->opt + 1 : NULL;
231 return get_value(p, options, OPT_SHORT);
237 static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
238 const struct option *options)
240 const char *arg_end = strchr(arg, '=');
241 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
242 int abbrev_flags = 0, ambiguous_flags = 0;
245 arg_end = arg + strlen(arg);
247 for (; options->type != OPTION_END; options++) {
251 if (!options->long_name)
254 rest = skip_prefix(arg, options->long_name);
255 if (options->type == OPTION_ARGUMENT) {
259 return opterror(options, "takes no value", flags);
262 p->out[p->cpidx++] = arg - 2;
266 if (!prefixcmp(options->long_name, "no-")) {
268 * The long name itself starts with "no-", so
269 * accept the option without "no-" so that users
270 * do not have to enter "no-no-" to get the
273 rest = skip_prefix(arg, options->long_name + 3);
278 /* Abbreviated case */
279 if (!prefixcmp(options->long_name + 3, arg)) {
285 if (!strncmp(options->long_name, arg, arg_end - arg)) {
289 * If this is abbreviated, it is
290 * ambiguous. So when there is no
291 * exact match later, we need to
294 ambiguous_option = abbrev_option;
295 ambiguous_flags = abbrev_flags;
297 if (!(flags & OPT_UNSET) && *arg_end)
298 p->opt = arg_end + 1;
299 abbrev_option = options;
300 abbrev_flags = flags;
303 /* negated and abbreviated very much? */
304 if (!prefixcmp("no-", arg)) {
309 if (strncmp(arg, "no-", 3))
312 rest = skip_prefix(arg + 3, options->long_name);
313 /* abbreviated and negated? */
314 if (!rest && !prefixcmp(options->long_name, arg + 3))
325 return get_value(p, options, flags);
328 if (ambiguous_option)
329 return error("Ambiguous option: %s "
330 "(could be --%s%s or --%s%s)",
332 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
333 ambiguous_option->long_name,
334 (abbrev_flags & OPT_UNSET) ? "no-" : "",
335 abbrev_option->long_name);
337 return get_value(p, abbrev_option, abbrev_flags);
341 static void check_typos(const char *arg, const struct option *options)
346 if (!prefixcmp(arg, "no-")) {
347 error ("did you mean `--%s` (with two dashes ?)", arg);
351 for (; options->type != OPTION_END; options++) {
352 if (!options->long_name)
354 if (!prefixcmp(options->long_name, arg)) {
355 error ("did you mean `--%s` (with two dashes ?)", arg);
361 void parse_options_start(struct parse_opt_ctx_t *ctx,
362 int argc, const char **argv, int flags)
364 memset(ctx, 0, sizeof(*ctx));
365 ctx->argc = argc - 1;
366 ctx->argv = argv + 1;
368 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
370 if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
371 (flags & PARSE_OPT_STOP_AT_NON_OPTION))
372 die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
375 static int usage_with_options_internal(const char * const *,
376 const struct option *, int);
378 int parse_options_step(struct parse_opt_ctx_t *ctx,
379 const struct option *options,
380 const char * const usagestr[])
382 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
383 int excl_short_opt = 1;
386 /* we must reset ->opt, unknown short option leave it dangling */
389 for (; ctx->argc; ctx->argc--, ctx->argv++) {
391 if (*arg != '-' || !arg[1]) {
392 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
394 ctx->out[ctx->cpidx++] = ctx->argv[0];
400 if (internal_help && *ctx->opt == 'h')
401 return usage_with_options_internal(usagestr, options, 0);
402 switch (parse_short_opt(ctx, options)) {
404 return parse_options_usage(usagestr, options, arg, 1);
413 check_typos(arg, options);
415 if (internal_help && *ctx->opt == 'h')
416 return usage_with_options_internal(usagestr, options, 0);
418 switch (parse_short_opt(ctx, options)) {
420 return parse_options_usage(usagestr, options, arg, 1);
422 /* fake a short option thing to hide the fact that we may have
423 * started to parse aggregated stuff
425 * This is leaky, too bad.
427 ctx->argv[0] = strdup(ctx->opt - 1);
428 *(char *)ctx->argv[0] = '-';
439 if (!arg[2]) { /* "--" */
440 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
448 if (internal_help && !strcmp(arg, "help-all"))
449 return usage_with_options_internal(usagestr, options, 1);
450 if (internal_help && !strcmp(arg, "help"))
451 return usage_with_options_internal(usagestr, options, 0);
452 if (!strcmp(arg, "list-opts"))
453 return PARSE_OPT_LIST_OPTS;
454 if (!strcmp(arg, "list-cmds"))
455 return PARSE_OPT_LIST_SUBCMDS;
456 switch (parse_long_opt(ctx, arg, options)) {
458 return parse_options_usage(usagestr, options, arg, 0);
469 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
470 return PARSE_OPT_UNKNOWN;
471 ctx->out[ctx->cpidx++] = ctx->argv[0];
474 return PARSE_OPT_DONE;
477 parse_options_usage(usagestr, options, arg, excl_short_opt);
478 if ((excl_short_opt && ctx->excl_opt->short_name) ||
479 ctx->excl_opt->long_name == NULL) {
480 char opt = ctx->excl_opt->short_name;
481 parse_options_usage(NULL, options, &opt, 1);
483 parse_options_usage(NULL, options, ctx->excl_opt->long_name, 0);
485 return PARSE_OPT_HELP;
488 int parse_options_end(struct parse_opt_ctx_t *ctx)
490 memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out));
491 ctx->out[ctx->cpidx + ctx->argc] = NULL;
492 return ctx->cpidx + ctx->argc;
495 int parse_options_subcommand(int argc, const char **argv, const struct option *options,
496 const char *const subcommands[], const char *usagestr[], int flags)
498 struct parse_opt_ctx_t ctx;
500 perf_env__set_cmdline(&perf_env, argc, argv);
502 /* build usage string if it's not provided */
503 if (subcommands && !usagestr[0]) {
504 struct strbuf buf = STRBUF_INIT;
506 strbuf_addf(&buf, "perf %s [<options>] {", argv[0]);
507 for (int i = 0; subcommands[i]; i++) {
509 strbuf_addstr(&buf, "|");
510 strbuf_addstr(&buf, subcommands[i]);
512 strbuf_addstr(&buf, "}");
514 usagestr[0] = strdup(buf.buf);
515 strbuf_release(&buf);
518 parse_options_start(&ctx, argc, argv, flags);
519 switch (parse_options_step(&ctx, options, usagestr)) {
524 case PARSE_OPT_LIST_OPTS:
525 while (options->type != OPTION_END) {
526 if (options->long_name)
527 printf("--%s ", options->long_name);
532 case PARSE_OPT_LIST_SUBCMDS:
534 for (int i = 0; subcommands[i]; i++)
535 printf("%s ", subcommands[i]);
539 default: /* PARSE_OPT_UNKNOWN */
540 if (ctx.argv[0][1] == '-') {
541 error("unknown option `%s'", ctx.argv[0] + 2);
543 error("unknown switch `%c'", *ctx.opt);
545 usage_with_options(usagestr, options);
548 return parse_options_end(&ctx);
551 int parse_options(int argc, const char **argv, const struct option *options,
552 const char * const usagestr[], int flags)
554 return parse_options_subcommand(argc, argv, options, NULL,
555 (const char **) usagestr, flags);
558 #define USAGE_OPTS_WIDTH 24
561 static void print_option_help(const struct option *opts, int full)
566 if (opts->type == OPTION_GROUP) {
569 fprintf(stderr, "%s\n", opts->help);
572 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
574 if (opts->flags & PARSE_OPT_DISABLED)
577 pos = fprintf(stderr, " ");
578 if (opts->short_name)
579 pos += fprintf(stderr, "-%c", opts->short_name);
581 pos += fprintf(stderr, " ");
583 if (opts->long_name && opts->short_name)
584 pos += fprintf(stderr, ", ");
586 pos += fprintf(stderr, "--%s", opts->long_name);
588 switch (opts->type) {
589 case OPTION_ARGUMENT:
594 case OPTION_UINTEGER:
595 if (opts->flags & PARSE_OPT_OPTARG)
597 pos += fprintf(stderr, "[=<n>]");
599 pos += fprintf(stderr, "[<n>]");
601 pos += fprintf(stderr, " <n>");
603 case OPTION_CALLBACK:
604 if (opts->flags & PARSE_OPT_NOARG)
609 if (opts->flags & PARSE_OPT_OPTARG)
611 pos += fprintf(stderr, "[=<%s>]", opts->argh);
613 pos += fprintf(stderr, "[<%s>]", opts->argh);
615 pos += fprintf(stderr, " <%s>", opts->argh);
617 if (opts->flags & PARSE_OPT_OPTARG)
619 pos += fprintf(stderr, "[=...]");
621 pos += fprintf(stderr, "[...]");
623 pos += fprintf(stderr, " ...");
626 default: /* OPTION_{BIT,BOOLEAN,SET_UINT,SET_PTR} */
632 case OPTION_SET_UINT:
637 if (pos <= USAGE_OPTS_WIDTH)
638 pad = USAGE_OPTS_WIDTH - pos;
641 pad = USAGE_OPTS_WIDTH;
643 fprintf(stderr, "%*s%s\n", pad + USAGE_GAP, "", opts->help);
646 static int option__cmp(const void *va, const void *vb)
648 const struct option *a = va, *b = vb;
649 int sa = tolower(a->short_name), sb = tolower(b->short_name), ret;
659 const char *la = a->long_name ?: "",
660 *lb = b->long_name ?: "";
661 ret = strcmp(la, lb);
667 static struct option *options__order(const struct option *opts)
670 const struct option *o = opts;
671 struct option *ordered;
673 for (o = opts; o->type != OPTION_END; o++)
676 ordered = memdup(opts, sizeof(*o) * (nr_opts + 1));
680 qsort(ordered, nr_opts, sizeof(*o), option__cmp);
685 int usage_with_options_internal(const char * const *usagestr,
686 const struct option *opts, int full)
688 struct option *ordered;
691 return PARSE_OPT_HELP;
693 fprintf(stderr, "\n Usage: %s\n", *usagestr++);
694 while (*usagestr && **usagestr)
695 fprintf(stderr, " or: %s\n", *usagestr++);
697 fprintf(stderr, "%s%s\n",
698 **usagestr ? " " : "",
703 if (opts->type != OPTION_GROUP)
706 ordered = options__order(opts);
710 for ( ; opts->type != OPTION_END; opts++)
711 print_option_help(opts, full);
717 return PARSE_OPT_HELP;
720 void usage_with_options(const char * const *usagestr,
721 const struct option *opts)
724 usage_with_options_internal(usagestr, opts, 0);
728 int parse_options_usage(const char * const *usagestr,
729 const struct option *opts,
730 const char *optstr, bool short_opt)
735 fprintf(stderr, "\n Usage: %s\n", *usagestr++);
736 while (*usagestr && **usagestr)
737 fprintf(stderr, " or: %s\n", *usagestr++);
739 fprintf(stderr, "%s%s\n",
740 **usagestr ? " " : "",
747 for ( ; opts->type != OPTION_END; opts++) {
749 if (opts->short_name == *optstr)
754 if (opts->long_name == NULL)
757 if (!prefixcmp(optstr, opts->long_name))
759 if (!prefixcmp(optstr, "no-") &&
760 !prefixcmp(optstr + 3, opts->long_name))
764 if (opts->type != OPTION_END)
765 print_option_help(opts, 0);
767 return PARSE_OPT_HELP;
771 int parse_opt_verbosity_cb(const struct option *opt,
772 const char *arg __maybe_unused,
775 int *target = opt->value;
778 /* --no-quiet, --no-verbose */
780 else if (opt->short_name == 'v') {
794 void set_option_flag(struct option *opts, int shortopt, const char *longopt,
797 for (; opts->type != OPTION_END; opts++) {
798 if ((shortopt && opts->short_name == shortopt) ||
799 (opts->long_name && longopt &&
800 !strcmp(opts->long_name, longopt))) {