|
18 | 18 | flag (``dry_run`` -> ``--dry-run``); pass an explicit ``Option("--my_flag")`` to opt out. |
19 | 19 | Positional-only parameters (before ``/``) and ``**kwargs`` raise ``TypeError``. The parameter |
20 | 20 | names ``dest`` and ``subcommand`` are reserved; ``cmd2_statement`` receives the parsed |
21 | | -``Statement`` and (with ``base_command=True``) ``cmd2_handler`` receives the subcommand handler: |
| 21 | +``Statement`` and (with ``base_command=True``) ``cmd2_subcommand_func`` receives the subcommand handler: |
22 | 22 |
|
23 | 23 | class MyApp(cmd2.Cmd): |
24 | 24 | @cmd2.with_annotated |
@@ -1704,7 +1704,7 @@ def _const_mismatches_type(a: _ArgparseArgument) -> bool: |
1704 | 1704 |
|
1705 | 1705 | # Parameters handled specially by the decorator and not added to the parser. The first positional |
1706 | 1706 | # parameter (self/cls) is always skipped by position; these cover additional decorator-managed names. |
1707 | | -_SKIP_PARAMS = frozenset({"cmd2_handler", "cmd2_statement"}) |
| 1707 | +_SKIP_PARAMS = frozenset({constants.NS_ATTR_SUBCOMMAND_FUNC, constants.NS_ATTR_STATEMENT}) |
1708 | 1708 |
|
1709 | 1709 |
|
1710 | 1710 | def _link_group_membership( |
@@ -1735,14 +1735,17 @@ def _resolve_parameters( |
1735 | 1735 | """Resolve a function signature into a list of argparse-argument builders. |
1736 | 1736 |
|
1737 | 1737 | ``base_command`` marks each argument's context for the base-command :data:`_CONSTRAINTS` rows and |
1738 | | - drives the function-level ``cmd2_handler`` check below. ``groups``/``mutually_exclusive_groups`` |
| 1738 | + drives the function-level ``cmd2_subcommand_func`` check below. ``groups``/``mutually_exclusive_groups`` |
1739 | 1739 | are linked onto each argument as membership facts for the cross-config constraint rows. |
1740 | 1740 | """ |
1741 | 1741 | sig = inspect.signature(func) |
1742 | 1742 | # Function-level check (not a per-argument _CONSTRAINTS row): a base command dispatches through |
1743 | | - # cmd2_handler, so it must exist. Here so it also fires when the function has zero parameters. |
1744 | | - if base_command and "cmd2_handler" not in sig.parameters: |
1745 | | - raise TypeError(f"with_annotated(base_command=True) requires a 'cmd2_handler' parameter in {func.__qualname__}") |
| 1743 | + # cmd2_subcommand_func, so it must exist. Here so it also fires when the function has zero parameters. |
| 1744 | + if base_command and constants.NS_ATTR_SUBCOMMAND_FUNC not in sig.parameters: |
| 1745 | + raise TypeError( |
| 1746 | + f"with_annotated(base_command=True) requires a '{constants.NS_ATTR_SUBCOMMAND_FUNC}' " |
| 1747 | + f"parameter in {func.__qualname__}" |
| 1748 | + ) |
1746 | 1749 | try: |
1747 | 1750 | hints = get_type_hints(func, include_extras=True) |
1748 | 1751 | except (NameError, AttributeError, TypeError) as exc: |
@@ -1849,8 +1852,6 @@ def _filtered_namespace_kwargs( |
1849 | 1852 | for key, value in vars(ns).items(): |
1850 | 1853 | if accepted is not None and key not in accepted: |
1851 | 1854 | continue |
1852 | | - if key == constants.NS_ATTR_SUBCOMMAND_FUNC: |
1853 | | - continue |
1854 | 1855 | if exclude_subcommand and key == "subcommand": |
1855 | 1856 | continue |
1856 | 1857 | filtered[key] = value |
@@ -2120,10 +2121,10 @@ def _build_subcommand_handler( |
2120 | 2121 | def handler(self_arg: Any, ns: Any) -> Any: |
2121 | 2122 | """Unpack Namespace into typed kwargs for the subcommand handler.""" |
2122 | 2123 | filtered = _filtered_namespace_kwargs(ns, accepted=_accepted) |
2123 | | - if "cmd2_handler" in filtered: |
2124 | | - cmd2_h = filtered["cmd2_handler"] |
| 2124 | + if constants.NS_ATTR_SUBCOMMAND_FUNC in filtered: |
| 2125 | + cmd2_h = filtered[constants.NS_ATTR_SUBCOMMAND_FUNC] |
2125 | 2126 | if isinstance(cmd2_h, functools.partial) and cmd2_h.func is handler: |
2126 | | - filtered["cmd2_handler"] = None |
| 2127 | + filtered[constants.NS_ATTR_SUBCOMMAND_FUNC] = None |
2127 | 2128 | return _invoke_command_func( |
2128 | 2129 | func, self_arg, filtered, leading_names=_leading_names, var_positional_name=_var_positional_name |
2129 | 2130 | ) |
@@ -2185,7 +2186,7 @@ def with_annotated( |
2185 | 2186 | :param ns_provider: callable returning a prepopulated Namespace (not with ``subcommand_to``) |
2186 | 2187 | :param preserve_quotes: preserve quotes in arguments (not with ``subcommand_to``) |
2187 | 2188 | :param with_unknown_args: capture unknown args as the ``_unknown`` kwarg (not with ``subcommand_to``) |
2188 | | - :param base_command: add ``add_subparsers()``; requires a ``cmd2_handler`` param and no positionals |
| 2189 | + :param base_command: add ``add_subparsers()``; requires a ``cmd2_subcommand_func`` param and no positionals |
2189 | 2190 | :param subcommand_to: parent command name; function must be named ``{parent_underscored}_{subcommand}`` |
2190 | 2191 | :param help: subcommand help text (only with ``subcommand_to``) |
2191 | 2192 | :param aliases: alternative subcommand names (only with ``subcommand_to``) |
@@ -2247,9 +2248,10 @@ def decorator(fn: Callable[..., Any]) -> Callable[..., Any]: |
2247 | 2248 | if unknown_param.kind is inspect.Parameter.POSITIONAL_ONLY: |
2248 | 2249 | raise TypeError("Parameter _unknown must be keyword-compatible when with_unknown_args=True") |
2249 | 2250 |
|
2250 | | - if not base_command and "cmd2_handler" in inspect.signature(fn).parameters: |
| 2251 | + if not base_command and constants.NS_ATTR_SUBCOMMAND_FUNC in inspect.signature(fn).parameters: |
2251 | 2252 | raise TypeError( |
2252 | | - f"Parameter 'cmd2_handler' in {fn.__qualname__} is only valid when with_annotated(base_command=True) is used." |
| 2253 | + f"Parameter '{constants.NS_ATTR_SUBCOMMAND_FUNC}' in {fn.__qualname__} " |
| 2254 | + "is only valid when with_annotated(base_command=True) is used." |
2253 | 2255 | ) |
2254 | 2256 |
|
2255 | 2257 | if subcommand_to is not None: |
@@ -2314,7 +2316,7 @@ def cmd_wrapper(*args: Any, **kwargs: Any) -> bool | None: |
2314 | 2316 | handler = getattr(ns, constants.NS_ATTR_SUBCOMMAND_FUNC, None) |
2315 | 2317 | if base_command and handler is not None: |
2316 | 2318 | handler = functools.partial(handler, ns) |
2317 | | - ns.cmd2_handler = handler |
| 2319 | + setattr(ns, constants.NS_ATTR_SUBCOMMAND_FUNC, handler) |
2318 | 2320 |
|
2319 | 2321 | func_kwargs = _filtered_namespace_kwargs(ns, accepted=accepted, exclude_subcommand=base_command) |
2320 | 2322 |
|
|
0 commit comments