Skip to content

Commit 30998bd

Browse files
committed
Added accessor methods for cmd2-specific attributes to the argparse.Action class.
Deprecated set_choices_provider() and set_completer() functions in favor of these new methods.
1 parent 658342d commit 30998bd

File tree

4 files changed

+319
-60
lines changed

4 files changed

+319
-60
lines changed

CHANGELOG.md

+20
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
## 2.1.2 (TBD, 2021)
2+
* Enhancements
3+
* Added the following accessor methods for cmd2-specific attributes to the `argparse.Action` class
4+
* `get_choices_callable()`
5+
* `set_choices_provider()`
6+
* `set_completer()`
7+
* `get_descriptive_header()`
8+
* `set_descriptive_header()`
9+
* `get_nargs_range()`
10+
* `set_nargs_range()`
11+
* `get_suppress_tab_hint()`
12+
* `set_suppress_tab_hint()`
13+
14+
* Deprecations
15+
* Now that `set_choices_provider()` and `set_completer()` have been added as methods to the
16+
`argparse.Action` class, the standalone functions of the same name will be removed in version
17+
2.2.0. To update to the new convention, do the following:
18+
* Change `set_choices_provider(action, provider)` to `action.set_choices_provider(provider)`
19+
* Change `set_completer(action, completer)` to `action.set_completer(completer)`
20+
121
## 2.1.1 (June 17, 2021)
222
* Bug Fixes
323
* Fixed handling of argparse's default options group name which was changed in Python 3.10

cmd2/argparse_completer.py

+4-10
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
deque,
1414
)
1515
from typing import (
16-
Any,
1716
Dict,
1817
List,
1918
Optional,
@@ -27,12 +26,7 @@
2726
constants,
2827
)
2928
from .argparse_custom import (
30-
ATTR_CHOICES_CALLABLE,
31-
ATTR_DESCRIPTIVE_COMPLETION_HEADER,
32-
ATTR_NARGS_RANGE,
33-
ATTR_SUPPRESS_TAB_HINT,
3429
ChoicesCallable,
35-
ChoicesProviderFuncBase,
3630
ChoicesProviderFuncWithTokens,
3731
CompletionItem,
3832
generate_range_error,
@@ -60,7 +54,7 @@
6054
def _build_hint(parser: argparse.ArgumentParser, arg_action: argparse.Action) -> str:
6155
"""Build tab completion hint for a given argument"""
6256
# Check if hinting is disabled for this argument
63-
suppress_hint = getattr(arg_action, ATTR_SUPPRESS_TAB_HINT, False)
57+
suppress_hint = arg_action.get_suppress_tab_hint() # type: ignore[attr-defined]
6458
if suppress_hint or arg_action.help == argparse.SUPPRESS:
6559
return ''
6660
else:
@@ -116,7 +110,7 @@ def __init__(self, arg_action: argparse.Action) -> None:
116110
self.is_remainder = self.action.nargs == argparse.REMAINDER
117111

118112
# Check if nargs is a range
119-
nargs_range = getattr(self.action, ATTR_NARGS_RANGE, None)
113+
nargs_range = self.action.get_nargs_range() # type: ignore[attr-defined]
120114
if nargs_range is not None:
121115
self.min = nargs_range[0]
122116
self.max = nargs_range[1]
@@ -562,7 +556,7 @@ def _format_completions(self, arg_state: _ArgumentState, completions: Union[List
562556
tuple_index = min(len(destination) - 1, arg_state.count)
563557
destination = destination[tuple_index]
564558

565-
desc_header = getattr(arg_state.action, ATTR_DESCRIPTIVE_COMPLETION_HEADER, None)
559+
desc_header = arg_state.action.get_descriptive_header() # type: ignore[attr-defined]
566560
if desc_header is None:
567561
desc_header = DEFAULT_DESCRIPTIVE_HEADER
568562

@@ -665,7 +659,7 @@ def _complete_arg(
665659
if not isinstance(choice, str):
666660
arg_choices[index] = str(choice) # type: ignore[unreachable]
667661
else:
668-
choices_attr = getattr(arg_state.action, ATTR_CHOICES_CALLABLE, None)
662+
choices_attr = arg_state.action.get_choices_callable() # type: ignore[attr-defined]
669663
if choices_attr is None:
670664
return []
671665
arg_choices = choices_attr

0 commit comments

Comments
 (0)