Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ htmlcov

# Visual Studio Code
.vscode

# mypy optional static type checker
.mypy_cache
1 change: 1 addition & 0 deletions cmd2/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#
# -*- coding: utf-8 -*-
"""This simply imports certain things for backwards compatibility."""
from .cmd2 import __version__, Cmd, CmdResult, Statement, EmptyStatement, categorize
from .cmd2 import with_argument_list, with_argparser, with_argparser_and_unknown_args, with_category
44 changes: 22 additions & 22 deletions cmd2/argparse_completer.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,16 @@ def my_completer(text: str, line: str, begidx: int, endidx:int, extra_param: str


class CompletionItem(str):
def __new__(cls, o, desc='', *args, **kwargs):
def __new__(cls, o, desc='', *args, **kwargs) -> str:
return str.__new__(cls, o, *args, **kwargs)

# noinspection PyMissingConstructor,PyUnusedLocal
def __init__(self, o, desc='', *args, **kwargs):
def __init__(self, o, desc='', *args, **kwargs) -> None:
self.description = desc


class _RangeAction(object):
def __init__(self, nargs: Union[int, str, Tuple[int, int], None]):
def __init__(self, nargs: Union[int, str, Tuple[int, int], None]) -> None:
self.nargs_min = None
self.nargs_max = None

Expand Down Expand Up @@ -128,7 +128,7 @@ def __init__(self,
choices=None,
required=False,
help=None,
metavar=None):
metavar=None) -> None:

_RangeAction.__init__(self, nargs)

Expand Down Expand Up @@ -157,7 +157,7 @@ def __init__(self,
choices=None,
required=False,
help=None,
metavar=None):
metavar=None) -> None:

_RangeAction.__init__(self, nargs)

Expand All @@ -174,7 +174,7 @@ def __init__(self,
metavar=metavar)


def register_custom_actions(parser: argparse.ArgumentParser):
def register_custom_actions(parser: argparse.ArgumentParser) -> None:
"""Register custom argument action types"""
parser.register('action', None, _StoreRangeAction)
parser.register('action', 'store', _StoreRangeAction)
Expand All @@ -185,14 +185,14 @@ class AutoCompleter(object):
"""Automatically command line tab completion based on argparse parameters"""

class _ArgumentState(object):
def __init__(self):
def __init__(self) -> None:
self.min = None
self.max = None
self.count = 0
self.needed = False
self.variable = False

def reset(self):
def reset(self) -> None:
"""reset tracking values"""
self.min = None
self.max = None
Expand All @@ -206,7 +206,7 @@ def __init__(self,
arg_choices: Dict[str, Union[List, Tuple, Callable]] = None,
subcmd_args_lookup: dict = None,
tab_for_arg_help: bool = True,
cmd2_app=None):
cmd2_app=None) -> None:
"""
Create an AutoCompleter

Expand Down Expand Up @@ -439,7 +439,7 @@ def consume_positional_argument() -> None:

return completion_results

def _format_completions(self, action, completions: List[Union[str, CompletionItem]]):
def _format_completions(self, action, completions: List[Union[str, CompletionItem]]) -> List[str]:
if completions and len(completions) > 1 and isinstance(completions[0], CompletionItem):
token_width = len(action.dest)
completions_with_desc = []
Expand Down Expand Up @@ -665,7 +665,7 @@ def basic_complete(text: str, line: str, begidx: int, endidx: int, match_against
class ACHelpFormatter(argparse.HelpFormatter):
"""Custom help formatter to configure ordering of help text"""

def _format_usage(self, usage, actions, groups, prefix):
def _format_usage(self, usage, actions, groups, prefix) -> str:
if prefix is None:
prefix = _('Usage: ')

Expand Down Expand Up @@ -778,7 +778,7 @@ def get_lines(parts, indent, prefix=None):
# prefix with 'usage:'
return '%s%s\n\n' % (prefix, usage)

def _format_action_invocation(self, action):
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)
Expand All @@ -803,7 +803,7 @@ def _format_action_invocation(self, action):
return ', '.join(action.option_strings) + ' ' + args_string
# End cmd2 customization

def _metavar_formatter(self, action, default_metavar):
def _metavar_formatter(self, action, default_metavar) -> Callable:
if action.metavar is not None:
result = action.metavar
elif action.choices is not None:
Expand All @@ -822,7 +822,7 @@ def format(tuple_size):
return (result, ) * tuple_size
return format

def _format_args(self, action, default_metavar):
def _format_args(self, action, default_metavar) -> str:
get_metavar = self._metavar_formatter(action, default_metavar)
# Begin cmd2 customization (less verbose)
if isinstance(action, _RangeAction) and \
Expand All @@ -837,15 +837,15 @@ def _format_args(self, action, default_metavar):
result = super()._format_args(action, default_metavar)
return result

def _split_lines(self, text, width):
def _split_lines(self, text: str, width) -> List[str]:
return text.splitlines()


# noinspection PyCompatibility
class ACArgumentParser(argparse.ArgumentParser):
"""Custom argparse class to override error method to change default help text."""

def __init__(self, *args, **kwargs):
def __init__(self, *args, **kwargs) -> None:
if 'formatter_class' not in kwargs:
kwargs['formatter_class'] = ACHelpFormatter

Expand All @@ -855,15 +855,15 @@ def __init__(self, *args, **kwargs):
self._custom_error_message = ''

# Begin cmd2 customization
def set_custom_message(self, custom_message=''):
def set_custom_message(self, custom_message: str='') -> None:
"""
Allows an error message override to the error() function, useful when forcing a
re-parse of arguments with newly required parameters
"""
self._custom_error_message = custom_message
# End cmd2 customization

def error(self, message):
def error(self, message: str) -> None:
"""Custom error override. Allows application to control the error being displayed by argparse"""
if len(self._custom_error_message) > 0:
message = self._custom_error_message
Expand All @@ -884,7 +884,7 @@ def error(self, message):
self.print_help()
sys.exit(1)

def format_help(self):
def format_help(self) -> str:
"""Copy of format_help() from argparse.ArgumentParser with tweaks to separately display required parameters"""
formatter = self._get_formatter()

Expand Down Expand Up @@ -934,7 +934,7 @@ def format_help(self):
# determine help from format above
return formatter.format_help()

def _get_nargs_pattern(self, action):
def _get_nargs_pattern(self, action) -> str:
# Override _get_nargs_pattern behavior to use the nargs ranges provided by AutoCompleter
if isinstance(action, _RangeAction) and \
action.nargs_min is not None and action.nargs_max is not None:
Expand All @@ -947,7 +947,7 @@ def _get_nargs_pattern(self, action):
return nargs_pattern
return super(ACArgumentParser, self)._get_nargs_pattern(action)

def _match_argument(self, action, arg_strings_pattern):
def _match_argument(self, action, arg_strings_pattern) -> int:
# match the pattern for this action to the arg strings
nargs_pattern = self._get_nargs_pattern(action)
match = _re.match(nargs_pattern, arg_strings_pattern)
Expand All @@ -963,7 +963,7 @@ def _match_argument(self, action, arg_strings_pattern):

# This is the official python implementation with a 5 year old patch applied
# See the comment below describing the patch
def _parse_known_args(self, arg_strings, namespace): # pragma: no cover
def _parse_known_args(self, arg_strings, namespace) -> Tuple[argparse.Namespace, List[str]]: # pragma: no cover
# replace arg strings that are file references
if self.fromfile_prefix_chars is not None:
arg_strings = self._read_args_from_files(arg_strings)
Expand Down
Loading