Skip to contentSkip to conversation

perf kvm: introduce --list-cmds for use by scripts

PATCH In review Ramkumar Ramachandra 4 participants 7 messages Mar 4, 2014 – Mar 18, 2014 perf events

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 perf kvm: introduce --list-cmds for use by scripts

Introduce

  $ perf kvm --list-cmds

to dump a raw list of commands for use by the completion script. In order to do this, introduce parse_options_subcommand() for handling subcommands as a special case in the parse-options machinery.

Cc David Ahern <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>

---
Does this example justify the creation of parse_options_subcommand()? Initializing the usagestr to { NULL, NULL } and passing it to parse_options_subcommand() isn't intuitive -- what can we do about that?

 tools/perf/builtin-kvm.c        | 12 +++++-------
 tools/perf/perf-completion.sh   |  2 +-
 tools/perf/util/parse-options.c | 37 +++++++++++++++++++++++++++++++++----
 tools/perf/util/parse-options.h |  8 +++++++-
 4 files changed, 46 insertions(+), 13 deletions(-)
tools/perf/builtin-kvm.c +5 -7
index a735051..21c164b 100644
@@ -1691,17 +1691,15 @@ int cmd_kvm(int argc, const char **argv, const char *prefix __maybe_unused)
 		OPT_END()
 	};
 
-
-	const char * const kvm_usage[] = {
-		"perf kvm [<options>] {top|record|report|diff|buildid-list|stat}",
-		NULL
-	};
+	const char *const kvm_subcommands[] = { "top", "record", "report", "diff",
+						"buildid-list", "stat", NULL };
+	const char *kvm_usage[] = { NULL, NULL };
 
 	perf_host  = 0;
 	perf_guest = 1;
 
-	argc = parse_options(argc, argv, kvm_options, kvm_usage,
-			PARSE_OPT_STOP_AT_NON_OPTION);
+	argc = parse_options_subcommand(argc, argv, kvm_options, kvm_subcommands, kvm_usage,
+					PARSE_OPT_STOP_AT_NON_OPTION);
 	if (!argc)
 		usage_with_options(kvm_usage, kvm_options);
 
tools/perf/perf-completion.sh +1 -1
index 496e2ab..ae3a576 100644
@@ -123,7 +123,7 @@ __perf_main ()
 		__perfcomp_colon "$evts" "$cur"
 	# List subcommands for 'perf kvm'
 	elif [[ $prev == "kvm" ]]; then
-		subcmds="top record report diff buildid-list stat"
+		subcmds=$($cmd $prev --list-cmds)
 		__perfcomp_colon "$subcmds" "$cur"
 	# List long option names
 	elif [[ $cur == --* ]];  then
tools/perf/util/parse-options.c +33 -4
index d22e3f8..bf48092 100644
@@ -407,7 +407,9 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
 		if (internal_help && !strcmp(arg + 2, "help"))
 			return usage_with_options_internal(usagestr, options, 0);
 		if (!strcmp(arg + 2, "list-opts"))
-			return PARSE_OPT_LIST;
+			return PARSE_OPT_LIST_OPTS;
+		if (!strcmp(arg + 2, "list-cmds"))
+			return PARSE_OPT_LIST_SUBCMDS;
 		switch (parse_long_opt(ctx, arg + 2, options)) {
 		case -1:
 			return parse_options_usage(usagestr, options, arg + 2, 0);
@@ -433,25 +435,45 @@ int parse_options_end(struct parse_opt_ctx_t *ctx)
 	return ctx->cpidx + ctx->argc;
 }
 
