summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cmd2/argparse_completer.py12
-rw-r--r--cmd2/cmd2.py12
-rw-r--r--cmd2/utils.py16
-rw-r--r--tests/test_completion.py10
4 files changed, 34 insertions, 16 deletions
diff --git a/cmd2/argparse_completer.py b/cmd2/argparse_completer.py
index da57a9f9..5f4a7a87 100644
--- a/cmd2/argparse_completer.py
+++ b/cmd2/argparse_completer.py
@@ -69,6 +69,8 @@ from typing import List, Dict, Tuple, Callable, Union
from .ansi import ansi_aware_write, ansi_safe_wcswidth, style_error
from .rl_utils import rl_force_redisplay
+from . import utils
+
# attribute that can optionally added to an argparse argument (called an Action) to
# define the completion choices for the argument. You may provide a Collection or a Function.
ACTION_ARG_CHOICES = 'arg_choices'
@@ -571,8 +573,8 @@ class AutoCompleter(object):
# a flag prefix then we'll complete the list of flag options
if not flag_arg.needed and len(tokens[-1]) > 0 and tokens[-1][0] in self._parser.prefix_chars and \
not skip_remaining_flags:
- return self._cmd2_app.basic_complete(text, line, begidx, endidx,
- [flag for flag in self._flags if flag not in matched_flags])
+ return utils.basic_complete(text, line, begidx, endidx,
+ [flag for flag in self._flags if flag not in matched_flags])
# we're not at a positional argument, see if we're in a flag argument
elif not current_is_positional:
if flag_action is not None:
@@ -644,7 +646,7 @@ class AutoCompleter(object):
if token in completers:
return completers[token].complete_command_help(tokens, text, line, begidx, endidx)
else:
- return self._cmd2_app.basic_complete(text, line, begidx, endidx, completers.keys())
+ return utils.basic_complete(text, line, begidx, endidx, completers.keys())
return []
def format_help(self, tokens: List[str]) -> str:
@@ -703,8 +705,8 @@ class AutoCompleter(object):
else:
return completer(text, line, begidx, endidx)
else:
- return self._cmd2_app.basic_complete(text, line, begidx, endidx,
- self._resolve_choices_for_arg(action, used_values))
+ return utils.basic_complete(text, line, begidx, endidx,
+ self._resolve_choices_for_arg(action, used_values))
return []
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py
index 0ca9f358..d6fc78eb 100644
--- a/cmd2/cmd2.py
+++ b/cmd2/cmd2.py
@@ -910,7 +910,7 @@ class Cmd(cmd.Cmd):
:param delimiter: what delimits each portion of the matches (ex: paths are delimited by a slash)
:return: a list of possible tab completions
"""
- matches = self.basic_complete(text, line, begidx, endidx, match_against)
+ matches = utils.basic_complete(text, line, begidx, endidx, match_against)
# Display only the portion of the match that's being completed based on delimiter
if matches:
@@ -971,7 +971,7 @@ class Cmd(cmd.Cmd):
# Perform tab completion using a Collection
if isinstance(match_against, Collection):
- completions_matches = self.basic_complete(text, line, begidx, endidx, match_against)
+ completions_matches = utils.basic_complete(text, line, begidx, endidx, match_against)
# Perform tab completion using a function
elif callable(match_against):
@@ -1015,7 +1015,7 @@ class Cmd(cmd.Cmd):
# Perform tab completion using a Collection
if isinstance(match_against, Collection):
- matches = self.basic_complete(text, line, begidx, endidx, match_against)
+ matches = utils.basic_complete(text, line, begidx, endidx, match_against)
# Perform tab completion using a function
elif callable(match_against):
@@ -1567,8 +1567,8 @@ class Cmd(cmd.Cmd):
else:
# Complete token against anything a user can run
- self.completion_matches = self.basic_complete(text, line, begidx, endidx,
- self._get_commands_aliases_and_macros_for_completion())
+ self.completion_matches = utils.basic_complete(text, line, begidx, endidx,
+ self._get_commands_aliases_and_macros_for_completion())
# Handle single result
if len(self.completion_matches) == 1:
@@ -2648,7 +2648,7 @@ class Cmd(cmd.Cmd):
topics = set(self.get_help_topics())
visible_commands = set(self.get_visible_commands())
strs_to_match = list(topics | visible_commands)
- return self.basic_complete(text, line, begidx, endidx, strs_to_match)
+ return utils.basic_complete(text, line, begidx, endidx, strs_to_match)
def complete_help_subcommand(self, text: str, line: str, begidx: int, endidx: int) -> List[str]:
"""Completes the subcommand argument of help"""
diff --git a/cmd2/utils.py b/cmd2/utils.py
index 812fa227..3ba1be72 100644
--- a/cmd2/utils.py
+++ b/cmd2/utils.py
@@ -615,3 +615,19 @@ class RedirectionSavedState(object):
# If the command created a process to pipe to, then then is its reader
self.pipe_proc_reader = None
+
+
+# noinspection PyUnusedLocal
+def basic_complete(text: str, line: str, begidx: int, endidx: int, match_against: Iterable) -> List[str]:
+ """
+ Basic tab completion function that matches against a list of strings without considering line contents
+ or cursor position. The args required by this function are defined in the header of Pythons's cmd.py.
+
+ :param text: the string prefix we are attempting to match (all returned matches must begin with it)
+ :param line: the current input line with leading whitespace removed
+ :param begidx: the beginning index of the prefix text
+ :param endidx: the ending index of the prefix text
+ :param match_against: the strings being matched against
+ :return: a list of possible tab completions
+ """
+ return [cur_match for cur_match in match_against if cur_match.startswith(text)]
diff --git a/tests/test_completion.py b/tests/test_completion.py
index eea34ba6..9bf6fc5f 100644
--- a/tests/test_completion.py
+++ b/tests/test_completion.py
@@ -67,7 +67,7 @@ class CompletionsExample(cmd2.Cmd):
pass
def complete_test_basic(self, text, line, begidx, endidx):
- return self.basic_complete(text, line, begidx, endidx, food_item_strs)
+ return utils.basic_complete(text, line, begidx, endidx, food_item_strs)
def do_test_delimited(self, args):
pass
@@ -80,7 +80,7 @@ class CompletionsExample(cmd2.Cmd):
def complete_test_sort_key(self, text, line, begidx, endidx):
num_strs = ['2', '11', '1']
- return self.basic_complete(text, line, begidx, endidx, num_strs)
+ return utils.basic_complete(text, line, begidx, endidx, num_strs)
def do_test_raise_exception(self, args):
pass
@@ -524,7 +524,7 @@ def test_basic_completion_single(cmd2_app):
endidx = len(line)
begidx = endidx - len(text)
- assert cmd2_app.basic_complete(text, line, begidx, endidx, food_item_strs) == ['Pizza']
+ assert utils.basic_complete(text, line, begidx, endidx, food_item_strs) == ['Pizza']
def test_basic_completion_multiple(cmd2_app):
text = ''
@@ -532,7 +532,7 @@ def test_basic_completion_multiple(cmd2_app):
endidx = len(line)
begidx = endidx - len(text)
- matches = sorted(cmd2_app.basic_complete(text, line, begidx, endidx, food_item_strs))
+ matches = sorted(utils.basic_complete(text, line, begidx, endidx, food_item_strs))
assert matches == sorted(food_item_strs)
def test_basic_completion_nomatch(cmd2_app):
@@ -541,7 +541,7 @@ def test_basic_completion_nomatch(cmd2_app):
endidx = len(line)
begidx = endidx - len(text)
- assert cmd2_app.basic_complete(text, line, begidx, endidx, food_item_strs) == []
+ assert utils.basic_complete(text, line, begidx, endidx, food_item_strs) == []
def test_delimiter_completion(cmd2_app):
text = '/home/'