Skip to content

Commit ee93add

Browse files
committed
Wrote examples for CompletionError and CompletionItem
1 parent cdd8190 commit ee93add

File tree

1 file changed

+34
-13
lines changed

1 file changed

+34
-13
lines changed

examples/argparse_completion.py

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,16 @@
66
import argparse
77
from typing import List
88

9-
from cmd2 import Cmd, Cmd2ArgumentParser, with_argparser
9+
from cmd2 import Cmd, Cmd2ArgumentParser, with_argparser, CompletionError, CompletionItem
1010
from cmd2.utils import basic_complete
1111

12+
# Data source for argparse.choices
1213
food_item_strs = ['Pizza', 'Ham', 'Ham Sandwich', 'Potato']
1314

14-
# This data is used to demonstrate delimiter_complete
15-
file_strs = \
16-
[
17-
'/home/user/file.db',
18-
'/home/user/file space.db',
19-
'/home/user/another.db',
20-
'/home/other user/maps.db',
21-
'/home/other user/tests.db'
22-
]
23-
2415

2516
def choices_function() -> List[str]:
2617
"""Choices functions are useful when the choice list is dynamically generated (e.g. from data in a database)"""
27-
return ['a', 'dynamic', 'list']
18+
return ['a', 'dynamic', 'list', 'goes', 'here']
2819

2920

3021
def completer_function(text: str, line: str, begidx: int, endidx: int) -> List[str]:
@@ -33,7 +24,18 @@ def completer_function(text: str, line: str, begidx: int, endidx: int) -> List[s
3324
need to modify cmd2's instance variables related to tab completion, it will be rare to need a completer
3425
function. completer_method should be used in those cases.
3526
"""
36-
return basic_complete(text, line, begidx, endidx, food_item_strs)
27+
match_against = ['a', 'dynamic', 'list', 'goes', 'here']
28+
return basic_complete(text, line, begidx, endidx, match_against)
29+
30+
31+
def choices_completion_item() -> List[CompletionItem]:
32+
"""Return CompletionItem instead of strings. These give more context to what's being tab completed."""
33+
items = {
34+
1: "My item",
35+
2: "Another item",
36+
3: "Yet another item"
37+
}
38+
return [CompletionItem(item_id, description) for item_id, description in items.items()]
3739

3840

3941
class ArgparseCompletion(Cmd):
@@ -45,6 +47,18 @@ def choices_method(self) -> List[str]:
4547
"""Choices methods are useful when the choice list is based on instance data of your application"""
4648
return self.sport_item_strs
4749

50+
def choices_completion_error(self) -> List[str]:
51+
"""
52+
CompletionErrors can be raised if an error occurs while tab completing.
53+
54+
Example use cases
55+
- Reading a database to retrieve a tab completion data set failed
56+
- A previous command line argument that determines the data set being completed is invalid
57+
"""
58+
if self.debug:
59+
return self.sport_item_strs
60+
raise CompletionError("Debug must be true")
61+
4862
# Parser for complete command
4963
complete_parser = Cmd2ArgumentParser(description="Command demonstrating tab completion with argparse\n"
5064
"Notice even the flags of this command tab complete")
@@ -61,6 +75,13 @@ def choices_method(self) -> List[str]:
6175
complete_parser.add_argument('--completer_function', completer_function=completer_function)
6276
complete_parser.add_argument('--completer_method', completer_method=Cmd.path_complete)
6377

78+
# Demonstrate raising a CompletionError while tab completing
79+
complete_parser.add_argument('--completion_error', choices_method=choices_completion_error)
80+
81+
# Demonstrate returning CompletionItems instead of strings
82+
complete_parser.add_argument('--completion_item', choices_function=choices_completion_item, metavar="ITEM_ID",
83+
descriptive_header="Description")
84+
6485
@with_argparser(complete_parser)
6586
def do_complete(self, _: argparse.Namespace) -> None:
6687
"""The complete command"""

0 commit comments

Comments
 (0)