summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2018-03-19 12:52:13 -0400
committerKevin Van Brunt <kmvanbrunt@gmail.com>2018-03-19 12:52:13 -0400
commit07af2eeaf45b558d5c9ccfbc7008df0c67f8d333 (patch)
tree45abe804cc04daff8b36fdd1a7020572118c85a1
parent6b95022fa30d5f5d9ceb2ac5e35bab2c4c4096fd (diff)
downloadcmd2-git-07af2eeaf45b558d5c9ccfbc7008df0c67f8d333.tar.gz
Allowing quotes in help command
-rwxr-xr-xcmd2.py26
-rw-r--r--tests/test_completion.py9
2 files changed, 21 insertions, 14 deletions
diff --git a/cmd2.py b/cmd2.py
index 748c0512..52028798 100755
--- a/cmd2.py
+++ b/cmd2.py
@@ -1471,7 +1471,6 @@ class Cmd(cmd.Cmd):
"""
if state == 0:
- import readline
if readline_lib is not None:
# Set GNU readline's rl_completion_suppress_quote to 1 so it won't automatically add a closing quote
@@ -1571,6 +1570,9 @@ class Cmd(cmd.Cmd):
Override of parent class method to handle tab completing subcommands
"""
+ # The command is the token at index 1 in the command line
+ cmd_index = 1
+
# The subcommand is the token at index 2 in the command line
subcmd_index = 2
@@ -1584,22 +1586,28 @@ class Cmd(cmd.Cmd):
# Get the index of the token being completed
index = len(tokens) - 1
+ # Check if we are completing a command
+ if index == cmd_index:
+ completions = cmd.Cmd.complete_help(self, text, line, begidx, endidx)
+
# Check if we are completing a subcommand
- if index == subcmd_index:
+ elif index == subcmd_index:
# Match subcommands if any exist
- command = tokens[subcmd_index - 1]
+ command = tokens[cmd_index]
subcommands = self.get_subcommands(command)
if subcommands is not None:
completions = [cur_sub for cur_sub in subcommands if cur_sub.startswith(text)]
- # Run normal help completion from the parent class
- else:
- completions = cmd.Cmd.complete_help(self, text, line, begidx, endidx)
+ # Handle a single completion
+ if len(completions) == 1:
- # Check if we should add a space to the end of the line
- if len(completions) == 1 and not unclosed_quote and endidx == len(line) and \
- self.get_subcommands(completions[0]) is not None:
+ # Close the quote
+ if unclosed_quote:
+ completions[0] += unclosed_quote
+
+ # If we are at the end of the line, then add a space
+ if endidx == len(line):
completions[0] += ' '
completions.sort()
diff --git a/tests/test_completion.py b/tests/test_completion.py
index 210296db..95487641 100644
--- a/tests/test_completion.py
+++ b/tests/test_completion.py
@@ -171,8 +171,7 @@ def test_cmd2_help_completion_single_end(cmd2_app):
line = 'help he'
endidx = len(line)
begidx = endidx - len(text)
- # Even though it is at end of line, no extra space is present when tab completing a command name to get help on
- assert cmd2_app.complete_help(text, line, begidx, endidx) == ['help']
+ assert cmd2_app.complete_help(text, line, begidx, endidx) == ['help ']
def test_cmd2_help_completion_single_mid(cmd2_app):
text = 'he'
@@ -1117,9 +1116,9 @@ def test_cmd2_submenu_completion_after_submenu_nomatch(sb_app):
def test_cmd2_help_submenu_completion_single_mid(sb_app):
text = 'sec'
- line = 'help sec'
- begidx = 5
- endidx = 8
+ line = 'help seco'
+ begidx = len(line) - 4
+ endidx = begidx + len(text)
assert sb_app.complete_help(text, line, begidx, endidx) == ['second']