summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xcmd2.py18
-rwxr-xr-xexamples/pirate.py4
-rw-r--r--tests/test_completion.py39
3 files changed, 50 insertions, 11 deletions
diff --git a/cmd2.py b/cmd2.py
index d357b02a..fe55cd93 100755
--- a/cmd2.py
+++ b/cmd2.py
@@ -1583,14 +1583,18 @@ class Cmd(cmd.Cmd):
break
i, n = 0, len(line)
- while i < n and line[i] in self.identchars:
- i += 1
- command, arg = line[:i], line[i:].strip()
- # Make sure there is a space between the command and args
- # This can occur when a character not in self.identchars bumps against the command (ex: help@)
- if len(command) > 0 and len(arg) > 0 and line[len(command)] != ' ':
- line = line.replace(command, command + ' ', 1)
+ # If we are allowing shell commands, then allow any character in the command
+ if self.default_to_shell:
+ while i < n and line[i] != ' ':
+ i += 1
+
+ # Otherwise only allow those in identchars
+ else:
+ while i < n and line[i] in self.identchars:
+ i += 1
+
+ command, arg = line[:i], line[i:].strip()
return command, arg, line
diff --git a/examples/pirate.py b/examples/pirate.py
index cfe545d6..f3a8fc7a 100755
--- a/examples/pirate.py
+++ b/examples/pirate.py
@@ -29,10 +29,6 @@ class Pirate(Cmd):
self.initial_gold = self.gold
self.prompt = 'arrr> '
- def default(self, line):
- """This handles unknown commands."""
- self.poutput('What mean ye by "{0}"?'.format(line))
-
def precmd(self, line):
"""Runs just before a command line is parsed, but after the prompt is presented."""
self.initial_gold = self.gold
diff --git a/tests/test_completion.py b/tests/test_completion.py
index a9f75dce..7f9a6db0 100644
--- a/tests/test_completion.py
+++ b/tests/test_completion.py
@@ -311,6 +311,45 @@ def test_path_completion_nomatch(request):
assert path_complete(text, line, begidx, endidx) == []
+
+def test_default_to_shell_completion(cmd2_app, request):
+ cmd2_app.default_to_shell = True
+ test_dir = os.path.dirname(request.module.__file__)
+
+ text = 'c'
+ path = os.path.join(test_dir, text)
+
+ if sys.platform == "win32":
+ command = 'calc.exe'
+ else:
+ command = 'egrep'
+
+ # Make sure the command is on the testing system
+ assert command in cmd2.Cmd._get_exes_in_path(command)
+ line = '{} {}'.format(command, path)
+
+ endidx = len(line)
+ begidx = endidx - len(text)
+ state = 0
+
+ def get_line():
+ return line
+
+ def get_begidx():
+ return begidx
+
+ def get_endidx():
+ return endidx
+
+ with mock.patch.object(readline, 'get_line_buffer', get_line):
+ with mock.patch.object(readline, 'get_begidx', get_begidx):
+ with mock.patch.object(readline, 'get_endidx', get_endidx):
+ # Run the readline tab-completion function with readline mocks in place
+ first_match = cmd2_app.complete(text, state)
+
+ assert first_match is not None and cmd2_app.completion_matches == ['conftest.py ']
+
+
def test_path_completion_cwd():
# Run path complete with no path and no search text
text = ''