diff options
author | ZhiQiang Fan <aji.zqfan@gmail.com> | 2013-12-14 16:33:29 +0800 |
---|---|---|
committer | ZhiQiang Fan <aji.zqfan@gmail.com> | 2013-12-16 23:04:24 +0800 |
commit | 992d6f5350952fc6501184b02c477134048952d6 (patch) | |
tree | 6270f4f48067aef01a06371d575d9ffa5d02f490 /heatclient/shell.py | |
parent | 619995a3b508ada07f9f454918598c3d9d3d4381 (diff) | |
download | python-heatclient-992d6f5350952fc6501184b02c477134048952d6.tar.gz |
Supports bash_completion for heatclient
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).
Closes-Bug: #1260939
Change-Id: I327e884e1c5907c9ff6f31131c70aee659cca58e
Diffstat (limited to 'heatclient/shell.py')
-rw-r--r-- | heatclient/shell.py | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/heatclient/shell.py b/heatclient/shell.py index 61e3c1a..7b3a40a 100644 --- a/heatclient/shell.py +++ b/heatclient/shell.py @@ -199,9 +199,19 @@ class HeatShell(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. @@ -299,6 +309,9 @@ class HeatShell(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_username and not args.os_auth_token: raise exc.CommandError("You must provide a username via" @@ -374,6 +387,22 @@ class HeatShell(object): args.func(client, args) + def do_bash_completion(self, args): + """Prints all of the commands and options to stdout. + + The heat.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): |