Skip to content

Commit c25a2b7

Browse files
committed
Add tests for POSIX=true and arguments containing spaces
1 parent 49bf448 commit c25a2b7

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

examples/argparse_example.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ def do_speak(self, arg, opts=None):
5959
# self.stdout.write is better than "print", because Cmd can be
6060
# initialized with a non-standard output destination
6161

62+
do_say = do_speak # now "say" is a synonym for "speak"
63+
do_orate = do_speak # another synonym, but this one takes multi-line input
64+
65+
6266
argparser = argparse.ArgumentParser(
6367
prog='sspeak',
6468
description='Repeats what you tell me to'
@@ -83,8 +87,20 @@ def do_sspeak(self, rawarg, args=None):
8387
self.stdout.write('\n')
8488
# self.stdout.write is better than "print", because Cmd can be
8589
# initialized with a non-standard output destination
86-
do_say = do_speak # now "say" is a synonym for "speak"
87-
do_orate = do_speak # another synonym, but this one takes multi-line input
90+
91+
92+
argparser = argparse.ArgumentParser(
93+
prog='tag',
94+
description='create an html tag, the first argument is the tag, the rest is the contents'
95+
)
96+
argparser.add_argument('tag', nargs=1, help='tag')
97+
argparser.add_argument('content', nargs='+', help='content to surround with tag')
98+
@with_argument_parser(argparser)
99+
def do_tag(self, cmdline, args=None):
100+
self.stdout.write('<{0}>{1}</{0}>'.format(args.tag[0], ' '.join(args.content)))
101+
self.stdout.write('\n')
102+
# self.stdout.write is better than "print", because Cmd can be
103+
# initialized with a non-standard output destination
88104

89105

90106
if __name__ == '__main__':

tests/test_argparse.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ def do_say(self, cmdline, args=None):
3737
self.stdout.write(' '.join(words))
3838
self.stdout.write('\n')
3939

40+
argparser = argparse.ArgumentParser(
41+
prog='tag',
42+
description='create an html tag, the first argument is the tag, the rest is the contents'
43+
)
44+
argparser.add_argument('tag', nargs=1, help='tag')
45+
argparser.add_argument('content', nargs='+', help='content to surround with tag')
46+
@cmd2.with_argument_parser(argparser)
47+
def do_tag(self, cmdline, args=None):
48+
self.stdout.write('<{0}>{1}</{0}>'.format(args.tag[0], ' '.join(args.content)))
49+
self.stdout.write('\n')
50+
51+
4052
@pytest.fixture
4153
def argparse_app():
4254
app = ArgparseApp()
@@ -53,8 +65,18 @@ def test_argparse_quoted_arguments(argparse_app):
5365
out = run_cmd(argparse_app, 'say "hello there"')
5466
assert out == ['hello there']
5567

56-
def test_pargparse_quoted_arguments_too_many(argparse_app):
68+
def test_argparse_quoted_arguments_multiple(argparse_app):
5769
argparse_app.POSIX = False
5870
argparse_app.STRIP_QUOTES_FOR_NON_POSIX = True
59-
out = run_cmd(argparse_app, 'say "hello there" morty')
60-
assert out == ['hello there morty']
71+
out = run_cmd(argparse_app, 'say "hello there" "rick & morty"')
72+
assert out == ['hello there rick & morty']
73+
74+
def test_argparse_quoted_arguments_posix(argparse_app):
75+
argparse_app.POSIX = True
76+
out = run_cmd(argparse_app, 'tag strong this should be loud')
77+
assert out == ['<strong>this should be loud</strong>']
78+
79+
def test_argparse_quoted_arguments_posix_multiple(argparse_app):
80+
argparse_app.POSIX = True
81+
out = run_cmd(argparse_app, 'tag strong this "should be" loud')
82+
assert out == ['<strong>this should be loud</strong>']

0 commit comments

Comments
 (0)