summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorTodd Leonhardt <todd.leonhardt@gmail.com>2018-01-10 20:11:22 -0500
committerGitHub <noreply@github.com>2018-01-10 20:11:22 -0500
commit38312dead63ea9d95cdb7f7b34a4d6030dbf0479 (patch)
tree684cdc7a22290d4595d5a25caae0f433b89ba0e3 /tests
parente6672fe278b877c82f8d011281b17dcf2654d257 (diff)
parent73e7b149a34722504281d48bdb252e477625d4d0 (diff)
downloadcmd2-git-38312dead63ea9d95cdb7f7b34a4d6030dbf0479.tar.gz
Merge pull request #247 from python-cmd2/argparse
Argparse support
Diffstat (limited to 'tests')
-rw-r--r--tests/test_argparse.py91
1 files changed, 91 insertions, 0 deletions
diff --git a/tests/test_argparse.py b/tests/test_argparse.py
new file mode 100644
index 00000000..e9dbc1b3
--- /dev/null
+++ b/tests/test_argparse.py
@@ -0,0 +1,91 @@
+# 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()
+ 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):
+ """Repeat what you tell me to."""
+ words = []
+ for word in args.words:
+ 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')
+
+ 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):
+ self.stdout.write('<{0}>{1}</{0}>'.format(args.tag[0], ' '.join(args.content)))
+ 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):
+ argparse_app.POSIX = False
+ argparse_app.STRIP_QUOTES_FOR_NON_POSIX = True
+ out = run_cmd(argparse_app, 'say "hello there"')
+ assert out == ['hello there']
+
+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" "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>']
+
+def test_argparse_help_docstring(argparse_app):
+ out = run_cmd(argparse_app, 'help say')
+ assert out[0] == 'Repeat what you tell me to.'
+
+def test_argparse_help_description(argparse_app):
+ out = run_cmd(argparse_app, 'help tag')
+ assert out[2] == 'create a html tag'
+
+def test_argparse_prog(argparse_app):
+ out = run_cmd(argparse_app, 'help tag')
+ progname = out[0].split(' ')[1]
+ assert progname == 'tag'
+ \ No newline at end of file