summaryrefslogtreecommitdiff
path: root/tests/test_argparse.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test_argparse.py')
-rw-r--r--tests/test_argparse.py99
1 files changed, 76 insertions, 23 deletions
diff --git a/tests/test_argparse.py b/tests/test_argparse.py
index 52ce7de8..fea9bebb 100644
--- a/tests/test_argparse.py
+++ b/tests/test_argparse.py
@@ -2,7 +2,6 @@
"""
Cmd2 testing for argument parsing
"""
-import re
import argparse
import pytest
@@ -14,13 +13,14 @@ class ArgparseApp(cmd2.Cmd):
self.maxrepeats = 3
cmd2.Cmd.__init__(self)
- argparser = argparse.ArgumentParser()
- 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('words', nargs='+', help='words to say')
- @cmd2.with_argument_parser(argparser)
- def do_say(self, cmdline, args=None):
+ say_parser = argparse.ArgumentParser()
+ say_parser.add_argument('-p', '--piglatin', action='store_true', help='atinLay')
+ say_parser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE')
+ say_parser.add_argument('-r', '--repeat', type=int, help='output [n] times')
+ say_parser.add_argument('words', nargs='+', help='words to say')
+
+ @cmd2.with_argument_parser(say_parser)
+ def do_say(self, args):
"""Repeat what you tell me to."""
words = []
for word in args.words:
@@ -36,31 +36,74 @@ class ArgparseApp(cmd2.Cmd):
self.stdout.write(' '.join(words))
self.stdout.write('\n')
- argparser = argparse.ArgumentParser(description='create a html tag')
- 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):
+ tag_parser = argparse.ArgumentParser(description='create a html tag')
+ tag_parser.add_argument('tag', nargs=1, help='tag')
+ tag_parser.add_argument('content', nargs='+', help='content to surround with tag')
+
+ @cmd2.with_argument_parser(tag_parser)
+ def do_tag(self, args):
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:
+ @cmd2.with_argument_list
+ def do_arglist(self, arglist):
+ if isinstance(arglist, list):
self.stdout.write('True')
else:
self.stdout.write('False')
+ @cmd2.with_argument_list
+ @cmd2.with_argument_list
+ def do_arglisttwice(self, arglist):
+ if isinstance(arglist, list):
+ self.stdout.write(' '.join(arglist))
+ else:
+ self.stdout.write('False')
+
+ known_parser = argparse.ArgumentParser()
+ known_parser.add_argument('-p', '--piglatin', action='store_true', help='atinLay')
+ known_parser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE')
+ known_parser.add_argument('-r', '--repeat', type=int, help='output [n] times')
+ @cmd2.with_argparser_and_unknown_args(known_parser)
+ def do_speak(self, args, extra):
+ """Repeat what you tell me to."""
+ words = []
+ for word in extra:
+ if word is None:
+ word = ''
+ if args.piglatin:
+ word = '%s%say' % (word[1:], word[0])
+ if args.shout:
+ word = word.upper()
+ words.append(word)
+ repetitions = args.repeat or 1
+ for i in range(min(repetitions, self.maxrepeats)):
+ self.stdout.write(' '.join(words))
+ self.stdout.write('\n')
+
+ @cmd2.with_argparser_and_unknown_args(known_parser)
+ def do_talk(self, args, extra):
+ words = []
+ for word in extra:
+ if word is None:
+ word = ''
+ if args.piglatin:
+ word = '%s%say' % (word[1:], word[0])
+ if args.shout:
+ word = word.upper()
+ words.append(word)
+ repetitions = args.repeat or 1
+ for i in range(min(repetitions, self.maxrepeats)):
+ self.stdout.write(' '.join(words))
+ 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']
@@ -71,6 +114,14 @@ def test_argparse_quoted_arguments(argparse_app):
out = run_cmd(argparse_app, 'say "hello there"')
assert out == ['hello there']
+def test_argparse_with_list(argparse_app):
+ out = run_cmd(argparse_app, 'speak -s hello world!')
+ assert out == ['HELLO WORLD!']
+
+def test_argparse_with_list_and_empty_doc(argparse_app):
+ out = run_cmd(argparse_app, 'speak -s hello world!')
+ assert out == ['HELLO WORLD!']
+
def test_argparse_quoted_arguments_multiple(argparse_app):
argparse_app.POSIX = False
argparse_app.STRIP_QUOTES_FOR_NON_POSIX = True
@@ -100,8 +151,10 @@ def test_argparse_prog(argparse_app):
progname = out[0].split(' ')[1]
assert progname == 'tag'
-def test_argparse_cmdline(argparse_app):
- out = run_cmd(argparse_app, 'compare this is a test')
+def test_arglist(argparse_app):
+ out = run_cmd(argparse_app, 'arglist "we should" get these')
assert out[0] == 'True'
- \ No newline at end of file
+def test_arglist_decorator_twice(argparse_app):
+ out = run_cmd(argparse_app, 'arglisttwice "we should" get these')
+ assert out[0] == 'we should get these'