4848 my_list = ['An Option', 'SomeOtherOption']
4949 parser.add_argument('-o', '--options', choices=my_list)
5050
51- ``choices_provider`` - pass a function that returns choices . This is good in
51+ ``choices_provider`` - pass a function that returns a Choices object . This is good in
5252cases where the choices are dynamically generated when the user hits tab.
5353
5454 Example::
5555
56- def my_choices_provider(self):
56+ def my_choices_provider(self) -> Choices :
5757 ...
5858 return my_choices
5959
6060 parser.add_argument("arg", choices_provider=my_choices_provider)
6161
62- ``completer`` - pass a function that does custom completion.
62+ ``completer`` - pass a function that does custom completion and returns a Completions object .
6363
6464cmd2 provides a few completer methods for convenience (e.g., path_complete,
6565delimiter_complete)
@@ -107,8 +107,8 @@ def my_choices_provider(self):
107107
108108 Example::
109109
110- def my_choices_provider(self, arg_tokens)
111- def my_completer(self, text, line, begidx, endidx, arg_tokens)
110+ def my_choices_provider(self, arg_tokens) -> Choices
111+ def my_completer(self, text, line, begidx, endidx, arg_tokens) -> Completions
112112
113113All values of the arg_tokens dictionary are lists, even if a particular
114114argument expects only 1 token. Since ArgparseCompleter is for completion,
@@ -117,12 +117,31 @@ def my_completer(self, text, line, begidx, endidx, arg_tokens)
117117the command line. It is up to the developer to determine if the user entered
118118the correct argument type (e.g. int) and validate their values.
119119
120- CompletionItem Class - This class was added to help in cases where
121- uninformative data is being completed. For instance, completing ID
122- numbers isn't very helpful to a user without context. Returning a list of
123- CompletionItems instead of a regular string for completion results will signal
124- the ArgparseCompleter to output the completion results in a table of completion
125- tokens with descriptive data instead of just a table of tokens::
120+ **CompletionItem Class**
121+
122+ This class represents a single completion result and what the ``Choices``
123+ and ``Completion`` classes contain.
124+
125+ ``CompletionItem`` provides the following optional metadata fields which enhance
126+ completion results displayed to the screen.
127+
128+ 1. display - string for displaying the completion differently in the completion menu
129+ 2. display_meta - meta information about completion which displays in the completion menu
130+ 3. table_row - row data for completion tables
131+
132+ They can also be used as argparse choices. When a ``CompletionItem`` is created, it
133+ stores the original value (e.g. ID number) and makes it accessible through a property
134+ called ``value``. cmd2 has patched argparse so that when evaluating choices, input
135+ is compared to ``CompletionItem.value`` instead of the ``CompletionItem`` instance.
136+
137+ **Completion Tables**
138+
139+ These were added to help in cases where uninformative data is being completed.
140+ For instance, completing ID numbers isn't very helpful to a user without context.
141+
142+ Providing ``table_row`` data in your ``CompletionItem`` signals ArgparseCompleter
143+ to output the completion results in a table with descriptive data instead of just a table
144+ of tokens::
126145
127146 Instead of this:
128147 1 2 3
@@ -139,16 +158,9 @@ def my_completer(self, text, line, begidx, endidx, arg_tokens)
139158that value's name. The right column header is defined using the
140159``table_header`` parameter of add_argument(), which is a list of header
141160names that defaults to ["Description"]. The right column values come from the
142- ``row_data `` argument to ``CompletionItem``. It's a ``Sequence`` with the
161+ ``table_row `` argument to ``CompletionItem``. It's a ``Sequence`` with the
143162same number of items as ``table_header``.
144163
145- To use CompletionItems, just return them from your choices_provider or
146- completer functions. They can also be used as argparse choices. When a
147- CompletionItem is created, it stores the original value (e.g. ID number) and
148- makes it accessible through a property called orig_value. cmd2 has patched
149- argparse so that when evaluating choices, input is compared to
150- CompletionItem.orig_value instead of the CompletionItem instance.
151-
152164Example::
153165
154166 Add an argument and define its table_header.
@@ -157,22 +169,23 @@ def my_completer(self, text, line, begidx, endidx, arg_tokens)
157169 add_argument(
158170 "item_id",
159171 type=int,
160- choices_provider=get_items ,
172+ choices_provider=get_choices ,
161173 table_header=["Item Name", "Checked Out", "Due Date"],
162174 )
163175
164- Implement the choices_provider to return CompletionItems .
176+ Implement the choices_provider to return Choices .
165177
166- def get_items (self) -> list[CompletionItems] :
178+ def get_choices (self) -> Choices :
167179 \" \" \" choices_provider which returns CompletionItems\" \" \"
168180
169181 # Populate CompletionItem's table_row argument.
170182 # Its item count should match that of table_header.
171- return [
183+ items = [
172184 CompletionItem(1, table_row=["My item", True, "02/02/2022"]),
173185 CompletionItem(2, table_row=["Another item", False, ""]),
174186 CompletionItem(3, table_row=["Yet another item", False, ""]),
175187 ]
188+ return Choices(items)
176189
177190 This is what the user will see during completion.
178191
@@ -197,10 +210,10 @@ def get_items(self) -> list[CompletionItems]:
197210``table_row`` items can include Rich objects, including styled Text and Tables.
198211
199212To avoid printing a excessive information to the screen at once when a user
200- presses tab, there is a maximum threshold for the number of CompletionItems
213+ presses tab, there is a maximum threshold for the number of `` CompletionItems``
201214that will be shown. Its value is defined in ``cmd2.Cmd.max_completion_table_items``.
202215It defaults to 50, but can be changed. If the number of completion suggestions
203- exceeds this number, they then a completion table won't be displayed.
216+ exceeds this number, then a completion table won't be displayed.
204217
205218
206219**Patched argparse functions**
0 commit comments