Skip to contentSkip to conversation

perf tools: Bash completion update

PATCH In review Namhyung Kim 2 participants 11 messages Oct 2, 2012 – Oct 5, 2012 perf events

lore
Share & tools
lore

Namhyung Kim

author maintainer
#0 lore raw

cover perf tools: Bash completion update

Hi,

This patchset improves bash completion support for perf tools. Some option names are really painful to type so here comes a support for completing those long option names. But I still think the --showcpuutilization option needs to be renamed (at least adding a couple of dashes in it).

Thanks,
Namhyung

Namhyung Kim (3):
perf tools: Check existence of _get_comp_words_by_ref when bash completing perf tools: Complete long option names of perf command
perf tools: Long option completion support for each subcommands

 tools/perf/bash_completion      |   36 +++++++++++++++++++++++++++++-------
 tools/perf/util/parse-options.c |    8 ++++++++
 tools/perf/util/parse-options.h |    1 +
 3 files changed, 38 insertions(+), 7 deletions(-)
Signature · 1 line
1.7.9.2
author maintainer
#1 lore raw

PATCH 1/3 perf tools: Check existence of _get_comp_words_by_ref when bash completing

The '_get_comp_words_by_ref' function is available from the bash completion v1.2 so that earlier version emits following warning:

  $ perf re<TAB>_get_comp_words_by_ref: command not found

Use older '_get_cword' method when the above function doesn't exist.

Cc Frederic Weisbecker <[email protected]>
Cc David Ahern <[email protected]>
---
 tools/perf/bash_completion |   15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)
tools/perf/bash_completion +13 -2
index 1958fa539d0f..3d48cee1b5e5 100644
@@ -1,12 +1,23 @@
 # perf completion
 
