|
11 | 11 | Optional,
|
12 | 12 | Sequence,
|
13 | 13 | Tuple,
|
| 14 | + Type, |
14 | 15 | TypeVar,
|
15 | 16 | Union,
|
16 |
| - overload, |
17 | 17 | )
|
18 | 18 |
|
19 | 19 | from . import (
|
@@ -65,19 +65,18 @@ def cat_decorator(func: CommandFunc) -> CommandFunc:
|
65 | 65 | return cat_decorator
|
66 | 66 |
|
67 | 67 |
|
68 |
| -########################## |
69 |
| -# The _parse_positionals and _arg_swap functions allow for additional positional args to be preserved |
70 |
| -# in cmd2 command functions/callables. As long as the 2-ple of arguments we expect to be there can be |
71 |
| -# found we can swap out the statement with each decorator's specific parameters |
72 |
| -########################## |
73 |
| - |
74 |
| - |
75 | 68 | CommandParent = TypeVar('CommandParent', bound=Union['cmd2.Cmd', CommandSet])
|
| 69 | +CommandParentType = TypeVar('CommandParentType', bound=Union[Type['cmd2.Cmd'], Type[CommandSet]]) |
76 | 70 |
|
77 | 71 |
|
78 | 72 | RawCommandFuncOptionalBoolReturn = Callable[[CommandParent, Union[Statement, str]], Optional[bool]]
|
79 | 73 |
|
80 | 74 |
|
| 75 | +########################## |
| 76 | +# The _parse_positionals and _arg_swap functions allow for additional positional args to be preserved |
| 77 | +# in cmd2 command functions/callables. As long as the 2-ple of arguments we expect to be there can be |
| 78 | +# found we can swap out the statement with each decorator's specific parameters |
| 79 | +########################## |
81 | 80 | def _parse_positionals(args: Tuple[Any, ...]) -> Tuple['cmd2.Cmd', Union[Statement, str]]:
|
82 | 81 | """
|
83 | 82 | Helper function for cmd2 decorators to inspect the positional arguments until the cmd2.Cmd argument is found
|
@@ -265,28 +264,12 @@ def _set_parser_prog(parser: argparse.ArgumentParser, prog: str) -> None:
|
265 | 264 | ]
|
266 | 265 |
|
267 | 266 |
|
268 |
| -@overload |
269 |
| -def with_argparser( |
270 |
| - parser: argparse.ArgumentParser, |
271 |
| - *, |
272 |
| - ns_provider: Optional[Callable[..., argparse.Namespace]] = None, |
273 |
| - preserve_quotes: bool = False, |
274 |
| - with_unknown_args: bool = False, |
275 |
| -) -> Callable[[ArgparseCommandFunc[CommandParent]], RawCommandFuncOptionalBoolReturn[CommandParent]]: ... # pragma: no cover |
276 |
| - |
277 |
| - |
278 |
| -@overload |
279 |
| -def with_argparser( |
280 |
| - parser: Callable[[], argparse.ArgumentParser], |
281 |
| - *, |
282 |
| - ns_provider: Optional[Callable[..., argparse.Namespace]] = None, |
283 |
| - preserve_quotes: bool = False, |
284 |
| - with_unknown_args: bool = False, |
285 |
| -) -> Callable[[ArgparseCommandFunc[CommandParent]], RawCommandFuncOptionalBoolReturn[CommandParent]]: ... # pragma: no cover |
286 |
| - |
287 |
| - |
288 | 267 | def with_argparser(
|
289 |
| - parser: Union[argparse.ArgumentParser, Callable[[], argparse.ArgumentParser]], |
| 268 | + parser: Union[ |
| 269 | + argparse.ArgumentParser, # existing parser |
| 270 | + Callable[[], argparse.ArgumentParser], # function or staticmethod |
| 271 | + Callable[[CommandParentType], argparse.ArgumentParser], # Cmd or CommandSet classmethod |
| 272 | + ], |
290 | 273 | *,
|
291 | 274 | ns_provider: Optional[Callable[..., argparse.Namespace]] = None,
|
292 | 275 | preserve_quotes: bool = False,
|
@@ -413,32 +396,14 @@ def cmd_wrapper(*args: Any, **kwargs: Dict[str, Any]) -> Optional[bool]:
|
413 | 396 | return arg_decorator
|
414 | 397 |
|
415 | 398 |
|
416 |
| -@overload |
417 |
| -def as_subcommand_to( |
418 |
| - command: str, |
419 |
| - subcommand: str, |
420 |
| - parser: argparse.ArgumentParser, |
421 |
| - *, |
422 |
| - help: Optional[str] = None, |
423 |
| - aliases: Optional[List[str]] = None, |
424 |
| -) -> Callable[[ArgparseCommandFunc[CommandParent]], ArgparseCommandFunc[CommandParent]]: ... # pragma: no cover |
425 |
| - |
426 |
| - |
427 |
| -@overload |
428 |
| -def as_subcommand_to( |
429 |
| - command: str, |
430 |
| - subcommand: str, |
431 |
| - parser: Callable[[], argparse.ArgumentParser], |
432 |
| - *, |
433 |
| - help: Optional[str] = None, |
434 |
| - aliases: Optional[List[str]] = None, |
435 |
| -) -> Callable[[ArgparseCommandFunc[CommandParent]], ArgparseCommandFunc[CommandParent]]: ... # pragma: no cover |
436 |
| - |
437 |
| - |
438 | 399 | def as_subcommand_to(
|
439 | 400 | command: str,
|
440 | 401 | subcommand: str,
|
441 |
| - parser: Union[argparse.ArgumentParser, Callable[[], argparse.ArgumentParser]], |
| 402 | + parser: Union[ |
| 403 | + argparse.ArgumentParser, # existing parser |
| 404 | + Callable[[], argparse.ArgumentParser], # function or staticmethod |
| 405 | + Callable[[CommandParentType], argparse.ArgumentParser], # Cmd or CommandSet classmethod |
| 406 | + ], |
442 | 407 | *,
|
443 | 408 | help: Optional[str] = None,
|
444 | 409 | aliases: Optional[List[str]] = None,
|
|
0 commit comments