|
1 | 1 | # coding=utf-8 |
2 | | -"""Decorators for cmd2 commands""" |
| 2 | +"""Decorators for ``cmd2`` commands""" |
3 | 3 | import argparse |
4 | 4 | from typing import Callable, List, Optional, Union |
5 | 5 |
|
@@ -44,9 +44,9 @@ def with_argument_list(*args: List[Callable], preserve_quotes: bool = False) -> |
44 | 44 | :Example: |
45 | 45 |
|
46 | 46 | >>> class MyApp(cmd2.Cmd): |
47 | | - >>> @cmd2.with_argument_list |
48 | | - >>> def do_echo(self, arglist): |
49 | | - >>> self.poutput(' '.join(arglist) |
| 47 | + >>> @cmd2.with_argument_list |
| 48 | + >>> def do_echo(self, arglist): |
| 49 | + >>> self.poutput(' '.join(arglist) |
50 | 50 | """ |
51 | 51 | import functools |
52 | 52 |
|
@@ -112,6 +112,20 @@ def with_argparser_and_unknown_args(parser: argparse.ArgumentParser, *, |
112 | 112 | of unknown argument strings. A member called ``__statement__`` is added to the |
113 | 113 | ``Namespace`` to provide command functions access to the :class:`cmd2.Statement` |
114 | 114 | object. This can be useful if the command function needs to know the command line. |
| 115 | +
|
| 116 | + :Example: |
| 117 | +
|
| 118 | + >>> parser = argparse.ArgumentParser() |
| 119 | + >>> parser.add_argument('-p', '--piglatin', action='store_true', help='atinLay') |
| 120 | + >>> parser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE') |
| 121 | + >>> parser.add_argument('-r', '--repeat', type=int, help='output [n] times') |
| 122 | + >>> |
| 123 | + >>> class MyApp(cmd2.Cmd): |
| 124 | + >>> @cmd2.with_argparser_and_unknown_args(parser) |
| 125 | + >>> def do_argprint(self, args, unknown): |
| 126 | + >>> "Print the options and argument list this options command was called with." |
| 127 | + >>> self.poutput('args: {!r}'.format(args)) |
| 128 | + >>> self.poutput('unknowns: {}'.format(unknown)) |
115 | 129 | """ |
116 | 130 | import functools |
117 | 131 |
|
@@ -170,6 +184,20 @@ def with_argparser(parser: argparse.ArgumentParser, *, |
170 | 184 | :return: function that gets passed the argparse-parsed args in a Namespace |
171 | 185 | A member called __statement__ is added to the Namespace to provide command functions access to the |
172 | 186 | Statement object. This can be useful if the command function needs to know the command line. |
| 187 | +
|
| 188 | + :Example: |
| 189 | +
|
| 190 | + >>> parser = argparse.ArgumentParser() |
| 191 | + >>> parser.add_argument('-p', '--piglatin', action='store_true', help='atinLay') |
| 192 | + >>> parser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE') |
| 193 | + >>> parser.add_argument('-r', '--repeat', type=int, help='output [n] times') |
| 194 | + >>> parser.add_argument('words', nargs='+', help='words to print') |
| 195 | + >>> |
| 196 | + >>> class MyApp(cmd2.Cmd): |
| 197 | + >>> @cmd2.with_argparser(parser, preserve_quotes=True) |
| 198 | + >>> def do_argprint(self, args): |
| 199 | + >>> "Print the options and argument list this options command was called with." |
| 200 | + >>> self.poutput('args: {!r}'.format(args)) |
173 | 201 | """ |
174 | 202 | import functools |
175 | 203 |
|
|
0 commit comments