Added bash completion
Change-Id: I1637c8412c4068a8bd067f0366649f2bbda91052
This commit is contained in:
parent
ec946e1df3
commit
12736650dc
@ -17,6 +17,13 @@ variable::
|
|||||||
|
|
||||||
You'll find complete documentation on the shell by running ``surveil help``.
|
You'll find complete documentation on the shell by running ``surveil help``.
|
||||||
|
|
||||||
|
Bash completion
|
||||||
|
~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
Basic command tab completion can be enabled by sourcing the bash completion script::
|
||||||
|
|
||||||
|
source /usr/local/share/monasca.bash_completion
|
||||||
|
|
||||||
Python API
|
Python API
|
||||||
----------
|
----------
|
||||||
|
|
||||||
|
@ -7,6 +7,8 @@ description-file =
|
|||||||
[files]
|
[files]
|
||||||
packages =
|
packages =
|
||||||
surveilclient
|
surveilclient
|
||||||
|
data_files =
|
||||||
|
share = tools/surveil.bash_completion
|
||||||
|
|
||||||
[entry_points]
|
[entry_points]
|
||||||
console_scripts =
|
console_scripts =
|
||||||
|
@ -62,8 +62,18 @@ class SurveilShell(object):
|
|||||||
submodule = utils.import_versioned_module(version, 'shell')
|
submodule = utils.import_versioned_module(version, 'shell')
|
||||||
self._find_actions(subparsers, submodule)
|
self._find_actions(subparsers, submodule)
|
||||||
self._find_actions(subparsers, self)
|
self._find_actions(subparsers, self)
|
||||||
|
self._add_bash_completion_subparser(subparsers)
|
||||||
return parser
|
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):
|
def _find_actions(self, subparsers, actions_module):
|
||||||
for attr in (a for a in dir(actions_module) if a.startswith('do_')):
|
for attr in (a for a in dir(actions_module) if a.startswith('do_')):
|
||||||
# I prefer to be hyphen-separated instead of underscores.
|
# I prefer to be hyphen-separated instead of underscores.
|
||||||
@ -99,6 +109,22 @@ class SurveilShell(object):
|
|||||||
else:
|
else:
|
||||||
self.parser.print_help()
|
self.parser.print_help()
|
||||||
|
|
||||||
|
def do_bash_completion(self, args):
|
||||||
|
"""Prints all of the commands and options to stdout.
|
||||||
|
|
||||||
|
The surveil.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))
|
||||||
|
|
||||||
def main(self, argv):
|
def main(self, argv):
|
||||||
# Parse args once to find version
|
# Parse args once to find version
|
||||||
parser = self.get_base_parser()
|
parser = self.get_base_parser()
|
||||||
@ -122,6 +148,9 @@ class SurveilShell(object):
|
|||||||
if args.func == self.do_help:
|
if args.func == self.do_help:
|
||||||
self.do_help(args)
|
self.do_help(args)
|
||||||
return 0
|
return 0
|
||||||
|
elif args.func == self.do_bash_completion:
|
||||||
|
self.do_bash_completion(args)
|
||||||
|
return 0
|
||||||
|
|
||||||
if not args.surveil_api_url:
|
if not args.surveil_api_url:
|
||||||
raise exc.CommandError("you must specify a Surveil API URL"
|
raise exc.CommandError("you must specify a Surveil API URL"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user