Skip to content

Commit 3f8a953

Browse files
committed
Renamed parser's table_header to table_columns.
Renamed CompletionItem's table_row to table_data.
1 parent ac35657 commit 3f8a953

File tree

8 files changed

+120
-117
lines changed

8 files changed

+120
-117
lines changed

CHANGELOG.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ prompt is displayed.
3737
longer needed
3838
- `completer` functions must now return a `cmd2.Completions` object instead of `list[str]`.
3939
- `choices_provider` functions must now return a `cmd2.Choices` object instead of `list[str]`.
40-
- An argparse argument's `descriptive_headers` field is now called `table_header`.
41-
- `CompletionItem.descriptive_data` is now called `CompletionItem.table_row`.
42-
- Removed `DEFAULT_DESCRIPTIVE_HEADERS`. This means you must define `table_header` when using
43-
`CompletionItem.table_row` data.
40+
- An argparse argument's `descriptive_headers` field is now called `table_columns`.
41+
- `CompletionItem.descriptive_data` is now called `CompletionItem.table_data`.
42+
- Removed `DEFAULT_DESCRIPTIVE_HEADERS`. This means you must define `table_columns` when using
43+
`CompletionItem.table_data` data.
4444
- `Cmd.default_sort_key` moved to `utils.DEFAULT_STR_SORT_KEY`.
4545
- Moved completion state data, which previously resided in `Cmd`, into other classes.
4646
- `Cmd.matches_sorted` -> `Completions.is_sorted` and `Choices.is_sorted`

