Skip to content

Commit 7e374ac

Browse files
committed
Added documentation for ns_provider
1 parent 7a3a84c commit 7e374ac

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

cmd2/cmd2.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -192,15 +192,16 @@ def cmd_wrapper(cmd2_instance, statement: Union[Statement, str]):
192192

193193

194194
def with_argparser_and_unknown_args(argparser: argparse.ArgumentParser, *,
195-
ns_provider: Optional[Callable[[None], argparse.Namespace]] = None,
195+
ns_provider: Optional[Callable[..., argparse.Namespace]] = None,
196196
preserve_quotes: bool = False) -> \
197197
Callable[[argparse.Namespace, List], Optional[bool]]:
198198
"""A decorator to alter a cmd2 method to populate its ``args`` argument by parsing arguments with the given
199199
instance of argparse.ArgumentParser, but also returning unknown args as a list.
200200
201201
:param argparser: unique instance of ArgumentParser
202-
:param ns_provider: an optional function that provides the Namespace for parse_known_args().
203-
this is useful if the Namespace needs to be prepopulated based on instance data.
202+
:param ns_provider: An optional function that accepts a cmd2.Cmd object as an argument and returns an
203+
argparse.Namespace. This is useful if the Namespace needs to be prepopulated with
204+
state data that affects parsing.
204205
:param preserve_quotes: if True, then arguments passed to argparse maintain their quotes
205206
:return: function that gets passed argparse-parsed args in a Namespace and a list of unknown argument strings
206207
A member called __statement__ is added to the Namespace to provide command functions access to the
@@ -251,14 +252,15 @@ def cmd_wrapper(cmd2_instance, statement: Union[Statement, str]):
251252

252253

253254
def with_argparser(argparser: argparse.ArgumentParser, *,
254-
ns_provider: Optional[Callable[[None], argparse.Namespace]] = None,
255+
ns_provider: Optional[Callable[..., argparse.Namespace]] = None,
255256
preserve_quotes: bool = False) -> Callable[[argparse.Namespace], Optional[bool]]:
256257
"""A decorator to alter a cmd2 method to populate its ``args`` argument by parsing arguments
257258
with the given instance of argparse.ArgumentParser.
258259
259260
:param argparser: unique instance of ArgumentParser
260-
:param ns_provider: an optional function that provides the Namespace for parse_args().
261-
this is useful if the Namespace needs to be prepopulated based on instance data.
261+
:param ns_provider: An optional function that accepts a cmd2.Cmd object as an argument and returns an
262+
argparse.Namespace. This is useful if the Namespace needs to be prepopulated with
263+
state data that affects parsing.
262264
:param preserve_quotes: if True, then arguments passed to argparse maintain their quotes
263265
:return: function that gets passed the argparse-parsed args in a Namespace
264266
A member called __statement__ is added to the Namespace to provide command functions access to the

docs/argument_processing.rst

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ argument list instead of a string::
247247
pass
248248

249249

250-
Using the argument parser decorator and also receiving a a list of unknown positional arguments
250+
Using the argument parser decorator and also receiving a list of unknown positional arguments
251251
===============================================================================================
252252
If you want all unknown arguments to be passed to your command as a list of strings, then
253253
decorate the command method with the ``@with_argparser_and_unknown_args`` decorator.
@@ -275,6 +275,28 @@ Here's what it looks like::
275275

276276
...
277277

278+
Using custom namespace with argument parser decorators
279+
===============================================================================================
280+
In some cases, it may be necessary to write custom ``argparse`` code that is dependent on state data of your
281+
application. To support this ability while still allowing use of the decorators, both ``@with_argparser`` and
282+
``@with_argparser_and_unknown_args`` have an optional argument called ``ns_provider``.
283+
284+
``ns_provider`` is a Callable that accepts a ``cmd2.Cmd`` object as an argument and returns an ``argparse.Namespace``::
285+
286+
Callable[[cmd2.Cmd], argparse.Namespace]
287+
288+
For example::
289+
290+
def settings_ns_provider(self) -> argparse.Namespace:
291+
"""Populate an argparse Namespace with current settings"""
292+
ns = argparse.Namespace()
293+
ns.app_settings = self.settings
294+
return ns
295+
296+
To use this function with the argparse decorators, do the following::
297+
298+
@with_argparser(my_parser, ns_provider=settings_ns_provider)
299+
278300
Sub-commands
279301
============
280302
Sub-commands are supported for commands using either the ``@with_argparser`` or

0 commit comments

Comments
 (0)