2 #include "parse-options.h"
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)
24 } else if ((opt->flags & PARSE_OPT_LASTARG_DEFAULT) && (p->argc == 1 ||
25 **(p->argv + 1) == '-')) {
26 *arg = (const char *)opt->defval;
27 } else if (p->argc > 1) {
31 return opterror(opt, "requires a value", flags);
35 static int get_value(struct parse_opt_ctx_t *p,
36 const struct option *opt, int flags)
38 const char *s, *arg = NULL;
39 const int unset = flags & OPT_UNSET;
42 return opterror(opt, "takes no value", flags);
43 if (unset && (opt->flags & PARSE_OPT_NONEG))
44 return opterror(opt, "isn't available", flags);
46 if (!(flags & OPT_SHORT) && p->opt) {
49 if (!(opt->flags & PARSE_OPT_NOARG))
57 return opterror(opt, "takes no value", flags);
74 *(int *)opt->value &= ~opt->defval;
76 *(int *)opt->value |= opt->defval;
80 *(bool *)opt->value = unset ? false : true;
82 *(bool *)opt->set = true;
86 *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
90 *(unsigned int *)opt->value = unset ? 0 : opt->defval;
94 *(void **)opt->value = unset ? NULL : (void *)opt->defval;
99 *(const char **)opt->value = NULL;
100 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
101 *(const char **)opt->value = (const char *)opt->defval;
103 return get_arg(p, opt, flags, (const char **)opt->value);
106 case OPTION_CALLBACK:
108 return (*opt->callback)(opt, NULL, 1) ? (-1) : 0;
109 if (opt->flags & PARSE_OPT_NOARG)
110 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
111 if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
112 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
113 if (get_arg(p, opt, flags, &arg))
115 return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
119 *(int *)opt->value = 0;
122 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
123 *(int *)opt->value = opt->defval;
126 if (get_arg(p, opt, flags, &arg))
128 *(int *)opt->value = strtol(arg, (char **)&s, 10);
130 return opterror(opt, "expects a numerical value", flags);
133 case OPTION_UINTEGER:
135 *(unsigned int *)opt->value = 0;
138 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
139 *(unsigned int *)opt->value = opt->defval;
142 if (get_arg(p, opt, flags, &arg))
144 *(unsigned int *)opt->value = strtol(arg, (char **)&s, 10);
146 return opterror(opt, "expects a numerical value", flags);
151 *(long *)opt->value = 0;
154 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
155 *(long *)opt->value = opt->defval;
158 if (get_arg(p, opt, flags, &arg))
160 *(long *)opt->value = strtol(arg, (char **)&s, 10);
162 return opterror(opt, "expects a numerical value", flags);
167 *(u64 *)opt->value = 0;
170 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
171 *(u64 *)opt->value = opt->defval;
174 if (get_arg(p, opt, flags, &arg))
176 *(u64 *)opt->value = strtoull(arg, (char **)&s, 10);
178 return opterror(opt, "expects a numerical value", flags);
182 case OPTION_ARGUMENT:
185 die("should not happen, someone must be hit on the forehead");
189 static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)
191 for (; options->type != OPTION_END; options++) {
192 if (options->short_name == *p->opt) {
193 p->opt = p->opt[1] ? p->opt + 1 : NULL;
194 return get_value(p, options, OPT_SHORT);
200 static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
201 const struct option *options)
203 const char *arg_end = strchr(arg, '=');
204 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
205 int abbrev_flags = 0, ambiguous_flags = 0;
208 arg_end = arg + strlen(arg);
210 for (; options->type != OPTION_END; options++) {
214 if (!options->long_name)
217 rest = skip_prefix(arg, options->long_name);
218 if (options->type == OPTION_ARGUMENT) {
222 return opterror(options, "takes no value", flags);
225 p->out[p->cpidx++] = arg - 2;
229 if (!prefixcmp(options->long_name, "no-")) {
231 * The long name itself starts with "no-", so
232 * accept the option without "no-" so that users
233 * do not have to enter "no-no-" to get the
236 rest = skip_prefix(arg, options->long_name + 3);
241 /* Abbreviated case */
242 if (!prefixcmp(options->long_name + 3, arg)) {
248 if (!strncmp(options->long_name, arg, arg_end - arg)) {
252 * If this is abbreviated, it is
253 * ambiguous. So when there is no
254 * exact match later, we need to
257 ambiguous_option = abbrev_option;
258 ambiguous_flags = abbrev_flags;
260 if (!(flags & OPT_UNSET) && *arg_end)
261 p->opt = arg_end + 1;
262 abbrev_option = options;
263 abbrev_flags = flags;
266 /* negated and abbreviated very much? */
267 if (!prefixcmp("no-", arg)) {
272 if (strncmp(arg, "no-", 3))
275 rest = skip_prefix(arg + 3, options->long_name);
276 /* abbreviated and negated? */
277 if (!rest && !prefixcmp(options->long_name, arg + 3))
288 return get_value(p, options, flags);
291 if (ambiguous_option)
292 return error("Ambiguous option: %s "
293 "(could be --%s%s or --%s%s)",
295 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
296 ambiguous_option->long_name,
297 (abbrev_flags & OPT_UNSET) ? "no-" : "",
298 abbrev_option->long_name);
300 return get_value(p, abbrev_option, abbrev_flags);
304 static void check_typos(const char *arg, const struct option *options)
309 if (!prefixcmp(arg, "no-")) {
310 error ("did you mean `--%s` (with two dashes ?)", arg);
314 for (; options->type != OPTION_END; options++) {
315 if (!options->long_name)
317 if (!prefixcmp(options->long_name, arg)) {
318 error ("did you mean `--%s` (with two dashes ?)", arg);
324 void parse_options_start(struct parse_opt_ctx_t *ctx,
325 int argc, const char **argv, int flags)
327 memset(ctx, 0, sizeof(*ctx));
328 ctx->argc = argc - 1;
329 ctx->argv = argv + 1;
331 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
333 if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
334 (flags & PARSE_OPT_STOP_AT_NON_OPTION))
335 die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
338 static int usage_with_options_internal(const char * const *,
339 const struct option *, int);
341 int parse_options_step(struct parse_opt_ctx_t *ctx,
342 const struct option *options,
343 const char * const usagestr[])
345 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
347 /* we must reset ->opt, unknown short option leave it dangling */
350 for (; ctx->argc; ctx->argc--, ctx->argv++) {
351 const char *arg = ctx->argv[0];
353 if (*arg != '-' || !arg[1]) {
354 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
356 ctx->out[ctx->cpidx++] = ctx->argv[0];
362 if (internal_help && *ctx->opt == 'h')
363 return usage_with_options_internal(usagestr, options, 0);
364 switch (parse_short_opt(ctx, options)) {
366 return parse_options_usage(usagestr, options, arg + 1, 1);
373 check_typos(arg + 1, options);
375 if (internal_help && *ctx->opt == 'h')
376 return usage_with_options_internal(usagestr, options, 0);
378 switch (parse_short_opt(ctx, options)) {
380 return parse_options_usage(usagestr, options, arg, 1);
382 /* fake a short option thing to hide the fact that we may have
383 * started to parse aggregated stuff
385 * This is leaky, too bad.
387 ctx->argv[0] = strdup(ctx->opt - 1);
388 *(char *)ctx->argv[0] = '-';
397 if (!arg[2]) { /* "--" */
398 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
405 if (internal_help && !strcmp(arg + 2, "help-all"))
406 return usage_with_options_internal(usagestr, options, 1);
407 if (internal_help && !strcmp(arg + 2, "help"))
408 return usage_with_options_internal(usagestr, options, 0);
409 if (!strcmp(arg + 2, "list-opts"))
410 return PARSE_OPT_LIST_OPTS;
411 if (!strcmp(arg + 2, "list-cmds"))
412 return PARSE_OPT_LIST_SUBCMDS;
413 switch (parse_long_opt(ctx, arg + 2, options)) {
415 return parse_options_usage(usagestr, options, arg + 2, 0);
423 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
424 return PARSE_OPT_UNKNOWN;
425 ctx->out[ctx->cpidx++] = ctx->argv[0];
428 return PARSE_OPT_DONE;
431 int parse_options_end(struct parse_opt_ctx_t *ctx)
433 memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out));
434 ctx->out[ctx->cpidx + ctx->argc] = NULL;
435 return ctx->cpidx + ctx->argc;
438 int parse_options_subcommand(int argc, const char **argv, const struct option *options,
439 const char *const subcommands[], const char *usagestr[], int flags)
441 struct parse_opt_ctx_t ctx;
443 perf_header__set_cmdline(argc, argv);
445 /* build usage string if it's not provided */
446 if (subcommands && !usagestr[0]) {
447 struct strbuf buf = STRBUF_INIT;
449 strbuf_addf(&buf, "perf %s [<options>] {", argv[0]);
450 for (int i = 0; subcommands[i]; i++) {
452 strbuf_addstr(&buf, "|");
453 strbuf_addstr(&buf, subcommands[i]);
455 strbuf_addstr(&buf, "}");
457 usagestr[0] = strdup(buf.buf);
458 strbuf_release(&buf);
461 parse_options_start(&ctx, argc, argv, flags);
462 switch (parse_options_step(&ctx, options, usagestr)) {
467 case PARSE_OPT_LIST_OPTS:
468 while (options->type != OPTION_END) {
469 printf("--%s ", options->long_name);
473 case PARSE_OPT_LIST_SUBCMDS:
474 for (int i = 0; subcommands[i]; i++)
475 printf("%s ", subcommands[i]);
477 default: /* PARSE_OPT_UNKNOWN */
478 if (ctx.argv[0][1] == '-') {
479 error("unknown option `%s'", ctx.argv[0] + 2);
481 error("unknown switch `%c'", *ctx.opt);
483 usage_with_options(usagestr, options);
486 return parse_options_end(&ctx);
489 int parse_options(int argc, const char **argv, const struct option *options,
490 const char * const usagestr[], int flags)
492 return parse_options_subcommand(argc, argv, options, NULL,
493 (const char **) usagestr, flags);
496 #define USAGE_OPTS_WIDTH 24
499 static void print_option_help(const struct option *opts, int full)
504 if (opts->type == OPTION_GROUP) {
507 fprintf(stderr, "%s\n", opts->help);
510 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
513 pos = fprintf(stderr, " ");
514 if (opts->short_name)
515 pos += fprintf(stderr, "-%c", opts->short_name);
517 pos += fprintf(stderr, " ");
519 if (opts->long_name && opts->short_name)
520 pos += fprintf(stderr, ", ");
522 pos += fprintf(stderr, "--%s", opts->long_name);
524 switch (opts->type) {
525 case OPTION_ARGUMENT:
530 case OPTION_UINTEGER:
531 if (opts->flags & PARSE_OPT_OPTARG)
533 pos += fprintf(stderr, "[=<n>]");
535 pos += fprintf(stderr, "[<n>]");
537 pos += fprintf(stderr, " <n>");
539 case OPTION_CALLBACK:
540 if (opts->flags & PARSE_OPT_NOARG)
545 if (opts->flags & PARSE_OPT_OPTARG)
547 pos += fprintf(stderr, "[=<%s>]", opts->argh);
549 pos += fprintf(stderr, "[<%s>]", opts->argh);
551 pos += fprintf(stderr, " <%s>", opts->argh);
553 if (opts->flags & PARSE_OPT_OPTARG)
555 pos += fprintf(stderr, "[=...]");
557 pos += fprintf(stderr, "[...]");
559 pos += fprintf(stderr, " ...");
562 default: /* OPTION_{BIT,BOOLEAN,SET_UINT,SET_PTR} */
568 case OPTION_SET_UINT:
573 if (pos <= USAGE_OPTS_WIDTH)
574 pad = USAGE_OPTS_WIDTH - pos;
577 pad = USAGE_OPTS_WIDTH;
579 fprintf(stderr, "%*s%s\n", pad + USAGE_GAP, "", opts->help);
582 int usage_with_options_internal(const char * const *usagestr,
583 const struct option *opts, int full)
586 return PARSE_OPT_HELP;
588 fprintf(stderr, "\n usage: %s\n", *usagestr++);
589 while (*usagestr && **usagestr)
590 fprintf(stderr, " or: %s\n", *usagestr++);
592 fprintf(stderr, "%s%s\n",
593 **usagestr ? " " : "",
598 if (opts->type != OPTION_GROUP)
601 for ( ; opts->type != OPTION_END; opts++)
602 print_option_help(opts, full);
606 return PARSE_OPT_HELP;
609 void usage_with_options(const char * const *usagestr,
610 const struct option *opts)
613 usage_with_options_internal(usagestr, opts, 0);
617 int parse_options_usage(const char * const *usagestr,
618 const struct option *opts,
619 const char *optstr, bool short_opt)
624 fprintf(stderr, "\n usage: %s\n", *usagestr++);
625 while (*usagestr && **usagestr)
626 fprintf(stderr, " or: %s\n", *usagestr++);
628 fprintf(stderr, "%s%s\n",
629 **usagestr ? " " : "",
636 for ( ; opts->type != OPTION_END; opts++) {
638 if (opts->short_name == *optstr)
643 if (opts->long_name == NULL)
646 if (!prefixcmp(optstr, opts->long_name))
648 if (!prefixcmp(optstr, "no-") &&
649 !prefixcmp(optstr + 3, opts->long_name))
653 if (opts->type != OPTION_END)
654 print_option_help(opts, 0);
656 return PARSE_OPT_HELP;
660 int parse_opt_verbosity_cb(const struct option *opt,
661 const char *arg __maybe_unused,
664 int *target = opt->value;
667 /* --no-quiet, --no-verbose */
669 else if (opt->short_name == 'v') {