diff --git a/keystoneclient/shell.py b/keystoneclient/shell.py index b1752e233..ef5c41883 100644 --- a/keystoneclient/shell.py +++ b/keystoneclient/shell.py @@ -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='', nargs='?', help='Display help for ') def do_help(self, args): diff --git a/tools/keystone.bash_completion b/tools/keystone.bash_completion new file mode 100644 index 000000000..d18668b99 --- /dev/null +++ b/tools/keystone.bash_completion @@ -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