summaryrefslogtreecommitdiff
path: root/cmd2
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2019-07-02 10:47:52 -0400
committerKevin Van Brunt <kmvanbrunt@gmail.com>2019-07-02 10:47:52 -0400
commitb7fa4e46593151086a4186c9d90dc72b809c9b45 (patch)
treea10a6888e0a487c52660af5b642b30414abded91 /cmd2
parent18207f62d4f70240e79935507bcde7cbb22212a7 (diff)
downloadcmd2-git-b7fa4e46593151086a4186c9d90dc72b809c9b45.tar.gz
Moved basic_complete to utils
Diffstat (limited to 'cmd2')
-rw-r--r--cmd2/argparse_completer.py12
-rw-r--r--cmd2/cmd2.py12
-rw-r--r--cmd2/utils.py16
3 files changed, 29 insertions, 11 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)]