summaryrefslogtreecommitdiff
path: root/docs
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2019-05-16 22:40:30 -0400
committerKevin Van Brunt <kmvanbrunt@gmail.com>2019-05-16 22:40:30 -0400
commit7e374ac3133b3547961e449bec1726178fc785d7 (patch)
tree372ef09cc0c370f3fe3bc2f9637055edf5e6d75c /docs
parent7a3a84cc99322ee349167861f5a03937ade88f24 (diff)
downloadcmd2-git-7e374ac3133b3547961e449bec1726178fc785d7.tar.gz
Added documentation for ns_provider
Diffstat (limited to 'docs')
-rw-r--r--docs/argument_processing.rst24
1 files changed, 23 insertions, 1 deletions
diff --git a/docs/argument_processing.rst b/docs/argument_processing.rst
index fc1f2433..4d8adb5e 100644
--- a/docs/argument_processing.rst
+++ b/docs/argument_processing.rst
@@ -247,7 +247,7 @@ argument list instead of a string::
pass
-Using the argument parser decorator and also receiving a a list of unknown positional arguments
+Using the argument parser decorator and also receiving a list of unknown positional arguments
===============================================================================================
If you want all unknown arguments to be passed to your command as a list of strings, then
decorate the command method with the ``@with_argparser_and_unknown_args`` decorator.
@@ -275,6 +275,28 @@ Here's what it looks like::
...
+Using custom namespace with argument parser decorators
+===============================================================================================
+In some cases, it may be necessary to write custom ``argparse`` code that is dependent on state data of your
+application. To support this ability while still allowing use of the decorators, both ``@with_argparser`` and
+``@with_argparser_and_unknown_args`` have an optional argument called ``ns_provider``.
+
+``ns_provider`` is a Callable that accepts a ``cmd2.Cmd`` object as an argument and returns an ``argparse.Namespace``::
+
+ Callable[[cmd2.Cmd], argparse.Namespace]
+
+For example::
+
+ def settings_ns_provider(self) -> argparse.Namespace:
+ """Populate an argparse Namespace with current settings"""
+ ns = argparse.Namespace()
+ ns.app_settings = self.settings
+ return ns
+
+To use this function with the argparse decorators, do the following::
+
+ @with_argparser(my_parser, ns_provider=settings_ns_provider)
+
Sub-commands
============
Sub-commands are supported for commands using either the ``@with_argparser`` or