From 1155530941ddeb1b948939fa400ba722ed5ccd0f Mon Sep 17 00:00:00 2001 From: Ramakrishnan G Date: Sun, 22 Jun 2014 23:31:50 +0530 Subject: [PATCH] Add bash completion support for ironic cli This commit adds the 'bash-completion' option to ironic cli which prints all sub-commands and options, and bash_completion scripts for bash auto completion. Change-Id: Ide5b29e262b4217046f124d86902cd0abfdab1b6 Closes-Bug: 1332408 --- doc/source/cli.rst | 10 ++++++++++ ironicclient/shell.py | 13 +++++++++++++ ironicclient/tests/test_shell.py | 13 +++++++++++++ tools/ironic.bash_completion | 27 +++++++++++++++++++++++++++ 4 files changed, 63 insertions(+) create mode 100644 tools/ironic.bash_completion diff --git a/doc/source/cli.rst b/doc/source/cli.rst index 40b353768..b0a7ee675 100644 --- a/doc/source/cli.rst +++ b/doc/source/cli.rst @@ -42,6 +42,16 @@ supplying an auth token using :option:`--ironic-url` and export IRONIC_URL=http://ironic.example.org:6385/ export OS_AUTH_TOKEN=3bcc3d3a03f44e3d8377f9247b0ad155 +Ironic cli supports bash completion. The command-line tool can automatically +fill partially typed commands. To use this feature, source the below file to +your terminal and then bash completion should work:: + + source tools/ironic.bash_completion + +To avoid doing this everytime, add this to your ``.bashrc`` or copy the +ironic.bash_completion file to the default bash completion scripts directory +on your linux distribution. + OPTIONS ======= diff --git a/ironicclient/shell.py b/ironicclient/shell.py index de4573338..1e6f18906 100644 --- a/ironicclient/shell.py +++ b/ironicclient/shell.py @@ -195,6 +195,16 @@ class IronicShell(object): format="%(levelname)s %(message)s", level=logging.CRITICAL) + def _bash_completion(self): + """Prints all of the commands and options for bash-completion.""" + 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) + print(' '.join(commands | options)) + def main(self, argv): # Parse args once to find version parser = self.get_base_parser() @@ -211,6 +221,9 @@ class IronicShell(object): if options.help or not argv: self.do_help(options) return 0 + elif argv[0] == 'bash-completion': + self._bash_completion() + return 0 # Parse args again and call whatever callback was selected args = subcommand_parser.parse_args(argv) diff --git a/ironicclient/tests/test_shell.py b/ironicclient/tests/test_shell.py index 80881e9f8..2f111b42f 100644 --- a/ironicclient/tests/test_shell.py +++ b/ironicclient/tests/test_shell.py @@ -93,3 +93,16 @@ class ShellTest(utils.BaseTestCase): def test_auth_param(self): self.make_env(exclude='OS_USERNAME') self.test_help() + + def test_bash_completion(self): + stdout = self.shell('bash-completion') + # just check we have some output + required = [ + '.*--driver_info', + '.*--chassis_uuid', + '.*help', + '.*node-create', + '.*chassis-create'] + for r in required: + self.assertThat(stdout, + matchers.MatchesRegex(r, self.re_options)) diff --git a/tools/ironic.bash_completion b/tools/ironic.bash_completion new file mode 100644 index 000000000..fbc32f2f0 --- /dev/null +++ b/tools/ironic.bash_completion @@ -0,0 +1,27 @@ +_ironic_opts="" # lazy init +_ironic_flags="" # lazy init +_ironic_opts_exp="" # lazy init +_ironic() +{ + local cur prev nbc cflags + COMPREPLY=() + cur="${COMP_WORDS[COMP_CWORD]}" + prev="${COMP_WORDS[COMP_CWORD-1]}" + + if [ "x$_ironic_opts" == "x" ] ; then + nbc="`ironic bash-completion | sed -e "s/ *-h */ /" -e "s/ *-i */ /"`" + _ironic_opts="`echo "$nbc" | sed -e "s/--[a-z0-9_-]*//g" -e "s/ */ /g"`" + _ironic_flags="`echo " $nbc" | sed -e "s/ [^-][^-][a-z0-9_-]*//g" -e "s/ */ /g"`" + _ironic_opts_exp="`echo "$_ironic_opts" | tr ' ' '|'`" + fi + + if [[ " ${COMP_WORDS[@]} " =~ " "($_ironic_opts_exp)" " && "$prev" != "help" ]] ; then + COMPLETION_CACHE=$HOME/.cache/python-ironicclient/*/*-cache + cflags="$_ironic_flags "$(cat $COMPLETION_CACHE 2> /dev/null | tr '\n' ' ') + COMPREPLY=($(compgen -W "${cflags}" -- ${cur})) + else + COMPREPLY=($(compgen -W "${_ironic_opts}" -- ${cur})) + fi + return 0 +} +complete -F _ironic ironic