summaryrefslogtreecommitdiff
path: root/examples/basic_completion.py
diff options
context:
space:
mode:
authorTodd Leonhardt <todd.leonhardt@gmail.com>2020-02-18 19:47:25 -0500
committerGitHub <noreply@github.com>2020-02-18 19:47:25 -0500
commit970c5fc42c69c1c03640c979df341e85e3c38848 (patch)
treed14b6c335ee6f5bfa6fbb2d509846cdbff23e98c /examples/basic_completion.py
parent2221e08e996a34660125536e3fc34eb231b3b060 (diff)
parent34ce17c95fae0a849cd90de2e65cd454b9fe51cb (diff)
downloadcmd2-git-970c5fc42c69c1c03640c979df341e85e3c38848.tar.gz
Merge pull request #892 from python-cmd2/completion_updates
Completion updates
Diffstat (limited to 'examples/basic_completion.py')
-rwxr-xr-xexamples/basic_completion.py28
1 files changed, 22 insertions, 6 deletions
diff --git a/examples/basic_completion.py b/examples/basic_completion.py
index e021828b..9523ac67 100755
--- a/examples/basic_completion.py
+++ b/examples/basic_completion.py
@@ -2,16 +2,18 @@
# coding=utf-8
"""
A simple example demonstrating how to enable tab completion by assigning a completer function to do_* commands.
-This also demonstrates capabilities of the following completer methods included with cmd2:
-- delimiter_completer
-- flag_based_complete (see note below)
-- index_based_complete (see note below)
+This also demonstrates capabilities of the following completer features included with cmd2:
+- CompletionError exceptions
+- delimiter_completer()
+- flag_based_complete() (see note below)
+- index_based_complete() (see note below)
flag_based_complete() and index_based_complete() are basic methods and should only be used if you are not
familiar with argparse. The recommended approach for tab completing positional tokens and flags is to use
argparse-based completion. For an example integrating tab completion with argparse, see argparse_completion.py
"""
import functools
+from typing import List
import cmd2
@@ -42,7 +44,7 @@ class BasicCompletion(cmd2.Cmd):
"""
self.poutput("Args: {}".format(statement.args))
- def complete_flag_based(self, text, line, begidx, endidx):
+ def complete_flag_based(self, text, line, begidx, endidx) -> List[str]:
"""Completion function for do_flag_based"""
flag_dict = \
{
@@ -65,7 +67,7 @@ class BasicCompletion(cmd2.Cmd):
"""Tab completes first 3 arguments using index_based_complete"""
self.poutput("Args: {}".format(statement.args))
- def complete_index_based(self, text, line, begidx, endidx):
+ def complete_index_based(self, text, line, begidx, endidx) -> List[str]:
"""Completion function for do_index_based"""
index_dict = \
{
@@ -84,6 +86,20 @@ class BasicCompletion(cmd2.Cmd):
complete_delimiter_complete = functools.partialmethod(cmd2.Cmd.delimiter_complete,
match_against=file_strs, delimiter='/')
+ def do_raise_error(self, statement: cmd2.Statement):
+ """Demonstrates effect of raising CompletionError"""
+ self.poutput("Args: {}".format(statement.args))
+
+ def complete_raise_error(self, text, line, begidx, endidx) -> List[str]:
+ """
+ CompletionErrors can be raised if an error occurs while tab completing.
+
+ Example use cases
+ - Reading a database to retrieve a tab completion data set failed
+ - A previous command line argument that determines the data set being completed is invalid
+ """
+ raise cmd2.CompletionError("This is how a CompletionError behaves")
+
if __name__ == '__main__':
import sys