summaryrefslogtreecommitdiff
path: root/cmd2
diff options
context:
space:
mode:
Diffstat (limited to 'cmd2')
-rw-r--r--cmd2/argparse_custom.py1
-rw-r--r--cmd2/cmd2.py15
-rw-r--r--cmd2/constants.py3
-rw-r--r--cmd2/decorators.py13
-rw-r--r--cmd2/exceptions.py1
5 files changed, 25 insertions, 8 deletions
diff --git a/cmd2/argparse_custom.py b/cmd2/argparse_custom.py
index e08db005..5dbb9f66 100644
--- a/cmd2/argparse_custom.py
+++ b/cmd2/argparse_custom.py
@@ -584,7 +584,6 @@ def _SubParsersAction_remove_parser(self, name: str):
del self.choices[name]
-
# noinspection PyProtectedMember
setattr(argparse._SubParsersAction, 'remove_parser', _SubParsersAction_remove_parser)
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py
index 65aa88e0..855431d0 100644
--- a/cmd2/cmd2.py
+++ b/cmd2/cmd2.py
@@ -413,9 +413,9 @@ class Cmd(cmd.Cmd):
existing_commandset_types = [type(command_set) for command_set in self._installed_command_sets]
for cmdset_type in all_commandset_defs:
init_sig = inspect.signature(cmdset_type.__init__)
- if not (cmdset_type in existing_commandset_types or
- len(init_sig.parameters) != 1 or
- 'self' not in init_sig.parameters):
+ if not (cmdset_type in existing_commandset_types
+ or len(init_sig.parameters) != 1
+ or 'self' not in init_sig.parameters):
cmdset = cmdset_type()
self.install_command_set(cmdset)
@@ -550,7 +550,7 @@ class Cmd(cmd.Cmd):
methods = inspect.getmembers(
cmdset,
predicate=lambda meth: isinstance(meth, Callable)
- and hasattr(meth, '__name__') and meth.__name__.startswith(COMMAND_FUNC_PREFIX))
+ and hasattr(meth, '__name__') and meth.__name__.startswith(COMMAND_FUNC_PREFIX))
for method in methods:
command_name = method[0][len(COMMAND_FUNC_PREFIX):]
@@ -562,6 +562,7 @@ class Cmd(cmd.Cmd):
command_func = self.cmd_func(command_name)
command_parser = getattr(command_func, constants.CMD_ATTR_ARGPARSER, None)
+
def check_parser_uninstallable(parser):
for action in parser._actions:
if isinstance(action, argparse._SubParsersAction):
@@ -621,7 +622,7 @@ class Cmd(cmd.Cmd):
command_handler = _partial_passthru(method, self)
else:
command_handler = method
- subcmd_parser.set_defaults(handler=command_handler)
+ subcmd_parser.set_defaults(cmd2_handler=command_handler)
def find_subcommand(action: argparse.ArgumentParser, subcmd_names: List[str]) -> argparse.ArgumentParser:
if not subcmd_names:
@@ -671,8 +672,8 @@ class Cmd(cmd.Cmd):
command_func = self.cmd_func(command_name)
if command_func is None: # pragma: no cover
- # This really shouldn't be possible since _register_subcommands would prevent this from happening
- # but keeping in case it does for some strange reason
+ # This really shouldn't be possible since _register_subcommands would prevent this from happening
+ # but keeping in case it does for some strange reason
raise CommandSetRegistrationError('Could not find command "{}" needed by subcommand: {}'
.format(command_name, str(method)))
command_parser = getattr(command_func, constants.CMD_ATTR_ARGPARSER, None)
diff --git a/cmd2/constants.py b/cmd2/constants.py
index a88ad1e2..aa2ccb6a 100644
--- a/cmd2/constants.py
+++ b/cmd2/constants.py
@@ -50,6 +50,9 @@ CMD_ATTR_ARGPARSER = 'argparser'
# Whether or not tokens are unquoted before sending to argparse
CMD_ATTR_PRESERVE_QUOTES = 'preserve_quotes'
+# optional attribute
+SUBCMD_HANDLER = 'cmd2_handler'
+
# subcommand attributes for the base command name and the subcommand name
SUBCMD_ATTR_COMMAND = 'parent_command'
SUBCMD_ATTR_NAME = 'subcommand_name'
diff --git a/cmd2/decorators.py b/cmd2/decorators.py
index 9704abbf..5947020f 100644
--- a/cmd2/decorators.py
+++ b/cmd2/decorators.py
@@ -1,6 +1,7 @@
# coding=utf-8
"""Decorators for ``cmd2`` commands"""
import argparse
+import types
from typing import TYPE_CHECKING, Any, Callable, Dict, Iterable, List, Optional, Tuple, Union
from . import constants
@@ -231,6 +232,12 @@ def with_argparser_and_unknown_args(parser: argparse.ArgumentParser, *,
raise Cmd2ArgparseError
else:
setattr(ns, '__statement__', statement)
+
+ def get_handler(self: argparse.Namespace) -> Optional[Callable]:
+ return getattr(self, constants.SUBCMD_HANDLER, None)
+
+ setattr(ns, 'get_handler', types.MethodType(get_handler, ns))
+
args_list = _arg_swap(args, statement, ns, unknown)
return func(*args_list, **kwargs)
@@ -316,6 +323,12 @@ def with_argparser(parser: argparse.ArgumentParser, *,
raise Cmd2ArgparseError
else:
setattr(ns, '__statement__', statement)
+
+ def get_handler(self: argparse.Namespace) -> Optional[Callable]:
+ return getattr(self, constants.SUBCMD_HANDLER, None)
+
+ setattr(ns, 'get_handler', types.MethodType(get_handler, ns))
+
args_list = _arg_swap(args, statement, ns)
return func(*args_list, **kwargs)
diff --git a/cmd2/exceptions.py b/cmd2/exceptions.py
index c1815e1b..b928f293 100644
--- a/cmd2/exceptions.py
+++ b/cmd2/exceptions.py
@@ -31,6 +31,7 @@ class CommandSetRegistrationError(Exception):
# The following exceptions are NOT part of the public API and are intended for internal use only.
############################################################################################################
+
class Cmd2ShlexError(Exception):
"""Raised when shlex fails to parse a command line string in StatementParser"""
pass