Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ News
* <Ctrl>+D now properly quits when case-sensitive command parsing is enabled
* Fixed some pyperclip clipboard interaction bugs on Linux
* Fixed some timing bugs when running unit tests in parallel by using monkeypatch
* Enhancements
* Enhanced tab-completion of cmd2 command names to support case-insensitive completion

0.7.5
-----
Expand Down
6 changes: 5 additions & 1 deletion cmd2.py
Original file line number Diff line number Diff line change
Expand Up @@ -582,8 +582,12 @@ def colorize(self, val, color):
# noinspection PyMethodOverriding
def completenames(self, text, line, begidx, endidx):
"""Override of cmd2 method which completes command names both for command completion and help."""
command = text
if self.case_insensitive:
command = text.lower()

# Call super class method. Need to do it this way for Python 2 and 3 compatibility
cmd_completion = cmd.Cmd.completenames(self, text)
cmd_completion = cmd.Cmd.completenames(self, command)

# If we are completing the initial command name and get exactly 1 result and are at end of line, add a space
if begidx == 0 and len(cmd_completion) == 1 and endidx == len(line):
Expand Down
22 changes: 22 additions & 0 deletions tests/test_completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ def cmd2_app():
c = cmd2.Cmd()
return c

@pytest.fixture
def cs_app():
cmd2.Cmd.case_insensitive = False
c = cmd2.Cmd()
return c


def test_cmd2_command_completion_single_end(cmd2_app):
text = 'he'
Expand All @@ -29,6 +35,22 @@ def test_cmd2_command_completion_single_end(cmd2_app):
# It is at end of line, so extra space is present
assert cmd2_app.completenames(text, line, begidx, endidx) == ['help ']

def test_cmd2_command_completion_is_case_insensitive_by_default(cmd2_app):
text = 'HE'
line = 'HE'
endidx = len(line)
begidx = endidx - len(text)
# It is at end of line, so extra space is present
assert cmd2_app.completenames(text, line, begidx, endidx) == ['help ']

def test_cmd2_case_sensitive_command_completion(cs_app):
text = 'HE'
line = 'HE'
endidx = len(line)
begidx = endidx - len(text)
# It is at end of line, so extra space is present
assert cs_app.completenames(text, line, begidx, endidx) == []

def test_cmd2_command_completion_single_mid(cmd2_app):
text = 'he'
line = 'he'
Expand Down