add keystone bash-completion

Change-Id: I84d3897fc056d411fdaaee301465c72e20a66ff8
This commit is contained in:
Dominik Heidler
2012-06-27 11:46:58 +02:00
parent abc7c47c18
commit c8aa2e06aa
2 changed files with 55 additions and 0 deletions

View File

@@ -180,9 +180,18 @@ class OpenStackIdentityShell(object):
self._find_actions(subparsers, actions_module)
self._find_actions(subparsers, shell_generic)
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=OpenStackHelpFormatter
)
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.
@@ -232,6 +241,9 @@ class OpenStackIdentityShell(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
#FIXME(usrleon): Here should be restrict for project id same as
# for username or apikey but for compatibility it is not.
@@ -320,6 +332,22 @@ class OpenStackIdentityShell(object):
except KeyError:
return shell_v2_0.CLIENT_CLASS
def do_bash_completion(self, args):
"""
Prints all of the commands and options to stdout.
The keystone.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 sc._optionals._option_string_actions.keys():
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 keystone
_keystone_opts="" # lazy init
_keystone_flags="" # lazy init
_keystone_opts_exp="" # lazy init
_keystone()
{
local cur prev kbc
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
if [ "x$_keystone_opts" == "x" ] ; then
kbc="`keystone bash-completion | sed -e "s/ -h / /"`"
_keystone_opts="`echo "$kbc" | sed -e "s/--[a-z0-9_-]*//g" -e "s/[ ][ ]*/ /g"`"
_keystone_flags="`echo " $kbc" | sed -e "s/ [^-][^-][a-z0-9_-]*//g" -e "s/[ ][ ]*/ /g"`"
_keystone_opts_exp="`echo $_keystone_opts | sed -e "s/[ ]/|/g"`"
fi
if [[ " ${COMP_WORDS[@]} " =~ " "($_keystone_opts_exp)" " && "$prev" != "help" ]] ; then
COMPREPLY=($(compgen -W "${_keystone_flags}" -- ${cur}))
else
COMPREPLY=($(compgen -W "${_keystone_opts}" -- ${cur}))
fi
return 0
}
complete -F _keystone keystone