Skip to content

Commit 04340d1

Browse files
authored
Merge pull request #191 from python-cmd2/insensitive_completion
Added support for case-insensitive tab-completion of cmd2 command names
2 parents 30a3b66 + dd9717e commit 04340d1

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ News
1111
* <Ctrl>+D now properly quits when case-sensitive command parsing is enabled
1212
* Fixed some pyperclip clipboard interaction bugs on Linux
1313
* Fixed some timing bugs when running unit tests in parallel by using monkeypatch
14+
* Enhancements
15+
* Enhanced tab-completion of cmd2 command names to support case-insensitive completion
1416

1517
0.7.5
1618
-----

cmd2.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -582,8 +582,12 @@ def colorize(self, val, color):
582582
# noinspection PyMethodOverriding
583583
def completenames(self, text, line, begidx, endidx):
584584
"""Override of cmd2 method which completes command names both for command completion and help."""
585+
command = text
586+
if self.case_insensitive:
587+
command = text.lower()
588+
585589
# Call super class method. Need to do it this way for Python 2 and 3 compatibility
586-
cmd_completion = cmd.Cmd.completenames(self, text)
590+
cmd_completion = cmd.Cmd.completenames(self, command)
587591

588592
# If we are completing the initial command name and get exactly 1 result and are at end of line, add a space
589593
if begidx == 0 and len(cmd_completion) == 1 and endidx == len(line):

tests/test_completion.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ def cmd2_app():
2020
c = cmd2.Cmd()
2121
return c
2222

23+
@pytest.fixture
24+
def cs_app():
25+
cmd2.Cmd.case_insensitive = False
26+
c = cmd2.Cmd()
27+
return c
28+
2329

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

38+
def test_cmd2_command_completion_is_case_insensitive_by_default(cmd2_app):
39+
text = 'HE'
40+
line = 'HE'
41+
endidx = len(line)
42+
begidx = endidx - len(text)
43+
# It is at end of line, so extra space is present
44+
assert cmd2_app.completenames(text, line, begidx, endidx) == ['help ']
45+
46+
def test_cmd2_case_sensitive_command_completion(cs_app):
47+
text = 'HE'
48+
line = 'HE'
49+
endidx = len(line)
50+
begidx = endidx - len(text)
51+
# It is at end of line, so extra space is present
52+
assert cs_app.completenames(text, line, begidx, endidx) == []
53+
3254
def test_cmd2_command_completion_single_mid(cmd2_app):
3355
text = 'he'
3456
line = 'he'

0 commit comments

Comments
 (0)