Skip to content

Commit 584fbdf

Browse files
committed
Fixed type checking for with_argument_list when passing preserve_quotes.
1 parent f4e9f31 commit 584fbdf

1 file changed

Lines changed: 36 additions & 12 deletions

File tree

cmd2/decorators.py

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
Any,
1111
ParamSpec,
1212
TypeAlias,
13+
overload,
1314
)
1415

1516
from . import constants
@@ -111,27 +112,50 @@ def _arg_swap(args: Sequence[Any], search_arg: Any, *replace_arg: Any) -> list[A
111112
ArgListCommandFunc: TypeAlias = UnboundCommandFunc[CmdOrSetT, [list[str]]]
112113

113114

115+
# Overload for: @with_argument_list
116+
@overload
114117
def with_argument_list(
115-
func_arg: ArgListCommandFunc[CmdOrSetT] | None = None,
118+
cmd_func: ArgListCommandFunc[CmdOrSetT],
119+
*,
120+
preserve_quotes: bool = False,
121+
) -> RawCommandFunc[CmdOrSetT]: ...
122+
123+
124+
# Overload for: @with_argument_list(preserve_quotes=True)
125+
@overload
126+
def with_argument_list(
127+
cmd_func: None = None,
128+
*,
129+
preserve_quotes: bool = False,
130+
) -> Callable[[ArgListCommandFunc[CmdOrSetT]], RawCommandFunc[CmdOrSetT]]: ...
131+
132+
133+
def with_argument_list(
134+
cmd_func: ArgListCommandFunc[CmdOrSetT] | None = None,
116135
*,
117136
preserve_quotes: bool = False,
118137
) -> RawCommandFunc[CmdOrSetT] | Callable[[ArgListCommandFunc[CmdOrSetT]], RawCommandFunc[CmdOrSetT]]:
119-
"""Decorate a ``do_*`` method to alter the arguments passed to it so it is passed a list[str].
138+
"""Decorate a ``do_*`` command function to receive a list of parsed arguments.
120139
121-
Default passes a string of whatever the user typed. With this decorator, the
122-
decorated method will receive a list of arguments parsed from user input.
140+
This decorator can be used either directly (``@with_argument_list``) or as a
141+
factory with arguments (``@with_argument_list(preserve_quotes=True)``).
123142
124-
:param func_arg: Single-element positional argument list containing ``do_*`` method
125-
this decorator is wrapping
126-
:param preserve_quotes: if ``True``, then argument quotes will not be stripped
127-
:return: function that gets passed a list of argument strings
143+
:param cmd_func: The command function being decorated.
144+
:param preserve_quotes: If ``True``, argument quotes will not be stripped from the input.
145+
:return: A command function that accepts a list of strings instead of a raw string.
128146
129147
Example:
130148
```py
131149
class MyApp(cmd2.Cmd):
150+
# Basic usage: receives a list of words with quotes stripped
132151
@cmd2.with_argument_list
133-
def do_echo(self, arglist):
134-
self.poutput(' '.join(arglist)
152+
def do_echo(self, arglist: list[str]):
153+
self.poutput(' '.join(arglist))
154+
155+
# Factory usage: preserves quotes in the argument list
156+
@cmd2.with_argument_list(preserve_quotes=True)
157+
def do_print_raw(self, arglist: list[str]):
158+
self.poutput(' '.join(arglist))
135159
```
136160
137161
"""
@@ -165,8 +189,8 @@ def cmd_wrapper(*args: Any, **kwargs: Any) -> bool | None:
165189
cmd_wrapper.__doc__ = func.__doc__
166190
return cmd_wrapper
167191

168-
if callable(func_arg):
169-
return arg_decorator(func_arg)
192+
if callable(cmd_func):
193+
return arg_decorator(cmd_func)
170194
return arg_decorator
171195

172196

0 commit comments

Comments
 (0)