diff options
author | Todd Leonhardt <todd.leonhardt@gmail.com> | 2021-02-20 16:28:30 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-02-20 16:28:30 -0500 |
commit | a7119e1b26a8bae531b118b7cb8c4b195f731df6 (patch) | |
tree | 46b53d0f530d6ae273c4379272684ee80026658a /cmd2/argparse_custom.py | |
parent | 06aaf962689840631325c70ea7e9056d176c7f67 (diff) | |
parent | 486b8c726a7d657ef320e68598077c31fa664790 (diff) | |
download | cmd2-git-a7119e1b26a8bae531b118b7cb8c4b195f731df6.tar.gz |
Merge pull request #1052 from python-cmd2/black
Add black and isort GitHub Action Workflows
Diffstat (limited to 'cmd2/argparse_custom.py')
-rw-r--r-- | cmd2/argparse_custom.py | 81 |
1 files changed, 47 insertions, 34 deletions
diff --git a/cmd2/argparse_custom.py b/cmd2/argparse_custom.py index 39676764..d3f819b2 100644 --- a/cmd2/argparse_custom.py +++ b/cmd2/argparse_custom.py @@ -192,6 +192,7 @@ more details. import argparse import re import sys + # noinspection PyUnresolvedReferences,PyProtectedMember from argparse import ( ONE_OR_MORE, @@ -258,6 +259,7 @@ class CompletionItem(str): See header of this file for more information """ + def __new__(cls, value: object, *args, **kwargs) -> str: return super().__new__(cls, value) @@ -283,6 +285,7 @@ class ChoicesCallable: Enables using a callable as the choices provider for an argparse argument. While argparse has the built-in choices attribute, it is limited to an iterable. """ + def __init__(self, is_completer: bool, to_call: Callable): """ Initializer @@ -303,12 +306,13 @@ def _set_choices_callable(action: argparse.Action, choices_callable: ChoicesCall """ # Verify consistent use of parameters if action.choices is not None: - err_msg = ("None of the following parameters can be used alongside a choices parameter:\n" - "choices_provider, completer") + err_msg = "None of the following parameters can be used alongside a choices parameter:\n" "choices_provider, completer" raise (TypeError(err_msg)) elif action.nargs == 0: - err_msg = ("None of the following parameters can be used on an action that takes no arguments:\n" - "choices_provider, completer") + err_msg = ( + "None of the following parameters can be used on an action that takes no arguments:\n" + "choices_provider, completer" + ) raise (TypeError(err_msg)) setattr(action, ATTR_CHOICES_CALLABLE, choices_callable) @@ -333,13 +337,16 @@ def set_completer(action: argparse.Action, completer: Callable) -> None: orig_actions_container_add_argument = argparse._ActionsContainer.add_argument -def _add_argument_wrapper(self, *args, - nargs: Union[int, str, Tuple[int], Tuple[int, int], None] = None, - choices_provider: Optional[Callable] = None, - completer: Optional[Callable] = None, - suppress_tab_hint: bool = False, - descriptive_header: Optional[str] = None, - **kwargs) -> argparse.Action: +def _add_argument_wrapper( + self, + *args, + nargs: Union[int, str, Tuple[int], Tuple[int, int], None] = None, + choices_provider: Optional[Callable] = None, + completer: Optional[Callable] = None, + suppress_tab_hint: bool = False, + descriptive_header: Optional[str] = None, + **kwargs +) -> argparse.Action: """ Wrapper around _ActionsContainer.add_argument() which supports more settings used by cmd2 @@ -377,8 +384,7 @@ def _add_argument_wrapper(self, *args, num_params_set = len(choices_callables) - choices_callables.count(None) if num_params_set > 1: - err_msg = ("Only one of the following parameters may be used at a time:\n" - "choices_provider, completer") + err_msg = "Only one of the following parameters may be used at a time:\n" "choices_provider, completer" raise (ValueError(err_msg)) # Pre-process special ranged nargs @@ -393,8 +399,11 @@ def _add_argument_wrapper(self, *args, nargs = (nargs[0], constants.INFINITY) # Validate nargs tuple - if len(nargs) != 2 or not isinstance(nargs[0], int) or \ - not (isinstance(nargs[1], int) or nargs[1] == constants.INFINITY): + if ( + len(nargs) != 2 + or not isinstance(nargs[0], int) + or not (isinstance(nargs[1], int) or nargs[1] == constants.INFINITY) + ): raise ValueError('Ranged values for nargs must be a tuple of 1 or 2 integers') if nargs[0] >= nargs[1]: raise ValueError('Invalid nargs range. The first value must be less than the second') @@ -637,7 +646,7 @@ class Cmd2HelpFormatter(argparse.RawTextHelpFormatter): if line: lines.append(indent + ' '.join(line)) if prefix is not None: - lines[0] = lines[0][len(indent):] + lines[0] = lines[0][len(indent) :] return lines # if prog is short, follow it with optionals or positionals @@ -680,7 +689,7 @@ class Cmd2HelpFormatter(argparse.RawTextHelpFormatter): def _format_action_invocation(self, action) -> str: if not action.option_strings: default = self._get_default_metavar_for_positional(action) - metavar, = self._metavar_formatter(action, default)(1) + (metavar,) = self._metavar_formatter(action, default)(1) return metavar else: @@ -724,7 +733,8 @@ class Cmd2HelpFormatter(argparse.RawTextHelpFormatter): if isinstance(metavar, tuple): return metavar else: - return (metavar, ) * tuple_size + return (metavar,) * tuple_size + return format # noinspection PyProtectedMember @@ -760,19 +770,21 @@ class Cmd2HelpFormatter(argparse.RawTextHelpFormatter): class Cmd2ArgumentParser(argparse.ArgumentParser): """Custom ArgumentParser class that improves error and help output""" - def __init__(self, - prog=None, - usage=None, - description=None, - epilog=None, - parents=None, - formatter_class=Cmd2HelpFormatter, - prefix_chars='-', - fromfile_prefix_chars=None, - argument_default=None, - conflict_handler='error', - add_help=True, - allow_abbrev=True) -> None: + def __init__( + self, + prog=None, + usage=None, + description=None, + epilog=None, + parents=None, + formatter_class=Cmd2HelpFormatter, + prefix_chars='-', + fromfile_prefix_chars=None, + argument_default=None, + conflict_handler='error', + add_help=True, + allow_abbrev=True, + ) -> None: super(Cmd2ArgumentParser, self).__init__( prog=prog, usage=usage, @@ -785,7 +797,8 @@ class Cmd2ArgumentParser(argparse.ArgumentParser): argument_default=argument_default, conflict_handler=conflict_handler, add_help=add_help, - allow_abbrev=allow_abbrev) + allow_abbrev=allow_abbrev, + ) def add_subparsers(self, **kwargs): """ @@ -821,8 +834,7 @@ class Cmd2ArgumentParser(argparse.ArgumentParser): formatter = self._get_formatter() # usage - formatter.add_usage(self.usage, self._actions, - self._mutually_exclusive_groups) + formatter.add_usage(self.usage, self._actions, self._mutually_exclusive_groups) # description formatter.add_text(self.description) @@ -880,6 +892,7 @@ class Cmd2AttributeWrapper: This makes it easy to know which attributes in a Namespace are arguments from a parser and which were added by cmd2. """ + def __init__(self, attribute: Any): self.__attribute = attribute |