Skip to content

Commit 5d74fb2

Browse files
committed
Updated comments.
1 parent 1801d64 commit 5d74fb2

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

cmd2/decorators.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,10 +253,10 @@ def do_argprint(self, args):
253253
254254
class MyApp(cmd2.Cmd):
255255
@cmd2.with_argparser(parser, with_unknown_args=True)
256-
def do_argprint(self, args, unknown):
256+
def do_argprint(self, args: argparse.Namespace, unknown_args: list[str]):
257257
"Print the options and argument list this options command was called with."
258258
self.poutput(f'args: {args!r}')
259-
self.poutput(f'unknowns: {unknown}')
259+
self.poutput(f'unknown_args: {unknown_args}')
260260
```
261261
262262
"""
@@ -351,6 +351,11 @@ def as_subcommand_to(
351351
with other decorators that may modify the function signature or return type of the
352352
subcommand function.
353353
354+
While this decorator has permissive type hints, the subcommand function's signature
355+
must match the root command's signature. For example, if the root command uses
356+
`with_unknown_args=True`, then the subcommand function must also accept the
357+
unknown arguments list.
358+
354359
:param command: Command Name. Space-delimited subcommands may optionally be specified
355360
:param subcommand: Subcommand name
356361
:param parser: instance of Cmd2ArgumentParser or a callable that returns a Cmd2ArgumentParser for this subcommand
@@ -360,7 +365,24 @@ def as_subcommand_to(
360365
subparsers.add_parser().
361366
:param add_parser_kwargs: other registration-specific kwargs for add_parser()
362367
(e.g. deprecated [Python 3.13+])
363-
:return: Wrapper function that can receive an argparse.Namespace
368+
:return: a decorator which configures the target function to be a subcommand handler
369+
370+
Example:
371+
```py
372+
base_parser = cmd2.Cmd2ArgumentParser()
373+
base_parser.add_subparsers(metavar='SUBCOMMAND', required=True)
374+
sub_parser = cmd2.Cmd2ArgumentParser()
375+
376+
class MyApp(cmd2.Cmd):
377+
@cmd2.with_argparser(base_parser)
378+
def do_base(self, args: argparse.Namespace) -> None:
379+
args.cmd2_subcmd_handler(args)
380+
381+
@cmd2.as_subcommand_to('base', 'sub', sub_parser, help="the subcommand") -> None:
382+
def sub_handler(self, args: argparse.Namespace):
383+
self.poutput('Subcommand executed')
384+
```
385+
364386
"""
365387

366388
def arg_decorator(func: F) -> F:

0 commit comments

Comments
 (0)