diff options
-rwxr-xr-x | examples/argparse_example.py | 20 | ||||
-rw-r--r-- | tests/test_argparse.py | 28 |
2 files changed, 43 insertions, 5 deletions
diff --git a/examples/argparse_example.py b/examples/argparse_example.py index 96f8118d..16de343c 100755 --- a/examples/argparse_example.py +++ b/examples/argparse_example.py @@ -59,6 +59,10 @@ class CmdLineApp(Cmd): # self.stdout.write is better than "print", because Cmd can be # initialized with a non-standard output destination + do_say = do_speak # now "say" is a synonym for "speak" + do_orate = do_speak # another synonym, but this one takes multi-line input + + argparser = argparse.ArgumentParser( prog='sspeak', description='Repeats what you tell me to' @@ -83,8 +87,20 @@ class CmdLineApp(Cmd): self.stdout.write('\n') # self.stdout.write is better than "print", because Cmd can be # initialized with a non-standard output destination - do_say = do_speak # now "say" is a synonym for "speak" - do_orate = do_speak # another synonym, but this one takes multi-line input + + + argparser = argparse.ArgumentParser( + prog='tag', + description='create an html tag, the first argument is the tag, the rest is the contents' + ) + argparser.add_argument('tag', nargs=1, help='tag') + argparser.add_argument('content', nargs='+', help='content to surround with tag') + @with_argument_parser(argparser) + def do_tag(self, cmdline, args=None): + self.stdout.write('<{0}>{1}</{0}>'.format(args.tag[0], ' '.join(args.content))) + self.stdout.write('\n') + # self.stdout.write is better than "print", because Cmd can be + # initialized with a non-standard output destination if __name__ == '__main__': diff --git a/tests/test_argparse.py b/tests/test_argparse.py index dec54c5d..9b49267e 100644 --- a/tests/test_argparse.py +++ b/tests/test_argparse.py @@ -37,6 +37,18 @@ class ArgparseApp(cmd2.Cmd): self.stdout.write(' '.join(words)) self.stdout.write('\n') + argparser = argparse.ArgumentParser( + prog='tag', + description='create an html tag, the first argument is the tag, the rest is the contents' + ) + argparser.add_argument('tag', nargs=1, help='tag') + argparser.add_argument('content', nargs='+', help='content to surround with tag') + @cmd2.with_argument_parser(argparser) + def do_tag(self, cmdline, args=None): + self.stdout.write('<{0}>{1}</{0}>'.format(args.tag[0], ' '.join(args.content))) + self.stdout.write('\n') + + @pytest.fixture def argparse_app(): app = ArgparseApp() @@ -53,8 +65,18 @@ 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): +def test_argparse_quoted_arguments_multiple(argparse_app): argparse_app.POSIX = False argparse_app.STRIP_QUOTES_FOR_NON_POSIX = True - out = run_cmd(argparse_app, 'say "hello there" morty') - assert out == ['hello there morty'] + out = run_cmd(argparse_app, 'say "hello there" "rick & morty"') + assert out == ['hello there rick & morty'] + +def test_argparse_quoted_arguments_posix(argparse_app): + argparse_app.POSIX = True + out = run_cmd(argparse_app, 'tag strong this should be loud') + assert out == ['<strong>this should be loud</strong>'] + +def test_argparse_quoted_arguments_posix_multiple(argparse_app): + argparse_app.POSIX = True + out = run_cmd(argparse_app, 'tag strong this "should be" loud') + assert out == ['<strong>this should be loud</strong>'] |