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;
411 switch (parse_long_opt(ctx, arg + 2, options)) {
413 return parse_options_usage(usagestr, options, arg + 2, 0);
421 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
422 return PARSE_OPT_UNKNOWN;
423 ctx->out[ctx->cpidx++] = ctx->argv[0];
426 return PARSE_OPT_DONE;
429 int parse_options_end(struct parse_opt_ctx_t *ctx)
431 memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out));
432 ctx->out[ctx->cpidx + ctx->argc] = NULL;
433 return ctx->cpidx + ctx->argc;
436 int parse_options(int argc, const char **argv, const struct option *options,
437 const char * const usagestr[], int flags)
439 struct parse_opt_ctx_t ctx;
441 perf_header__set_cmdline(argc, argv);
443 parse_options_start(&ctx, argc, argv, flags);
444 switch (parse_options_step(&ctx, options, usagestr)) {
450 while (options->type != OPTION_END) {
451 printf("--%s ", options->long_name);
455 default: /* PARSE_OPT_UNKNOWN */
456 if (ctx.argv[0][1] == '-') {
457 error("unknown option `%s'", ctx.argv[0] + 2);
459 error("unknown switch `%c'", *ctx.opt);
461 usage_with_options(usagestr, options);
464 return parse_options_end(&ctx);
467 #define USAGE_OPTS_WIDTH 24
470 static void print_option_help(const struct option *opts, int full)
475 if (opts->type == OPTION_GROUP) {
478 fprintf(stderr, "%s\n", opts->help);
481 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
484 pos = fprintf(stderr, " ");
485 if (opts->short_name)
486 pos += fprintf(stderr, "-%c", opts->short_name);
488 pos += fprintf(stderr, " ");
490 if (opts->long_name && opts->short_name)
491 pos += fprintf(stderr, ", ");
493 pos += fprintf(stderr, "--%s", opts->long_name);
495 switch (opts->type) {
496 case OPTION_ARGUMENT:
501 case OPTION_UINTEGER:
502 if (opts->flags & PARSE_OPT_OPTARG)
504 pos += fprintf(stderr, "[=<n>]");
506 pos += fprintf(stderr, "[<n>]");
508 pos += fprintf(stderr, " <n>");
510 case OPTION_CALLBACK:
511 if (opts->flags & PARSE_OPT_NOARG)
516 if (opts->flags & PARSE_OPT_OPTARG)
518 pos += fprintf(stderr, "[=<%s>]", opts->argh);
520 pos += fprintf(stderr, "[<%s>]", opts->argh);
522 pos += fprintf(stderr, " <%s>", opts->argh);
524 if (opts->flags & PARSE_OPT_OPTARG)
526 pos += fprintf(stderr, "[=...]");
528 pos += fprintf(stderr, "[...]");
530 pos += fprintf(stderr, " ...");
533 default: /* OPTION_{BIT,BOOLEAN,SET_UINT,SET_PTR} */
539 case OPTION_SET_UINT:
544 if (pos <= USAGE_OPTS_WIDTH)
545 pad = USAGE_OPTS_WIDTH - pos;
548 pad = USAGE_OPTS_WIDTH;
550 fprintf(stderr, "%*s%s\n", pad + USAGE_GAP, "", opts->help);
553 int usage_with_options_internal(const char * const *usagestr,
554 const struct option *opts, int full)
557 return PARSE_OPT_HELP;
559 fprintf(stderr, "\n usage: %s\n", *usagestr++);
560 while (*usagestr && **usagestr)
561 fprintf(stderr, " or: %s\n", *usagestr++);
563 fprintf(stderr, "%s%s\n",
564 **usagestr ? " " : "",
569 if (opts->type != OPTION_GROUP)
572 for ( ; opts->type != OPTION_END; opts++)
573 print_option_help(opts, full);
577 return PARSE_OPT_HELP;
580 void usage_with_options(const char * const *usagestr,
581 const struct option *opts)
584 usage_with_options_internal(usagestr, opts, 0);
588 int parse_options_usage(const char * const *usagestr,
589 const struct option *opts,
590 const char *optstr, bool short_opt)
595 fprintf(stderr, "\n usage: %s\n", *usagestr++);
596 while (*usagestr && **usagestr)
597 fprintf(stderr, " or: %s\n", *usagestr++);
599 fprintf(stderr, "%s%s\n",
600 **usagestr ? " " : "",
607 for ( ; opts->type != OPTION_END; opts++) {
609 if (opts->short_name == *optstr)
614 if (opts->long_name == NULL)
617 if (!prefixcmp(optstr, opts->long_name))
619 if (!prefixcmp(optstr, "no-") &&
620 !prefixcmp(optstr + 3, opts->long_name))
624 if (opts->type != OPTION_END)
625 print_option_help(opts, 0);
627 return PARSE_OPT_HELP;
631 int parse_opt_verbosity_cb(const struct option *opt,
632 const char *arg __maybe_unused,
635 int *target = opt->value;
638 /* --no-quiet, --no-verbose */
640 else if (opt->short_name == 'v') {