summaryrefslogtreecommitdiff
path: root/cmd2/cmd2.py
diff options
context:
space:
mode:
authorTodd Leonhardt <todd.leonhardt@gmail.com>2019-05-20 10:03:51 -0400
committerGitHub <noreply@github.com>2019-05-20 10:03:51 -0400
commit1fd474fc11d22e0c1201784cf4602139e3f7c637 (patch)
tree6d90e20556a22d0b7320c4264f390f2a52e3a2bb /cmd2/cmd2.py
parent3ee97d121887d3055fc6326b1d9bc290f5235866 (diff)
parent35d25d5aa33324fa0fa3ce74f5af48d246fc5361 (diff)
downloadcmd2-git-1fd474fc11d22e0c1201784cf4602139e3f7c637.tar.gz
Merge pull request #681 from python-cmd2/ns_provider
Ns provider
Diffstat (limited to 'cmd2/cmd2.py')
-rw-r--r--cmd2/cmd2.py28
1 files changed, 24 insertions, 4 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py
index c29a1812..f5a2a844 100644
--- a/cmd2/cmd2.py
+++ b/cmd2/cmd2.py
@@ -191,12 +191,17 @@ def with_argument_list(*args: List[Callable], preserve_quotes: bool = False) ->
return arg_decorator
-def with_argparser_and_unknown_args(argparser: argparse.ArgumentParser, preserve_quotes: bool = False) -> \
+def with_argparser_and_unknown_args(argparser: argparse.ArgumentParser, *,
+ ns_provider: Optional[Callable[..., argparse.Namespace]] = None,
+ preserve_quotes: bool = False) -> \
Callable[[argparse.Namespace, List], Optional[bool]]:
"""A decorator to alter a cmd2 method to populate its ``args`` argument by parsing arguments with the given
instance of argparse.ArgumentParser, but also returning unknown args as a list.
:param argparser: unique instance of ArgumentParser
+ :param ns_provider: An optional function that accepts a cmd2.Cmd object as an argument and returns an
+ argparse.Namespace. This is useful if the Namespace needs to be prepopulated with
+ state data that affects parsing.
:param preserve_quotes: if True, then arguments passed to argparse maintain their quotes
:return: function that gets passed argparse-parsed args in a Namespace and a list of unknown argument strings
A member called __statement__ is added to the Namespace to provide command functions access to the
@@ -213,8 +218,13 @@ def with_argparser_and_unknown_args(argparser: argparse.ArgumentParser, preserve
statement,
preserve_quotes)
+ if ns_provider is None:
+ namespace = None
+ else:
+ namespace = ns_provider(cmd2_instance)
+
try:
- args, unknown = argparser.parse_known_args(parsed_arglist)
+ args, unknown = argparser.parse_known_args(parsed_arglist, namespace)
except SystemExit:
return
else:
@@ -241,12 +251,16 @@ def with_argparser_and_unknown_args(argparser: argparse.ArgumentParser, preserve
return arg_decorator
-def with_argparser(argparser: argparse.ArgumentParser,
+def with_argparser(argparser: argparse.ArgumentParser, *,
+ ns_provider: Optional[Callable[..., argparse.Namespace]] = None,
preserve_quotes: bool = False) -> Callable[[argparse.Namespace], Optional[bool]]:
"""A decorator to alter a cmd2 method to populate its ``args`` argument by parsing arguments
with the given instance of argparse.ArgumentParser.
:param argparser: unique instance of ArgumentParser
+ :param ns_provider: An optional function that accepts a cmd2.Cmd object as an argument and returns an
+ argparse.Namespace. This is useful if the Namespace needs to be prepopulated with
+ state data that affects parsing.
:param preserve_quotes: if True, then arguments passed to argparse maintain their quotes
:return: function that gets passed the argparse-parsed args in a Namespace
A member called __statement__ is added to the Namespace to provide command functions access to the
@@ -261,8 +275,14 @@ def with_argparser(argparser: argparse.ArgumentParser,
statement, parsed_arglist = cmd2_instance.statement_parser.get_command_arg_list(command_name,
statement,
preserve_quotes)
+
+ if ns_provider is None:
+ namespace = None
+ else:
+ namespace = ns_provider(cmd2_instance)
+
try:
- args = argparser.parse_args(parsed_arglist)
+ args = argparser.parse_args(parsed_arglist, namespace)
except SystemExit:
return
else: