Merge "Correct completion in interactive mode"

This commit is contained in:
Jenkins
2015-03-04 02:59:54 +00:00
committed by Gerrit Code Review
2 changed files with 87 additions and 9 deletions

View File

@@ -52,17 +52,33 @@ class InteractiveApp(cmd2.Cmd):
line_parts = shlex.split(line.parsed.raw)
self.parent_app.run_subcommand(line_parts)
def completedefault(self, text, line, begidx, endidx):
# Tab-completion for commands known to the command manager.
# Does not handle options on the commands.
if not text:
completions = sorted(n for n, v in self.command_manager)
else:
completions = sorted(n for n, v in self.command_manager
if n.startswith(text)
)
def completenames(self, text, *ignored):
"""Tab-completion for command prefix without completer delimiter.
This method returns cmd style and cliff style commands matching
provided command prefix (text).
"""
completions = cmd2.Cmd.completenames(self, text, *ignored)
completions += self._complete_prefix(text)
return completions
def completedefault(self, text, line, begidx, endidx):
"""Default tab-completion for command prefix with completer delimiter.
This method filters only cliff style commands matching provided
command prefix (line) as cmd2 style commands cannot contain spaces.
This method returns text + missing command part of matching commands.
This method does not handle options in cmd2/cliff style commands, you
must define complete_$method to handle them.
"""
return [x[begidx:] for x in self._complete_prefix(line)]
def _complete_prefix(self, prefix):
"""Returns cliff style commands with a specific prefix."""
if not prefix:
return [n for n, v in self.command_manager]
return [n for n, v in self.command_manager if n.startswith(prefix)]
def help_help(self):
# Use the command manager to get instructions for "help"
self.default('help help')

View File

@@ -0,0 +1,62 @@
# -*- encoding: utf-8 -*-
from cliff.interactive import InteractiveApp
class FakeApp(object):
NAME = 'Fake'
def make_interactive_app(*command_names):
fake_command_manager = [(x, None) for x in command_names]
return InteractiveApp(FakeApp, fake_command_manager,
stdin=None, stdout=None)
def _test_completenames(expecteds, prefix):
app = make_interactive_app('hips', 'hippo', 'nonmatching')
assert set(app.completenames(prefix)) == set(expecteds)
def test_cmd2_completenames():
# cmd2.Cmd define do_help method
_test_completenames(['help'], 'he')
def test_cliff_completenames():
_test_completenames(['hips', 'hippo'], 'hip')
def test_no_completenames():
_test_completenames([], 'taz')
def test_both_completenames():
# cmd2.Cmd define do_hi and do_history methods
_test_completenames(['hi', 'history', 'hips', 'hippo'], 'hi')
def _test_completedefault(expecteds, line, begidx):
command_names = set(['show file', 'show folder', 'show long', 'list all'])
app = make_interactive_app(*command_names)
observeds = app.completedefault(None, line, begidx, None)
assert set(observeds) == set(expecteds)
assert set([line[:begidx] + x for x in observeds]) <= command_names
def test_empty_text_completedefault():
# line = 'show ' + begidx = 5 implies text = ''
_test_completedefault(['file', 'folder', ' long'], 'show ', 5)
def test_nonempty_text_completedefault2():
# line = 'show f' + begidx = 6 implies text = 'f'
_test_completedefault(['file', 'folder'], 'show f', 5)
def test_long_completedefault():
_test_completedefault(['long'], 'show ', 6)
def test_no_completedefault():
_test_completedefault([], 'taz ', 4)