|
4 | 4 | from typing import TYPE_CHECKING, Any, Callable, Dict, Iterable, List, Optional, Tuple, Union
|
5 | 5 |
|
6 | 6 | from . import constants
|
| 7 | +from .argparse_custom import Cmd2AttributeWrapper |
7 | 8 | from .exceptions import Cmd2ArgparseError
|
8 | 9 | from .parsing import Statement
|
9 | 10 |
|
@@ -186,10 +187,9 @@ def with_argparser_and_unknown_args(parser: argparse.ArgumentParser, *,
|
186 | 187 | needs to be prepopulated with state data that affects parsing.
|
187 | 188 | :param preserve_quotes: if ``True``, then arguments passed to argparse maintain their quotes
|
188 | 189 | :return: function that gets passed argparse-parsed args in a ``Namespace`` and a list
|
189 |
| - of unknown argument strings. A member called ``__statement__`` is added to the |
| 190 | + of unknown argument strings. A function called ``get_statement()`` is added to the |
190 | 191 | ``Namespace`` to provide command functions access to the :class:`cmd2.Statement`
|
191 | 192 | object. This can be useful if the command function needs to know the command line.
|
192 |
| - ``__statement__`` can also be retrieved by calling ``get_statement()`` on the ``Namespace``. |
193 | 193 |
|
194 | 194 | :Example:
|
195 | 195 |
|
@@ -223,12 +223,12 @@ def with_argparser(parser: argparse.ArgumentParser, *,
|
223 | 223 | :param ns_provider: An optional function that accepts a cmd2.Cmd object as an argument and returns an
|
224 | 224 | argparse.Namespace. This is useful if the Namespace needs to be prepopulated with
|
225 | 225 | state data that affects parsing.
|
226 |
| - :param preserve_quotes: if True, then arguments passed to argparse maintain their quotes |
| 226 | + :param preserve_quotes: if ``True``, then arguments passed to argparse maintain their quotes |
227 | 227 | :param with_unknown_args: if true, then capture unknown args
|
228 |
| - :return: function that gets passed the argparse-parsed args in a Namespace |
229 |
| - A member called __statement__ is added to the Namespace to provide command functions access to the |
230 |
| - Statement object. This can be useful if the command function needs to know the command line. |
231 |
| - ``__statement__`` can also be retrieved by calling ``get_statement()`` on the ``Namespace``. |
| 228 | + :return: function that gets passed argparse-parsed args in a ``Namespace``. A function called |
| 229 | + ``get_statement()`` is added to the ``Namespace`` to provide command functions access to |
| 230 | + the :class:`cmd2.Statement` object. This can be useful if the command function needs to know |
| 231 | + the command line. |
232 | 232 |
|
233 | 233 | :Example:
|
234 | 234 |
|
@@ -300,11 +300,16 @@ def cmd_wrapper(*args: Any, **kwargs: Dict[str, Any]) -> Optional[bool]:
|
300 | 300 | else:
|
301 | 301 | # Add statement to Namespace and a getter function for it
|
302 | 302 | setattr(ns, constants.NS_ATTR_STATEMENT, statement)
|
303 |
| - setattr(ns, 'get_statement', lambda: statement) |
| 303 | + setattr(ns, 'get_statement', Cmd2AttributeWrapper(statement)) |
304 | 304 |
|
305 | 305 | # Add getter function for subcmd handler, which can be None
|
306 |
| - subcmd_handler = getattr(ns, constants.NS_ATTR_SUBCMD_HANDLER, None) |
307 |
| - setattr(ns, 'get_handler', lambda: subcmd_handler) |
| 306 | + handler = getattr(ns, constants.NS_ATTR_SUBCMD_HANDLER, None) |
| 307 | + setattr(ns, 'get_handler', Cmd2AttributeWrapper(handler)) |
| 308 | + |
| 309 | + # Remove the subcmd handler attribute from the Namespace |
| 310 | + # since get_handler() is how a developer retrieves it. |
| 311 | + if hasattr(ns, constants.NS_ATTR_SUBCMD_HANDLER): |
| 312 | + delattr(ns, constants.NS_ATTR_SUBCMD_HANDLER) |
308 | 313 |
|
309 | 314 | args_list = _arg_swap(args, statement, *new_args)
|
310 | 315 | return func(*args_list, **kwargs)
|
|
0 commit comments