66 Any ,
77 Callable ,
88 Dict ,
9- Iterable ,
109 List ,
1110 Optional ,
1211 Tuple ,
1918from .argparse_custom import (
2019 Cmd2AttributeWrapper ,
2120)
21+ from .command_definition import (
22+ CommandFunc ,
23+ CommandSet ,
24+ )
2225from .exceptions import (
2326 Cmd2ArgparseError ,
2427)
3033 import cmd2
3134
3235
33- def with_category (category : str ) -> Callable :
36+ def with_category (category : str ) -> Callable [[ CommandFunc ], CommandFunc ] :
3437 """A decorator to apply a category to a ``do_*`` command method.
3538
3639 :param category: the name of the category in which this command should
@@ -47,7 +50,7 @@ def with_category(category: str) -> Callable:
4750 :func:`~cmd2.utils.categorize`
4851 """
4952
50- def cat_decorator (func ) :
53+ def cat_decorator (func : CommandFunc ) -> CommandFunc :
5154 from .utils import (
5255 categorize ,
5356 )
@@ -65,7 +68,7 @@ def cat_decorator(func):
6568##########################
6669
6770
68- def _parse_positionals (args : Tuple ) -> Tuple [Union [ 'cmd2.Cmd' , 'cmd2.CommandSet' ] , Union [Statement , str ]]:
71+ def _parse_positionals (args : Tuple [ Any , ...] ) -> Tuple ['cmd2.Cmd' , Union [Statement , str ]]:
6972 """
7073 Helper function for cmd2 decorators to inspect the positional arguments until the cmd2.Cmd argument is found
7174 Assumes that we will find cmd2.Cmd followed by the command statement object or string.
@@ -75,7 +78,6 @@ def _parse_positionals(args: Tuple) -> Tuple[Union['cmd2.Cmd', 'cmd2.CommandSet'
7578 for pos , arg in enumerate (args ):
7679 from cmd2 import (
7780 Cmd ,
78- CommandSet ,
7981 )
8082
8183 if (isinstance (arg , Cmd ) or isinstance (arg , CommandSet )) and len (args ) > pos :
@@ -104,13 +106,15 @@ def _arg_swap(args: Union[Tuple[Any], List[Any]], search_arg: Any, *replace_arg:
104106 return args_list
105107
106108
107- def with_argument_list (* args : List [Callable ], preserve_quotes : bool = False ) -> Callable [[List ], Optional [bool ]]:
109+ def with_argument_list (
110+ func_arg : Optional [Callable [[List [str ]], Optional [bool ]]] = None , * , preserve_quotes : bool = False
111+ ) -> Union [Callable [[List [str ]], Optional [bool ]],]:
108112 """
109113 A decorator to alter the arguments passed to a ``do_*`` method. Default
110114 passes a string of whatever the user typed. With this decorator, the
111115 decorated method will receive a list of arguments parsed from user input.
112116
113- :param args : Single-element positional argument list containing ``do_*`` method
117+ :param func_arg : Single-element positional argument list containing ``do_*`` method
114118 this decorator is wrapping
115119 :param preserve_quotes: if ``True``, then argument quotes will not be stripped
116120 :return: function that gets passed a list of argument strings
@@ -124,9 +128,9 @@ def with_argument_list(*args: List[Callable], preserve_quotes: bool = False) ->
124128 """
125129 import functools
126130
127- def arg_decorator (func : Callable ) :
131+ def arg_decorator (func : Callable [[ List [ str ]], Optional [ bool ]]) -> Callable [..., Optional [ bool ]] :
128132 @functools .wraps (func )
129- def cmd_wrapper (* args , ** kwargs : Dict [ str , Any ] ) -> Optional [bool ]:
133+ def cmd_wrapper (* args : Any , ** kwargs : Any ) -> Optional [bool ]:
130134 """
131135 Command function wrapper which translates command line into an argument list and calls actual command function
132136
@@ -145,9 +149,9 @@ def cmd_wrapper(*args, **kwargs: Dict[str, Any]) -> Optional[bool]:
145149 cmd_wrapper .__doc__ = func .__doc__
146150 return cmd_wrapper
147151
148- if len ( args ) == 1 and callable (args [ 0 ] ):
152+ if callable (func_arg ):
149153 # noinspection PyTypeChecker
150- return arg_decorator (args [ 0 ] )
154+ return arg_decorator (func_arg )
151155 else :
152156 # noinspection PyTypeChecker
153157 return arg_decorator
@@ -196,13 +200,30 @@ def _set_parser_prog(parser: argparse.ArgumentParser, prog: str):
196200 break
197201
198202
203+ ArgparseCommandFunc = Union [
204+ Callable [['cmd2.Cmd' , argparse .Namespace ], Optional [bool ]],
205+ Callable [[CommandSet , argparse .Namespace ], Optional [bool ]],
206+ ]
207+ ArgparseCommandFuncBoolReturn = Union [
208+ Callable [['cmd2.Cmd' , argparse .Namespace ], bool ],
209+ Callable [[CommandSet , argparse .Namespace ], bool ],
210+ ]
211+ ArgparseCommandFuncNoneReturn = Union [
212+ Callable [['cmd2.Cmd' , argparse .Namespace ], None ],
213+ Callable [[CommandSet , argparse .Namespace ], None ],
214+ ]
215+
216+
199217def with_argparser (
200218 parser : argparse .ArgumentParser ,
201219 * ,
202220 ns_provider : Optional [Callable [..., argparse .Namespace ]] = None ,
203221 preserve_quotes : bool = False ,
204- with_unknown_args : bool = False
205- ) -> Callable [[argparse .Namespace ], Optional [bool ]]:
222+ with_unknown_args : bool = False ,
223+ ) -> Callable [
224+ [Union [ArgparseCommandFunc , ArgparseCommandFuncNoneReturn , ArgparseCommandFuncBoolReturn ]],
225+ Callable [[Union ['cmd2.Cmd' , CommandSet ], str ], Optional [bool ]],
226+ ]:
206227 """A decorator to alter a cmd2 method to populate its ``args`` argument by parsing arguments
207228 with the given instance of argparse.ArgumentParser.
208229
@@ -248,7 +269,9 @@ def with_argparser(
248269 """
249270 import functools
250271
251- def arg_decorator (func : Callable ):
272+ def arg_decorator (
273+ func : Union [ArgparseCommandFunc , ArgparseCommandFuncBoolReturn , ArgparseCommandFuncNoneReturn ],
274+ ) -> ArgparseCommandFunc :
252275 @functools .wraps (func )
253276 def cmd_wrapper (* args : Any , ** kwargs : Dict [str , Any ]) -> Optional [bool ]:
254277 """
@@ -327,8 +350,11 @@ def as_subcommand_to(
327350 parser : argparse .ArgumentParser ,
328351 * ,
329352 help : Optional [str ] = None ,
330- aliases : Iterable [str ] = None
331- ) -> Callable [[argparse .Namespace ], Optional [bool ]]:
353+ aliases : Optional [List [str ]] = None ,
354+ ) -> Callable [
355+ [Union [ArgparseCommandFunc , ArgparseCommandFuncNoneReturn , ArgparseCommandFuncBoolReturn ]],
356+ Union [ArgparseCommandFunc , ArgparseCommandFuncBoolReturn , ArgparseCommandFuncNoneReturn ],
357+ ]:
332358 """
333359 Tag this method as a subcommand to an existing argparse decorated command.
334360
@@ -342,7 +368,9 @@ def as_subcommand_to(
342368 :return: Wrapper function that can receive an argparse.Namespace
343369 """
344370
345- def arg_decorator (func : Callable ):
371+ def arg_decorator (
372+ func : Union [ArgparseCommandFunc , ArgparseCommandFuncBoolReturn , ArgparseCommandFuncNoneReturn ]
373+ ) -> Union [ArgparseCommandFunc , ArgparseCommandFuncBoolReturn , ArgparseCommandFuncNoneReturn ]:
346374 _set_parser_prog (parser , command + ' ' + subcommand )
347375
348376 # If the description has not been set, then use the method docstring if one exists
@@ -355,10 +383,10 @@ def arg_decorator(func: Callable):
355383 setattr (func , constants .SUBCMD_ATTR_NAME , subcommand )
356384
357385 # Keyword arguments for ArgumentParser.add_subparser()
358- add_parser_kwargs = dict ()
386+ add_parser_kwargs : Dict [ str , Any ] = dict ()
359387 if help is not None :
360388 add_parser_kwargs ['help' ] = help
361- if aliases is not None :
389+ if aliases :
362390 add_parser_kwargs ['aliases' ] = aliases [:]
363391
364392 setattr (func , constants .SUBCMD_ATTR_ADD_PARSER_KWARGS , add_parser_kwargs )
0 commit comments