Skip to content

Commit 28e44aa

Browse files
committed
Fix for when with_argument_list is called with preserve_quotes optional argument
1 parent de70108 commit 28e44aa

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

cmd2/cmd2.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -175,25 +175,30 @@ def cat_decorator(func):
175175
return cat_decorator
176176

177177

178-
def with_argument_list(func: Callable[[Statement], Optional[bool]],
179-
preserve_quotes: bool = False) -> Callable[[List], Optional[bool]]:
178+
def with_argument_list(*args: List[Callable], preserve_quotes: bool = False) -> Callable[[List], Optional[bool]]:
180179
"""A decorator to alter the arguments passed to a do_* cmd2 method. Default passes a string of whatever the user
181180
typed. With this decorator, the decorated method will receive a list of arguments parsed from user input using
182181
shlex.split().
183182
184-
:param func: do_* method this decorator is wrapping
183+
:param args: Single-element positional argument list containing do_* method this decorator is wrapping
185184
:param preserve_quotes: if True, then argument quotes will not be stripped
186185
:return: function that gets passed a list of argument strings
187186
"""
188187
import functools
189188

190-
@functools.wraps(func)
191-
def cmd_wrapper(self, cmdline):
192-
lexed_arglist = parse_quoted_string(cmdline, preserve_quotes)
193-
return func(self, lexed_arglist)
189+
def arg_decorator(func: Callable[[Statement], Optional[bool]]):
190+
@functools.wraps(func)
191+
def cmd_wrapper(self, cmdline):
192+
lexed_arglist = parse_quoted_string(cmdline, preserve_quotes)
193+
return func(self, lexed_arglist)
194194

195-
cmd_wrapper.__doc__ = func.__doc__
196-
return cmd_wrapper
195+
cmd_wrapper.__doc__ = func.__doc__
196+
return cmd_wrapper
197+
198+
if len(args) == 1 and callable(args[0]):
199+
return arg_decorator(args[0])
200+
else:
201+
return arg_decorator
197202

198203

199204
def with_argparser_and_unknown_args(argparser: argparse.ArgumentParser, preserve_quotes: bool = False) -> \

examples/arg_print.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ def do_lprint(self, arglist):
3838
"""Print the argument list this basic command is called with."""
3939
self.poutput('lprint was called with the following list of arguments: {!r}'.format(arglist))
4040

41+
@cmd2.with_argument_list(preserve_quotes=True)
42+
def do_rprint(self, arglist):
43+
"""Print the argument list this basic command is called with (with quotes preserved)."""
44+
self.poutput('rprint was called with the following list of arguments: {!r}'.format(arglist))
45+
4146
oprint_parser = argparse.ArgumentParser()
4247
oprint_parser.add_argument('-p', '--piglatin', action='store_true', help='atinLay')
4348
oprint_parser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE')

tests/test_argparse.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ def do_arglist(self, arglist):
6868
else:
6969
self.stdout.write('False')
7070

71+
@cmd2.with_argument_list(preserve_quotes=True)
72+
def do_preservelist(self, arglist):
73+
self.stdout.write('{}'.format(arglist))
74+
7175
@cmd2.with_argument_list
7276
@cmd2.with_argument_list
7377
def do_arglisttwice(self, arglist):
@@ -174,6 +178,10 @@ def test_arglist(argparse_app):
174178
out = run_cmd(argparse_app, 'arglist "we should" get these')
175179
assert out[0] == 'True'
176180

181+
def test_preservelist(argparse_app):
182+
out = run_cmd(argparse_app, 'preservelist foo "bar baz"')
183+
assert out[0] == "['foo', '\"bar baz\"']"
184+
177185
def test_arglist_decorator_twice(argparse_app):
178186
out = run_cmd(argparse_app, 'arglisttwice "we should" get these')
179187
assert out[0] == 'we should get these'

0 commit comments

Comments
 (0)