@@ -71,6 +71,27 @@ def do_media(self, args):
7171 # No subcommand was provided, so call help
7272 self .do_help ('media' )
7373
74+ foo_parser = argparse_completer .ACArgumentParser (prog = 'foo' )
75+ foo_parser .add_argument ('-c' , dest = 'counter' , action = 'count' )
76+ foo_parser .add_argument ('-t' , dest = 'trueval' , action = 'store_true' )
77+ foo_parser .add_argument ('-n' , dest = 'constval' , action = 'store_const' , const = 42 )
78+ foo_parser .add_argument ('variable' , nargs = (2 , 3 ))
79+ foo_parser .add_argument ('optional' , nargs = '?' )
80+ foo_parser .add_argument ('zeroormore' , nargs = '*' )
81+
82+ @with_argparser (foo_parser )
83+ def do_foo (self , args ):
84+ print ('foo ' + str (args .__dict__ ))
85+
86+ bar_parser = argparse_completer .ACArgumentParser (prog = 'bar' )
87+ bar_parser .add_argument ('first' )
88+ bar_parser .add_argument ('oneormore' , nargs = '+' )
89+ bar_parser .add_argument ('-a' , dest = 'aaa' )
90+
91+ @with_argparser (bar_parser )
92+ def do_bar (self , args ):
93+ print ('bar ' + str (args .__dict__ ))
94+
7495
7596@pytest .fixture
7697def ps_app ():
@@ -108,6 +129,10 @@ def test_pyscript_help(ps_app, capsys, request, command, pyscript_file):
108129 'media_movies_add1.py' ),
109130 ('media movies add "My Movie" PG-13 --director "George Lucas" "J. J. Abrams" "Mark Hamill"' ,
110131 'media_movies_add2.py' ),
132+ ('foo aaa bbb -ccc -t -n' , 'foo1.py' ),
133+ ('foo 11 22 33 44 -ccc -t -n' , 'foo2.py' ),
134+ ('foo 11 22 33 44 55 66 -ccc' , 'foo3.py' ),
135+ ('bar 11 22' , 'bar1.py' )
111136])
112137def test_pyscript_out (ps_app , capsys , request , command , pyscript_file ):
113138 test_dir = os .path .dirname (request .module .__file__ )
@@ -122,26 +147,18 @@ def test_pyscript_out(ps_app, capsys, request, command, pyscript_file):
122147 assert out == expected
123148
124149
125- @pytest .mark .parametrize ('command' , [
126- 'app.noncommand' ,
127- 'app.media.noncommand' ,
150+ @pytest .mark .parametrize ('command, error' , [
151+ ('app.noncommand' , 'AttributeError' ),
152+ ('app.media.noncommand' , 'AttributeError' ),
153+ ('app.media.movies.list(artist="Invalid Keyword")' , 'TypeError' ),
154+ ('app.foo(counter="a")' , 'TypeError' ),
155+ ('app.foo("aaa")' , 'ValueError' ),
128156])
129- def test_pyscript_unknown_command (ps_app , capsys , command ):
157+ def test_pyscript_errors (ps_app , capsys , command , error ):
130158 run_cmd (ps_app , 'py {}' .format (command ))
131159 _ , err = capsys .readouterr ()
132160
133161 assert len (err ) > 0
134162 assert 'Traceback' in err
135- assert 'AttributeError' in err
136-
163+ assert error in err
137164
138- @pytest .mark .parametrize ('command' , [
139- 'app.media.movies.list(artist="Invalid Keyword")' ,
140- ])
141- def test_pyscript_unknown_kw (ps_app , capsys , command ):
142- run_cmd (ps_app , 'py {}' .format (command ))
143- _ , err = capsys .readouterr ()
144-
145- assert len (err ) > 0
146- assert 'Traceback' in err
147- assert 'TypeError' in err
0 commit comments