10
10
Optional ,
11
11
Sequence ,
12
12
Tuple ,
13
+ TypeVar ,
13
14
Union ,
14
15
)
15
16
@@ -72,7 +73,10 @@ def cat_decorator(func: CommandFunc) -> CommandFunc:
72
73
##########################
73
74
74
75
75
- RawCommandFuncOptionalBoolReturn = Callable [[Union [CommandSet , 'cmd2.Cmd' ], Union [Statement , str ]], Optional [bool ]]
76
+ CommandParent = TypeVar ('CommandParent' , bound = Union ['cmd2.Cmd' , CommandSet ])
77
+
78
+
79
+ RawCommandFuncOptionalBoolReturn = Callable [[CommandParent , Union [Statement , str ]], Optional [bool ]]
76
80
77
81
78
82
def _parse_positionals (args : Tuple [Any , ...]) -> Tuple ['cmd2.Cmd' , Union [Statement , str ]]:
@@ -116,38 +120,36 @@ def _arg_swap(args: Union[Sequence[Any]], search_arg: Any, *replace_arg: Any) ->
116
120
117
121
#: Function signature for an Command Function that accepts a pre-processed argument list from user input
118
122
#: and optionally returns a boolean
119
- ArgListCommandFuncOptionalBoolReturn = Union [
120
- Callable [['cmd2.Cmd' , List [str ]], Optional [bool ]],
121
- Callable [[CommandSet , List [str ]], Optional [bool ]],
122
- ]
123
+ ArgListCommandFuncOptionalBoolReturn = Callable [[CommandParent , List [str ]], Optional [bool ]]
123
124
#: Function signature for an Command Function that accepts a pre-processed argument list from user input
124
125
#: and returns a boolean
125
- ArgListCommandFuncBoolReturn = Union [
126
- Callable [['cmd2.Cmd' , List [str ]], bool ],
127
- Callable [[CommandSet , List [str ]], bool ],
128
- ]
126
+ ArgListCommandFuncBoolReturn = Callable [[CommandParent , List [str ]], bool ]
129
127
#: Function signature for an Command Function that accepts a pre-processed argument list from user input
130
128
#: and returns Nothing
131
- ArgListCommandFuncNoneReturn = Union [
132
- Callable [['cmd2.Cmd' , List [str ]], None ],
133
- Callable [[CommandSet , List [str ]], None ],
134
- ]
129
+ ArgListCommandFuncNoneReturn = Callable [[CommandParent , List [str ]], None ]
135
130
136
131
#: Aggregate of all accepted function signatures for Command Functions that accept a pre-processed argument list
137
- ArgListCommandFunc = Union [ArgListCommandFuncOptionalBoolReturn , ArgListCommandFuncBoolReturn , ArgListCommandFuncNoneReturn ]
132
+ ArgListCommandFunc = Union [
133
+ ArgListCommandFuncOptionalBoolReturn [CommandParent ],
134
+ ArgListCommandFuncBoolReturn [CommandParent ],
135
+ ArgListCommandFuncNoneReturn [CommandParent ],
136
+ ]
138
137
139
138
140
139
def with_argument_list (
141
- func_arg : Optional [ArgListCommandFunc ] = None ,
140
+ func_arg : Optional [ArgListCommandFunc [ CommandParent ] ] = None ,
142
141
* ,
143
142
preserve_quotes : bool = False ,
144
- ) -> Union [RawCommandFuncOptionalBoolReturn , Callable [[ArgListCommandFunc ], RawCommandFuncOptionalBoolReturn ]]:
143
+ ) -> Union [
144
+ RawCommandFuncOptionalBoolReturn [CommandParent ],
145
+ Callable [[ArgListCommandFunc [CommandParent ]], RawCommandFuncOptionalBoolReturn [CommandParent ]],
146
+ ]:
145
147
"""
146
148
A decorator to alter the arguments passed to a ``do_*`` method. Default
147
149
passes a string of whatever the user typed. With this decorator, the
148
150
decorated method will receive a list of arguments parsed from user input.
149
151
150
- :param func_arg: Single-element positional argument list containing ``do_ *`` method
152
+ :param func_arg: Single-element positional argument list containing ``doi_ *`` method
151
153
this decorator is wrapping
152
154
:param preserve_quotes: if ``True``, then argument quotes will not be stripped
153
155
:return: function that gets passed a list of argument strings
@@ -161,7 +163,7 @@ def with_argument_list(
161
163
"""
162
164
import functools
163
165
164
- def arg_decorator (func : ArgListCommandFunc ) -> RawCommandFuncOptionalBoolReturn :
166
+ def arg_decorator (func : ArgListCommandFunc [ CommandParent ] ) -> RawCommandFuncOptionalBoolReturn [ CommandParent ] :
165
167
"""
166
168
Decorator function that ingests an Argument List function and returns a raw command function.
167
169
The returned function will process the raw input into an argument list to be passed to the wrapped function.
@@ -243,28 +245,19 @@ def _set_parser_prog(parser: argparse.ArgumentParser, prog: str) -> None:
243
245
244
246
#: Function signature for a Command Function that uses an argparse.ArgumentParser to process user input
245
247
#: and optionally returns a boolean
246
- ArgparseCommandFuncOptionalBoolReturn = Union [
247
- Callable [['cmd2.Cmd' , argparse .Namespace ], Optional [bool ]],
248
- Callable [[CommandSet , argparse .Namespace ], Optional [bool ]],
249
- ]
248
+ ArgparseCommandFuncOptionalBoolReturn = Callable [[CommandParent , argparse .Namespace ], Optional [bool ]]
250
249
#: Function signature for a Command Function that uses an argparse.ArgumentParser to process user input
251
250
#: and returns a boolean
252
- ArgparseCommandFuncBoolReturn = Union [
253
- Callable [['cmd2.Cmd' , argparse .Namespace ], bool ],
254
- Callable [[CommandSet , argparse .Namespace ], bool ],
255
- ]
251
+ ArgparseCommandFuncBoolReturn = Callable [[CommandParent , argparse .Namespace ], bool ]
256
252
#: Function signature for an Command Function that uses an argparse.ArgumentParser to process user input
257
253
#: and returns nothing
258
- ArgparseCommandFuncNoneReturn = Union [
259
- Callable [['cmd2.Cmd' , argparse .Namespace ], None ],
260
- Callable [[CommandSet , argparse .Namespace ], None ],
261
- ]
254
+ ArgparseCommandFuncNoneReturn = Callable [[CommandParent , argparse .Namespace ], None ]
262
255
263
256
#: Aggregate of all accepted function signatures for an argparse Command Function
264
257
ArgparseCommandFunc = Union [
265
- ArgparseCommandFuncOptionalBoolReturn ,
266
- ArgparseCommandFuncBoolReturn ,
267
- ArgparseCommandFuncNoneReturn ,
258
+ ArgparseCommandFuncOptionalBoolReturn [ CommandParent ] ,
259
+ ArgparseCommandFuncBoolReturn [ CommandParent ] ,
260
+ ArgparseCommandFuncNoneReturn [ CommandParent ] ,
268
261
]
269
262
270
263
@@ -274,7 +267,7 @@ def with_argparser(
274
267
ns_provider : Optional [Callable [..., argparse .Namespace ]] = None ,
275
268
preserve_quotes : bool = False ,
276
269
with_unknown_args : bool = False ,
277
- ) -> Callable [[ArgparseCommandFunc ] , RawCommandFuncOptionalBoolReturn ]:
270
+ ) -> Callable [[ArgparseCommandFunc [ CommandParent ]] , RawCommandFuncOptionalBoolReturn [ CommandParent ] ]:
278
271
"""A decorator to alter a cmd2 method to populate its ``args`` argument by parsing arguments
279
272
with the given instance of argparse.ArgumentParser.
280
273
@@ -320,7 +313,7 @@ def with_argparser(
320
313
"""
321
314
import functools
322
315
323
- def arg_decorator (func : ArgparseCommandFunc ) -> RawCommandFuncOptionalBoolReturn :
316
+ def arg_decorator (func : ArgparseCommandFunc [ CommandParent ] ) -> RawCommandFuncOptionalBoolReturn [ CommandParent ] :
324
317
"""
325
318
Decorator function that ingests an Argparse Command Function and returns a raw command function.
326
319
The returned function will process the raw input into an argparse Namespace to be passed to the wrapped function.
@@ -409,7 +402,7 @@ def as_subcommand_to(
409
402
* ,
410
403
help : Optional [str ] = None ,
411
404
aliases : Optional [List [str ]] = None ,
412
- ) -> Callable [[ArgparseCommandFunc ] , ArgparseCommandFunc ]:
405
+ ) -> Callable [[ArgparseCommandFunc [ CommandParent ]] , ArgparseCommandFunc [ CommandParent ] ]:
413
406
"""
414
407
Tag this method as a subcommand to an existing argparse decorated command.
415
408
@@ -423,7 +416,7 @@ def as_subcommand_to(
423
416
:return: Wrapper function that can receive an argparse.Namespace
424
417
"""
425
418
426
- def arg_decorator (func : ArgparseCommandFunc ) -> ArgparseCommandFunc :
419
+ def arg_decorator (func : ArgparseCommandFunc [ CommandParent ] ) -> ArgparseCommandFunc [ CommandParent ] :
427
420
_set_parser_prog (parser , command + ' ' + subcommand )
428
421
429
422
# If the description has not been set, then use the method docstring if one exists
0 commit comments