Skip to content

Commit ff2a732

Browse files
authored
Merge pull request #460 from python-cmd2/autocompleter
Added some explanation of CompletionItem usage in code
2 parents 3c8880e + 8427886 commit ff2a732

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

cmd2/argparse_completer.py

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,33 @@ def my_completer(text: str, line: str, begidx: int, endidx:int, extra_param: str
8181

8282

8383
class CompletionItem(str):
84+
"""
85+
Completion item with descriptive text attached
86+
87+
Returning this instead of a regular string for completion results will signal the
88+
autocompleter to output the completions results in a table of completion tokens
89+
with descriptions instead of just a table of tokens.
90+
91+
For example, you'd see this:
92+
TOKEN Description
93+
MY_TOKEN Info about my token
94+
SOME_TOKEN Info about some token
95+
YET_ANOTHER Yet more info
96+
97+
Instead of this:
98+
TOKEN_ID SOME_TOKEN YET_ANOTHER
99+
100+
This is especially useful if you want to complete ID numbers in a more
101+
user-friendly manner. For example, you can provide this:
102+
103+
ITEM_ID Item Name
104+
1 My item
105+
2 Another item
106+
3 Yet another item
107+
108+
Instead of this:
109+
1 2 3
110+
"""
84111
def __new__(cls, o, desc='', *args, **kwargs) -> str:
85112
return str.__new__(cls, o, *args, **kwargs)
86113

@@ -1196,9 +1223,9 @@ def consume_positionals(start_index):
11961223
# twice (which may fail) if the argument was given, but
11971224
# only if it was defined already in the namespace
11981225
if (action.default is not None and
1199-
isinstance(action.default, str) and
1200-
hasattr(namespace, action.dest) and
1201-
action.default is getattr(namespace, action.dest)):
1226+
isinstance(action.default, str) and
1227+
hasattr(namespace, action.dest) and
1228+
action.default is getattr(namespace, action.dest)):
12021229
setattr(namespace, action.dest,
12031230
self._get_value(action, action.default))
12041231

0 commit comments

Comments
 (0)