diff options
author | Jared Crapo <jared@kotfu.net> | 2018-01-07 12:06:11 -0700 |
---|---|---|
committer | Jared Crapo <jared@kotfu.net> | 2018-01-07 12:06:11 -0700 |
commit | d63c878413006630834324d71bd22f012bc543a8 (patch) | |
tree | 70cd68c89067338921653038df21d3947bdfa46e | |
parent | 67e669e3345fb637e4f4779691a7a8ec4b1763f6 (diff) | |
download | cmd2-git-d63c878413006630834324d71bd22f012bc543a8.tar.gz |
New test cases for argparse
-rwxr-xr-x | cmd2.py | 16 | ||||
-rw-r--r-- | tests/test_argparse.py | 54 |
2 files changed, 68 insertions, 2 deletions
@@ -247,10 +247,22 @@ def with_argument_parser(argparser): """ def arg_decorator(func): def cmd_wrapper(instance, arg): - print("before command") + #print("before command") + # Use shlex to split the command line into a list of arguments based on shell rules opts = argparser.parse_args(shlex.split(arg, posix=POSIX_SHLEX)) + #import ipdb; ipdb.set_trace() + + + # If not using POSIX shlex, make sure to strip off outer quotes for convenience + if not POSIX_SHLEX and STRIP_QUOTES_FOR_NON_POSIX: + newopts = opts +# for key, val in vars(opts): +# if isinstance(val, str): +# newopts[key] = strip_quotes(val) + opts = newopts +### opts = argparser.parse_args(shlex.split(arg, posix=POSIX_SHLEX)) func(instance, arg, opts) - print("after command") + #print("after command") return cmd_wrapper return arg_decorator diff --git a/tests/test_argparse.py b/tests/test_argparse.py new file mode 100644 index 00000000..82932e6d --- /dev/null +++ b/tests/test_argparse.py @@ -0,0 +1,54 @@ +# coding=utf-8 +""" +Cmd2 testing for argument parsing +""" +import argparse +import pytest + +import cmd2 +from conftest import run_cmd, StdOut + +class ArgparseApp(cmd2.Cmd): + def __init__(self): + self.maxrepeats = 3 + cmd2.Cmd.__init__(self) + + argparser = argparse.ArgumentParser( + prog='say', + description='Repeats what you tell me to' + ) + argparser.add_argument('-p', '--piglatin', action='store_true', help='atinLay') + argparser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE') + argparser.add_argument('-r', '--repeat', type=int, help='output [n] times') + argparser.add_argument('word', nargs='?', help='word to say') + @cmd2.with_argument_parser(argparser) + def do_say(self, cmdline, args=None): + word = args.word + if word is None: + word = '' + if args.piglatin: + word = '%s%say' % (word[1:], word[0]) + if args.shout: + word = word.upper() + repetitions = args.repeat or 1 + for i in range(min(repetitions, self.maxrepeats)): + self.stdout.write(word) + self.stdout.write('\n') + +@pytest.fixture +def argparse_app(): + app = ArgparseApp() + app.stdout = StdOut() + return app + +def test_argparse_basic_command(argparse_app): + out = run_cmd(argparse_app, 'say hello') + assert out == ['hello'] + +#def test_argparse_quoted_arguments(argparse_app): +# out = run_cmd(argparse_app, 'say "hello there"') +# assert out == ['hello there'] + +#def test_pargparse_quoted_arguments_too_many(argparse_app): +# out = run_cmd(argparse_app, 'say "hello there" morty') +# assert out == ['hello there morty'] |