Supports bash_completion for ceilometerclient

bash_completion feature can improve CLI user experience, projects like
nova, keystone, and cinder already support it.

NOTE: this patch just provides simple functionality, which means cache
for IDs and names is not used (like nova).

Change-Id: I1b04998a5e7c8818aed4b51f5fe5bf01efd0e14a
Closes-Bug: #1260939
This commit is contained in:
ZhiQiang Fan
2013-12-23 19:17:13 +08:00
parent d663570b77
commit 5b19eae864
2 changed files with 56 additions and 0 deletions

View File

@@ -186,9 +186,19 @@ class CeilometerShell(object):
submodule = utils.import_versioned_module(version, 'shell')
self._find_actions(subparsers, submodule)
self._find_actions(subparsers, self)
self._add_bash_completion_subparser(subparsers)
return parser
def _add_bash_completion_subparser(self, subparsers):
subparser = subparsers.add_parser(
'bash_completion',
add_help=False,
formatter_class=HelpFormatter
)
self.subcommands['bash_completion'] = subparser
subparser.set_defaults(func=self.do_bash_completion)
def _find_actions(self, subparsers, actions_module):
for attr in (a for a in dir(actions_module) if a.startswith('do_')):
# I prefer to be hypen-separated instead of underscores.
@@ -241,6 +251,9 @@ class CeilometerShell(object):
if args.func == self.do_help:
self.do_help(args)
return 0
elif args.func == self.do_bash_completion:
self.do_bash_completion(args)
return 0
if not (args.os_auth_token and args.ceilometer_url):
if not args.os_username:
@@ -270,6 +283,22 @@ class CeilometerShell(object):
except exc.Unauthorized:
raise exc.CommandError("Invalid OpenStack Identity credentials.")
def do_bash_completion(self, args):
"""Prints all of the commands and options to stdout.
The ceilometer.bash_completion script doesn't have to hard code them.
"""
commands = set()
options = set()
for sc_str, sc in self.subcommands.items():
commands.add(sc_str)
for option in list(sc._optionals._option_string_actions):
options.add(option)
commands.remove('bash-completion')
commands.remove('bash_completion')
print(' '.join(commands | options))
@utils.arg('command', metavar='<subcommand>', nargs='?',
help='Display help for <subcommand>')
def do_help(self, args):

View File

@@ -0,0 +1,27 @@
# bash completion for openstack ceilometer
_ceilometer_opts="" # lazy init
_ceilometer_flags="" # lazy init
_ceilometer_opts_exp="" # lazy init
_ceilometer()
{
local cur prev kbc
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
if [ "x$_ceilometer_opts" == "x" ] ; then
kbc="`ceilometer bash-completion | sed -e "s/ -h / /"`"
_ceilometer_opts="`echo "$kbc" | sed -e "s/--[a-z0-9_-]*//g" -e "s/[ ][ ]*/ /g"`"
_ceilometer_flags="`echo " $kbc" | sed -e "s/ [^-][^-][a-z0-9_-]*//g" -e "s/[ ][ ]*/ /g"`"
_ceilometer_opts_exp="`echo $_ceilometer_opts | sed -e "s/[ ]/|/g"`"
fi
if [[ " ${COMP_WORDS[@]} " =~ " "($_ceilometer_opts_exp)" " && "$prev" != "help" ]] ; then
COMPREPLY=($(compgen -W "${_ceilometer_flags}" -- ${cur}))
else
COMPREPLY=($(compgen -W "${_ceilometer_opts}" -- ${cur}))
fi
return 0
}
complete -F _ceilometer ceilometer