diff options
| author | Jared Crapo <jared@kotfu.net> | 2018-01-10 23:17:46 -0700 |
|---|---|---|
| committer | Jared Crapo <jared@kotfu.net> | 2018-01-10 23:17:46 -0700 |
| commit | de320b61a894736e58bc5d427564eddca70bfc8e (patch) | |
| tree | 18b32e976d2d72d49aabb6f28d93cb5f4903afde | |
| parent | 0a03ab88f8bb70cc2f384c0b98a18bdead7bd417 (diff) | |
| download | cmd2-git-de320b61a894736e58bc5d427564eddca70bfc8e.tar.gz | |
Fix a nasty bug in @with_argument_parser
second argument of do_* methods was getting mangled when we strip quotes
| -rwxr-xr-x | cmd2.py | 6 | ||||
| -rw-r--r-- | tests/test_argparse.py | 16 |
2 files changed, 19 insertions, 3 deletions
@@ -248,9 +248,9 @@ def with_argument_parser(argparser): argparse.ArgumentParser. """ def arg_decorator(func): - def cmd_wrapper(instance, arg): + def cmd_wrapper(instance, cmdline): # Use shlex to split the command line into a list of arguments based on shell rules - lexed_arglist = shlex.split(arg, posix=POSIX_SHLEX) + lexed_arglist = shlex.split(cmdline, posix=POSIX_SHLEX) # If not using POSIX shlex, make sure to strip off outer quotes for convenience if not POSIX_SHLEX and STRIP_QUOTES_FOR_NON_POSIX: temp_arglist = [] @@ -258,7 +258,7 @@ def with_argument_parser(argparser): temp_arglist.append(strip_quotes(arg)) lexed_arglist = temp_arglist opts = argparser.parse_args(lexed_arglist) - func(instance, arg, opts) + func(instance, cmdline, opts) # argparser defaults the program name to sys.argv[0] # we want it to be the name of our command diff --git a/tests/test_argparse.py b/tests/test_argparse.py index e9dbc1b3..52ce7de8 100644 --- a/tests/test_argparse.py +++ b/tests/test_argparse.py @@ -2,6 +2,7 @@ """ Cmd2 testing for argument parsing """ +import re import argparse import pytest @@ -43,6 +44,16 @@ class ArgparseApp(cmd2.Cmd): self.stdout.write('<{0}>{1}</{0}>'.format(args.tag[0], ' '.join(args.content))) self.stdout.write('\n') + argparser = argparse.ArgumentParser() + argparser.add_argument('args', nargs='*') + @cmd2.with_argument_parser(argparser) + def do_compare(self, cmdline, args=None): + cmdline_str = re.sub('\s+', ' ', cmdline) + args_str = re.sub('\s+', ' ', ' '.join(args.args)) + if cmdline_str == args_str: + self.stdout.write('True') + else: + self.stdout.write('False') @pytest.fixture def argparse_app(): @@ -88,4 +99,9 @@ def test_argparse_prog(argparse_app): out = run_cmd(argparse_app, 'help tag') progname = out[0].split(' ')[1] assert progname == 'tag' + +def test_argparse_cmdline(argparse_app): + out = run_cmd(argparse_app, 'compare this is a test') + assert out[0] == 'True' +
\ No newline at end of file |