cmd2/argparse_completer.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -594,42 +594,46 @@ def _validate_table_data(arg_state: _ArgumentState, completions: Completions) ->
594594
595595
:raises ValueError: if there is an error with the data.
596596
"""
597-
table_header = arg_state.action.get_table_header() # type: ignore[attr-defined]
598-
has_table_data = any(item.table_row for item in completions)
597+
table_columns = arg_state.action.get_table_columns() # type: ignore[attr-defined]
598+
has_table_data = any(item.table_data for item in completions)
599599

600-
if table_header is None:
600+
if table_columns is None:
601601
if has_table_data:
602602
raise ValueError(
603-
f"Argument '{arg_state.action.dest}' has CompletionItems with table_row, "
604-
f"but no table_header was defined in add_argument()."
603+
f"Argument '{arg_state.action.dest}' has CompletionItems with table_data, "
604+
f"but no table_columns were defined in add_argument()."
605605
)
606606
return
607607

608-
# If header is defined, then every item must have data, and lengths must match
608+
# If columns are defined, then every item must have data, and lengths must match
609609
for item in completions:
610-
if not item.table_row:
610+
if not item.table_data:
611611
raise ValueError(
612-
f"Argument '{arg_state.action.dest}' has table_header defined, "
613-
f"but the CompletionItem for '{item.text}' is missing table_row."
612+
f"Argument '{arg_state.action.dest}' has table_columns defined, "
613+
f"but the CompletionItem for '{item.text}' is missing table_data."
614614
)
615-
if len(item.table_row) != len(table_header):
615+
if len(item.table_data) != len(table_columns):
616616
raise ValueError(
617-
f"Argument '{arg_state.action.dest}': table_row length ({len(item.table_row)}) "
618-
f"does not match table_header length ({len(table_header)}) for item '{item.text}'."
617+
f"Argument '{arg_state.action.dest}': table_data length ({len(item.table_data)}) "
618+
f"does not match table_columns length ({len(table_columns)}) for item '{item.text}'."
619619
)
620620

621621
def _build_completion_table(self, arg_state: _ArgumentState, completions: Completions) -> Completions:
622622
"""Build a rich.Table for completion results if applicable."""
623623
# Verify integrity of completion data
624624
self._validate_table_data(arg_state, completions)
625625

626-
table_header = cast(
626+
table_columns = cast(
627627
Sequence[str | Column] | None,
628-
arg_state.action.get_table_header(), # type: ignore[attr-defined]
628+
arg_state.action.get_table_columns(), # type: ignore[attr-defined]
629629
)
630630

631631
# Skip table generation if results are outside thresholds or no columns are defined
632-
if len(completions) < 2 or len(completions) > self._cmd2_app.max_completion_table_items or table_header is None:
632+
if (
633+
len(completions) < 2
634+
or len(completions) > self._cmd2_app.max_completion_table_items
635+
or table_columns is None
636+
): # fmt: skip
633637
return completions
634638

635639
# If a metavar was defined, use that instead of the dest field
@@ -650,13 +654,13 @@ def _build_completion_table(self, arg_state: _ArgumentState, completions: Comple
650654
rich_columns: list[Column] = []
651655
rich_columns.append(Column(destination.upper(), justify="right" if all_nums else "left", no_wrap=True))
652656
rich_columns.extend(
653-
column if isinstance(column, Column) else Column(column, overflow="fold") for column in table_header
657+
column if isinstance(column, Column) else Column(column, overflow="fold") for column in table_columns
654658
)
655659

656660
# Build the table
657661
table = Table(*rich_columns, box=SIMPLE_HEAD, show_edge=False, border_style=Cmd2Style.TABLE_BORDER)
658662
for item in completions:
659-
table.add_row(Text.from_ansi(item.display), *item.table_row)
663+
table.add_row(Text.from_ansi(item.display), *item.table_data)
660664

661665
return dataclasses.replace(
662666
completions,

cmd2/argparse_custom.py

Lines changed: 40 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def my_completer(self, text, line, begidx, endidx, arg_tokens) -> Completions
127127
128128
1. display - string for displaying the completion differently in the completion menu
129129
2. display_meta - meta information about completion which displays in the completion menu
130-
3. table_row - row data for completion tables
130+
3. table_data - supplemental data for completion tables
131131
132132
They can also be used as argparse choices. When a ``CompletionItem`` is created, it
133133
stores the original value (e.g. ID number) and makes it accessible through a property
@@ -139,8 +139,8 @@ def my_completer(self, text, line, begidx, endidx, arg_tokens) -> Completions
139139
These were added to help in cases where uninformative data is being completed.
140140
For instance, completing ID numbers isn't very helpful to a user without context.
141141
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
142+
Providing ``table_data`` in your ``CompletionItem`` signals ArgparseCompleter
143+
to output the completion results in a table with supplemental data instead of just a table
144144
of tokens::
145145
146146
Instead of this:
@@ -155,35 +155,34 @@ def my_completer(self, text, line, begidx, endidx, arg_tokens) -> Completions
155155
156156
157157
The left-most column is the actual value being completed and its header is
158-
that value's name. The right column header is defined using the
159-
``table_header`` parameter of add_argument(), which is a list of header
160-
names that defaults to ["Description"]. The right column values come from the
161-
``table_row`` argument to ``CompletionItem``. It's a ``Sequence`` with the
162-
same number of items as ``table_header``.
158+
that value's name. Any additional column headers are defined using the
159+
``table_columns`` parameter of add_argument(), which is a list of header
160+
names. The supplemental column values come from the
161+
``table_data`` argument to ``CompletionItem``. It's a ``Sequence`` with the
162+
same number of items as ``table_columns``.
163163
164164
Example::
165165
166-
Add an argument and define its table_header.
166+
Add an argument and define its table_columns.
167167
168168
parser.add_argument(
169-
add_argument(
170169
"item_id",
171170
type=int,
172171
choices_provider=get_choices,
173-
table_header=["Item Name", "Checked Out", "Due Date"],
172+
table_columns=["Item Name", "Checked Out", "Due Date"],
174173
)
175174
176175
Implement the choices_provider to return Choices.
177176
178177
def get_choices(self) -> Choices:
179178
\"\"\"choices_provider which returns CompletionItems\"\"\"
180179
181-
# Populate CompletionItem's table_row argument.
182-
# Its item count should match that of table_header.
180+
# Populate CompletionItem's table_data argument.
181+
# Its item count should match that of table_columns.
183182
items = [
184-
CompletionItem(1, table_row=["My item", True, "02/02/2022"]),
185-
CompletionItem(2, table_row=["Another item", False, ""]),
186-
CompletionItem(3, table_row=["Yet another item", False, ""]),
183+
CompletionItem(1, table_data=["My item", True, "02/02/2022"]),
184+
CompletionItem(2, table_data=["Another item", False, ""]),
185+
CompletionItem(3, table_data=["Yet another item", False, ""]),
187186
]
188187
return Choices(items)
189188
@@ -195,7 +194,7 @@ def get_choices(self) -> Choices:
195194
2 Another item False
196195
3 Yet another item False
197196
198-
``table_header`` can be strings or ``Rich.table.Columns`` for more
197+
``table_columns`` can be strings or ``Rich.table.Columns`` for more
199198
control over things like alignment.
200199
201200
- If a header is a string, it will render as a left-aligned column with its
@@ -207,9 +206,9 @@ def get_choices(self) -> Choices:
207206
truncated with an ellipsis at the end. You can override this and other settings
208207
when you create the ``Column``.
209208
210-
``table_row`` items can include Rich objects, including styled Text and Tables.
209+
``table_data`` items can include Rich objects, including styled Text and Tables.
211210
212-
To avoid printing a excessive information to the screen at once when a user
211+
To avoid printing excessive information to the screen at once when a user
213212
presses tab, there is a maximum threshold for the number of ``CompletionItems``
214213
that will be shown. Its value is defined in ``cmd2.Cmd.max_completion_table_items``.
215214
It defaults to 50, but can be changed. If the number of completion suggestions
@@ -240,8 +239,8 @@ def get_choices(self) -> Choices:
240239
- ``argparse.Action.get_choices_callable()`` - See `action_get_choices_callable` for more details.
241240
- ``argparse.Action.set_choices_provider()`` - See `_action_set_choices_provider` for more details.
242241
- ``argparse.Action.set_completer()`` - See `_action_set_completer` for more details.
243-
- ``argparse.Action.get_table_header()`` - See `_action_get_table_header` for more details.
244-
- ``argparse.Action.set_table_header()`` - See `_action_set_table_header` for more details.
242+
- ``argparse.Action.get_table_columns()`` - See `_action_get_table_columns` for more details.
243+
- ``argparse.Action.set_table_columns()`` - See `_action_set_table_columns` for more details.
245244
- ``argparse.Action.get_nargs_range()`` - See `_action_get_nargs_range` for more details.
246245
- ``argparse.Action.set_nargs_range()`` - See `_action_set_nargs_range` for more details.
247246
- ``argparse.Action.get_suppress_tab_hint()`` - See `_action_get_suppress_tab_hint` for more details.
@@ -418,8 +417,8 @@ def completer(self) -> CompleterUnbound[CmdOrSet]:
418417
# ChoicesCallable object that specifies the function to be called which provides choices to the argument
419418
ATTR_CHOICES_CALLABLE = 'choices_callable'
420419

421-
# A completion table header
422-
ATTR_TABLE_HEADER = 'table_header'
420+
# Completion table columns
421+
ATTR_TABLE_COLUMNS = 'table_columns'
423422

424423
# A tuple specifying nargs as a range (min, max)
425424
ATTR_NARGS_RANGE = 'nargs_range'
@@ -516,38 +515,38 @@ def _action_set_completer(
516515

517516

518517
############################################################################################################
519-
# Patch argparse.Action with accessors for table_header attribute
518+
# Patch argparse.Action with accessors for table_columns attribute
520519
############################################################################################################
521-
def _action_get_table_header(self: argparse.Action) -> Sequence[str | Column] | None:
522-
"""Get the table_header attribute of an argparse Action.
520+
def _action_get_table_columns(self: argparse.Action) -> Sequence[str | Column] | None:
521+
"""Get the table_columns attribute of an argparse Action.
523522
524-
This function is added by cmd2 as a method called ``get_table_header()`` to ``argparse.Action`` class.
523+
This function is added by cmd2 as a method called ``get_table_columns()`` to ``argparse.Action`` class.
525524
526-
To call: ``action.get_table_header()``
525+
To call: ``action.get_table_columns()``
527526
528527
:param self: argparse Action being queried
529-
:return: The value of table_header or None if attribute does not exist
528+
:return: The value of table_columns or None if attribute does not exist
530529
"""
531-
return cast(Sequence[str | Column] | None, getattr(self, ATTR_TABLE_HEADER, None))
530+
return cast(Sequence[str | Column] | None, getattr(self, ATTR_TABLE_COLUMNS, None))
532531