+function_exists()
+{
+	declare -F $1 > /dev/null
+	return $?
+}
+
 have perf &&
 _perf()
 {
-	local cur cmd
+	local cur prev cmd
 
 	COMPREPLY=()
-	_get_comp_words_by_ref cur prev
+	if function_exists _get_comp_words_by_ref; then
+		_get_comp_words_by_ref cur prev
+	else
+		cur=$(_get_cword)
+		prev=${COMP_WORDS[COMP_CWORD-1]}
+	fi
 
 	cmd=${COMP_WORDS[0]}
 
Signature · 1 line
1.7.9.2
author maintainer
#8 lore raw

PATCH 3/3 perf tools: Long option completion support for each subcommands

In reply to Namhyung Kim

Add internal --list-opts option to print all of long option names to stdout so that it can be used for bash completion engine.

Cc Frederic Weisbecker <[email protected]>
Cc David Ahern <[email protected]>
---
 tools/perf/bash_completion      |    9 +++++++--
 tools/perf/util/parse-options.c |    8 ++++++++
 tools/perf/util/parse-options.h |    1 +
 3 files changed, 16 insertions(+), 2 deletions(-)
tools/perf/bash_completion +7 -2
index bef06f0deeb5..5c355ababf80 100644
@@ -33,8 +33,13 @@ _perf()
 		fi
 	# List possible events for -e option
 	elif [[ $prev == "-e" && "${COMP_WORDS[1]}" == @(record|stat|top) ]]; then
-		cmds=$($cmd list --raw-dump)
-		COMPREPLY=( $( compgen -W '$cmds' -- "$cur" ) )
+		evts=$($cmd list --raw-dump)
+		COMPREPLY=( $( compgen -W '$evts' -- "$cur" ) )
+	# List long option names
+	elif [[ $cur == --* ]];  then
+		subcmd=${COMP_WORDS[1]}
+		opts=$($cmd $subcmd --list-opts)
+		COMPREPLY=( $( compgen -W '$opts' -- "$cur" ) )
 	# Fall down to list regular files
 	else
 		_filedir
tools/perf/util/parse-options.c +8 -0
index 443fc116512b..2bc9e70df7e2 100644
@@ -384,6 +384,8 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
 			return usage_with_options_internal(usagestr, options, 1);
 		if (internal_help && !strcmp(arg + 2, "help"))
 			return parse_options_usage(usagestr, options);
+		if (!strcmp(arg + 2, "list-opts"))
+			return PARSE_OPT_LIST;
 		switch (parse_long_opt(ctx, arg + 2, options)) {
 		case -1:
 			return parse_options_usage(usagestr, options);
@@ -422,6 +424,12 @@ int parse_options(int argc, const char **argv, const struct option *options,
 		exit(129);
 	case PARSE_OPT_DONE:
 		break;
+	case PARSE_OPT_LIST:
+		while (options->type != OPTION_END) {
+			printf("--%s ", options->long_name);
+			options++;
+		}
+		exit(130);
 	default: /* PARSE_OPT_UNKNOWN */
 		if (ctx.argv[0][1] == '-') {
 			error("unknown option `%s'", ctx.argv[0] + 2);
tools/perf/util/parse-options.h +1 -0
index abc31a1dac1a..7bb5999940ca 100644
@@ -140,6 +140,7 @@ extern NORETURN void usage_with_options(const char * const *usagestr,
 enum {
 	PARSE_OPT_HELP = -1,
 	PARSE_OPT_DONE,
+	PARSE_OPT_LIST,
 	PARSE_OPT_UNKNOWN,
 };
 
Signature · 1 line
1.7.9.2
author maintainer
#6 lore raw

PATCH 2/3 perf tools: Complete long option names of perf command

In reply to Namhyung Kim

The main perf binary can receive a number of options that configure working environment. Add them to the completion script.

Cc Frederic Weisbecker <[email protected]>
Cc David Ahern <[email protected]>
---
 tools/perf/bash_completion |   12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
tools/perf/bash_completion +9 -3
index 3d48cee1b5e5..bef06f0deeb5 100644
@@ -21,10 +21,16 @@ _perf()
 
 	cmd=${COMP_WORDS[0]}
 
-	# List perf subcommands
+	# List perf subcommands or long options
 	if [ $COMP_CWORD -eq 1 ]; then
-		cmds=$($cmd --list-cmds)
-		COMPREPLY=( $( compgen -W '$cmds' -- "$cur" ) )
+		if [[ $cur == --* ]]; then
+			COMPREPLY=( $( compgen -W '--help --version \
+			--exec-path --html-path --paginate --no-pager \
+			--perf-dir --work-tree --debugfs-dir' -- "$cur" ) )
+		else
+			cmds=$($cmd --list-cmds)
+			COMPREPLY=( $( compgen -W '$cmds' -- "$cur" ) )
+		fi
 	# List possible events for -e option
 	elif [[ $prev == "-e" && "${COMP_WORDS[1]}" == @(record|stat|top) ]]; then
 		cmds=$($cmd list --raw-dump)
Signature · 1 line
1.7.9.2
#2 lore raw

Re: [PATCH 1/3] perf tools: Check existence of _get_comp_words_by_ref when bash completing

In reply to Namhyung Kim
Quoted 6 lines · Namhyung Kim

The '_get_comp_words_by_ref' function is available from the bash completion v1.2 so that earlier version emits following warning:

  $ perf re<TAB>_get_comp_words_by_ref: command not found

Use older '_get_cword' method when the above function doesn't exist.

May be only use _get_cword then, if it works everywhere?

Quoted 41 lines
Cc Frederic Weisbecker <[email protected]>
Cc David Ahern <[email protected]>
---
 tools/perf/bash_completion |   15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)
tools/perf/bash_completion +13 -2
index 1958fa539d0f..3d48cee1b5e5 100644
@@ -1,12 +1,23 @@
 # perf completion
 
+function_exists()
+{
+	declare -F $1 > /dev/null
+	return $?
+}
+
 have perf &&
 _perf()
 {
-	local cur cmd
+	local cur prev cmd
 
 	COMPREPLY=()
-	_get_comp_words_by_ref cur prev
+	if function_exists _get_comp_words_by_ref; then
+		_get_comp_words_by_ref cur prev
+	else
+		cur=$(_get_cword)
+		prev=${COMP_WORDS[COMP_CWORD-1]}
+	fi
 
 	cmd=${COMP_WORDS[0]}
 
Signature · 1 line
1.7.9.2
Acked-by on the v1 series
#10 lore raw
In reply to Namhyung Kim
Quoted 10 lines · Namhyung Kim

Hi,

This patchset improves bash completion support for perf tools. Some option names are really painful to type so here comes a support for completing those long option names. But I still think the --showcpuutilization option needs to be renamed (at least adding a couple of dashes in it).

Thanks,
Namhyung

Thanks Namhyung!

Quoted 15 lines

Namhyung Kim (3):
perf tools: Check existence of _get_comp_words_by_ref when bash completing perf tools: Complete long option names of perf command
perf tools: Long option completion support for each subcommands

 tools/perf/bash_completion      |   36 +++++++++++++++++++++++++++++-------
 tools/perf/util/parse-options.c |    8 ++++++++
 tools/perf/util/parse-options.h |    1 +
 3 files changed, 38 insertions(+), 7 deletions(-)
Signature · 1 line
1.7.9.2
author maintainer
#3 lore raw

Re: [PATCH 1/3] perf tools: Check existence of _get_comp_words_by_ref when bash completing

In reply to Frederic Weisbecker

Hi Frederic,

Quoted 9 lines · Frederic Weisbecker
Quoted 6 lines · Namhyung Kim

The '_get_comp_words_by_ref' function is available from the bash completion v1.2 so that earlier version emits following warning:

  $ perf re<TAB>_get_comp_words_by_ref: command not found

Use older '_get_cword' method when the above function doesn't exist.

May be only use _get_cword then, if it works everywhere?

It'll work but it's deprecated.

Thanks,
Namhyung

#4 lore raw

Re: [PATCH 1/3] perf tools: Check existence of _get_comp_words_by_ref when bash completing

Quoted 7 lines · Namhyung Kim

Hi Frederic,

Quoted 4 lines · Frederic Weisbecker
Quoted 3 lines · Namhyung Kim

The '_get_comp_words_by_ref' function is available from the bash completion v1.2 so that earlier version emits following warning:

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
  $ perf re<TAB>_get_comp_words_by_ref: command not found

Use older '_get_cword' method when the above function doesn't exist.

May be only use _get_cword then, if it works everywhere?

It'll work but it's deprecated.

Ok.

author maintainer
#5 lore raw

[tip:perf/urgent] perf tools: Check existence of _get_comp_words_by_ref when bash completing

In reply to Namhyung Kim
Commit-ID:  a1d668c3ffd38d611f8446615e8f797dcfdfc397
Gitweb:     http://git.kernel.org/tip/a1d668c3ffd38d611f8446615e8f797dcfdfc397
Author:     Namhyung Kim <[email protected]>
AuthorDate: Wed, 3 Oct 2012 00:21:32 +0900
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Tue, 2 Oct 2012 18:36:42 -0300

perf tools: Check existence of _get_comp_words_by_ref when bash completing

The '_get_comp_words_by_ref' function is available from the bash completion v1.2 so that earlier version emits following warning:

  $ perf re<TAB>_get_comp_words_by_ref: command not found

Use older '_get_cword' method when the above function doesn't exist.

---
 tools/perf/bash_completion |   15 +++++++++++++--
 1 files changed, 13 insertions(+), 2 deletions(-)
tools/perf/bash_completion +13 -2
index 1958fa5..3d48cee 100644
@@ -1,12 +1,23 @@
 # perf completion
 
+function_exists()
+{
+	declare -F $1 > /dev/null
+	return $?
+}
+
 have perf &&
 _perf()
 {
-	local cur cmd
+	local cur prev cmd
 
 	COMPREPLY=()
-	_get_comp_words_by_ref cur prev
+	if function_exists _get_comp_words_by_ref; then
+		_get_comp_words_by_ref cur prev
+	else
+		cur=$(_get_cword)
+		prev=${COMP_WORDS[COMP_CWORD-1]}
+	fi
 
 	cmd=${COMP_WORDS[0]}
 
author maintainer
#7 lore raw

[tip:perf/urgent] perf tools: Complete long option names of perf command

In reply to Namhyung Kim
Commit-ID:  35c2fde1155cc7225361edf43d8efd0aabd28a0c
Gitweb:     http://git.kernel.org/tip/35c2fde1155cc7225361edf43d8efd0aabd28a0c
Author:     Namhyung Kim <[email protected]>
AuthorDate: Wed, 3 Oct 2012 00:21:33 +0900
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Tue, 2 Oct 2012 18:36:43 -0300

perf tools: Complete long option names of perf command

The main perf binary can receive a number of options that configure working environment. Add them to the completion script.

---
 tools/perf/bash_completion |   12 +++++++++---
 1 files changed, 9 insertions(+), 3 deletions(-)
tools/perf/bash_completion +9 -3
index 3d48cee..bef06f0 100644
@@ -21,10 +21,16 @@ _perf()
 
 	cmd=${COMP_WORDS[0]}
 
-	# List perf subcommands
+	# List perf subcommands or long options
 	if [ $COMP_CWORD -eq 1 ]; then
-		cmds=$($cmd --list-cmds)
-		COMPREPLY=( $( compgen -W '$cmds' -- "$cur" ) )
+		if [[ $cur == --* ]]; then
+			COMPREPLY=( $( compgen -W '--help --version \
+			--exec-path --html-path --paginate --no-pager \
+			--perf-dir --work-tree --debugfs-dir' -- "$cur" ) )
+		else
+			cmds=$($cmd --list-cmds)
+			COMPREPLY=( $( compgen -W '$cmds' -- "$cur" ) )
+		fi
 	# List possible events for -e option
 	elif [[ $prev == "-e" && "${COMP_WORDS[1]}" == @(record|stat|top) ]]; then
 		cmds=$($cmd list --raw-dump)
author maintainer
#9 lore raw

[tip:perf/urgent] perf tools: Long option completion support for each subcommands

In reply to Namhyung Kim
Commit-ID:  4d8061faca7a50010f037374410f0c3647c3ecf8
Gitweb:     http://git.kernel.org/tip/4d8061faca7a50010f037374410f0c3647c3ecf8
Author:     Namhyung Kim <[email protected]>
AuthorDate: Wed, 3 Oct 2012 00:21:34 +0900
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Tue, 2 Oct 2012 18:36:44 -0300

perf tools: Long option completion support for each subcommands

Add internal --list-opts option to print all of long option names to stdout so that it can be used for bash completion engine.

---
 tools/perf/bash_completion      |    9 +++++++--
 tools/perf/util/parse-options.c |    8 ++++++++
 tools/perf/util/parse-options.h |    1 +
 3 files changed, 16 insertions(+), 2 deletions(-)
tools/perf/bash_completion +7 -2
index bef06f0..5c355ab 100644
@@ -33,8 +33,13 @@ _perf()
 		fi
 	# List possible events for -e option
 	elif [[ $prev == "-e" && "${COMP_WORDS[1]}" == @(record|stat|top) ]]; then
-		cmds=$($cmd list --raw-dump)
-		COMPREPLY=( $( compgen -W '$cmds' -- "$cur" ) )
+		evts=$($cmd list --raw-dump)
+		COMPREPLY=( $( compgen -W '$evts' -- "$cur" ) )
+	# List long option names
+	elif [[ $cur == --* ]];  then
+		subcmd=${COMP_WORDS[1]}
+		opts=$($cmd $subcmd --list-opts)
+		COMPREPLY=( $( compgen -W '$opts' -- "$cur" ) )
 	# Fall down to list regular files
 	else
 		_filedir
tools/perf/util/parse-options.c +8 -0
index 443fc11..2bc9e70 100644
@@ -384,6 +384,8 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
 			return usage_with_options_internal(usagestr, options, 1);
 		if (internal_help && !strcmp(arg + 2, "help"))
 			return parse_options_usage(usagestr, options);
+		if (!strcmp(arg + 2, "list-opts"))
+			return PARSE_OPT_LIST;
 		switch (parse_long_opt(ctx, arg + 2, options)) {
 		case -1:
 			return parse_options_usage(usagestr, options);
@@ -422,6 +424,12 @@ int parse_options(int argc, const char **argv, const struct option *options,
 		exit(129);
 	case PARSE_OPT_DONE:
 		break;
+	case PARSE_OPT_LIST:
+		while (options->type != OPTION_END) {
+			printf("--%s ", options->long_name);
+			options++;
+		}
+		exit(130);
 	default: /* PARSE_OPT_UNKNOWN */
 		if (ctx.argv[0][1] == '-') {
 			error("unknown option `%s'", ctx.argv[0] + 2);
tools/perf/util/parse-options.h +1 -0
index abc31a1..7bb5999 100644
@@ -140,6 +140,7 @@ extern NORETURN void usage_with_options(const char * const *usagestr,
 enum {
 	PARSE_OPT_HELP = -1,
 	PARSE_OPT_DONE,
+	PARSE_OPT_LIST,
 	PARSE_OPT_UNKNOWN,
 };