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;
84 *(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
88 *(unsigned int *)opt->value = unset ? 0 : opt->defval;
92 *(void **)opt->value = unset ? NULL : (void *)opt->defval;
97 *(const char **)opt->value = NULL;
98 else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
99 *(const char **)opt->value = (const char *)opt->defval;
101 return get_arg(p, opt, flags, (const char **)opt->value);
104 case OPTION_CALLBACK:
106 return (*opt->callback)(opt, NULL, 1) ? (-1) : 0;
107 if (opt->flags & PARSE_OPT_NOARG)
108 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
109 if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
110 return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
111 if (get_arg(p, opt, flags, &arg))
113 return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
117 *(int *)opt->value = 0;
120 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
121 *(int *)opt->value = opt->defval;
124 if (get_arg(p, opt, flags, &arg))
126 *(int *)opt->value = strtol(arg, (char **)&s, 10);
128 return opterror(opt, "expects a numerical value", flags);
131 case OPTION_UINTEGER:
133 *(unsigned int *)opt->value = 0;
136 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
137 *(unsigned int *)opt->value = opt->defval;
140 if (get_arg(p, opt, flags, &arg))
142 *(unsigned int *)opt->value = strtol(arg, (char **)&s, 10);
144 return opterror(opt, "expects a numerical value", flags);
149 *(long *)opt->value = 0;
152 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
153 *(long *)opt->value = opt->defval;
156 if (get_arg(p, opt, flags, &arg))
158 *(long *)opt->value = strtol(arg, (char **)&s, 10);
160 return opterror(opt, "expects a numerical value", flags);
165 *(u64 *)opt->value = 0;
168 if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
169 *(u64 *)opt->value = opt->defval;
172 if (get_arg(p, opt, flags, &arg))
174 *(u64 *)opt->value = strtoull(arg, (char **)&s, 10);
176 return opterror(opt, "expects a numerical value", flags);
180 case OPTION_ARGUMENT:
183 die("should not happen, someone must be hit on the forehead");
187 static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)
189 for (; options->type != OPTION_END; options++) {
190 if (options->short_name == *p->opt) {
191 p->opt = p->opt[1] ? p->opt + 1 : NULL;
192 return get_value(p, options, OPT_SHORT);
198 static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
199 const struct option *options)
201 const char *arg_end = strchr(arg, '=');
202 const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
203 int abbrev_flags = 0, ambiguous_flags = 0;
206 arg_end = arg + strlen(arg);
208 for (; options->type != OPTION_END; options++) {
212 if (!options->long_name)
215 rest = skip_prefix(arg, options->long_name);
216 if (options->type == OPTION_ARGUMENT) {
220 return opterror(options, "takes no value", flags);
223 p->out[p->cpidx++] = arg - 2;
228 if (!strncmp(options->long_name, arg, arg_end - arg)) {
232 * If this is abbreviated, it is
233 * ambiguous. So when there is no
234 * exact match later, we need to
237 ambiguous_option = abbrev_option;
238 ambiguous_flags = abbrev_flags;
240 if (!(flags & OPT_UNSET) && *arg_end)
241 p->opt = arg_end + 1;
242 abbrev_option = options;
243 abbrev_flags = flags;
246 /* negated and abbreviated very much? */
247 if (!prefixcmp("no-", arg)) {
252 if (strncmp(arg, "no-", 3))
255 rest = skip_prefix(arg + 3, options->long_name);
256 /* abbreviated and negated? */
257 if (!rest && !prefixcmp(options->long_name, arg + 3))
267 return get_value(p, options, flags);
270 if (ambiguous_option)
271 return error("Ambiguous option: %s "
272 "(could be --%s%s or --%s%s)",
274 (ambiguous_flags & OPT_UNSET) ? "no-" : "",
275 ambiguous_option->long_name,
276 (abbrev_flags & OPT_UNSET) ? "no-" : "",
277 abbrev_option->long_name);
279 return get_value(p, abbrev_option, abbrev_flags);
283 static void check_typos(const char *arg, const struct option *options)
288 if (!prefixcmp(arg, "no-")) {
289 error ("did you mean `--%s` (with two dashes ?)", arg);
293 for (; options->type != OPTION_END; options++) {
294 if (!options->long_name)
296 if (!prefixcmp(options->long_name, arg)) {
297 error ("did you mean `--%s` (with two dashes ?)", arg);
303 void parse_options_start(struct parse_opt_ctx_t *ctx,
304 int argc, const char **argv, int flags)
306 memset(ctx, 0, sizeof(*ctx));
307 ctx->argc = argc - 1;
308 ctx->argv = argv + 1;
310 ctx->cpidx = ((flags & PARSE_OPT_KEEP_ARGV0) != 0);
312 if ((flags & PARSE_OPT_KEEP_UNKNOWN) &&
313 (flags & PARSE_OPT_STOP_AT_NON_OPTION))
314 die("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together");
317 static int usage_with_options_internal(const char * const *,
318 const struct option *, int);
320 int parse_options_step(struct parse_opt_ctx_t *ctx,
321 const struct option *options,
322 const char * const usagestr[])
324 int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
326 /* we must reset ->opt, unknown short option leave it dangling */
329 for (; ctx->argc; ctx->argc--, ctx->argv++) {
330 const char *arg = ctx->argv[0];
332 if (*arg != '-' || !arg[1]) {
333 if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
335 ctx->out[ctx->cpidx++] = ctx->argv[0];
341 if (internal_help && *ctx->opt == 'h')
342 return parse_options_usage(usagestr, options);
343 switch (parse_short_opt(ctx, options)) {
345 return parse_options_usage(usagestr, options);
352 check_typos(arg + 1, options);
354 if (internal_help && *ctx->opt == 'h')
355 return parse_options_usage(usagestr, options);
356 switch (parse_short_opt(ctx, options)) {
358 return parse_options_usage(usagestr, options);
360 /* fake a short option thing to hide the fact that we may have
361 * started to parse aggregated stuff
363 * This is leaky, too bad.
365 ctx->argv[0] = strdup(ctx->opt - 1);
366 *(char *)ctx->argv[0] = '-';
375 if (!arg[2]) { /* "--" */
376 if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
383 if (internal_help && !strcmp(arg + 2, "help-all"))
384 return usage_with_options_internal(usagestr, options, 1);
385 if (internal_help && !strcmp(arg + 2, "help"))
386 return parse_options_usage(usagestr, options);
387 if (!strcmp(arg + 2, "list-opts"))
388 return PARSE_OPT_LIST;
389 switch (parse_long_opt(ctx, arg + 2, options)) {
391 return parse_options_usage(usagestr, options);
399 if (!(ctx->flags & PARSE_OPT_KEEP_UNKNOWN))
400 return PARSE_OPT_UNKNOWN;
401 ctx->out[ctx->cpidx++] = ctx->argv[0];
404 return PARSE_OPT_DONE;
407 int parse_options_end(struct parse_opt_ctx_t *ctx)
409 memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out));
410 ctx->out[ctx->cpidx + ctx->argc] = NULL;
411 return ctx->cpidx + ctx->argc;
414 int parse_options(int argc, const char **argv, const struct option *options,
415 const char * const usagestr[], int flags)
417 struct parse_opt_ctx_t ctx;
419 perf_header__set_cmdline(argc, argv);
421 parse_options_start(&ctx, argc, argv, flags);
422 switch (parse_options_step(&ctx, options, usagestr)) {
428 while (options->type != OPTION_END) {
429 printf("--%s ", options->long_name);
433 default: /* PARSE_OPT_UNKNOWN */
434 if (ctx.argv[0][1] == '-') {
435 error("unknown option `%s'", ctx.argv[0] + 2);
437 error("unknown switch `%c'", *ctx.opt);
439 usage_with_options(usagestr, options);
442 return parse_options_end(&ctx);
445 #define USAGE_OPTS_WIDTH 24
448 int usage_with_options_internal(const char * const *usagestr,
449 const struct option *opts, int full)
452 return PARSE_OPT_HELP;
454 fprintf(stderr, "\n usage: %s\n", *usagestr++);
455 while (*usagestr && **usagestr)
456 fprintf(stderr, " or: %s\n", *usagestr++);
458 fprintf(stderr, "%s%s\n",
459 **usagestr ? " " : "",
464 if (opts->type != OPTION_GROUP)
467 for (; opts->type != OPTION_END; opts++) {
471 if (opts->type == OPTION_GROUP) {
474 fprintf(stderr, "%s\n", opts->help);
477 if (!full && (opts->flags & PARSE_OPT_HIDDEN))
480 pos = fprintf(stderr, " ");
481 if (opts->short_name)
482 pos += fprintf(stderr, "-%c", opts->short_name);
484 pos += fprintf(stderr, " ");
486 if (opts->long_name && opts->short_name)
487 pos += fprintf(stderr, ", ");
489 pos += fprintf(stderr, "--%s", opts->long_name);
491 switch (opts->type) {
492 case OPTION_ARGUMENT:
497 case OPTION_UINTEGER:
498 if (opts->flags & PARSE_OPT_OPTARG)
500 pos += fprintf(stderr, "[=<n>]");
502 pos += fprintf(stderr, "[<n>]");
504 pos += fprintf(stderr, " <n>");
506 case OPTION_CALLBACK:
507 if (opts->flags & PARSE_OPT_NOARG)
512 if (opts->flags & PARSE_OPT_OPTARG)
514 pos += fprintf(stderr, "[=<%s>]", opts->argh);
516 pos += fprintf(stderr, "[<%s>]", opts->argh);
518 pos += fprintf(stderr, " <%s>", opts->argh);
520 if (opts->flags & PARSE_OPT_OPTARG)
522 pos += fprintf(stderr, "[=...]");
524 pos += fprintf(stderr, "[...]");
526 pos += fprintf(stderr, " ...");
529 default: /* OPTION_{BIT,BOOLEAN,SET_UINT,SET_PTR} */
535 case OPTION_SET_UINT:
540 if (pos <= USAGE_OPTS_WIDTH)
541 pad = USAGE_OPTS_WIDTH - pos;
544 pad = USAGE_OPTS_WIDTH;
546 fprintf(stderr, "%*s%s\n", pad + USAGE_GAP, "", opts->help);
550 return PARSE_OPT_HELP;
553 void usage_with_options(const char * const *usagestr,
554 const struct option *opts)
557 usage_with_options_internal(usagestr, opts, 0);
561 int parse_options_usage(const char * const *usagestr,
562 const struct option *opts)
564 return usage_with_options_internal(usagestr, opts, 0);
568 int parse_opt_verbosity_cb(const struct option *opt,
569 const char *arg __maybe_unused,
572 int *target = opt->value;
575 /* --no-quiet, --no-verbose */
577 else if (opt->short_name == 'v') {