summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cmd2/cmd2.py23
-rwxr-xr-xexamples/arg_print.py5
-rw-r--r--tests/test_argparse.py8
3 files changed, 27 insertions, 9 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py
index 24e140fd..d124ac23 100644
--- a/cmd2/cmd2.py
+++ b/cmd2/cmd2.py
@@ -175,25 +175,30 @@ def with_category(category: str) -> Callable:
return cat_decorator
-def with_argument_list(func: Callable[[Statement], Optional[bool]],
- preserve_quotes: bool = False) -> Callable[[List], Optional[bool]]:
+def with_argument_list(*args: List[Callable], preserve_quotes: bool = False) -> Callable[[List], Optional[bool]]:
"""A decorator to alter the arguments passed to a do_* cmd2 method. Default passes a string of whatever the user
typed. With this decorator, the decorated method will receive a list of arguments parsed from user input using
shlex.split().
- :param func: do_* method this decorator is wrapping
+ :param args: Single-element positional argument list containing do_* method this decorator is wrapping
:param preserve_quotes: if True, then argument quotes will not be stripped
:return: function that gets passed a list of argument strings
"""
import functools
- @functools.wraps(func)
- def cmd_wrapper(self, cmdline):
- lexed_arglist = parse_quoted_string(cmdline, preserve_quotes)
- return func(self, lexed_arglist)
+ def arg_decorator(func: Callable[[Statement], Optional[bool]]):
+ @functools.wraps(func)
+ def cmd_wrapper(self, cmdline):
+ lexed_arglist = parse_quoted_string(cmdline, preserve_quotes)
+ return func(self, lexed_arglist)
- cmd_wrapper.__doc__ = func.__doc__
- return cmd_wrapper
+ cmd_wrapper.__doc__ = func.__doc__
+ return cmd_wrapper
+
+ if len(args) == 1 and callable(args[0]):
+ return arg_decorator(args[0])
+ else:
+ return arg_decorator
def with_argparser_and_unknown_args(argparser: argparse.ArgumentParser, preserve_quotes: bool = False) -> \
diff --git a/examples/arg_print.py b/examples/arg_print.py
index 1a103858..18d21787 100755
--- a/examples/arg_print.py
+++ b/examples/arg_print.py
@@ -38,6 +38,11 @@ class ArgumentAndOptionPrinter(cmd2.Cmd):
"""Print the argument list this basic command is called with."""
self.poutput('lprint was called with the following list of arguments: {!r}'.format(arglist))
+ @cmd2.with_argument_list(preserve_quotes=True)
+ def do_rprint(self, arglist):
+ """Print the argument list this basic command is called with (with quotes preserved)."""
+ self.poutput('rprint was called with the following list of arguments: {!r}'.format(arglist))
+
oprint_parser = argparse.ArgumentParser()
oprint_parser.add_argument('-p', '--piglatin', action='store_true', help='atinLay')
oprint_parser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE')
diff --git a/tests/test_argparse.py b/tests/test_argparse.py
index 7db35c71..a055ac72 100644
--- a/tests/test_argparse.py
+++ b/tests/test_argparse.py
@@ -68,6 +68,10 @@ class ArgparseApp(cmd2.Cmd):
else:
self.stdout.write('False')
+ @cmd2.with_argument_list(preserve_quotes=True)
+ def do_preservelist(self, arglist):
+ self.stdout.write('{}'.format(arglist))
+
@cmd2.with_argument_list
@cmd2.with_argument_list
def do_arglisttwice(self, arglist):
@@ -174,6 +178,10 @@ def test_arglist(argparse_app):
out = run_cmd(argparse_app, 'arglist "we should" get these')
assert out[0] == 'True'
+def test_preservelist(argparse_app):
+ out = run_cmd(argparse_app, 'preservelist foo "bar baz"')
+ assert out[0] == "['foo', '\"bar baz\"']"
+
def test_arglist_decorator_twice(argparse_app):
out = run_cmd(argparse_app, 'arglisttwice "we should" get these')
assert out[0] == 'we should get these'