summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2020-09-05 17:24:00 -0400
committerKevin Van Brunt <kmvanbrunt@gmail.com>2020-09-05 17:24:00 -0400
commit5bc9b967d347356556d30bb04f0a2a9f7662aa06 (patch)
treebf309327b8961f9ab27fa290700c62981fa83a37
parentf90c8d161437e2f289be2bb578973e3616b844c1 (diff)
downloadcmd2-git-5bc9b967d347356556d30bb04f0a2a9f7662aa06.tar.gz
Made CustomCompletionSettings public since it's optionally passed to public method cmd2.Cmd.complete().
-rw-r--r--cmd2/cmd2.py15
-rw-r--r--docs/api/cmd.rst27
2 files changed, 31 insertions, 11 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py
index dd229c45..4e88c4ff 100644
--- a/cmd2/cmd2.py
+++ b/cmd2/cmd2.py
@@ -98,7 +98,7 @@ except ImportError: # pragma: no cover
class CompletionMode(enum.Enum):
- """Enum for what type of tab completion to perform in read_input"""
+ """Enum for what type of tab completion to perform in cmd2.Cmd.read_input()"""
# Tab completion will be disabled during read_input() call
# Use of custom up-arrow history supported
NONE = 1
@@ -114,11 +114,12 @@ class CompletionMode(enum.Enum):
CUSTOM = 3
-class _CustomCompletionSettings:
- """Used to tab complete things other than command arguments"""
+class CustomCompletionSettings:
+ """Used by cmd2.Cmd.complete() to tab complete strings other than command arguments"""
def __init__(self, parser: argparse.ArgumentParser, *, preserve_quotes: bool = False):
"""
Initializer
+
:param parser: arg parser defining format of string being tab completed
:param preserve_quotes: if True, then quoted tokens will keep their quotes when processed by
ArgparseCompleter. This is helpful in cases when you're tab completing
@@ -1632,7 +1633,7 @@ class Cmd(cmd.Cmd):
orig_pyreadline_display(matches_to_display)
def _perform_completion(self, text: str, line: str, begidx: int, endidx: int,
- custom_settings: Optional[_CustomCompletionSettings] = None) -> None:
+ custom_settings: Optional[CustomCompletionSettings] = None) -> None:
"""
Helper function for complete() that performs the actual completion
@@ -1804,7 +1805,7 @@ class Cmd(cmd.Cmd):
if len(self.completion_matches) == 1 and self.allow_closing_quote and unclosed_quote:
self.completion_matches[0] += unclosed_quote
- def complete(self, text: str, state: int, custom_settings: Optional[_CustomCompletionSettings] = None) -> Optional[str]:
+ def complete(self, text: str, state: int, custom_settings: Optional[CustomCompletionSettings] = None) -> Optional[str]:
"""Override of cmd2's complete method which returns the next possible completion for 'text'
This completer function is called by readline as complete(text, state), for state in 0, 1, 2, …,
@@ -1862,7 +1863,7 @@ class Cmd(cmd.Cmd):
parser = DEFAULT_ARGUMENT_PARSER(add_help=False)
parser.add_argument('command', metavar="COMMAND", help="command, alias, or macro name",
choices=self._get_commands_aliases_and_macros_for_completion())
- custom_settings = _CustomCompletionSettings(parser)
+ custom_settings = CustomCompletionSettings(parser)
self._perform_completion(text, line, begidx, endidx, custom_settings)
@@ -2630,7 +2631,7 @@ class Cmd(cmd.Cmd):
parser.add_argument('arg', suppress_tab_hint=True, choices=choices,
choices_provider=choices_provider, completer=completer)
- custom_settings = _CustomCompletionSettings(parser, preserve_quotes=preserve_quotes)
+ custom_settings = CustomCompletionSettings(parser, preserve_quotes=preserve_quotes)
complete_func = functools.partial(self.complete, custom_settings=custom_settings)
readline.set_completer(complete_func)
diff --git a/docs/api/cmd.rst b/docs/api/cmd.rst
index ced49e3b..3bcc302f 100644
--- a/docs/api/cmd.rst
+++ b/docs/api/cmd.rst
@@ -1,10 +1,6 @@
cmd2.Cmd
========
-.. autoclass:: cmd2.CompletionMode
- :members:
- :undoc-members:
-
.. autoclass:: cmd2.Cmd
:members:
@@ -69,3 +65,26 @@ cmd2.Cmd
The symbol name which :ref:`features/scripting:Python Scripts` run
using the :ref:`features/builtin_commands:run_pyscript` command can use
to reference the parent ``cmd2`` application.
+
+.. autoclass:: cmd2.CompletionMode
+
+ .. attribute:: NONE
+
+ Tab completion will be disabled during read_input() call. Use of custom
+ up-arrow history supported.
+
+ .. attribute:: COMMANDS
+
+ read_input() will tab complete cmd2 commands and their arguments.
+ cmd2's command line history will be used for up arrow if history is not
+ provided. Otherwise use of custom up-arrow history supported.
+
+ .. attribute:: CUSTOM
+
+ read_input() will tab complete based on one of its following parameters
+ (choices, choices_provider, completer, parser). Use of custom up-arrow
+ history supported
+
+.. autoclass:: cmd2.cmd2.CustomCompletionSettings
+
+ .. automethod:: __init__ \ No newline at end of file