|
10 | 10 | Any, |
11 | 11 | ParamSpec, |
12 | 12 | TypeAlias, |
| 13 | + overload, |
13 | 14 | ) |
14 | 15 |
|
15 | 16 | from . import constants |
@@ -111,27 +112,50 @@ def _arg_swap(args: Sequence[Any], search_arg: Any, *replace_arg: Any) -> list[A |
111 | 112 | ArgListCommandFunc: TypeAlias = UnboundCommandFunc[CmdOrSetT, [list[str]]] |
112 | 113 |
|
113 | 114 |
|
| 115 | +# Overload for: @with_argument_list |
| 116 | +@overload |
114 | 117 | 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, |
116 | 135 | *, |
117 | 136 | preserve_quotes: bool = False, |
118 | 137 | ) -> 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. |
120 | 139 |
|
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)``). |
123 | 142 |
|
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. |
128 | 146 |
|
129 | 147 | Example: |
130 | 148 | ```py |
131 | 149 | class MyApp(cmd2.Cmd): |
| 150 | + # Basic usage: receives a list of words with quotes stripped |
132 | 151 | @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)) |
135 | 159 | ``` |
136 | 160 |
|
137 | 161 | """ |
@@ -165,8 +189,8 @@ def cmd_wrapper(*args: Any, **kwargs: Any) -> bool | None: |
165 | 189 | cmd_wrapper.__doc__ = func.__doc__ |
166 | 190 | return cmd_wrapper |
167 | 191 |
|
168 | | - if callable(func_arg): |
169 | | - return arg_decorator(func_arg) |
| 192 | + if callable(cmd_func): |
| 193 | + return arg_decorator(cmd_func) |
170 | 194 | return arg_decorator |
171 | 195 |
|
172 | 196 |
|
|
0 commit comments