Skip to content

Commit

Permalink
Added accessor methods for cmd2-specific attributes to the argparse.A…
Browse files Browse the repository at this point in the history
…ction class.

Deprecated set_choices_provider() and set_completer() functions in favor of these new methods.
  • Loading branch information
kmvanbrunt committed Jul 4, 2021
1 parent 658342d commit 30998bd
Show file tree
Hide file tree
Showing 4 changed files with 319 additions and 60 deletions.
20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
## 2.1.2 (TBD, 2021)
* Enhancements
* Added the following accessor methods for cmd2-specific attributes to the `argparse.Action` class
* `get_choices_callable()`
* `set_choices_provider()`
* `set_completer()`
* `get_descriptive_header()`
* `set_descriptive_header()`
* `get_nargs_range()`
* `set_nargs_range()`
* `get_suppress_tab_hint()`
* `set_suppress_tab_hint()`

* Deprecations
* Now that `set_choices_provider()` and `set_completer()` have been added as methods to the
`argparse.Action` class, the standalone functions of the same name will be removed in version
2.2.0. To update to the new convention, do the following:
* Change `set_choices_provider(action, provider)` to `action.set_choices_provider(provider)`
* Change `set_completer(action, completer)` to `action.set_completer(completer)`

## 2.1.1 (June 17, 2021)
* Bug Fixes
* Fixed handling of argparse's default options group name which was changed in Python 3.10
Expand Down
14 changes: 4 additions & 10 deletions cmd2/argparse_completer.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
deque,
)
from typing import (
Any,
Dict,
List,
Optional,
Expand All @@ -27,12 +26,7 @@
constants,
)
from .argparse_custom import (
ATTR_CHOICES_CALLABLE,
ATTR_DESCRIPTIVE_COMPLETION_HEADER,
ATTR_NARGS_RANGE,
ATTR_SUPPRESS_TAB_HINT,
ChoicesCallable,
ChoicesProviderFuncBase,
ChoicesProviderFuncWithTokens,
CompletionItem,
generate_range_error,
Expand Down Expand Up @@ -60,7 +54,7 @@
def _build_hint(parser: argparse.ArgumentParser, arg_action: argparse.Action) -> str:
"""Build tab completion hint for a given argument"""
# Check if hinting is disabled for this argument
suppress_hint = getattr(arg_action, ATTR_SUPPRESS_TAB_HINT, False)
suppress_hint = arg_action.get_suppress_tab_hint() # type: ignore[attr-defined]
if suppress_hint or arg_action.help == argparse.SUPPRESS:
return ''
else:
Expand Down Expand Up @@ -116,7 +110,7 @@ def __init__(self, arg_action: argparse.Action) -> None:
self.is_remainder = self.action.nargs == argparse.REMAINDER

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

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

Expand Down Expand Up @@ -665,7 +659,7 @@ def _complete_arg(
if not isinstance(choice, str):
arg_choices[index] = str(choice) # type: ignore[unreachable]
else:
choices_attr = getattr(arg_state.action, ATTR_CHOICES_CALLABLE, None)
choices_attr = arg_state.action.get_choices_callable() # type: ignore[attr-defined]
if choices_attr is None:
return []
arg_choices = choices_attr
Expand Down

0 comments on commit 30998bd

Please sign in to comment.