-int parse_options(int argc, const char **argv, const struct option *options,
-		  const char * const usagestr[], int flags)
+int parse_options_subcommand(int argc, const char **argv, const struct option *options,
+			const char *const subcommands[], const char *usagestr[], int flags)
 {
 	struct parse_opt_ctx_t ctx;
 
 	perf_header__set_cmdline(argc, argv);
 
+	/* build usage string if it's not provided */
+	if (subcommands && !usagestr[0]) {
+		struct strbuf buf = STRBUF_INIT;
+
+		strbuf_addf(&buf, "perf %s [<options>] {", argv[0]);
+		for (int i = 0; subcommands[i]; i++) {
+			if (i)
+				strbuf_addstr(&buf, "|");
+			strbuf_addstr(&buf, subcommands[i]);
+		}
+		strbuf_addstr(&buf, "}");
+
+		usagestr[0] = strdup(buf.buf);
+		strbuf_release(&buf);
+	}
+
 	parse_options_start(&ctx, argc, argv, flags);
 	switch (parse_options_step(&ctx, options, usagestr)) {
 	case PARSE_OPT_HELP:
 		exit(129);
 	case PARSE_OPT_DONE:
 		break;
-	case PARSE_OPT_LIST:
+	case PARSE_OPT_LIST_OPTS:
 		while (options->type != OPTION_END) {
 			printf("--%s ", options->long_name);
 			options++;
 		}
 		exit(130);
+	case PARSE_OPT_LIST_SUBCMDS:
+		for (int i = 0; subcommands[i]; i++)
+			printf("%s ", subcommands[i]);
+		exit(130);
 	default: /* PARSE_OPT_UNKNOWN */
 		if (ctx.argv[0][1] == '-') {
 			error("unknown option `%s'", ctx.argv[0] + 2);
@@ -464,6 +486,13 @@ int parse_options(int argc, const char **argv, const struct option *options,
 	return parse_options_end(&ctx);
 }
 
+int parse_options(int argc, const char **argv, const struct option *options,
+		  const char * const usagestr[], int flags)
+{
+	return parse_options_subcommand(argc, argv, options, NULL,
+					(const char **) usagestr, flags);
+}
+
 #define USAGE_OPTS_WIDTH 24
 #define USAGE_GAP         2
 
tools/perf/util/parse-options.h +7 -1
index cbf0149..d8dac8a 100644
@@ -140,6 +140,11 @@ extern int parse_options(int argc, const char **argv,
                          const struct option *options,
                          const char * const usagestr[], int flags);
 
+extern int parse_options_subcommand(int argc, const char **argv,
+				const struct option *options,
+				const char *const subcommands[],
+				const char *usagestr[], int flags);
+
 extern NORETURN void usage_with_options(const char * const *usagestr,
                                         const struct option *options);
 
@@ -148,7 +153,8 @@ extern NORETURN void usage_with_options(const char * const *usagestr,
 enum {
 	PARSE_OPT_HELP = -1,
 	PARSE_OPT_DONE,
-	PARSE_OPT_LIST,
+	PARSE_OPT_LIST_OPTS,
+	PARSE_OPT_LIST_SUBCMDS,
 	PARSE_OPT_UNKNOWN,
 };
 
Signature · 1 line
1.9.rc0.1.g9d22d25
#1 lore raw
Quoted 11 lines · Ramkumar Ramachandra

Introduce

   $ perf kvm --list-cmds

to dump a raw list of commands for use by the completion script. In order to do this, introduce parse_options_subcommand() for handling subcommands as a special case in the parse-options machinery.

Cc David Ahern <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>

Looks ok to me. Jiri?

David

Quoted 157 lines

---
Does this example justify the creation of parse_options_subcommand()? Initializing the usagestr to { NULL, NULL } and passing it to parse_options_subcommand() isn't intuitive -- what can we do about that?

  tools/perf/builtin-kvm.c        | 12 +++++-------
  tools/perf/perf-completion.sh   |  2 +-
  tools/perf/util/parse-options.c | 37 +++++++++++++++++++++++++++++++++----
  tools/perf/util/parse-options.h |  8 +++++++-
  4 files changed, 46 insertions(+), 13 deletions(-)
tools/perf/builtin-kvm.c +5 -7
index a735051..21c164b 100644
@@ -1691,17 +1691,15 @@ int cmd_kvm(int argc, const char **argv, const char *prefix __maybe_unused)
  		OPT_END()
  	};
 
-
-	const char * const kvm_usage[] = {
-		"perf kvm [<options>] {top|record|report|diff|buildid-list|stat}",
-		NULL
-	};
+	const char *const kvm_subcommands[] = { "top", "record", "report", "diff",
+						"buildid-list", "stat", NULL };
+	const char *kvm_usage[] = { NULL, NULL };
 
  	perf_host  = 0;
  	perf_guest = 1;
 
-	argc = parse_options(argc, argv, kvm_options, kvm_usage,
-			PARSE_OPT_STOP_AT_NON_OPTION);
+	argc = parse_options_subcommand(argc, argv, kvm_options, kvm_subcommands, kvm_usage,
+					PARSE_OPT_STOP_AT_NON_OPTION);
  	if (!argc)
  		usage_with_options(kvm_usage, kvm_options);
 
tools/perf/perf-completion.sh +1 -1
index 496e2ab..ae3a576 100644
@@ -123,7 +123,7 @@ __perf_main ()
  		__perfcomp_colon "$evts" "$cur"
  	# List subcommands for 'perf kvm'
  	elif [[ $prev == "kvm" ]]; then
-		subcmds="top record report diff buildid-list stat"
+		subcmds=$($cmd $prev --list-cmds)
  		__perfcomp_colon "$subcmds" "$cur"
  	# List long option names
  	elif [[ $cur == --* ]];  then
tools/perf/util/parse-options.c +33 -4
index d22e3f8..bf48092 100644
@@ -407,7 +407,9 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
  		if (internal_help && !strcmp(arg + 2, "help"))
  			return usage_with_options_internal(usagestr, options, 0);
  		if (!strcmp(arg + 2, "list-opts"))
-			return PARSE_OPT_LIST;
+			return PARSE_OPT_LIST_OPTS;
+		if (!strcmp(arg + 2, "list-cmds"))
+			return PARSE_OPT_LIST_SUBCMDS;
  		switch (parse_long_opt(ctx, arg + 2, options)) {
  		case -1:
  			return parse_options_usage(usagestr, options, arg + 2, 0);
@@ -433,25 +435,45 @@ int parse_options_end(struct parse_opt_ctx_t *ctx)
  	return ctx->cpidx + ctx->argc;
  }
 
-int parse_options(int argc, const char **argv, const struct option *options,
-		  const char * const usagestr[], int flags)
+int parse_options_subcommand(int argc, const char **argv, const struct option *options,
+			const char *const subcommands[], const char *usagestr[], int flags)
  {
  	struct parse_opt_ctx_t ctx;
 
  	perf_header__set_cmdline(argc, argv);
 
+	/* build usage string if it's not provided */
+	if (subcommands && !usagestr[0]) {
+		struct strbuf buf = STRBUF_INIT;
+
+		strbuf_addf(&buf, "perf %s [<options>] {", argv[0]);
+		for (int i = 0; subcommands[i]; i++) {
+			if (i)
+				strbuf_addstr(&buf, "|");
+			strbuf_addstr(&buf, subcommands[i]);
+		}
+		strbuf_addstr(&buf, "}");
+
+		usagestr[0] = strdup(buf.buf);
+		strbuf_release(&buf);
+	}
+
  	parse_options_start(&ctx, argc, argv, flags);
  	switch (parse_options_step(&ctx, options, usagestr)) {
  	case PARSE_OPT_HELP:
  		exit(129);
  	case PARSE_OPT_DONE:
  		break;
-	case PARSE_OPT_LIST:
+	case PARSE_OPT_LIST_OPTS:
  		while (options->type != OPTION_END) {
  			printf("--%s ", options->long_name);
  			options++;
  		}
  		exit(130);
+	case PARSE_OPT_LIST_SUBCMDS:
+		for (int i = 0; subcommands[i]; i++)
+			printf("%s ", subcommands[i]);
+		exit(130);
  	default: /* PARSE_OPT_UNKNOWN */
  		if (ctx.argv[0][1] == '-') {
  			error("unknown option `%s'", ctx.argv[0] + 2);
@@ -464,6 +486,13 @@ int parse_options(int argc, const char **argv, const struct option *options,
  	return parse_options_end(&ctx);
  }
 
+int parse_options(int argc, const char **argv, const struct option *options,
+		  const char * const usagestr[], int flags)
+{
+	return parse_options_subcommand(argc, argv, options, NULL,
+					(const char **) usagestr, flags);
+}
+
  #define USAGE_OPTS_WIDTH 24
  #define USAGE_GAP         2
 
tools/perf/util/parse-options.h +7 -1
index cbf0149..d8dac8a 100644
@@ -140,6 +140,11 @@ extern int parse_options(int argc, const char **argv,
                           const struct option *options,
                           const char * const usagestr[], int flags);
 
+extern int parse_options_subcommand(int argc, const char **argv,
+				const struct option *options,
+				const char *const subcommands[],
+				const char *usagestr[], int flags);
+
  extern NORETURN void usage_with_options(const char * const *usagestr,
                                          const struct option *options);
 
@@ -148,7 +153,8 @@ extern NORETURN void usage_with_options(const char * const *usagestr,
  enum {
  	PARSE_OPT_HELP = -1,
  	PARSE_OPT_DONE,
-	PARSE_OPT_LIST,
+	PARSE_OPT_LIST_OPTS,
+	PARSE_OPT_LIST_SUBCMDS,
  	PARSE_OPT_UNKNOWN,
  };
 
#2 lore raw
In reply to Ramkumar Ramachandra
Quoted 7 lines · Ramkumar Ramachandra

Introduce

  $ perf kvm --list-cmds

to dump a raw list of commands for use by the completion script. In order to do this, introduce parse_options_subcommand() for handling subcommands as a special case in the parse-options machinery.

so this doesn't change the behaviour at all, right?

SNIP

Quoted 13 lines
tools/perf/builtin-kvm.c +2 -2
-	argc = parse_options(argc, argv, kvm_options, kvm_usage,
-			PARSE_OPT_STOP_AT_NON_OPTION);
+	argc = parse_options_subcommand(argc, argv, kvm_options, kvm_subcommands, kvm_usage,
+					PARSE_OPT_STOP_AT_NON_OPTION);
 	if (!argc)
 		usage_with_options(kvm_usage, kvm_options);
tools/perf/perf-completion.sh +0 -0
index 496e2ab..ae3a576 100644

tools/perf/perf-completion.sh __perf_main () · line 126

Quoted 5 lines
tools/perf/perf-completion.sh +1 -1
@@ -123,7 +123,7 @@ __perf_main ()
 		__perfcomp_colon "$evts" "$cur"
 	# List subcommands for 'perf kvm'
 	elif [[ $prev == "kvm" ]]; then
-		subcmds="top record report diff buildid-list stat"
+		subcmds=$($cmd $prev --list-cmds)

Do we want some generic approach here.. if we're
adding generic --list-cmds option anyway.. hm?

SNIP

Quoted 14 lines
tools/perf/perf-completion.sh +2 -2
-int parse_options(int argc, const char **argv, const struct option *options,
-		  const char * const usagestr[], int flags)
+int parse_options_subcommand(int argc, const char **argv, const struct option *options,
+			const char *const subcommands[], const char *usagestr[], int flags)
 {
tools/perf/perf-completion.sh +5 -0
 	struct parse_opt_ctx_t ctx;
 
 	perf_header__set_cmdline(argc, argv);
 
+	/* build usage string if it's not provided */
+	if (subcommands && !usagestr[0]) {
+		struct strbuf buf = STRBUF_INIT;
+
+		strbuf_addf(&buf, "perf %s [<options>] {", argv[0]);

tools/perf/perf-completion.sh

Quoted 10 lines
tools/perf/perf-completion.sh +10 -0
+		for (int i = 0; subcommands[i]; i++) {
+			if (i)
+				strbuf_addstr(&buf, "|");
+			strbuf_addstr(&buf, subcommands[i]);
+		}
+		strbuf_addstr(&buf, "}");
+
+		usagestr[0] = strdup(buf.buf);
+		strbuf_release(&buf);
+	}

is above code ever used now?

jirka

#3 lore raw
Quoted 10 lines · Jiri Olsa
Quoted 7 lines · Ramkumar Ramachandra

Introduce

  $ perf kvm --list-cmds

to dump a raw list of commands for use by the completion script. In order to do this, introduce parse_options_subcommand() for handling subcommands as a special case in the parse-options machinery.

so this doesn't change the behaviour at all, right?

It just introduces --list-cmds; no, it doesn't change behavior otherwise.

Quoted 8 lines
Quoted 8 lines
tools/perf/builtin-kvm.c +2 -2
-     argc = parse_options(argc, argv, kvm_options, kvm_usage,
-                     PARSE_OPT_STOP_AT_NON_OPTION);
+     argc = parse_options_subcommand(argc, argv, kvm_options, kvm_subcommands, kvm_usage,
+                                     PARSE_OPT_STOP_AT_NON_OPTION);
      if (!argc)
              usage_with_options(kvm_usage, kvm_options);
Quoted 13 lines

tools/perf/perf-completion.sh __perf_main () · line 126

Quoted 10 lines
tools/perf/perf-completion.sh +1 -1
index 496e2ab..ae3a576 100644
@@ -123,7 +123,7 @@ __perf_main ()
              __perfcomp_colon "$evts" "$cur"
      # List subcommands for 'perf kvm'
      elif [[ $prev == "kvm" ]]; then
-             subcmds="top record report diff buildid-list stat"
+             subcmds=$($cmd $prev --list-cmds)

Do we want some generic approach here.. if we're
adding generic --list-cmds option anyway.. hm?

Currently, we're using $cmd $prev --list-cmds instead of hard-coding a list for 'perf kvm'. Once I submit future patches to convert other commands to use parse_options_subcommand(), we can use the same line for completions.

Or did you mean something else when you said generic approach?

Quoted 18 lines
Quoted 18 lines
tools/perf/perf-completion.sh +2 -2
-int parse_options(int argc, const char **argv, const struct option *options,
-               const char * const usagestr[], int flags)
+int parse_options_subcommand(int argc, const char **argv, const struct option *options,
+                     const char *const subcommands[], const char *usagestr[], int flags)
 {
      struct parse_opt_ctx_t ctx;

      perf_header__set_cmdline(argc, argv);
tools/perf/perf-completion.sh +9 -0
+     /* build usage string if it's not provided */
+     if (subcommands && !usagestr[0]) {
+             struct strbuf buf = STRBUF_INIT;
+
+             strbuf_addf(&buf, "perf %s [<options>] {", argv[0]);
+             for (int i = 0; subcommands[i]; i++) {
+                     if (i)
+                             strbuf_addstr(&buf, "|");
+                     strbuf_addstr(&buf, subcommands[i]);
Quoted 8 lines

tools/perf/perf-completion.sh

Quoted 6 lines
tools/perf/perf-completion.sh +6 -0
+             }
+             strbuf_addstr(&buf, "}");
+
+             usagestr[0] = strdup(buf.buf);
+             strbuf_release(&buf);
+     }

is above code ever used now?

Yeah; note that kvm_usage is initialized to { NULL, NULL } and passed -- this code fills in the usage string then.

#4 lore raw
Quoted 9 lines · Ramkumar Ramachandra
Quoted 8 lines · Jiri Olsa
Quoted 7 lines · Ramkumar Ramachandra

Introduce

  $ perf kvm --list-cmds

to dump a raw list of commands for use by the completion script. In order to do this, introduce parse_options_subcommand() for handling subcommands as a special case in the parse-options machinery.

Jiri? How do we proceed with this?

#5 lore raw
Quoted 5 lines · Ramkumar Ramachandra
Quoted 4 lines · Ramkumar Ramachandra
Quoted 3 lines · Jiri Olsa
Quoted 2 lines · Ramkumar Ramachandra

Introduce

Quoted 7 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
  $ perf kvm --list-cmds

to dump a raw list of commands for use by the completion script. In order to do this, introduce parse_options_subcommand() for handling subcommands as a special case in the parse-options machinery.

Jiri? How do we proceed with this?

I think Arnaldo will process it eventually ;-)
I dont have any objections..

jirka

#6 lore raw
In reply to Ramkumar Ramachandra
perf kvm: introduce --list-cmds for use by scripts
Commit-ID:  09a71b97cce70551356b13b668aa1d7d6da84457
Gitweb:     http://git.kernel.org/tip/09a71b97cce70551356b13b668aa1d7d6da84457
Author:     Ramkumar Ramachandra <[email protected]>
AuthorDate: Mon, 3 Mar 2014 20:26:36 -0500
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Fri, 14 Mar 2014 18:08:41 -0300

perf kvm: introduce --list-cmds for use by scripts

Introduce

  $ perf kvm --list-cmds

to dump a raw list of commands for use by the completion script. In order to do this, introduce parse_options_subcommand() for handling subcommands as a special case in the parse-options machinery.

---
 tools/perf/builtin-kvm.c        | 12 +++++-------
 tools/perf/perf-completion.sh   |  2 +-
 tools/perf/util/parse-options.c | 37 +++++++++++++++++++++++++++++++++----
 tools/perf/util/parse-options.h |  8 +++++++-
 4 files changed, 46 insertions(+), 13 deletions(-)
tools/perf/builtin-kvm.c +5 -7
index a735051..21c164b 100644
@@ -1691,17 +1691,15 @@ int cmd_kvm(int argc, const char **argv, const char *prefix __maybe_unused)
 		OPT_END()
 	};
 
-
-	const char * const kvm_usage[] = {
-		"perf kvm [<options>] {top|record|report|diff|buildid-list|stat}",
-		NULL
-	};
+	const char *const kvm_subcommands[] = { "top", "record", "report", "diff",
+						"buildid-list", "stat", NULL };
+	const char *kvm_usage[] = { NULL, NULL };
 
 	perf_host  = 0;
 	perf_guest = 1;
 
-	argc = parse_options(argc, argv, kvm_options, kvm_usage,
-			PARSE_OPT_STOP_AT_NON_OPTION);
+	argc = parse_options_subcommand(argc, argv, kvm_options, kvm_subcommands, kvm_usage,
+					PARSE_OPT_STOP_AT_NON_OPTION);
 	if (!argc)
 		usage_with_options(kvm_usage, kvm_options);
 
tools/perf/perf-completion.sh +1 -1
index 496e2ab..ae3a576 100644
@@ -123,7 +123,7 @@ __perf_main ()
 		__perfcomp_colon "$evts" "$cur"
 	# List subcommands for 'perf kvm'
 	elif [[ $prev == "kvm" ]]; then
-		subcmds="top record report diff buildid-list stat"
+		subcmds=$($cmd $prev --list-cmds)
 		__perfcomp_colon "$subcmds" "$cur"
 	# List long option names
 	elif [[ $cur == --* ]];  then
tools/perf/util/parse-options.c +33 -4
index d22e3f8..bf48092 100644
@@ -407,7 +407,9 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
 		if (internal_help && !strcmp(arg + 2, "help"))
 			return usage_with_options_internal(usagestr, options, 0);
 		if (!strcmp(arg + 2, "list-opts"))
-			return PARSE_OPT_LIST;
+			return PARSE_OPT_LIST_OPTS;
+		if (!strcmp(arg + 2, "list-cmds"))
+			return PARSE_OPT_LIST_SUBCMDS;
 		switch (parse_long_opt(ctx, arg + 2, options)) {
 		case -1:
 			return parse_options_usage(usagestr, options, arg + 2, 0);
@@ -433,25 +435,45 @@ int parse_options_end(struct parse_opt_ctx_t *ctx)
 	return ctx->cpidx + ctx->argc;
 }
 
-int parse_options(int argc, const char **argv, const struct option *options,
-		  const char * const usagestr[], int flags)
+int parse_options_subcommand(int argc, const char **argv, const struct option *options,
+			const char *const subcommands[], const char *usagestr[], int flags)
 {
 	struct parse_opt_ctx_t ctx;
 
 	perf_header__set_cmdline(argc, argv);
 
+	/* build usage string if it's not provided */
+	if (subcommands && !usagestr[0]) {
+		struct strbuf buf = STRBUF_INIT;
+
+		strbuf_addf(&buf, "perf %s [<options>] {", argv[0]);
+		for (int i = 0; subcommands[i]; i++) {
+			if (i)
+				strbuf_addstr(&buf, "|");
+			strbuf_addstr(&buf, subcommands[i]);
+		}
+		strbuf_addstr(&buf, "}");
+
+		usagestr[0] = strdup(buf.buf);
+		strbuf_release(&buf);
+	}
+
 	parse_options_start(&ctx, argc, argv, flags);
 	switch (parse_options_step(&ctx, options, usagestr)) {
 	case PARSE_OPT_HELP:
 		exit(129);
 	case PARSE_OPT_DONE:
 		break;
-	case PARSE_OPT_LIST:
+	case PARSE_OPT_LIST_OPTS:
 		while (options->type != OPTION_END) {
 			printf("--%s ", options->long_name);
 			options++;
 		}
 		exit(130);
+	case PARSE_OPT_LIST_SUBCMDS:
+		for (int i = 0; subcommands[i]; i++)
+			printf("%s ", subcommands[i]);
+		exit(130);
 	default: /* PARSE_OPT_UNKNOWN */
 		if (ctx.argv[0][1] == '-') {
 			error("unknown option `%s'", ctx.argv[0] + 2);
@@ -464,6 +486,13 @@ int parse_options(int argc, const char **argv, const struct option *options,
 	return parse_options_end(&ctx);
 }
 
+int parse_options(int argc, const char **argv, const struct option *options,
+		  const char * const usagestr[], int flags)
+{
+	return parse_options_subcommand(argc, argv, options, NULL,
+					(const char **) usagestr, flags);
+}
+
 #define USAGE_OPTS_WIDTH 24
 #define USAGE_GAP         2
 
tools/perf/util/parse-options.h +7 -1
index cbf0149..d8dac8a 100644
@@ -140,6 +140,11 @@ extern int parse_options(int argc, const char **argv,
                          const struct option *options,
                          const char * const usagestr[], int flags);
 
+extern int parse_options_subcommand(int argc, const char **argv,
+				const struct option *options,
+				const char *const subcommands[],
+				const char *usagestr[], int flags);
+
 extern NORETURN void usage_with_options(const char * const *usagestr,
                                         const struct option *options);
 
@@ -148,7 +153,8 @@ extern NORETURN void usage_with_options(const char * const *usagestr,
 enum {
 	PARSE_OPT_HELP = -1,
 	PARSE_OPT_DONE,
-	PARSE_OPT_LIST,
+	PARSE_OPT_LIST_OPTS,
+	PARSE_OPT_LIST_SUBCMDS,
 	PARSE_OPT_UNKNOWN,
 };