diff options
| author | ZhiQiang Fan <zhiqiang.fan@huawei.com> | 2013-12-23 19:17:13 +0800 |
|---|---|---|
| committer | ZhiQiang Fan <zhiqiang.fan@huawei.com> | 2013-12-23 19:17:13 +0800 |
| commit | 5b19eae864c6a13247b6fcb72462d7828e2c5a60 (patch) | |
| tree | cb532dd3e9165b6a7d18dc535df0ae7ef4904e62 /ceilometerclient/shell.py | |
| parent | d663570b775bf429c9f6b183d1e7809e4d4a1b2d (diff) | |
| download | python-ceilometerclient-5b19eae864c6a13247b6fcb72462d7828e2c5a60.tar.gz | |
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
Diffstat (limited to 'ceilometerclient/shell.py')
| -rw-r--r-- | ceilometerclient/shell.py | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/ceilometerclient/shell.py b/ceilometerclient/shell.py index 8b8b0c9..57cb604 100644 --- a/ceilometerclient/shell.py +++ b/ceilometerclient/shell.py @@ -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): |
