Skip to content

Commit 2e831b5

Browse files
committed
Improved documentation for argument parsing decorators
Also: - Improved type hinting for these decorators
1 parent 62fdd73 commit 2e831b5

File tree

2 files changed

+38
-13
lines changed

2 files changed

+38
-13
lines changed

cmd2/cmd2.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,17 @@ def cat_decorator(func):
173173
return cat_decorator
174174

175175

176-
def with_argument_list(func: Callable, preserve_quotes: bool=False) -> Callable:
177-
"""A decorator to alter the arguments passed to a do_* cmd2
178-
method. Default passes a string of whatever the user typed.
179-
With this decorator, the decorated method will receive a list
180-
of arguments parsed from user input using shlex.split()."""
176+
def with_argument_list(func: Callable[[Statement], Optional[bool]],
177+
preserve_quotes: bool=False) -> Callable[[List], Optional[bool]]:
178+
"""A decorator to alter the arguments passed to a do_* cmd2 method. Default passes a string of whatever the user
179+
typed. With this decorator, the decorated method will receive a list of arguments parsed from user input using
180+
shlex.split().
181+
182+
:param func: do_* method this decorator is wrapping
183+
:param preserve_quotes: if True, then arguments passed to arparse maintain their quotes
184+
:return: function that gets passed a list of argument strings
185+
"""
186+
""""""
181187
import functools
182188

183189
@functools.wraps(func)
@@ -189,18 +195,19 @@ def cmd_wrapper(self, cmdline):
189195
return cmd_wrapper
190196

191197

192-
def with_argparser_and_unknown_args(argparser: argparse.ArgumentParser, preserve_quotes: bool=False) -> Callable:
198+
def with_argparser_and_unknown_args(argparser: argparse.ArgumentParser, preserve_quotes: bool=False) -> \
199+
Callable[[argparse.Namespace, List], Optional[bool]]:
193200
"""A decorator to alter a cmd2 method to populate its ``args`` argument by parsing arguments with the given
194201
instance of argparse.ArgumentParser, but also returning unknown args as a list.
195202
196203
:param argparser: unique instance of ArgumentParser
197-
:param preserve_quotes: if True, then the arguments passed to arparse be maintain their quotes
198-
:return: function that gets passed parsed args and a list of unknown args
204+
:param preserve_quotes: if True, then arguments passed to arparse maintain their quotes
205+
:return: function that gets passed argparse-parsed args and a list of unknown argument strings
199206
"""
200207
import functools
201208

202209
# noinspection PyProtectedMember
203-
def arg_decorator(func: Callable):
210+
def arg_decorator(func: Callable[[Statement], Optional[bool]]):
204211
@functools.wraps(func)
205212
def cmd_wrapper(instance, cmdline):
206213
lexed_arglist = parse_quoted_string(cmdline, preserve_quotes)
@@ -230,18 +237,19 @@ def cmd_wrapper(instance, cmdline):
230237
return arg_decorator
231238

232239

233-
def with_argparser(argparser: argparse.ArgumentParser, preserve_quotes: bool=False) -> Callable:
240+
def with_argparser(argparser: argparse.ArgumentParser,
241+
preserve_quotes: bool=False) -> Callable[[argparse.Namespace], Optional[bool]]:
234242
"""A decorator to alter a cmd2 method to populate its ``args`` argument by parsing arguments
235243
with the given instance of argparse.ArgumentParser.
236244
237245
:param argparser: unique instance of ArgumentParser
238-
:param preserve_quotes: if True, then the arguments passed to arparse be maintain their quotes
239-
:return: function that gets passed parsed args
246+
:param preserve_quotes: if True, then arguments passed to arparse maintain their quotes
247+
:return: function that gets passed the argparse-parsed args
240248
"""
241249
import functools
242250

243251
# noinspection PyProtectedMember
244-
def arg_decorator(func: Callable):
252+
def arg_decorator(func: Callable[[Statement], Optional[bool]]):
245253
@functools.wraps(func)
246254
def cmd_wrapper(instance, cmdline):
247255
lexed_arglist = parse_quoted_string(cmdline, preserve_quotes)

docs/argument_processing.rst

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,20 @@ processing decorators in your ``cmd2`` applications.
2121
.. _argprint: https://github.com/python-cmd2/cmd2/blob/master/examples/arg_print.py
2222
.. _decorator: https://github.com/python-cmd2/cmd2/blob/master/examples/decorator_example.py
2323

24+
25+
Decorators provided by cmd2 for argument processing
26+
===================================================
27+
``cmd2`` provides the following decorators for assisting with parsing arguments passed to commands:
28+
29+
.. automethod:: cmd2.cmd2.with_argument_list
30+
.. automethod:: cmd2.cmd2.with_argparser
31+
.. automethod:: cmd2.cmd2.with_argparser_and_unknown_args
32+
33+
All of these decorators accept an optional **preserve_quotes** argument which defaults to ``False``.
34+
Setting this argument to ``True`` is useful for cases where you are passing the arguments to another
35+
command which might have its own argument parsing.
36+
37+
2438
Using the argument parser decorator
2539
===================================
2640

@@ -402,3 +416,6 @@ See the subcommands_ and tab_autocompletion_ example to learn more about how to
402416

403417
.. _subcommands: https://github.com/python-cmd2/cmd2/blob/master/examples/subcommands.py
404418
.. _tab_autocompletion: https://github.com/python-cmd2/cmd2/blob/master/examples/tab_autocompletion.py
419+
420+
421+

0 commit comments

Comments
 (0)