533532

534-
setattr(argparse.Action, 'get_table_header', _action_get_table_header)
533+
setattr(argparse.Action, 'get_table_columns', _action_get_table_columns)
535534

536535

537-
def _action_set_table_header(self: argparse.Action, table_header: Sequence[str | Column] | None) -> None:
538-
"""Set the table_header attribute of an argparse Action.
536+
def _action_set_table_columns(self: argparse.Action, table_columns: Sequence[str | Column] | None) -> None:
537+
"""Set the table_columns attribute of an argparse Action.
539538
540-
This function is added by cmd2 as a method called ``set_table_header()`` to ``argparse.Action`` class.
539+
This function is added by cmd2 as a method called ``set_table_columns()`` to ``argparse.Action`` class.
541540
542-
To call: ``action.set_table_header(table_header)``
541+
To call: ``action.set_table_columns(table_columns)``
543542
544543
:param self: argparse Action being updated
545-
:param table_header: value being assigned
544+
:param table_columns: value being assigned
546545
"""
547-
setattr(self, ATTR_TABLE_HEADER, table_header)
546+
setattr(self, ATTR_TABLE_COLUMNS, table_columns)
548547

549548

550-
setattr(argparse.Action, 'set_table_header', _action_set_table_header)
549+
setattr(argparse.Action, 'set_table_columns', _action_set_table_columns)
551550

552551

553552
############################################################################################################
@@ -698,7 +697,7 @@ def _add_argument_wrapper(
698697
choices_provider: ChoicesProviderUnbound[CmdOrSet] | None = None,
699698
completer: CompleterUnbound[CmdOrSet] | None = None,
700699
suppress_tab_hint: bool = False,
701-
table_header: Sequence[str | Column] | None = None,
700+
table_columns: Sequence[str | Column] | None = None,
702701
**kwargs: Any,
703702
) -> argparse.Action:
704703
"""Wrap ActionsContainer.add_argument() which supports more settings used by cmd2.
@@ -718,7 +717,7 @@ def _add_argument_wrapper(
718717
current argument's help text as a hint. Set this to True to suppress the hint. If this
719718
argument's help text is set to argparse.SUPPRESS, then tab hints will not display
720719
regardless of the value passed for suppress_tab_hint. Defaults to False.
721-
:param table_header: optional header for when displaying a completion table. Defaults to None.
720+
:param table_columns: optional headers for when displaying a completion table. Defaults to None.
722721
723722
# Args from original function
724723
:param kwargs: keyword-arguments recognized by argparse._ActionsContainer.add_argument
@@ -809,7 +808,7 @@ def _add_argument_wrapper(
809808
new_arg.set_completer(completer) # type: ignore[attr-defined]
810809

811810
new_arg.set_suppress_tab_hint(suppress_tab_hint) # type: ignore[attr-defined]
812-
new_arg.set_table_header(table_header) # type: ignore[attr-defined]
811+
new_arg.set_table_columns(table_columns) # type: ignore[attr-defined]
813812

814813
for keyword, value in custom_attribs.items():
815814
attr_setter = getattr(new_arg, f'set_{keyword}', None)

cmd2/cmd2.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2526,7 +2526,7 @@ def _get_alias_choices(self) -> Choices:
25262526
items: list[CompletionItem] = []
25272527

25282528
for name, value in self.aliases.items():
2529-
items.append(CompletionItem(name, display_meta=value, table_row=[value]))
2529+
items.append(CompletionItem(name, display_meta=value, table_data=[value]))
25302530

25312531
return Choices(items=items)
25322532

@@ -2535,7 +2535,7 @@ def _get_macro_choices(self) -> Choices:
25352535
items: list[CompletionItem] = []
25362536

25372537
for name, macro in self.macros.items():
2538-
items.append(CompletionItem(name, display_meta=macro.value, table_row=[macro.value]))
2538+
items.append(CompletionItem(name, display_meta=macro.value, table_data=[macro.value]))
25392539

25402540
return Choices(items=items)
25412541

@@ -2545,12 +2545,12 @@ def _get_settable_choices(self) -> Choices:
25452545

25462546
for name, settable in self.settables.items():
25472547
value_str = str(settable.value)
2548-
table_row = [
2548+
table_data = [
25492549
value_str,
25502550
settable.description,
25512551
]
25522552
display_meta = f"[Current: {su.stylize(value_str, Style(bold=True))}] {settable.description}"
2553-
items.append(CompletionItem(name, display_meta=display_meta, table_row=table_row))
2553+
items.append(CompletionItem(name, display_meta=display_meta, table_data=table_data))
25542554

25552555
return Choices(items=items)
25562556

@@ -3658,7 +3658,7 @@ def _build_alias_delete_parser(cls) -> Cmd2ArgumentParser:
36583658
nargs=argparse.ZERO_OR_MORE,
36593659
help='alias(es) to delete',
36603660
choices_provider=cls._get_alias_choices,
3661-
table_header=["Value"],
3661+
table_columns=["Value"],
36623662
)
36633663

36643664
return alias_delete_parser
@@ -3700,7 +3700,7 @@ def _build_alias_list_parser(cls) -> Cmd2ArgumentParser:
37003700
nargs=argparse.ZERO_OR_MORE,
37013701
help='alias(es) to list',
37023702
choices_provider=cls._get_alias_choices,
3703-
table_header=["Value"],
3703+
table_columns=["Value"],
37043704
)
37053705

37063706
return alias_list_parser
@@ -3949,7 +3949,7 @@ def _build_macro_delete_parser(cls) -> Cmd2ArgumentParser:
39493949
nargs=argparse.ZERO_OR_MORE,
39503950
help='macro(s) to delete',
39513951
choices_provider=cls._get_macro_choices,
3952-
table_header=["Value"],
3952+
table_columns=["Value"],
39533953
)
39543954

39553955
return macro_delete_parser
@@ -3991,7 +3991,7 @@ def _build_macro_list_parser(cls) -> Cmd2ArgumentParser:
39913991
nargs=argparse.ZERO_OR_MORE,
39923992
help='macro(s) to list',
39933993
choices_provider=cls._get_macro_choices,
3994-
table_header=["Value"],
3994+
table_columns=["Value"],
39953995
)
39963996

39973997
return macro_list_parser
@@ -4475,7 +4475,7 @@ def _build_base_set_parser(cls) -> Cmd2ArgumentParser:
44754475
nargs=argparse.OPTIONAL,
44764476
help='parameter to set or view',
44774477
choices_provider=cls._get_settable_choices,
4478-
table_header=["Value", "Description"],
4478+
table_columns=["Value", "Description"],
44794479
)
44804480

44814481
return base_set_parser

0 commit comments

Comments
 (0)