From dd9717efb9cbbd2f791057742e6f7323b317d46b Mon Sep 17 00:00:00 2001 From: Todd Leonhardt Date: Thu, 13 Jul 2017 12:35:47 -0400 Subject: [PATCH] Added support for case-insensitive tab-completion of cmd2 command names --- CHANGES.md | 2 ++ cmd2.py | 6 +++++- tests/test_completion.py | 22 ++++++++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index 919142b02..3006d60d0 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -11,6 +11,8 @@ News * +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 ----- diff --git a/cmd2.py b/cmd2.py index f76072fe2..4b4bc458f 100755 --- a/cmd2.py +++ b/cmd2.py @@ -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): diff --git a/tests/test_completion.py b/tests/test_completion.py index a219a9047..efc329861 100644 --- a/tests/test_completion.py +++ b/tests/test_completion.py @@ -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' @@ -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'