Skip to content

Commit 065536a

Browse files
committed
Added use of CompletionError to basic completion example
1 parent d214709 commit 065536a

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

examples/basic_completion.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,21 @@
22
# coding=utf-8
33
"""
44
A simple example demonstrating how to enable tab completion by assigning a completer function to do_* commands.
5-
This also demonstrates capabilities of the following completer methods included with cmd2:
6-
- delimiter_completer
7-
- flag_based_complete (see note below)
8-
- index_based_complete (see note below)
5+
This also demonstrates capabilities of the following completer features included with cmd2:
6+
- CompletionError exceptions
7+
- delimiter_completer()
8+
- flag_based_complete() (see note below)
9+
- index_based_complete() (see note below)
910
1011
flag_based_complete() and index_based_complete() are basic methods and should only be used if you are not
1112
familiar with argparse. The recommended approach for tab completing positional tokens and flags is to use
1213
argparse-based completion. For an example integrating tab completion with argparse, see argparse_completion.py
1314
"""
1415
import functools
16+
from typing import List
1517

1618
import cmd2
19+
from cmd2 import ansi
1720

1821
# List of strings used with completion functions
1922
food_item_strs = ['Pizza', 'Ham', 'Ham Sandwich', 'Potato']
@@ -42,7 +45,7 @@ def do_flag_based(self, statement: cmd2.Statement):
4245
"""
4346
self.poutput("Args: {}".format(statement.args))
4447

45-
def complete_flag_based(self, text, line, begidx, endidx):
48+
def complete_flag_based(self, text, line, begidx, endidx) -> List[str]:
4649
"""Completion function for do_flag_based"""
4750
flag_dict = \
4851
{
@@ -65,7 +68,7 @@ def do_index_based(self, statement: cmd2.Statement):
6568
"""Tab completes first 3 arguments using index_based_complete"""
6669
self.poutput("Args: {}".format(statement.args))
6770

68-
def complete_index_based(self, text, line, begidx, endidx):
71+
def complete_index_based(self, text, line, begidx, endidx) -> List[str]:
6972
"""Completion function for do_index_based"""
7073
index_dict = \
7174
{
@@ -84,6 +87,20 @@ def do_delimiter_complete(self, statement: cmd2.Statement):
8487
complete_delimiter_complete = functools.partialmethod(cmd2.Cmd.delimiter_complete,
8588
match_against=file_strs, delimiter='/')
8689

90+
def do_raise_error(self, statement: cmd2.Statement):
91+
"""Demonstrates effect of raising CompletionError"""
92+
self.poutput("Args: {}".format(statement.args))
93+
94+
def complete_raise_error(self, text, line, begidx, endidx) -> List[str]:
95+
"""
96+
CompletionErrors can be raised if an error occurs while tab completing.
97+
98+
Example use cases
99+
- Reading a database to retrieve a tab completion data set failed
100+
- A previous command line argument that determines the data set being completed is invalid
101+
"""
102+
raise cmd2.CompletionError(ansi.style_error("This is how a CompletionError behaves"))
103+
87104

88105
if __name__ == '__main__':
89106
import sys

0 commit comments

Comments
 (0)