@@ -29,6 +29,11 @@ def __init__(self):
2929 self .maxrepeats = 3
3030 cmd2 .Cmd .__init__ (self )
3131
32+ def namespace_provider (self ) -> argparse .Namespace :
33+ ns = argparse .Namespace ()
34+ ns .custom_stuff = "custom"
35+ return ns
36+
3237 say_parser = argparse .ArgumentParser ()
3338 say_parser .add_argument ('-p' , '--piglatin' , action = 'store_true' , help = 'atinLay' )
3439 say_parser .add_argument ('-s' , '--shout' , action = 'store_true' , help = 'N00B EMULATION MODE' )
@@ -56,11 +61,15 @@ def do_say(self, args):
5661 tag_parser .add_argument ('tag' , help = 'tag' )
5762 tag_parser .add_argument ('content' , nargs = '+' , help = 'content to surround with tag' )
5863
59- @cmd2 .with_argparser (tag_parser )
64+ @cmd2 .with_argparser (tag_parser , preserve_quotes = True )
6065 def do_tag (self , args ):
6166 self .stdout .write ('<{0}>{1}</{0}>' .format (args .tag , ' ' .join (args .content )))
6267 self .stdout .write ('\n ' )
6368
69+ @cmd2 .with_argparser (argparse .ArgumentParser (), ns_provider = namespace_provider )
70+ def do_test_argparse_ns (self , args ):
71+ self .stdout .write ('{}' .format (args .custom_stuff ))
72+
6473 @cmd2 .with_argument_list
6574 def do_arglist (self , arglist ):
6675 if isinstance (arglist , list ):
@@ -93,21 +102,14 @@ def do_speak(self, args, extra):
93102 self .stdout .write (' ' .join (words ))
94103 self .stdout .write ('\n ' )
95104
96- @cmd2 .with_argparser_and_unknown_args (known_parser )
97- def do_talk (self , args , extra ):
98- words = []
99- for word in extra :
100- if word is None :
101- word = ''
102- if args .piglatin :
103- word = '%s%say' % (word [1 :], word [0 ])
104- if args .shout :
105- word = word .upper ()
106- words .append (word )
107- repetitions = args .repeat or 1
108- for i in range (min (repetitions , self .maxrepeats )):
109- self .stdout .write (' ' .join (words ))
110- self .stdout .write ('\n ' )
105+ @cmd2 .with_argparser_and_unknown_args (argparse .ArgumentParser (), preserve_quotes = True )
106+ def do_test_argparse_with_list_quotes (self , args , extra ):
107+ self .stdout .write ('{}' .format (' ' .join (extra )))
108+
109+ @cmd2 .with_argparser_and_unknown_args (argparse .ArgumentParser (), ns_provider = namespace_provider )
110+ def do_test_argparse_with_list_ns (self , args , extra ):
111+ self .stdout .write ('{}' .format (args .custom_stuff ))
112+
111113
112114@pytest .fixture
113115def argparse_app ():
@@ -123,14 +125,34 @@ def test_argparse_basic_command(argparse_app):
123125 out , err = run_cmd (argparse_app , 'say hello' )
124126 assert out == ['hello' ]
125127
126- def test_argparse_quoted_arguments (argparse_app ):
128+ def test_argparse_remove_quotes (argparse_app ):
127129 out , err = run_cmd (argparse_app , 'say "hello there"' )
128130 assert out == ['hello there' ]
129131
132+ def test_argparse_preserve_quotes (argparse_app ):
133+ out , err = run_cmd (argparse_app , 'tag mytag "hello"' )
134+ assert out [0 ] == '<mytag>"hello"</mytag>'
135+
136+ def test_argparse_custom_namespace (argparse_app ):
137+ out , err = run_cmd (argparse_app , 'test_argparse_ns' )
138+ assert out [0 ] == 'custom'
139+
130140def test_argparse_with_list (argparse_app ):
131141 out , err = run_cmd (argparse_app , 'speak -s hello world!' )
132142 assert out == ['HELLO WORLD!' ]
133143
144+ def test_argparse_with_list_remove_quotes (argparse_app ):
145+ out , err = run_cmd (argparse_app , 'speak -s hello "world!"' )
146+ assert out == ['HELLO WORLD!' ]
147+
148+ def test_argparse_with_list_preserve_quotes (argparse_app ):
149+ out , err = run_cmd (argparse_app , 'test_argparse_with_list_quotes "hello" person' )
150+ assert out [0 ] == '"hello" person'
151+
152+ def test_argparse_with_list_custom_namespace (argparse_app ):
153+ out , err = run_cmd (argparse_app , 'test_argparse_with_list_ns' )
154+ assert out [0 ] == 'custom'
155+
134156def test_argparse_with_list_and_empty_doc (argparse_app ):
135157 out , err = run_cmd (argparse_app , 'speak -s hello world!' )
136158 assert out == ['HELLO WORLD!' ]
0 commit comments