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,
377 struct parse_opt_ctx_t
*);
379 int parse_options_step(struct parse_opt_ctx_t
*ctx
,
380 const struct option
*options
,
381 const char * const usagestr
[])
383 int internal_help
= !(ctx
->flags
& PARSE_OPT_NO_INTERNAL_HELP
);
384 int excl_short_opt
= 1;
387 /* we must reset ->opt, unknown short option leave it dangling */
390 for (; ctx
->argc
; ctx
->argc
--, ctx
->argv
++) {
392 if (*arg
!= '-' || !arg
[1]) {
393 if (ctx
->flags
& PARSE_OPT_STOP_AT_NON_OPTION
)
395 ctx
->out
[ctx
->cpidx
++] = ctx
->argv
[0];
401 if (internal_help
&& *ctx
->opt
== 'h') {
402 return usage_with_options_internal(usagestr
, options
, 0, ctx
);
404 switch (parse_short_opt(ctx
, options
)) {
406 return parse_options_usage(usagestr
, options
, arg
, 1);
415 check_typos(arg
, options
);
417 if (internal_help
&& *ctx
->opt
== 'h')
418 return usage_with_options_internal(usagestr
, options
, 0, ctx
);
420 switch (parse_short_opt(ctx
, options
)) {
422 return parse_options_usage(usagestr
, options
, arg
, 1);
424 /* fake a short option thing to hide the fact that we may have
425 * started to parse aggregated stuff
427 * This is leaky, too bad.
429 ctx
->argv
[0] = strdup(ctx
->opt
- 1);
430 *(char *)ctx
->argv
[0] = '-';
441 if (!arg
[2]) { /* "--" */
442 if (!(ctx
->flags
& PARSE_OPT_KEEP_DASHDASH
)) {
450 if (internal_help
&& !strcmp(arg
, "help-all"))
451 return usage_with_options_internal(usagestr
, options
, 1, ctx
);
452 if (internal_help
&& !strcmp(arg
, "help"))
453 return usage_with_options_internal(usagestr
, options
, 0, ctx
);
454 if (!strcmp(arg
, "list-opts"))
455 return PARSE_OPT_LIST_OPTS
;
456 if (!strcmp(arg
, "list-cmds"))
457 return PARSE_OPT_LIST_SUBCMDS
;
458 switch (parse_long_opt(ctx
, arg
, options
)) {
460 return parse_options_usage(usagestr
, options
, arg
, 0);
471 if (!(ctx
->flags
& PARSE_OPT_KEEP_UNKNOWN
))
472 return PARSE_OPT_UNKNOWN
;
473 ctx
->out
[ctx
->cpidx
++] = ctx
->argv
[0];
476 return PARSE_OPT_DONE
;
479 parse_options_usage(usagestr
, options
, arg
, excl_short_opt
);
480 if ((excl_short_opt
&& ctx
->excl_opt
->short_name
) ||
481 ctx
->excl_opt
->long_name
== NULL
) {
482 char opt
= ctx
->excl_opt
->short_name
;
483 parse_options_usage(NULL
, options
, &opt
, 1);
485 parse_options_usage(NULL
, options
, ctx
->excl_opt
->long_name
, 0);
487 return PARSE_OPT_HELP
;
490 int parse_options_end(struct parse_opt_ctx_t
*ctx
)
492 memmove(ctx
->out
+ ctx
->cpidx
, ctx
->argv
, ctx
->argc
* sizeof(*ctx
->out
));
493 ctx
->out
[ctx
->cpidx
+ ctx
->argc
] = NULL
;
494 return ctx
->cpidx
+ ctx
->argc
;
497 int parse_options_subcommand(int argc
, const char **argv
, const struct option
*options
,
498 const char *const subcommands
[], const char *usagestr
[], int flags
)
500 struct parse_opt_ctx_t ctx
;
502 perf_env__set_cmdline(&perf_env
, argc
, argv
);
504 /* build usage string if it's not provided */
505 if (subcommands
&& !usagestr
[0]) {
506 struct strbuf buf
= STRBUF_INIT
;
508 strbuf_addf(&buf
, "perf %s [<options>] {", argv
[0]);
509 for (int i
= 0; subcommands
[i
]; i
++) {
511 strbuf_addstr(&buf
, "|");
512 strbuf_addstr(&buf
, subcommands
[i
]);
514 strbuf_addstr(&buf
, "}");
516 usagestr
[0] = strdup(buf
.buf
);
517 strbuf_release(&buf
);
520 parse_options_start(&ctx
, argc
, argv
, flags
);
521 switch (parse_options_step(&ctx
, options
, usagestr
)) {
526 case PARSE_OPT_LIST_OPTS
:
527 while (options
->type
!= OPTION_END
) {
528 if (options
->long_name
)
529 printf("--%s ", options
->long_name
);
534 case PARSE_OPT_LIST_SUBCMDS
:
536 for (int i
= 0; subcommands
[i
]; i
++)
537 printf("%s ", subcommands
[i
]);
541 default: /* PARSE_OPT_UNKNOWN */
542 if (ctx
.argv
[0][1] == '-') {
543 error("unknown option `%s'", ctx
.argv
[0] + 2);
545 error("unknown switch `%c'", *ctx
.opt
);
547 usage_with_options(usagestr
, options
);
550 return parse_options_end(&ctx
);
553 int parse_options(int argc
, const char **argv
, const struct option
*options
,
554 const char * const usagestr
[], int flags
)
556 return parse_options_subcommand(argc
, argv
, options
, NULL
,
557 (const char **) usagestr
, flags
);
560 #define USAGE_OPTS_WIDTH 24
563 static void print_option_help(const struct option
*opts
, int full
)
568 if (opts
->type
== OPTION_GROUP
) {
571 fprintf(stderr
, "%s\n", opts
->help
);
574 if (!full
&& (opts
->flags
& PARSE_OPT_HIDDEN
))
576 if (opts
->flags
& PARSE_OPT_DISABLED
)
579 pos
= fprintf(stderr
, " ");
580 if (opts
->short_name
)
581 pos
+= fprintf(stderr
, "-%c", opts
->short_name
);
583 pos
+= fprintf(stderr
, " ");
585 if (opts
->long_name
&& opts
->short_name
)
586 pos
+= fprintf(stderr
, ", ");
588 pos
+= fprintf(stderr
, "--%s", opts
->long_name
);
590 switch (opts
->type
) {
591 case OPTION_ARGUMENT
:
596 case OPTION_UINTEGER
:
597 if (opts
->flags
& PARSE_OPT_OPTARG
)
599 pos
+= fprintf(stderr
, "[=<n>]");
601 pos
+= fprintf(stderr
, "[<n>]");
603 pos
+= fprintf(stderr
, " <n>");
605 case OPTION_CALLBACK
:
606 if (opts
->flags
& PARSE_OPT_NOARG
)
611 if (opts
->flags
& PARSE_OPT_OPTARG
)
613 pos
+= fprintf(stderr
, "[=<%s>]", opts
->argh
);
615 pos
+= fprintf(stderr
, "[<%s>]", opts
->argh
);
617 pos
+= fprintf(stderr
, " <%s>", opts
->argh
);
619 if (opts
->flags
& PARSE_OPT_OPTARG
)
621 pos
+= fprintf(stderr
, "[=...]");
623 pos
+= fprintf(stderr
, "[...]");
625 pos
+= fprintf(stderr
, " ...");
628 default: /* OPTION_{BIT,BOOLEAN,SET_UINT,SET_PTR} */
634 case OPTION_SET_UINT
:
639 if (pos
<= USAGE_OPTS_WIDTH
)
640 pad
= USAGE_OPTS_WIDTH
- pos
;
643 pad
= USAGE_OPTS_WIDTH
;
645 fprintf(stderr
, "%*s%s\n", pad
+ USAGE_GAP
, "", opts
->help
);
648 static int option__cmp(const void *va
, const void *vb
)
650 const struct option
*a
= va
, *b
= vb
;
651 int sa
= tolower(a
->short_name
), sb
= tolower(b
->short_name
), ret
;
661 const char *la
= a
->long_name
?: "",
662 *lb
= b
->long_name
?: "";
663 ret
= strcmp(la
, lb
);
669 static struct option
*options__order(const struct option
*opts
)
672 const struct option
*o
= opts
;
673 struct option
*ordered
;
675 for (o
= opts
; o
->type
!= OPTION_END
; o
++)
678 ordered
= memdup(opts
, sizeof(*o
) * (nr_opts
+ 1));
682 qsort(ordered
, nr_opts
, sizeof(*o
), option__cmp
);
687 static bool option__in_argv(const struct option
*opt
, const struct parse_opt_ctx_t
*ctx
)
691 for (i
= 1; i
< ctx
->argc
; ++i
) {
692 const char *arg
= ctx
->argv
[i
];
697 if (arg
[1] == opt
->short_name
||
698 (arg
[1] == '-' && opt
->long_name
&& strcmp(opt
->long_name
, arg
+ 2) == 0))
705 int usage_with_options_internal(const char * const *usagestr
,
706 const struct option
*opts
, int full
,
707 struct parse_opt_ctx_t
*ctx
)
709 struct option
*ordered
;
712 return PARSE_OPT_HELP
;
714 fprintf(stderr
, "\n Usage: %s\n", *usagestr
++);
715 while (*usagestr
&& **usagestr
)
716 fprintf(stderr
, " or: %s\n", *usagestr
++);
718 fprintf(stderr
, "%s%s\n",
719 **usagestr
? " " : "",
724 if (opts
->type
!= OPTION_GROUP
)
727 ordered
= options__order(opts
);
731 for ( ; opts
->type
!= OPTION_END
; opts
++) {
732 if (ctx
&& ctx
->argc
> 1 && !option__in_argv(opts
, ctx
))
734 print_option_help(opts
, full
);
741 return PARSE_OPT_HELP
;
744 void usage_with_options(const char * const *usagestr
,
745 const struct option
*opts
)
748 usage_with_options_internal(usagestr
, opts
, 0, NULL
);
752 int parse_options_usage(const char * const *usagestr
,
753 const struct option
*opts
,
754 const char *optstr
, bool short_opt
)
759 fprintf(stderr
, "\n Usage: %s\n", *usagestr
++);
760 while (*usagestr
&& **usagestr
)
761 fprintf(stderr
, " or: %s\n", *usagestr
++);
763 fprintf(stderr
, "%s%s\n",
764 **usagestr
? " " : "",
771 for ( ; opts
->type
!= OPTION_END
; opts
++) {
773 if (opts
->short_name
== *optstr
) {
774 print_option_help(opts
, 0);
780 if (opts
->long_name
== NULL
)
783 if (!prefixcmp(opts
->long_name
, optstr
))
784 print_option_help(opts
, 0);
785 if (!prefixcmp("no-", optstr
) &&
786 !prefixcmp(opts
->long_name
, optstr
+ 3))
787 print_option_help(opts
, 0);
790 return PARSE_OPT_HELP
;
794 int parse_opt_verbosity_cb(const struct option
*opt
,
795 const char *arg __maybe_unused
,
798 int *target
= opt
->value
;
801 /* --no-quiet, --no-verbose */
803 else if (opt
->short_name
== 'v') {
817 void set_option_flag(struct option
*opts
, int shortopt
, const char *longopt
,
820 for (; opts
->type
!= OPTION_END
; opts
++) {
821 if ((shortopt
&& opts
->short_name
== shortopt
) ||
822 (opts
->long_name
&& longopt
&&
823 !strcmp(opts
->long_name
, longopt
))) {