Correct the bash completion of CLI

Currently, the "neutron help" command in CLI doesn't expose the
"bash-completion" command to users, but actually, the "neutron
bash-completion" command is available. Additionally, there is a "complete"
command in outputs of "neutron help", but this command will prints a wrong
result. This patch adds the "bash-completion" command to commands list of
neutron CLI and removes the "complete" command.

Change-Id: I0f2ec22da7ba36f79197acb41d0621fc726cc0f3
Closes-Bug: #1340647
This commit is contained in:
liu-sheng
2014-07-16 11:06:50 +08:00
parent 19de15dd60
commit 187c36c19b
2 changed files with 33 additions and 1 deletions

View File

@@ -38,6 +38,7 @@ from cliff import app
from cliff import commandmanager
from neutronclient.common import clientmanager
from neutronclient.common import command as openstack_command
from neutronclient.common import exceptions as exc
from neutronclient.common import utils
from neutronclient.i18n import _
@@ -116,7 +117,12 @@ def check_non_negative_int(value):
return value
class BashCompletionCommand(openstack_command.OpenStackCommand):
"""Prints all of the commands and options for bash-completion."""
resource = "bash_completion"
COMMAND_V2 = {
'bash-completion': BashCompletionCommand,
'net-list': network.ListNetwork,
'net-external-list': network.ListExternalNetwork,
'net-show': network.ShowNetwork,
@@ -345,6 +351,9 @@ class NeutronShell(app.App):
for k, v in self.commands[apiversion].items():
self.command_manager.add_command(k, v)
# Pop the 'complete' to correct the outputs of 'neutron help'.
self.command_manager.commands.pop('complete')
# This is instantiated in initialize_app() only when using
# password flow auth
self.auth_client = None
@@ -644,7 +653,7 @@ class NeutronShell(app.App):
help_pos = -1
help_command_pos = -1
for arg in argv:
if arg == 'bash-completion':
if arg == 'bash-completion' and help_command_pos == -1:
self._bash_completion()
return 0
if arg in self.commands[self.api_version]:

View File

@@ -127,6 +127,29 @@ class ShellTest(testtools.TestCase):
matchers.MatchesRegex(required))
self.assertFalse(stderr)
def test_bash_completion_in_outputs_of_help_command(self):
help_text, stderr = self.shell('help')
self.assertFalse(stderr)
completion_cmd = "bash-completion"
completion_help_str = ("Prints all of the commands and options "
"for bash-completion.")
self.assertIn(completion_cmd, help_text)
self.assertIn(completion_help_str, help_text)
def test_bash_completion_command(self):
# just check we have some output
required = [
'.*--tenant_id',
'.*--client-certificate',
'.*help',
'.*gateway-device-create',
'.*--dns-nameserver']
help_text, stderr = self.shell('neutron bash-completion')
self.assertFalse(stderr)
for r in required:
self.assertThat(help_text,
matchers.MatchesRegex(r, re.DOTALL | re.MULTILINE))
def test_unknown_auth_strategy(self):
self.useFixture(fixtures.FakeLogger(level=logging.DEBUG))
stdout, stderr = self.shell('--os-auth-strategy fake quota-list')