1 | n/a | # Author: Steven J. Bethard <steven.bethard@gmail.com>. |
---|
2 | n/a | |
---|
3 | n/a | import codecs |
---|
4 | n/a | import inspect |
---|
5 | n/a | import os |
---|
6 | n/a | import shutil |
---|
7 | n/a | import stat |
---|
8 | n/a | import sys |
---|
9 | n/a | import textwrap |
---|
10 | n/a | import tempfile |
---|
11 | n/a | import unittest |
---|
12 | n/a | import argparse |
---|
13 | n/a | |
---|
14 | n/a | from io import StringIO |
---|
15 | n/a | |
---|
16 | n/a | from test import support |
---|
17 | n/a | from unittest import mock |
---|
18 | n/a | class StdIOBuffer(StringIO): |
---|
19 | n/a | pass |
---|
20 | n/a | |
---|
21 | n/a | class TestCase(unittest.TestCase): |
---|
22 | n/a | |
---|
23 | n/a | def setUp(self): |
---|
24 | n/a | # The tests assume that line wrapping occurs at 80 columns, but this |
---|
25 | n/a | # behaviour can be overridden by setting the COLUMNS environment |
---|
26 | n/a | # variable. To ensure that this assumption is true, unset COLUMNS. |
---|
27 | n/a | env = support.EnvironmentVarGuard() |
---|
28 | n/a | env.unset("COLUMNS") |
---|
29 | n/a | self.addCleanup(env.__exit__) |
---|
30 | n/a | |
---|
31 | n/a | |
---|
32 | n/a | class TempDirMixin(object): |
---|
33 | n/a | |
---|
34 | n/a | def setUp(self): |
---|
35 | n/a | self.temp_dir = tempfile.mkdtemp() |
---|
36 | n/a | self.old_dir = os.getcwd() |
---|
37 | n/a | os.chdir(self.temp_dir) |
---|
38 | n/a | |
---|
39 | n/a | def tearDown(self): |
---|
40 | n/a | os.chdir(self.old_dir) |
---|
41 | n/a | for root, dirs, files in os.walk(self.temp_dir, topdown=False): |
---|
42 | n/a | for name in files: |
---|
43 | n/a | os.chmod(os.path.join(self.temp_dir, name), stat.S_IWRITE) |
---|
44 | n/a | shutil.rmtree(self.temp_dir, True) |
---|
45 | n/a | |
---|
46 | n/a | def create_readonly_file(self, filename): |
---|
47 | n/a | file_path = os.path.join(self.temp_dir, filename) |
---|
48 | n/a | with open(file_path, 'w') as file: |
---|
49 | n/a | file.write(filename) |
---|
50 | n/a | os.chmod(file_path, stat.S_IREAD) |
---|
51 | n/a | |
---|
52 | n/a | class Sig(object): |
---|
53 | n/a | |
---|
54 | n/a | def __init__(self, *args, **kwargs): |
---|
55 | n/a | self.args = args |
---|
56 | n/a | self.kwargs = kwargs |
---|
57 | n/a | |
---|
58 | n/a | |
---|
59 | n/a | class NS(object): |
---|
60 | n/a | |
---|
61 | n/a | def __init__(self, **kwargs): |
---|
62 | n/a | self.__dict__.update(kwargs) |
---|
63 | n/a | |
---|
64 | n/a | def __repr__(self): |
---|
65 | n/a | sorted_items = sorted(self.__dict__.items()) |
---|
66 | n/a | kwarg_str = ', '.join(['%s=%r' % tup for tup in sorted_items]) |
---|
67 | n/a | return '%s(%s)' % (type(self).__name__, kwarg_str) |
---|
68 | n/a | |
---|
69 | n/a | def __eq__(self, other): |
---|
70 | n/a | return vars(self) == vars(other) |
---|
71 | n/a | |
---|
72 | n/a | |
---|
73 | n/a | class ArgumentParserError(Exception): |
---|
74 | n/a | |
---|
75 | n/a | def __init__(self, message, stdout=None, stderr=None, error_code=None): |
---|
76 | n/a | Exception.__init__(self, message, stdout, stderr) |
---|
77 | n/a | self.message = message |
---|
78 | n/a | self.stdout = stdout |
---|
79 | n/a | self.stderr = stderr |
---|
80 | n/a | self.error_code = error_code |
---|
81 | n/a | |
---|
82 | n/a | |
---|
83 | n/a | def stderr_to_parser_error(parse_args, *args, **kwargs): |
---|
84 | n/a | # if this is being called recursively and stderr or stdout is already being |
---|
85 | n/a | # redirected, simply call the function and let the enclosing function |
---|
86 | n/a | # catch the exception |
---|
87 | n/a | if isinstance(sys.stderr, StdIOBuffer) or isinstance(sys.stdout, StdIOBuffer): |
---|
88 | n/a | return parse_args(*args, **kwargs) |
---|
89 | n/a | |
---|
90 | n/a | # if this is not being called recursively, redirect stderr and |
---|
91 | n/a | # use it as the ArgumentParserError message |
---|
92 | n/a | old_stdout = sys.stdout |
---|
93 | n/a | old_stderr = sys.stderr |
---|
94 | n/a | sys.stdout = StdIOBuffer() |
---|
95 | n/a | sys.stderr = StdIOBuffer() |
---|
96 | n/a | try: |
---|
97 | n/a | try: |
---|
98 | n/a | result = parse_args(*args, **kwargs) |
---|
99 | n/a | for key in list(vars(result)): |
---|
100 | n/a | if getattr(result, key) is sys.stdout: |
---|
101 | n/a | setattr(result, key, old_stdout) |
---|
102 | n/a | if getattr(result, key) is sys.stderr: |
---|
103 | n/a | setattr(result, key, old_stderr) |
---|
104 | n/a | return result |
---|
105 | n/a | except SystemExit: |
---|
106 | n/a | code = sys.exc_info()[1].code |
---|
107 | n/a | stdout = sys.stdout.getvalue() |
---|
108 | n/a | stderr = sys.stderr.getvalue() |
---|
109 | n/a | raise ArgumentParserError("SystemExit", stdout, stderr, code) |
---|
110 | n/a | finally: |
---|
111 | n/a | sys.stdout = old_stdout |
---|
112 | n/a | sys.stderr = old_stderr |
---|
113 | n/a | |
---|
114 | n/a | |
---|
115 | n/a | class ErrorRaisingArgumentParser(argparse.ArgumentParser): |
---|
116 | n/a | |
---|
117 | n/a | def parse_args(self, *args, **kwargs): |
---|
118 | n/a | parse_args = super(ErrorRaisingArgumentParser, self).parse_args |
---|
119 | n/a | return stderr_to_parser_error(parse_args, *args, **kwargs) |
---|
120 | n/a | |
---|
121 | n/a | def exit(self, *args, **kwargs): |
---|
122 | n/a | exit = super(ErrorRaisingArgumentParser, self).exit |
---|
123 | n/a | return stderr_to_parser_error(exit, *args, **kwargs) |
---|
124 | n/a | |
---|
125 | n/a | def error(self, *args, **kwargs): |
---|
126 | n/a | error = super(ErrorRaisingArgumentParser, self).error |
---|
127 | n/a | return stderr_to_parser_error(error, *args, **kwargs) |
---|
128 | n/a | |
---|
129 | n/a | |
---|
130 | n/a | class ParserTesterMetaclass(type): |
---|
131 | n/a | """Adds parser tests using the class attributes. |
---|
132 | n/a | |
---|
133 | n/a | Classes of this type should specify the following attributes: |
---|
134 | n/a | |
---|
135 | n/a | argument_signatures -- a list of Sig objects which specify |
---|
136 | n/a | the signatures of Argument objects to be created |
---|
137 | n/a | failures -- a list of args lists that should cause the parser |
---|
138 | n/a | to fail |
---|
139 | n/a | successes -- a list of (initial_args, options, remaining_args) tuples |
---|
140 | n/a | where initial_args specifies the string args to be parsed, |
---|
141 | n/a | options is a dict that should match the vars() of the options |
---|
142 | n/a | parsed out of initial_args, and remaining_args should be any |
---|
143 | n/a | remaining unparsed arguments |
---|
144 | n/a | """ |
---|
145 | n/a | |
---|
146 | n/a | def __init__(cls, name, bases, bodydict): |
---|
147 | n/a | if name == 'ParserTestCase': |
---|
148 | n/a | return |
---|
149 | n/a | |
---|
150 | n/a | # default parser signature is empty |
---|
151 | n/a | if not hasattr(cls, 'parser_signature'): |
---|
152 | n/a | cls.parser_signature = Sig() |
---|
153 | n/a | if not hasattr(cls, 'parser_class'): |
---|
154 | n/a | cls.parser_class = ErrorRaisingArgumentParser |
---|
155 | n/a | |
---|
156 | n/a | # --------------------------------------- |
---|
157 | n/a | # functions for adding optional arguments |
---|
158 | n/a | # --------------------------------------- |
---|
159 | n/a | def no_groups(parser, argument_signatures): |
---|
160 | n/a | """Add all arguments directly to the parser""" |
---|
161 | n/a | for sig in argument_signatures: |
---|
162 | n/a | parser.add_argument(*sig.args, **sig.kwargs) |
---|
163 | n/a | |
---|
164 | n/a | def one_group(parser, argument_signatures): |
---|
165 | n/a | """Add all arguments under a single group in the parser""" |
---|
166 | n/a | group = parser.add_argument_group('foo') |
---|
167 | n/a | for sig in argument_signatures: |
---|
168 | n/a | group.add_argument(*sig.args, **sig.kwargs) |
---|
169 | n/a | |
---|
170 | n/a | def many_groups(parser, argument_signatures): |
---|
171 | n/a | """Add each argument in its own group to the parser""" |
---|
172 | n/a | for i, sig in enumerate(argument_signatures): |
---|
173 | n/a | group = parser.add_argument_group('foo:%i' % i) |
---|
174 | n/a | group.add_argument(*sig.args, **sig.kwargs) |
---|
175 | n/a | |
---|
176 | n/a | # -------------------------- |
---|
177 | n/a | # functions for parsing args |
---|
178 | n/a | # -------------------------- |
---|
179 | n/a | def listargs(parser, args): |
---|
180 | n/a | """Parse the args by passing in a list""" |
---|
181 | n/a | return parser.parse_args(args) |
---|
182 | n/a | |
---|
183 | n/a | def sysargs(parser, args): |
---|
184 | n/a | """Parse the args by defaulting to sys.argv""" |
---|
185 | n/a | old_sys_argv = sys.argv |
---|
186 | n/a | sys.argv = [old_sys_argv[0]] + args |
---|
187 | n/a | try: |
---|
188 | n/a | return parser.parse_args() |
---|
189 | n/a | finally: |
---|
190 | n/a | sys.argv = old_sys_argv |
---|
191 | n/a | |
---|
192 | n/a | # class that holds the combination of one optional argument |
---|
193 | n/a | # addition method and one arg parsing method |
---|
194 | n/a | class AddTests(object): |
---|
195 | n/a | |
---|
196 | n/a | def __init__(self, tester_cls, add_arguments, parse_args): |
---|
197 | n/a | self._add_arguments = add_arguments |
---|
198 | n/a | self._parse_args = parse_args |
---|
199 | n/a | |
---|
200 | n/a | add_arguments_name = self._add_arguments.__name__ |
---|
201 | n/a | parse_args_name = self._parse_args.__name__ |
---|
202 | n/a | for test_func in [self.test_failures, self.test_successes]: |
---|
203 | n/a | func_name = test_func.__name__ |
---|
204 | n/a | names = func_name, add_arguments_name, parse_args_name |
---|
205 | n/a | test_name = '_'.join(names) |
---|
206 | n/a | |
---|
207 | n/a | def wrapper(self, test_func=test_func): |
---|
208 | n/a | test_func(self) |
---|
209 | n/a | try: |
---|
210 | n/a | wrapper.__name__ = test_name |
---|
211 | n/a | except TypeError: |
---|
212 | n/a | pass |
---|
213 | n/a | setattr(tester_cls, test_name, wrapper) |
---|
214 | n/a | |
---|
215 | n/a | def _get_parser(self, tester): |
---|
216 | n/a | args = tester.parser_signature.args |
---|
217 | n/a | kwargs = tester.parser_signature.kwargs |
---|
218 | n/a | parser = tester.parser_class(*args, **kwargs) |
---|
219 | n/a | self._add_arguments(parser, tester.argument_signatures) |
---|
220 | n/a | return parser |
---|
221 | n/a | |
---|
222 | n/a | def test_failures(self, tester): |
---|
223 | n/a | parser = self._get_parser(tester) |
---|
224 | n/a | for args_str in tester.failures: |
---|
225 | n/a | args = args_str.split() |
---|
226 | n/a | with tester.assertRaises(ArgumentParserError, msg=args): |
---|
227 | n/a | parser.parse_args(args) |
---|
228 | n/a | |
---|
229 | n/a | def test_successes(self, tester): |
---|
230 | n/a | parser = self._get_parser(tester) |
---|
231 | n/a | for args, expected_ns in tester.successes: |
---|
232 | n/a | if isinstance(args, str): |
---|
233 | n/a | args = args.split() |
---|
234 | n/a | result_ns = self._parse_args(parser, args) |
---|
235 | n/a | tester.assertEqual(expected_ns, result_ns) |
---|
236 | n/a | |
---|
237 | n/a | # add tests for each combination of an optionals adding method |
---|
238 | n/a | # and an arg parsing method |
---|
239 | n/a | for add_arguments in [no_groups, one_group, many_groups]: |
---|
240 | n/a | for parse_args in [listargs, sysargs]: |
---|
241 | n/a | AddTests(cls, add_arguments, parse_args) |
---|
242 | n/a | |
---|
243 | n/a | bases = TestCase, |
---|
244 | n/a | ParserTestCase = ParserTesterMetaclass('ParserTestCase', bases, {}) |
---|
245 | n/a | |
---|
246 | n/a | # =============== |
---|
247 | n/a | # Optionals tests |
---|
248 | n/a | # =============== |
---|
249 | n/a | |
---|
250 | n/a | class TestOptionalsSingleDash(ParserTestCase): |
---|
251 | n/a | """Test an Optional with a single-dash option string""" |
---|
252 | n/a | |
---|
253 | n/a | argument_signatures = [Sig('-x')] |
---|
254 | n/a | failures = ['-x', 'a', '--foo', '-x --foo', '-x -y'] |
---|
255 | n/a | successes = [ |
---|
256 | n/a | ('', NS(x=None)), |
---|
257 | n/a | ('-x a', NS(x='a')), |
---|
258 | n/a | ('-xa', NS(x='a')), |
---|
259 | n/a | ('-x -1', NS(x='-1')), |
---|
260 | n/a | ('-x-1', NS(x='-1')), |
---|
261 | n/a | ] |
---|
262 | n/a | |
---|
263 | n/a | |
---|
264 | n/a | class TestOptionalsSingleDashCombined(ParserTestCase): |
---|
265 | n/a | """Test an Optional with a single-dash option string""" |
---|
266 | n/a | |
---|
267 | n/a | argument_signatures = [ |
---|
268 | n/a | Sig('-x', action='store_true'), |
---|
269 | n/a | Sig('-yyy', action='store_const', const=42), |
---|
270 | n/a | Sig('-z'), |
---|
271 | n/a | ] |
---|
272 | n/a | failures = ['a', '--foo', '-xa', '-x --foo', '-x -z', '-z -x', |
---|
273 | n/a | '-yx', '-yz a', '-yyyx', '-yyyza', '-xyza'] |
---|
274 | n/a | successes = [ |
---|
275 | n/a | ('', NS(x=False, yyy=None, z=None)), |
---|
276 | n/a | ('-x', NS(x=True, yyy=None, z=None)), |
---|
277 | n/a | ('-za', NS(x=False, yyy=None, z='a')), |
---|
278 | n/a | ('-z a', NS(x=False, yyy=None, z='a')), |
---|
279 | n/a | ('-xza', NS(x=True, yyy=None, z='a')), |
---|
280 | n/a | ('-xz a', NS(x=True, yyy=None, z='a')), |
---|
281 | n/a | ('-x -za', NS(x=True, yyy=None, z='a')), |
---|
282 | n/a | ('-x -z a', NS(x=True, yyy=None, z='a')), |
---|
283 | n/a | ('-y', NS(x=False, yyy=42, z=None)), |
---|
284 | n/a | ('-yyy', NS(x=False, yyy=42, z=None)), |
---|
285 | n/a | ('-x -yyy -za', NS(x=True, yyy=42, z='a')), |
---|
286 | n/a | ('-x -yyy -z a', NS(x=True, yyy=42, z='a')), |
---|
287 | n/a | ] |
---|
288 | n/a | |
---|
289 | n/a | |
---|
290 | n/a | class TestOptionalsSingleDashLong(ParserTestCase): |
---|
291 | n/a | """Test an Optional with a multi-character single-dash option string""" |
---|
292 | n/a | |
---|
293 | n/a | argument_signatures = [Sig('-foo')] |
---|
294 | n/a | failures = ['-foo', 'a', '--foo', '-foo --foo', '-foo -y', '-fooa'] |
---|
295 | n/a | successes = [ |
---|
296 | n/a | ('', NS(foo=None)), |
---|
297 | n/a | ('-foo a', NS(foo='a')), |
---|
298 | n/a | ('-foo -1', NS(foo='-1')), |
---|
299 | n/a | ('-fo a', NS(foo='a')), |
---|
300 | n/a | ('-f a', NS(foo='a')), |
---|
301 | n/a | ] |
---|
302 | n/a | |
---|
303 | n/a | |
---|
304 | n/a | class TestOptionalsSingleDashSubsetAmbiguous(ParserTestCase): |
---|
305 | n/a | """Test Optionals where option strings are subsets of each other""" |
---|
306 | n/a | |
---|
307 | n/a | argument_signatures = [Sig('-f'), Sig('-foobar'), Sig('-foorab')] |
---|
308 | n/a | failures = ['-f', '-foo', '-fo', '-foo b', '-foob', '-fooba', '-foora'] |
---|
309 | n/a | successes = [ |
---|
310 | n/a | ('', NS(f=None, foobar=None, foorab=None)), |
---|
311 | n/a | ('-f a', NS(f='a', foobar=None, foorab=None)), |
---|
312 | n/a | ('-fa', NS(f='a', foobar=None, foorab=None)), |
---|
313 | n/a | ('-foa', NS(f='oa', foobar=None, foorab=None)), |
---|
314 | n/a | ('-fooa', NS(f='ooa', foobar=None, foorab=None)), |
---|
315 | n/a | ('-foobar a', NS(f=None, foobar='a', foorab=None)), |
---|
316 | n/a | ('-foorab a', NS(f=None, foobar=None, foorab='a')), |
---|
317 | n/a | ] |
---|
318 | n/a | |
---|
319 | n/a | |
---|
320 | n/a | class TestOptionalsSingleDashAmbiguous(ParserTestCase): |
---|
321 | n/a | """Test Optionals that partially match but are not subsets""" |
---|
322 | n/a | |
---|
323 | n/a | argument_signatures = [Sig('-foobar'), Sig('-foorab')] |
---|
324 | n/a | failures = ['-f', '-f a', '-fa', '-foa', '-foo', '-fo', '-foo b'] |
---|
325 | n/a | successes = [ |
---|
326 | n/a | ('', NS(foobar=None, foorab=None)), |
---|
327 | n/a | ('-foob a', NS(foobar='a', foorab=None)), |
---|
328 | n/a | ('-foor a', NS(foobar=None, foorab='a')), |
---|
329 | n/a | ('-fooba a', NS(foobar='a', foorab=None)), |
---|
330 | n/a | ('-foora a', NS(foobar=None, foorab='a')), |
---|
331 | n/a | ('-foobar a', NS(foobar='a', foorab=None)), |
---|
332 | n/a | ('-foorab a', NS(foobar=None, foorab='a')), |
---|
333 | n/a | ] |
---|
334 | n/a | |
---|
335 | n/a | |
---|
336 | n/a | class TestOptionalsNumeric(ParserTestCase): |
---|
337 | n/a | """Test an Optional with a short opt string""" |
---|
338 | n/a | |
---|
339 | n/a | argument_signatures = [Sig('-1', dest='one')] |
---|
340 | n/a | failures = ['-1', 'a', '-1 --foo', '-1 -y', '-1 -1', '-1 -2'] |
---|
341 | n/a | successes = [ |
---|
342 | n/a | ('', NS(one=None)), |
---|
343 | n/a | ('-1 a', NS(one='a')), |
---|
344 | n/a | ('-1a', NS(one='a')), |
---|
345 | n/a | ('-1-2', NS(one='-2')), |
---|
346 | n/a | ] |
---|
347 | n/a | |
---|
348 | n/a | |
---|
349 | n/a | class TestOptionalsDoubleDash(ParserTestCase): |
---|
350 | n/a | """Test an Optional with a double-dash option string""" |
---|
351 | n/a | |
---|
352 | n/a | argument_signatures = [Sig('--foo')] |
---|
353 | n/a | failures = ['--foo', '-f', '-f a', 'a', '--foo -x', '--foo --bar'] |
---|
354 | n/a | successes = [ |
---|
355 | n/a | ('', NS(foo=None)), |
---|
356 | n/a | ('--foo a', NS(foo='a')), |
---|
357 | n/a | ('--foo=a', NS(foo='a')), |
---|
358 | n/a | ('--foo -2.5', NS(foo='-2.5')), |
---|
359 | n/a | ('--foo=-2.5', NS(foo='-2.5')), |
---|
360 | n/a | ] |
---|
361 | n/a | |
---|
362 | n/a | |
---|
363 | n/a | class TestOptionalsDoubleDashPartialMatch(ParserTestCase): |
---|
364 | n/a | """Tests partial matching with a double-dash option string""" |
---|
365 | n/a | |
---|
366 | n/a | argument_signatures = [ |
---|
367 | n/a | Sig('--badger', action='store_true'), |
---|
368 | n/a | Sig('--bat'), |
---|
369 | n/a | ] |
---|
370 | n/a | failures = ['--bar', '--b', '--ba', '--b=2', '--ba=4', '--badge 5'] |
---|
371 | n/a | successes = [ |
---|
372 | n/a | ('', NS(badger=False, bat=None)), |
---|
373 | n/a | ('--bat X', NS(badger=False, bat='X')), |
---|
374 | n/a | ('--bad', NS(badger=True, bat=None)), |
---|
375 | n/a | ('--badg', NS(badger=True, bat=None)), |
---|
376 | n/a | ('--badge', NS(badger=True, bat=None)), |
---|
377 | n/a | ('--badger', NS(badger=True, bat=None)), |
---|
378 | n/a | ] |
---|
379 | n/a | |
---|
380 | n/a | |
---|
381 | n/a | class TestOptionalsDoubleDashPrefixMatch(ParserTestCase): |
---|
382 | n/a | """Tests when one double-dash option string is a prefix of another""" |
---|
383 | n/a | |
---|
384 | n/a | argument_signatures = [ |
---|
385 | n/a | Sig('--badger', action='store_true'), |
---|
386 | n/a | Sig('--ba'), |
---|
387 | n/a | ] |
---|
388 | n/a | failures = ['--bar', '--b', '--ba', '--b=2', '--badge 5'] |
---|
389 | n/a | successes = [ |
---|
390 | n/a | ('', NS(badger=False, ba=None)), |
---|
391 | n/a | ('--ba X', NS(badger=False, ba='X')), |
---|
392 | n/a | ('--ba=X', NS(badger=False, ba='X')), |
---|
393 | n/a | ('--bad', NS(badger=True, ba=None)), |
---|
394 | n/a | ('--badg', NS(badger=True, ba=None)), |
---|
395 | n/a | ('--badge', NS(badger=True, ba=None)), |
---|
396 | n/a | ('--badger', NS(badger=True, ba=None)), |
---|
397 | n/a | ] |
---|
398 | n/a | |
---|
399 | n/a | |
---|
400 | n/a | class TestOptionalsSingleDoubleDash(ParserTestCase): |
---|
401 | n/a | """Test an Optional with single- and double-dash option strings""" |
---|
402 | n/a | |
---|
403 | n/a | argument_signatures = [ |
---|
404 | n/a | Sig('-f', action='store_true'), |
---|
405 | n/a | Sig('--bar'), |
---|
406 | n/a | Sig('-baz', action='store_const', const=42), |
---|
407 | n/a | ] |
---|
408 | n/a | failures = ['--bar', '-fbar', '-fbaz', '-bazf', '-b B', 'B'] |
---|
409 | n/a | successes = [ |
---|
410 | n/a | ('', NS(f=False, bar=None, baz=None)), |
---|
411 | n/a | ('-f', NS(f=True, bar=None, baz=None)), |
---|
412 | n/a | ('--ba B', NS(f=False, bar='B', baz=None)), |
---|
413 | n/a | ('-f --bar B', NS(f=True, bar='B', baz=None)), |
---|
414 | n/a | ('-f -b', NS(f=True, bar=None, baz=42)), |
---|
415 | n/a | ('-ba -f', NS(f=True, bar=None, baz=42)), |
---|
416 | n/a | ] |
---|
417 | n/a | |
---|
418 | n/a | |
---|
419 | n/a | class TestOptionalsAlternatePrefixChars(ParserTestCase): |
---|
420 | n/a | """Test an Optional with option strings with custom prefixes""" |
---|
421 | n/a | |
---|
422 | n/a | parser_signature = Sig(prefix_chars='+:/', add_help=False) |
---|
423 | n/a | argument_signatures = [ |
---|
424 | n/a | Sig('+f', action='store_true'), |
---|
425 | n/a | Sig('::bar'), |
---|
426 | n/a | Sig('/baz', action='store_const', const=42), |
---|
427 | n/a | ] |
---|
428 | n/a | failures = ['--bar', '-fbar', '-b B', 'B', '-f', '--bar B', '-baz', '-h', '--help', '+h', '::help', '/help'] |
---|
429 | n/a | successes = [ |
---|
430 | n/a | ('', NS(f=False, bar=None, baz=None)), |
---|
431 | n/a | ('+f', NS(f=True, bar=None, baz=None)), |
---|
432 | n/a | ('::ba B', NS(f=False, bar='B', baz=None)), |
---|
433 | n/a | ('+f ::bar B', NS(f=True, bar='B', baz=None)), |
---|
434 | n/a | ('+f /b', NS(f=True, bar=None, baz=42)), |
---|
435 | n/a | ('/ba +f', NS(f=True, bar=None, baz=42)), |
---|
436 | n/a | ] |
---|
437 | n/a | |
---|
438 | n/a | |
---|
439 | n/a | class TestOptionalsAlternatePrefixCharsAddedHelp(ParserTestCase): |
---|
440 | n/a | """When ``-`` not in prefix_chars, default operators created for help |
---|
441 | n/a | should use the prefix_chars in use rather than - or -- |
---|
442 | n/a | http://bugs.python.org/issue9444""" |
---|
443 | n/a | |
---|
444 | n/a | parser_signature = Sig(prefix_chars='+:/', add_help=True) |
---|
445 | n/a | argument_signatures = [ |
---|
446 | n/a | Sig('+f', action='store_true'), |
---|
447 | n/a | Sig('::bar'), |
---|
448 | n/a | Sig('/baz', action='store_const', const=42), |
---|
449 | n/a | ] |
---|
450 | n/a | failures = ['--bar', '-fbar', '-b B', 'B', '-f', '--bar B', '-baz'] |
---|
451 | n/a | successes = [ |
---|
452 | n/a | ('', NS(f=False, bar=None, baz=None)), |
---|
453 | n/a | ('+f', NS(f=True, bar=None, baz=None)), |
---|
454 | n/a | ('::ba B', NS(f=False, bar='B', baz=None)), |
---|
455 | n/a | ('+f ::bar B', NS(f=True, bar='B', baz=None)), |
---|
456 | n/a | ('+f /b', NS(f=True, bar=None, baz=42)), |
---|
457 | n/a | ('/ba +f', NS(f=True, bar=None, baz=42)) |
---|
458 | n/a | ] |
---|
459 | n/a | |
---|
460 | n/a | |
---|
461 | n/a | class TestOptionalsAlternatePrefixCharsMultipleShortArgs(ParserTestCase): |
---|
462 | n/a | """Verify that Optionals must be called with their defined prefixes""" |
---|
463 | n/a | |
---|
464 | n/a | parser_signature = Sig(prefix_chars='+-', add_help=False) |
---|
465 | n/a | argument_signatures = [ |
---|
466 | n/a | Sig('-x', action='store_true'), |
---|
467 | n/a | Sig('+y', action='store_true'), |
---|
468 | n/a | Sig('+z', action='store_true'), |
---|
469 | n/a | ] |
---|
470 | n/a | failures = ['-w', |
---|
471 | n/a | '-xyz', |
---|
472 | n/a | '+x', |
---|
473 | n/a | '-y', |
---|
474 | n/a | '+xyz', |
---|
475 | n/a | ] |
---|
476 | n/a | successes = [ |
---|
477 | n/a | ('', NS(x=False, y=False, z=False)), |
---|
478 | n/a | ('-x', NS(x=True, y=False, z=False)), |
---|
479 | n/a | ('+y -x', NS(x=True, y=True, z=False)), |
---|
480 | n/a | ('+yz -x', NS(x=True, y=True, z=True)), |
---|
481 | n/a | ] |
---|
482 | n/a | |
---|
483 | n/a | |
---|
484 | n/a | class TestOptionalsShortLong(ParserTestCase): |
---|
485 | n/a | """Test a combination of single- and double-dash option strings""" |
---|
486 | n/a | |
---|
487 | n/a | argument_signatures = [ |
---|
488 | n/a | Sig('-v', '--verbose', '-n', '--noisy', action='store_true'), |
---|
489 | n/a | ] |
---|
490 | n/a | failures = ['--x --verbose', '-N', 'a', '-v x'] |
---|
491 | n/a | successes = [ |
---|
492 | n/a | ('', NS(verbose=False)), |
---|
493 | n/a | ('-v', NS(verbose=True)), |
---|
494 | n/a | ('--verbose', NS(verbose=True)), |
---|
495 | n/a | ('-n', NS(verbose=True)), |
---|
496 | n/a | ('--noisy', NS(verbose=True)), |
---|
497 | n/a | ] |
---|
498 | n/a | |
---|
499 | n/a | |
---|
500 | n/a | class TestOptionalsDest(ParserTestCase): |
---|
501 | n/a | """Tests various means of setting destination""" |
---|
502 | n/a | |
---|
503 | n/a | argument_signatures = [Sig('--foo-bar'), Sig('--baz', dest='zabbaz')] |
---|
504 | n/a | failures = ['a'] |
---|
505 | n/a | successes = [ |
---|
506 | n/a | ('--foo-bar f', NS(foo_bar='f', zabbaz=None)), |
---|
507 | n/a | ('--baz g', NS(foo_bar=None, zabbaz='g')), |
---|
508 | n/a | ('--foo-bar h --baz i', NS(foo_bar='h', zabbaz='i')), |
---|
509 | n/a | ('--baz j --foo-bar k', NS(foo_bar='k', zabbaz='j')), |
---|
510 | n/a | ] |
---|
511 | n/a | |
---|
512 | n/a | |
---|
513 | n/a | class TestOptionalsDefault(ParserTestCase): |
---|
514 | n/a | """Tests specifying a default for an Optional""" |
---|
515 | n/a | |
---|
516 | n/a | argument_signatures = [Sig('-x'), Sig('-y', default=42)] |
---|
517 | n/a | failures = ['a'] |
---|
518 | n/a | successes = [ |
---|
519 | n/a | ('', NS(x=None, y=42)), |
---|
520 | n/a | ('-xx', NS(x='x', y=42)), |
---|
521 | n/a | ('-yy', NS(x=None, y='y')), |
---|
522 | n/a | ] |
---|
523 | n/a | |
---|
524 | n/a | |
---|
525 | n/a | class TestOptionalsNargsDefault(ParserTestCase): |
---|
526 | n/a | """Tests not specifying the number of args for an Optional""" |
---|
527 | n/a | |
---|
528 | n/a | argument_signatures = [Sig('-x')] |
---|
529 | n/a | failures = ['a', '-x'] |
---|
530 | n/a | successes = [ |
---|
531 | n/a | ('', NS(x=None)), |
---|
532 | n/a | ('-x a', NS(x='a')), |
---|
533 | n/a | ] |
---|
534 | n/a | |
---|
535 | n/a | |
---|
536 | n/a | class TestOptionalsNargs1(ParserTestCase): |
---|
537 | n/a | """Tests specifying 1 arg for an Optional""" |
---|
538 | n/a | |
---|
539 | n/a | argument_signatures = [Sig('-x', nargs=1)] |
---|
540 | n/a | failures = ['a', '-x'] |
---|
541 | n/a | successes = [ |
---|
542 | n/a | ('', NS(x=None)), |
---|
543 | n/a | ('-x a', NS(x=['a'])), |
---|
544 | n/a | ] |
---|
545 | n/a | |
---|
546 | n/a | |
---|
547 | n/a | class TestOptionalsNargs3(ParserTestCase): |
---|
548 | n/a | """Tests specifying 3 args for an Optional""" |
---|
549 | n/a | |
---|
550 | n/a | argument_signatures = [Sig('-x', nargs=3)] |
---|
551 | n/a | failures = ['a', '-x', '-x a', '-x a b', 'a -x', 'a -x b'] |
---|
552 | n/a | successes = [ |
---|
553 | n/a | ('', NS(x=None)), |
---|
554 | n/a | ('-x a b c', NS(x=['a', 'b', 'c'])), |
---|
555 | n/a | ] |
---|
556 | n/a | |
---|
557 | n/a | |
---|
558 | n/a | class TestOptionalsNargsOptional(ParserTestCase): |
---|
559 | n/a | """Tests specifying an Optional arg for an Optional""" |
---|
560 | n/a | |
---|
561 | n/a | argument_signatures = [ |
---|
562 | n/a | Sig('-w', nargs='?'), |
---|
563 | n/a | Sig('-x', nargs='?', const=42), |
---|
564 | n/a | Sig('-y', nargs='?', default='spam'), |
---|
565 | n/a | Sig('-z', nargs='?', type=int, const='42', default='84'), |
---|
566 | n/a | ] |
---|
567 | n/a | failures = ['2'] |
---|
568 | n/a | successes = [ |
---|
569 | n/a | ('', NS(w=None, x=None, y='spam', z=84)), |
---|
570 | n/a | ('-w', NS(w=None, x=None, y='spam', z=84)), |
---|
571 | n/a | ('-w 2', NS(w='2', x=None, y='spam', z=84)), |
---|
572 | n/a | ('-x', NS(w=None, x=42, y='spam', z=84)), |
---|
573 | n/a | ('-x 2', NS(w=None, x='2', y='spam', z=84)), |
---|
574 | n/a | ('-y', NS(w=None, x=None, y=None, z=84)), |
---|
575 | n/a | ('-y 2', NS(w=None, x=None, y='2', z=84)), |
---|
576 | n/a | ('-z', NS(w=None, x=None, y='spam', z=42)), |
---|
577 | n/a | ('-z 2', NS(w=None, x=None, y='spam', z=2)), |
---|
578 | n/a | ] |
---|
579 | n/a | |
---|
580 | n/a | |
---|
581 | n/a | class TestOptionalsNargsZeroOrMore(ParserTestCase): |
---|
582 | n/a | """Tests specifying args for an Optional that accepts zero or more""" |
---|
583 | n/a | |
---|
584 | n/a | argument_signatures = [ |
---|
585 | n/a | Sig('-x', nargs='*'), |
---|
586 | n/a | Sig('-y', nargs='*', default='spam'), |
---|
587 | n/a | ] |
---|
588 | n/a | failures = ['a'] |
---|
589 | n/a | successes = [ |
---|
590 | n/a | ('', NS(x=None, y='spam')), |
---|
591 | n/a | ('-x', NS(x=[], y='spam')), |
---|
592 | n/a | ('-x a', NS(x=['a'], y='spam')), |
---|
593 | n/a | ('-x a b', NS(x=['a', 'b'], y='spam')), |
---|
594 | n/a | ('-y', NS(x=None, y=[])), |
---|
595 | n/a | ('-y a', NS(x=None, y=['a'])), |
---|
596 | n/a | ('-y a b', NS(x=None, y=['a', 'b'])), |
---|
597 | n/a | ] |
---|
598 | n/a | |
---|
599 | n/a | |
---|
600 | n/a | class TestOptionalsNargsOneOrMore(ParserTestCase): |
---|
601 | n/a | """Tests specifying args for an Optional that accepts one or more""" |
---|
602 | n/a | |
---|
603 | n/a | argument_signatures = [ |
---|
604 | n/a | Sig('-x', nargs='+'), |
---|
605 | n/a | Sig('-y', nargs='+', default='spam'), |
---|
606 | n/a | ] |
---|
607 | n/a | failures = ['a', '-x', '-y', 'a -x', 'a -y b'] |
---|
608 | n/a | successes = [ |
---|
609 | n/a | ('', NS(x=None, y='spam')), |
---|
610 | n/a | ('-x a', NS(x=['a'], y='spam')), |
---|
611 | n/a | ('-x a b', NS(x=['a', 'b'], y='spam')), |
---|
612 | n/a | ('-y a', NS(x=None, y=['a'])), |
---|
613 | n/a | ('-y a b', NS(x=None, y=['a', 'b'])), |
---|
614 | n/a | ] |
---|
615 | n/a | |
---|
616 | n/a | |
---|
617 | n/a | class TestOptionalsChoices(ParserTestCase): |
---|
618 | n/a | """Tests specifying the choices for an Optional""" |
---|
619 | n/a | |
---|
620 | n/a | argument_signatures = [ |
---|
621 | n/a | Sig('-f', choices='abc'), |
---|
622 | n/a | Sig('-g', type=int, choices=range(5))] |
---|
623 | n/a | failures = ['a', '-f d', '-fad', '-ga', '-g 6'] |
---|
624 | n/a | successes = [ |
---|
625 | n/a | ('', NS(f=None, g=None)), |
---|
626 | n/a | ('-f a', NS(f='a', g=None)), |
---|
627 | n/a | ('-f c', NS(f='c', g=None)), |
---|
628 | n/a | ('-g 0', NS(f=None, g=0)), |
---|
629 | n/a | ('-g 03', NS(f=None, g=3)), |
---|
630 | n/a | ('-fb -g4', NS(f='b', g=4)), |
---|
631 | n/a | ] |
---|
632 | n/a | |
---|
633 | n/a | |
---|
634 | n/a | class TestOptionalsRequired(ParserTestCase): |
---|
635 | n/a | """Tests an optional action that is required""" |
---|
636 | n/a | |
---|
637 | n/a | argument_signatures = [ |
---|
638 | n/a | Sig('-x', type=int, required=True), |
---|
639 | n/a | ] |
---|
640 | n/a | failures = ['a', ''] |
---|
641 | n/a | successes = [ |
---|
642 | n/a | ('-x 1', NS(x=1)), |
---|
643 | n/a | ('-x42', NS(x=42)), |
---|
644 | n/a | ] |
---|
645 | n/a | |
---|
646 | n/a | |
---|
647 | n/a | class TestOptionalsActionStore(ParserTestCase): |
---|
648 | n/a | """Tests the store action for an Optional""" |
---|
649 | n/a | |
---|
650 | n/a | argument_signatures = [Sig('-x', action='store')] |
---|
651 | n/a | failures = ['a', 'a -x'] |
---|
652 | n/a | successes = [ |
---|
653 | n/a | ('', NS(x=None)), |
---|
654 | n/a | ('-xfoo', NS(x='foo')), |
---|
655 | n/a | ] |
---|
656 | n/a | |
---|
657 | n/a | |
---|
658 | n/a | class TestOptionalsActionStoreConst(ParserTestCase): |
---|
659 | n/a | """Tests the store_const action for an Optional""" |
---|
660 | n/a | |
---|
661 | n/a | argument_signatures = [Sig('-y', action='store_const', const=object)] |
---|
662 | n/a | failures = ['a'] |
---|
663 | n/a | successes = [ |
---|
664 | n/a | ('', NS(y=None)), |
---|
665 | n/a | ('-y', NS(y=object)), |
---|
666 | n/a | ] |
---|
667 | n/a | |
---|
668 | n/a | |
---|
669 | n/a | class TestOptionalsActionStoreFalse(ParserTestCase): |
---|
670 | n/a | """Tests the store_false action for an Optional""" |
---|
671 | n/a | |
---|
672 | n/a | argument_signatures = [Sig('-z', action='store_false')] |
---|
673 | n/a | failures = ['a', '-za', '-z a'] |
---|
674 | n/a | successes = [ |
---|
675 | n/a | ('', NS(z=True)), |
---|
676 | n/a | ('-z', NS(z=False)), |
---|
677 | n/a | ] |
---|
678 | n/a | |
---|
679 | n/a | |
---|
680 | n/a | class TestOptionalsActionStoreTrue(ParserTestCase): |
---|
681 | n/a | """Tests the store_true action for an Optional""" |
---|
682 | n/a | |
---|
683 | n/a | argument_signatures = [Sig('--apple', action='store_true')] |
---|
684 | n/a | failures = ['a', '--apple=b', '--apple b'] |
---|
685 | n/a | successes = [ |
---|
686 | n/a | ('', NS(apple=False)), |
---|
687 | n/a | ('--apple', NS(apple=True)), |
---|
688 | n/a | ] |
---|
689 | n/a | |
---|
690 | n/a | |
---|
691 | n/a | class TestOptionalsActionAppend(ParserTestCase): |
---|
692 | n/a | """Tests the append action for an Optional""" |
---|
693 | n/a | |
---|
694 | n/a | argument_signatures = [Sig('--baz', action='append')] |
---|
695 | n/a | failures = ['a', '--baz', 'a --baz', '--baz a b'] |
---|
696 | n/a | successes = [ |
---|
697 | n/a | ('', NS(baz=None)), |
---|
698 | n/a | ('--baz a', NS(baz=['a'])), |
---|
699 | n/a | ('--baz a --baz b', NS(baz=['a', 'b'])), |
---|
700 | n/a | ] |
---|
701 | n/a | |
---|
702 | n/a | |
---|
703 | n/a | class TestOptionalsActionAppendWithDefault(ParserTestCase): |
---|
704 | n/a | """Tests the append action for an Optional""" |
---|
705 | n/a | |
---|
706 | n/a | argument_signatures = [Sig('--baz', action='append', default=['X'])] |
---|
707 | n/a | failures = ['a', '--baz', 'a --baz', '--baz a b'] |
---|
708 | n/a | successes = [ |
---|
709 | n/a | ('', NS(baz=['X'])), |
---|
710 | n/a | ('--baz a', NS(baz=['X', 'a'])), |
---|
711 | n/a | ('--baz a --baz b', NS(baz=['X', 'a', 'b'])), |
---|
712 | n/a | ] |
---|
713 | n/a | |
---|
714 | n/a | |
---|
715 | n/a | class TestOptionalsActionAppendConst(ParserTestCase): |
---|
716 | n/a | """Tests the append_const action for an Optional""" |
---|
717 | n/a | |
---|
718 | n/a | argument_signatures = [ |
---|
719 | n/a | Sig('-b', action='append_const', const=Exception), |
---|
720 | n/a | Sig('-c', action='append', dest='b'), |
---|
721 | n/a | ] |
---|
722 | n/a | failures = ['a', '-c', 'a -c', '-bx', '-b x'] |
---|
723 | n/a | successes = [ |
---|
724 | n/a | ('', NS(b=None)), |
---|
725 | n/a | ('-b', NS(b=[Exception])), |
---|
726 | n/a | ('-b -cx -b -cyz', NS(b=[Exception, 'x', Exception, 'yz'])), |
---|
727 | n/a | ] |
---|
728 | n/a | |
---|
729 | n/a | |
---|
730 | n/a | class TestOptionalsActionAppendConstWithDefault(ParserTestCase): |
---|
731 | n/a | """Tests the append_const action for an Optional""" |
---|
732 | n/a | |
---|
733 | n/a | argument_signatures = [ |
---|
734 | n/a | Sig('-b', action='append_const', const=Exception, default=['X']), |
---|
735 | n/a | Sig('-c', action='append', dest='b'), |
---|
736 | n/a | ] |
---|
737 | n/a | failures = ['a', '-c', 'a -c', '-bx', '-b x'] |
---|
738 | n/a | successes = [ |
---|
739 | n/a | ('', NS(b=['X'])), |
---|
740 | n/a | ('-b', NS(b=['X', Exception])), |
---|
741 | n/a | ('-b -cx -b -cyz', NS(b=['X', Exception, 'x', Exception, 'yz'])), |
---|
742 | n/a | ] |
---|
743 | n/a | |
---|
744 | n/a | |
---|
745 | n/a | class TestOptionalsActionCount(ParserTestCase): |
---|
746 | n/a | """Tests the count action for an Optional""" |
---|
747 | n/a | |
---|
748 | n/a | argument_signatures = [Sig('-x', action='count')] |
---|
749 | n/a | failures = ['a', '-x a', '-x b', '-x a -x b'] |
---|
750 | n/a | successes = [ |
---|
751 | n/a | ('', NS(x=None)), |
---|
752 | n/a | ('-x', NS(x=1)), |
---|
753 | n/a | ] |
---|
754 | n/a | |
---|
755 | n/a | |
---|
756 | n/a | class TestOptionalsAllowLongAbbreviation(ParserTestCase): |
---|
757 | n/a | """Allow long options to be abbreviated unambiguously""" |
---|
758 | n/a | |
---|
759 | n/a | argument_signatures = [ |
---|
760 | n/a | Sig('--foo'), |
---|
761 | n/a | Sig('--foobaz'), |
---|
762 | n/a | Sig('--fooble', action='store_true'), |
---|
763 | n/a | ] |
---|
764 | n/a | failures = ['--foob 5', '--foob'] |
---|
765 | n/a | successes = [ |
---|
766 | n/a | ('', NS(foo=None, foobaz=None, fooble=False)), |
---|
767 | n/a | ('--foo 7', NS(foo='7', foobaz=None, fooble=False)), |
---|
768 | n/a | ('--fooba a', NS(foo=None, foobaz='a', fooble=False)), |
---|
769 | n/a | ('--foobl --foo g', NS(foo='g', foobaz=None, fooble=True)), |
---|
770 | n/a | ] |
---|
771 | n/a | |
---|
772 | n/a | |
---|
773 | n/a | class TestOptionalsDisallowLongAbbreviation(ParserTestCase): |
---|
774 | n/a | """Do not allow abbreviations of long options at all""" |
---|
775 | n/a | |
---|
776 | n/a | parser_signature = Sig(allow_abbrev=False) |
---|
777 | n/a | argument_signatures = [ |
---|
778 | n/a | Sig('--foo'), |
---|
779 | n/a | Sig('--foodle', action='store_true'), |
---|
780 | n/a | Sig('--foonly'), |
---|
781 | n/a | ] |
---|
782 | n/a | failures = ['-foon 3', '--foon 3', '--food', '--food --foo 2'] |
---|
783 | n/a | successes = [ |
---|
784 | n/a | ('', NS(foo=None, foodle=False, foonly=None)), |
---|
785 | n/a | ('--foo 3', NS(foo='3', foodle=False, foonly=None)), |
---|
786 | n/a | ('--foonly 7 --foodle --foo 2', NS(foo='2', foodle=True, foonly='7')), |
---|
787 | n/a | ] |
---|
788 | n/a | |
---|
789 | n/a | # ================ |
---|
790 | n/a | # Positional tests |
---|
791 | n/a | # ================ |
---|
792 | n/a | |
---|
793 | n/a | class TestPositionalsNargsNone(ParserTestCase): |
---|
794 | n/a | """Test a Positional that doesn't specify nargs""" |
---|
795 | n/a | |
---|
796 | n/a | argument_signatures = [Sig('foo')] |
---|
797 | n/a | failures = ['', '-x', 'a b'] |
---|
798 | n/a | successes = [ |
---|
799 | n/a | ('a', NS(foo='a')), |
---|
800 | n/a | ] |
---|
801 | n/a | |
---|
802 | n/a | |
---|
803 | n/a | class TestPositionalsNargs1(ParserTestCase): |
---|
804 | n/a | """Test a Positional that specifies an nargs of 1""" |
---|
805 | n/a | |
---|
806 | n/a | argument_signatures = [Sig('foo', nargs=1)] |
---|
807 | n/a | failures = ['', '-x', 'a b'] |
---|
808 | n/a | successes = [ |
---|
809 | n/a | ('a', NS(foo=['a'])), |
---|
810 | n/a | ] |
---|
811 | n/a | |
---|
812 | n/a | |
---|
813 | n/a | class TestPositionalsNargs2(ParserTestCase): |
---|
814 | n/a | """Test a Positional that specifies an nargs of 2""" |
---|
815 | n/a | |
---|
816 | n/a | argument_signatures = [Sig('foo', nargs=2)] |
---|
817 | n/a | failures = ['', 'a', '-x', 'a b c'] |
---|
818 | n/a | successes = [ |
---|
819 | n/a | ('a b', NS(foo=['a', 'b'])), |
---|
820 | n/a | ] |
---|
821 | n/a | |
---|
822 | n/a | |
---|
823 | n/a | class TestPositionalsNargsZeroOrMore(ParserTestCase): |
---|
824 | n/a | """Test a Positional that specifies unlimited nargs""" |
---|
825 | n/a | |
---|
826 | n/a | argument_signatures = [Sig('foo', nargs='*')] |
---|
827 | n/a | failures = ['-x'] |
---|
828 | n/a | successes = [ |
---|
829 | n/a | ('', NS(foo=[])), |
---|
830 | n/a | ('a', NS(foo=['a'])), |
---|
831 | n/a | ('a b', NS(foo=['a', 'b'])), |
---|
832 | n/a | ] |
---|
833 | n/a | |
---|
834 | n/a | |
---|
835 | n/a | class TestPositionalsNargsZeroOrMoreDefault(ParserTestCase): |
---|
836 | n/a | """Test a Positional that specifies unlimited nargs and a default""" |
---|
837 | n/a | |
---|
838 | n/a | argument_signatures = [Sig('foo', nargs='*', default='bar')] |
---|
839 | n/a | failures = ['-x'] |
---|
840 | n/a | successes = [ |
---|
841 | n/a | ('', NS(foo='bar')), |
---|
842 | n/a | ('a', NS(foo=['a'])), |
---|
843 | n/a | ('a b', NS(foo=['a', 'b'])), |
---|
844 | n/a | ] |
---|
845 | n/a | |
---|
846 | n/a | |
---|
847 | n/a | class TestPositionalsNargsOneOrMore(ParserTestCase): |
---|
848 | n/a | """Test a Positional that specifies one or more nargs""" |
---|
849 | n/a | |
---|
850 | n/a | argument_signatures = [Sig('foo', nargs='+')] |
---|
851 | n/a | failures = ['', '-x'] |
---|
852 | n/a | successes = [ |
---|
853 | n/a | ('a', NS(foo=['a'])), |
---|
854 | n/a | ('a b', NS(foo=['a', 'b'])), |
---|
855 | n/a | ] |
---|
856 | n/a | |
---|
857 | n/a | |
---|
858 | n/a | class TestPositionalsNargsOptional(ParserTestCase): |
---|
859 | n/a | """Tests an Optional Positional""" |
---|
860 | n/a | |
---|
861 | n/a | argument_signatures = [Sig('foo', nargs='?')] |
---|
862 | n/a | failures = ['-x', 'a b'] |
---|
863 | n/a | successes = [ |
---|
864 | n/a | ('', NS(foo=None)), |
---|
865 | n/a | ('a', NS(foo='a')), |
---|
866 | n/a | ] |
---|
867 | n/a | |
---|
868 | n/a | |
---|
869 | n/a | class TestPositionalsNargsOptionalDefault(ParserTestCase): |
---|
870 | n/a | """Tests an Optional Positional with a default value""" |
---|
871 | n/a | |
---|
872 | n/a | argument_signatures = [Sig('foo', nargs='?', default=42)] |
---|
873 | n/a | failures = ['-x', 'a b'] |
---|
874 | n/a | successes = [ |
---|
875 | n/a | ('', NS(foo=42)), |
---|
876 | n/a | ('a', NS(foo='a')), |
---|
877 | n/a | ] |
---|
878 | n/a | |
---|
879 | n/a | |
---|
880 | n/a | class TestPositionalsNargsOptionalConvertedDefault(ParserTestCase): |
---|
881 | n/a | """Tests an Optional Positional with a default value |
---|
882 | n/a | that needs to be converted to the appropriate type. |
---|
883 | n/a | """ |
---|
884 | n/a | |
---|
885 | n/a | argument_signatures = [ |
---|
886 | n/a | Sig('foo', nargs='?', type=int, default='42'), |
---|
887 | n/a | ] |
---|
888 | n/a | failures = ['-x', 'a b', '1 2'] |
---|
889 | n/a | successes = [ |
---|
890 | n/a | ('', NS(foo=42)), |
---|
891 | n/a | ('1', NS(foo=1)), |
---|
892 | n/a | ] |
---|
893 | n/a | |
---|
894 | n/a | |
---|
895 | n/a | class TestPositionalsNargsNoneNone(ParserTestCase): |
---|
896 | n/a | """Test two Positionals that don't specify nargs""" |
---|
897 | n/a | |
---|
898 | n/a | argument_signatures = [Sig('foo'), Sig('bar')] |
---|
899 | n/a | failures = ['', '-x', 'a', 'a b c'] |
---|
900 | n/a | successes = [ |
---|
901 | n/a | ('a b', NS(foo='a', bar='b')), |
---|
902 | n/a | ] |
---|
903 | n/a | |
---|
904 | n/a | |
---|
905 | n/a | class TestPositionalsNargsNone1(ParserTestCase): |
---|
906 | n/a | """Test a Positional with no nargs followed by one with 1""" |
---|
907 | n/a | |
---|
908 | n/a | argument_signatures = [Sig('foo'), Sig('bar', nargs=1)] |
---|
909 | n/a | failures = ['', '--foo', 'a', 'a b c'] |
---|
910 | n/a | successes = [ |
---|
911 | n/a | ('a b', NS(foo='a', bar=['b'])), |
---|
912 | n/a | ] |
---|
913 | n/a | |
---|
914 | n/a | |
---|
915 | n/a | class TestPositionalsNargs2None(ParserTestCase): |
---|
916 | n/a | """Test a Positional with 2 nargs followed by one with none""" |
---|
917 | n/a | |
---|
918 | n/a | argument_signatures = [Sig('foo', nargs=2), Sig('bar')] |
---|
919 | n/a | failures = ['', '--foo', 'a', 'a b', 'a b c d'] |
---|
920 | n/a | successes = [ |
---|
921 | n/a | ('a b c', NS(foo=['a', 'b'], bar='c')), |
---|
922 | n/a | ] |
---|
923 | n/a | |
---|
924 | n/a | |
---|
925 | n/a | class TestPositionalsNargsNoneZeroOrMore(ParserTestCase): |
---|
926 | n/a | """Test a Positional with no nargs followed by one with unlimited""" |
---|
927 | n/a | |
---|
928 | n/a | argument_signatures = [Sig('foo'), Sig('bar', nargs='*')] |
---|
929 | n/a | failures = ['', '--foo'] |
---|
930 | n/a | successes = [ |
---|
931 | n/a | ('a', NS(foo='a', bar=[])), |
---|
932 | n/a | ('a b', NS(foo='a', bar=['b'])), |
---|
933 | n/a | ('a b c', NS(foo='a', bar=['b', 'c'])), |
---|
934 | n/a | ] |
---|
935 | n/a | |
---|
936 | n/a | |
---|
937 | n/a | class TestPositionalsNargsNoneOneOrMore(ParserTestCase): |
---|
938 | n/a | """Test a Positional with no nargs followed by one with one or more""" |
---|
939 | n/a | |
---|
940 | n/a | argument_signatures = [Sig('foo'), Sig('bar', nargs='+')] |
---|
941 | n/a | failures = ['', '--foo', 'a'] |
---|
942 | n/a | successes = [ |
---|
943 | n/a | ('a b', NS(foo='a', bar=['b'])), |
---|
944 | n/a | ('a b c', NS(foo='a', bar=['b', 'c'])), |
---|
945 | n/a | ] |
---|
946 | n/a | |
---|
947 | n/a | |
---|
948 | n/a | class TestPositionalsNargsNoneOptional(ParserTestCase): |
---|
949 | n/a | """Test a Positional with no nargs followed by one with an Optional""" |
---|
950 | n/a | |
---|
951 | n/a | argument_signatures = [Sig('foo'), Sig('bar', nargs='?')] |
---|
952 | n/a | failures = ['', '--foo', 'a b c'] |
---|
953 | n/a | successes = [ |
---|
954 | n/a | ('a', NS(foo='a', bar=None)), |
---|
955 | n/a | ('a b', NS(foo='a', bar='b')), |
---|
956 | n/a | ] |
---|
957 | n/a | |
---|
958 | n/a | |
---|
959 | n/a | class TestPositionalsNargsZeroOrMoreNone(ParserTestCase): |
---|
960 | n/a | """Test a Positional with unlimited nargs followed by one with none""" |
---|
961 | n/a | |
---|
962 | n/a | argument_signatures = [Sig('foo', nargs='*'), Sig('bar')] |
---|
963 | n/a | failures = ['', '--foo'] |
---|
964 | n/a | successes = [ |
---|
965 | n/a | ('a', NS(foo=[], bar='a')), |
---|
966 | n/a | ('a b', NS(foo=['a'], bar='b')), |
---|
967 | n/a | ('a b c', NS(foo=['a', 'b'], bar='c')), |
---|
968 | n/a | ] |
---|
969 | n/a | |
---|
970 | n/a | |
---|
971 | n/a | class TestPositionalsNargsOneOrMoreNone(ParserTestCase): |
---|
972 | n/a | """Test a Positional with one or more nargs followed by one with none""" |
---|
973 | n/a | |
---|
974 | n/a | argument_signatures = [Sig('foo', nargs='+'), Sig('bar')] |
---|
975 | n/a | failures = ['', '--foo', 'a'] |
---|
976 | n/a | successes = [ |
---|
977 | n/a | ('a b', NS(foo=['a'], bar='b')), |
---|
978 | n/a | ('a b c', NS(foo=['a', 'b'], bar='c')), |
---|
979 | n/a | ] |
---|
980 | n/a | |
---|
981 | n/a | |
---|
982 | n/a | class TestPositionalsNargsOptionalNone(ParserTestCase): |
---|
983 | n/a | """Test a Positional with an Optional nargs followed by one with none""" |
---|
984 | n/a | |
---|
985 | n/a | argument_signatures = [Sig('foo', nargs='?', default=42), Sig('bar')] |
---|
986 | n/a | failures = ['', '--foo', 'a b c'] |
---|
987 | n/a | successes = [ |
---|
988 | n/a | ('a', NS(foo=42, bar='a')), |
---|
989 | n/a | ('a b', NS(foo='a', bar='b')), |
---|
990 | n/a | ] |
---|
991 | n/a | |
---|
992 | n/a | |
---|
993 | n/a | class TestPositionalsNargs2ZeroOrMore(ParserTestCase): |
---|
994 | n/a | """Test a Positional with 2 nargs followed by one with unlimited""" |
---|
995 | n/a | |
---|
996 | n/a | argument_signatures = [Sig('foo', nargs=2), Sig('bar', nargs='*')] |
---|
997 | n/a | failures = ['', '--foo', 'a'] |
---|
998 | n/a | successes = [ |
---|
999 | n/a | ('a b', NS(foo=['a', 'b'], bar=[])), |
---|
1000 | n/a | ('a b c', NS(foo=['a', 'b'], bar=['c'])), |
---|
1001 | n/a | ] |
---|
1002 | n/a | |
---|
1003 | n/a | |
---|
1004 | n/a | class TestPositionalsNargs2OneOrMore(ParserTestCase): |
---|
1005 | n/a | """Test a Positional with 2 nargs followed by one with one or more""" |
---|
1006 | n/a | |
---|
1007 | n/a | argument_signatures = [Sig('foo', nargs=2), Sig('bar', nargs='+')] |
---|
1008 | n/a | failures = ['', '--foo', 'a', 'a b'] |
---|
1009 | n/a | successes = [ |
---|
1010 | n/a | ('a b c', NS(foo=['a', 'b'], bar=['c'])), |
---|
1011 | n/a | ] |
---|
1012 | n/a | |
---|
1013 | n/a | |
---|
1014 | n/a | class TestPositionalsNargs2Optional(ParserTestCase): |
---|
1015 | n/a | """Test a Positional with 2 nargs followed by one optional""" |
---|
1016 | n/a | |
---|
1017 | n/a | argument_signatures = [Sig('foo', nargs=2), Sig('bar', nargs='?')] |
---|
1018 | n/a | failures = ['', '--foo', 'a', 'a b c d'] |
---|
1019 | n/a | successes = [ |
---|
1020 | n/a | ('a b', NS(foo=['a', 'b'], bar=None)), |
---|
1021 | n/a | ('a b c', NS(foo=['a', 'b'], bar='c')), |
---|
1022 | n/a | ] |
---|
1023 | n/a | |
---|
1024 | n/a | |
---|
1025 | n/a | class TestPositionalsNargsZeroOrMore1(ParserTestCase): |
---|
1026 | n/a | """Test a Positional with unlimited nargs followed by one with 1""" |
---|
1027 | n/a | |
---|
1028 | n/a | argument_signatures = [Sig('foo', nargs='*'), Sig('bar', nargs=1)] |
---|
1029 | n/a | failures = ['', '--foo', ] |
---|
1030 | n/a | successes = [ |
---|
1031 | n/a | ('a', NS(foo=[], bar=['a'])), |
---|
1032 | n/a | ('a b', NS(foo=['a'], bar=['b'])), |
---|
1033 | n/a | ('a b c', NS(foo=['a', 'b'], bar=['c'])), |
---|
1034 | n/a | ] |
---|
1035 | n/a | |
---|
1036 | n/a | |
---|
1037 | n/a | class TestPositionalsNargsOneOrMore1(ParserTestCase): |
---|
1038 | n/a | """Test a Positional with one or more nargs followed by one with 1""" |
---|
1039 | n/a | |
---|
1040 | n/a | argument_signatures = [Sig('foo', nargs='+'), Sig('bar', nargs=1)] |
---|
1041 | n/a | failures = ['', '--foo', 'a'] |
---|
1042 | n/a | successes = [ |
---|
1043 | n/a | ('a b', NS(foo=['a'], bar=['b'])), |
---|
1044 | n/a | ('a b c', NS(foo=['a', 'b'], bar=['c'])), |
---|
1045 | n/a | ] |
---|
1046 | n/a | |
---|
1047 | n/a | |
---|
1048 | n/a | class TestPositionalsNargsOptional1(ParserTestCase): |
---|
1049 | n/a | """Test a Positional with an Optional nargs followed by one with 1""" |
---|
1050 | n/a | |
---|
1051 | n/a | argument_signatures = [Sig('foo', nargs='?'), Sig('bar', nargs=1)] |
---|
1052 | n/a | failures = ['', '--foo', 'a b c'] |
---|
1053 | n/a | successes = [ |
---|
1054 | n/a | ('a', NS(foo=None, bar=['a'])), |
---|
1055 | n/a | ('a b', NS(foo='a', bar=['b'])), |
---|
1056 | n/a | ] |
---|
1057 | n/a | |
---|
1058 | n/a | |
---|
1059 | n/a | class TestPositionalsNargsNoneZeroOrMore1(ParserTestCase): |
---|
1060 | n/a | """Test three Positionals: no nargs, unlimited nargs and 1 nargs""" |
---|
1061 | n/a | |
---|
1062 | n/a | argument_signatures = [ |
---|
1063 | n/a | Sig('foo'), |
---|
1064 | n/a | Sig('bar', nargs='*'), |
---|
1065 | n/a | Sig('baz', nargs=1), |
---|
1066 | n/a | ] |
---|
1067 | n/a | failures = ['', '--foo', 'a'] |
---|
1068 | n/a | successes = [ |
---|
1069 | n/a | ('a b', NS(foo='a', bar=[], baz=['b'])), |
---|
1070 | n/a | ('a b c', NS(foo='a', bar=['b'], baz=['c'])), |
---|
1071 | n/a | ] |
---|
1072 | n/a | |
---|
1073 | n/a | |
---|
1074 | n/a | class TestPositionalsNargsNoneOneOrMore1(ParserTestCase): |
---|
1075 | n/a | """Test three Positionals: no nargs, one or more nargs and 1 nargs""" |
---|
1076 | n/a | |
---|
1077 | n/a | argument_signatures = [ |
---|
1078 | n/a | Sig('foo'), |
---|
1079 | n/a | Sig('bar', nargs='+'), |
---|
1080 | n/a | Sig('baz', nargs=1), |
---|
1081 | n/a | ] |
---|
1082 | n/a | failures = ['', '--foo', 'a', 'b'] |
---|
1083 | n/a | successes = [ |
---|
1084 | n/a | ('a b c', NS(foo='a', bar=['b'], baz=['c'])), |
---|
1085 | n/a | ('a b c d', NS(foo='a', bar=['b', 'c'], baz=['d'])), |
---|
1086 | n/a | ] |
---|
1087 | n/a | |
---|
1088 | n/a | |
---|
1089 | n/a | class TestPositionalsNargsNoneOptional1(ParserTestCase): |
---|
1090 | n/a | """Test three Positionals: no nargs, optional narg and 1 nargs""" |
---|
1091 | n/a | |
---|
1092 | n/a | argument_signatures = [ |
---|
1093 | n/a | Sig('foo'), |
---|
1094 | n/a | Sig('bar', nargs='?', default=0.625), |
---|
1095 | n/a | Sig('baz', nargs=1), |
---|
1096 | n/a | ] |
---|
1097 | n/a | failures = ['', '--foo', 'a'] |
---|
1098 | n/a | successes = [ |
---|
1099 | n/a | ('a b', NS(foo='a', bar=0.625, baz=['b'])), |
---|
1100 | n/a | ('a b c', NS(foo='a', bar='b', baz=['c'])), |
---|
1101 | n/a | ] |
---|
1102 | n/a | |
---|
1103 | n/a | |
---|
1104 | n/a | class TestPositionalsNargsOptionalOptional(ParserTestCase): |
---|
1105 | n/a | """Test two optional nargs""" |
---|
1106 | n/a | |
---|
1107 | n/a | argument_signatures = [ |
---|
1108 | n/a | Sig('foo', nargs='?'), |
---|
1109 | n/a | Sig('bar', nargs='?', default=42), |
---|
1110 | n/a | ] |
---|
1111 | n/a | failures = ['--foo', 'a b c'] |
---|
1112 | n/a | successes = [ |
---|
1113 | n/a | ('', NS(foo=None, bar=42)), |
---|
1114 | n/a | ('a', NS(foo='a', bar=42)), |
---|
1115 | n/a | ('a b', NS(foo='a', bar='b')), |
---|
1116 | n/a | ] |
---|
1117 | n/a | |
---|
1118 | n/a | |
---|
1119 | n/a | class TestPositionalsNargsOptionalZeroOrMore(ParserTestCase): |
---|
1120 | n/a | """Test an Optional narg followed by unlimited nargs""" |
---|
1121 | n/a | |
---|
1122 | n/a | argument_signatures = [Sig('foo', nargs='?'), Sig('bar', nargs='*')] |
---|
1123 | n/a | failures = ['--foo'] |
---|
1124 | n/a | successes = [ |
---|
1125 | n/a | ('', NS(foo=None, bar=[])), |
---|
1126 | n/a | ('a', NS(foo='a', bar=[])), |
---|
1127 | n/a | ('a b', NS(foo='a', bar=['b'])), |
---|
1128 | n/a | ('a b c', NS(foo='a', bar=['b', 'c'])), |
---|
1129 | n/a | ] |
---|
1130 | n/a | |
---|
1131 | n/a | |
---|
1132 | n/a | class TestPositionalsNargsOptionalOneOrMore(ParserTestCase): |
---|
1133 | n/a | """Test an Optional narg followed by one or more nargs""" |
---|
1134 | n/a | |
---|
1135 | n/a | argument_signatures = [Sig('foo', nargs='?'), Sig('bar', nargs='+')] |
---|
1136 | n/a | failures = ['', '--foo'] |
---|
1137 | n/a | successes = [ |
---|
1138 | n/a | ('a', NS(foo=None, bar=['a'])), |
---|
1139 | n/a | ('a b', NS(foo='a', bar=['b'])), |
---|
1140 | n/a | ('a b c', NS(foo='a', bar=['b', 'c'])), |
---|
1141 | n/a | ] |
---|
1142 | n/a | |
---|
1143 | n/a | |
---|
1144 | n/a | class TestPositionalsChoicesString(ParserTestCase): |
---|
1145 | n/a | """Test a set of single-character choices""" |
---|
1146 | n/a | |
---|
1147 | n/a | argument_signatures = [Sig('spam', choices=set('abcdefg'))] |
---|
1148 | n/a | failures = ['', '--foo', 'h', '42', 'ef'] |
---|
1149 | n/a | successes = [ |
---|
1150 | n/a | ('a', NS(spam='a')), |
---|
1151 | n/a | ('g', NS(spam='g')), |
---|
1152 | n/a | ] |
---|
1153 | n/a | |
---|
1154 | n/a | |
---|
1155 | n/a | class TestPositionalsChoicesInt(ParserTestCase): |
---|
1156 | n/a | """Test a set of integer choices""" |
---|
1157 | n/a | |
---|
1158 | n/a | argument_signatures = [Sig('spam', type=int, choices=range(20))] |
---|
1159 | n/a | failures = ['', '--foo', 'h', '42', 'ef'] |
---|
1160 | n/a | successes = [ |
---|
1161 | n/a | ('4', NS(spam=4)), |
---|
1162 | n/a | ('15', NS(spam=15)), |
---|
1163 | n/a | ] |
---|
1164 | n/a | |
---|
1165 | n/a | |
---|
1166 | n/a | class TestPositionalsActionAppend(ParserTestCase): |
---|
1167 | n/a | """Test the 'append' action""" |
---|
1168 | n/a | |
---|
1169 | n/a | argument_signatures = [ |
---|
1170 | n/a | Sig('spam', action='append'), |
---|
1171 | n/a | Sig('spam', action='append', nargs=2), |
---|
1172 | n/a | ] |
---|
1173 | n/a | failures = ['', '--foo', 'a', 'a b', 'a b c d'] |
---|
1174 | n/a | successes = [ |
---|
1175 | n/a | ('a b c', NS(spam=['a', ['b', 'c']])), |
---|
1176 | n/a | ] |
---|
1177 | n/a | |
---|
1178 | n/a | # ======================================== |
---|
1179 | n/a | # Combined optionals and positionals tests |
---|
1180 | n/a | # ======================================== |
---|
1181 | n/a | |
---|
1182 | n/a | class TestOptionalsNumericAndPositionals(ParserTestCase): |
---|
1183 | n/a | """Tests negative number args when numeric options are present""" |
---|
1184 | n/a | |
---|
1185 | n/a | argument_signatures = [ |
---|
1186 | n/a | Sig('x', nargs='?'), |
---|
1187 | n/a | Sig('-4', dest='y', action='store_true'), |
---|
1188 | n/a | ] |
---|
1189 | n/a | failures = ['-2', '-315'] |
---|
1190 | n/a | successes = [ |
---|
1191 | n/a | ('', NS(x=None, y=False)), |
---|
1192 | n/a | ('a', NS(x='a', y=False)), |
---|
1193 | n/a | ('-4', NS(x=None, y=True)), |
---|
1194 | n/a | ('-4 a', NS(x='a', y=True)), |
---|
1195 | n/a | ] |
---|
1196 | n/a | |
---|
1197 | n/a | |
---|
1198 | n/a | class TestOptionalsAlmostNumericAndPositionals(ParserTestCase): |
---|
1199 | n/a | """Tests negative number args when almost numeric options are present""" |
---|
1200 | n/a | |
---|
1201 | n/a | argument_signatures = [ |
---|
1202 | n/a | Sig('x', nargs='?'), |
---|
1203 | n/a | Sig('-k4', dest='y', action='store_true'), |
---|
1204 | n/a | ] |
---|
1205 | n/a | failures = ['-k3'] |
---|
1206 | n/a | successes = [ |
---|
1207 | n/a | ('', NS(x=None, y=False)), |
---|
1208 | n/a | ('-2', NS(x='-2', y=False)), |
---|
1209 | n/a | ('a', NS(x='a', y=False)), |
---|
1210 | n/a | ('-k4', NS(x=None, y=True)), |
---|
1211 | n/a | ('-k4 a', NS(x='a', y=True)), |
---|
1212 | n/a | ] |
---|
1213 | n/a | |
---|
1214 | n/a | |
---|
1215 | n/a | class TestEmptyAndSpaceContainingArguments(ParserTestCase): |
---|
1216 | n/a | |
---|
1217 | n/a | argument_signatures = [ |
---|
1218 | n/a | Sig('x', nargs='?'), |
---|
1219 | n/a | Sig('-y', '--yyy', dest='y'), |
---|
1220 | n/a | ] |
---|
1221 | n/a | failures = ['-y'] |
---|
1222 | n/a | successes = [ |
---|
1223 | n/a | ([''], NS(x='', y=None)), |
---|
1224 | n/a | (['a badger'], NS(x='a badger', y=None)), |
---|
1225 | n/a | (['-a badger'], NS(x='-a badger', y=None)), |
---|
1226 | n/a | (['-y', ''], NS(x=None, y='')), |
---|
1227 | n/a | (['-y', 'a badger'], NS(x=None, y='a badger')), |
---|
1228 | n/a | (['-y', '-a badger'], NS(x=None, y='-a badger')), |
---|
1229 | n/a | (['--yyy=a badger'], NS(x=None, y='a badger')), |
---|
1230 | n/a | (['--yyy=-a badger'], NS(x=None, y='-a badger')), |
---|
1231 | n/a | ] |
---|
1232 | n/a | |
---|
1233 | n/a | |
---|
1234 | n/a | class TestPrefixCharacterOnlyArguments(ParserTestCase): |
---|
1235 | n/a | |
---|
1236 | n/a | parser_signature = Sig(prefix_chars='-+') |
---|
1237 | n/a | argument_signatures = [ |
---|
1238 | n/a | Sig('-', dest='x', nargs='?', const='badger'), |
---|
1239 | n/a | Sig('+', dest='y', type=int, default=42), |
---|
1240 | n/a | Sig('-+-', dest='z', action='store_true'), |
---|
1241 | n/a | ] |
---|
1242 | n/a | failures = ['-y', '+ -'] |
---|
1243 | n/a | successes = [ |
---|
1244 | n/a | ('', NS(x=None, y=42, z=False)), |
---|
1245 | n/a | ('-', NS(x='badger', y=42, z=False)), |
---|
1246 | n/a | ('- X', NS(x='X', y=42, z=False)), |
---|
1247 | n/a | ('+ -3', NS(x=None, y=-3, z=False)), |
---|
1248 | n/a | ('-+-', NS(x=None, y=42, z=True)), |
---|
1249 | n/a | ('- ===', NS(x='===', y=42, z=False)), |
---|
1250 | n/a | ] |
---|
1251 | n/a | |
---|
1252 | n/a | |
---|
1253 | n/a | class TestNargsZeroOrMore(ParserTestCase): |
---|
1254 | n/a | """Tests specifying args for an Optional that accepts zero or more""" |
---|
1255 | n/a | |
---|
1256 | n/a | argument_signatures = [Sig('-x', nargs='*'), Sig('y', nargs='*')] |
---|
1257 | n/a | failures = [] |
---|
1258 | n/a | successes = [ |
---|
1259 | n/a | ('', NS(x=None, y=[])), |
---|
1260 | n/a | ('-x', NS(x=[], y=[])), |
---|
1261 | n/a | ('-x a', NS(x=['a'], y=[])), |
---|
1262 | n/a | ('-x a -- b', NS(x=['a'], y=['b'])), |
---|
1263 | n/a | ('a', NS(x=None, y=['a'])), |
---|
1264 | n/a | ('a -x', NS(x=[], y=['a'])), |
---|
1265 | n/a | ('a -x b', NS(x=['b'], y=['a'])), |
---|
1266 | n/a | ] |
---|
1267 | n/a | |
---|
1268 | n/a | |
---|
1269 | n/a | class TestNargsRemainder(ParserTestCase): |
---|
1270 | n/a | """Tests specifying a positional with nargs=REMAINDER""" |
---|
1271 | n/a | |
---|
1272 | n/a | argument_signatures = [Sig('x'), Sig('y', nargs='...'), Sig('-z')] |
---|
1273 | n/a | failures = ['', '-z', '-z Z'] |
---|
1274 | n/a | successes = [ |
---|
1275 | n/a | ('X', NS(x='X', y=[], z=None)), |
---|
1276 | n/a | ('-z Z X', NS(x='X', y=[], z='Z')), |
---|
1277 | n/a | ('X A B -z Z', NS(x='X', y=['A', 'B', '-z', 'Z'], z=None)), |
---|
1278 | n/a | ('X Y --foo', NS(x='X', y=['Y', '--foo'], z=None)), |
---|
1279 | n/a | ] |
---|
1280 | n/a | |
---|
1281 | n/a | |
---|
1282 | n/a | class TestOptionLike(ParserTestCase): |
---|
1283 | n/a | """Tests options that may or may not be arguments""" |
---|
1284 | n/a | |
---|
1285 | n/a | argument_signatures = [ |
---|
1286 | n/a | Sig('-x', type=float), |
---|
1287 | n/a | Sig('-3', type=float, dest='y'), |
---|
1288 | n/a | Sig('z', nargs='*'), |
---|
1289 | n/a | ] |
---|
1290 | n/a | failures = ['-x', '-y2.5', '-xa', '-x -a', |
---|
1291 | n/a | '-x -3', '-x -3.5', '-3 -3.5', |
---|
1292 | n/a | '-x -2.5', '-x -2.5 a', '-3 -.5', |
---|
1293 | n/a | 'a x -1', '-x -1 a', '-3 -1 a'] |
---|
1294 | n/a | successes = [ |
---|
1295 | n/a | ('', NS(x=None, y=None, z=[])), |
---|
1296 | n/a | ('-x 2.5', NS(x=2.5, y=None, z=[])), |
---|
1297 | n/a | ('-x 2.5 a', NS(x=2.5, y=None, z=['a'])), |
---|
1298 | n/a | ('-3.5', NS(x=None, y=0.5, z=[])), |
---|
1299 | n/a | ('-3-.5', NS(x=None, y=-0.5, z=[])), |
---|
1300 | n/a | ('-3 .5', NS(x=None, y=0.5, z=[])), |
---|
1301 | n/a | ('a -3.5', NS(x=None, y=0.5, z=['a'])), |
---|
1302 | n/a | ('a', NS(x=None, y=None, z=['a'])), |
---|
1303 | n/a | ('a -x 1', NS(x=1.0, y=None, z=['a'])), |
---|
1304 | n/a | ('-x 1 a', NS(x=1.0, y=None, z=['a'])), |
---|
1305 | n/a | ('-3 1 a', NS(x=None, y=1.0, z=['a'])), |
---|
1306 | n/a | ] |
---|
1307 | n/a | |
---|
1308 | n/a | |
---|
1309 | n/a | class TestDefaultSuppress(ParserTestCase): |
---|
1310 | n/a | """Test actions with suppressed defaults""" |
---|
1311 | n/a | |
---|
1312 | n/a | argument_signatures = [ |
---|
1313 | n/a | Sig('foo', nargs='?', default=argparse.SUPPRESS), |
---|
1314 | n/a | Sig('bar', nargs='*', default=argparse.SUPPRESS), |
---|
1315 | n/a | Sig('--baz', action='store_true', default=argparse.SUPPRESS), |
---|
1316 | n/a | ] |
---|
1317 | n/a | failures = ['-x'] |
---|
1318 | n/a | successes = [ |
---|
1319 | n/a | ('', NS()), |
---|
1320 | n/a | ('a', NS(foo='a')), |
---|
1321 | n/a | ('a b', NS(foo='a', bar=['b'])), |
---|
1322 | n/a | ('--baz', NS(baz=True)), |
---|
1323 | n/a | ('a --baz', NS(foo='a', baz=True)), |
---|
1324 | n/a | ('--baz a b', NS(foo='a', bar=['b'], baz=True)), |
---|
1325 | n/a | ] |
---|
1326 | n/a | |
---|
1327 | n/a | |
---|
1328 | n/a | class TestParserDefaultSuppress(ParserTestCase): |
---|
1329 | n/a | """Test actions with a parser-level default of SUPPRESS""" |
---|
1330 | n/a | |
---|
1331 | n/a | parser_signature = Sig(argument_default=argparse.SUPPRESS) |
---|
1332 | n/a | argument_signatures = [ |
---|
1333 | n/a | Sig('foo', nargs='?'), |
---|
1334 | n/a | Sig('bar', nargs='*'), |
---|
1335 | n/a | Sig('--baz', action='store_true'), |
---|
1336 | n/a | ] |
---|
1337 | n/a | failures = ['-x'] |
---|
1338 | n/a | successes = [ |
---|
1339 | n/a | ('', NS()), |
---|
1340 | n/a | ('a', NS(foo='a')), |
---|
1341 | n/a | ('a b', NS(foo='a', bar=['b'])), |
---|
1342 | n/a | ('--baz', NS(baz=True)), |
---|
1343 | n/a | ('a --baz', NS(foo='a', baz=True)), |
---|
1344 | n/a | ('--baz a b', NS(foo='a', bar=['b'], baz=True)), |
---|
1345 | n/a | ] |
---|
1346 | n/a | |
---|
1347 | n/a | |
---|
1348 | n/a | class TestParserDefault42(ParserTestCase): |
---|
1349 | n/a | """Test actions with a parser-level default of 42""" |
---|
1350 | n/a | |
---|
1351 | n/a | parser_signature = Sig(argument_default=42) |
---|
1352 | n/a | argument_signatures = [ |
---|
1353 | n/a | Sig('--version', action='version', version='1.0'), |
---|
1354 | n/a | Sig('foo', nargs='?'), |
---|
1355 | n/a | Sig('bar', nargs='*'), |
---|
1356 | n/a | Sig('--baz', action='store_true'), |
---|
1357 | n/a | ] |
---|
1358 | n/a | failures = ['-x'] |
---|
1359 | n/a | successes = [ |
---|
1360 | n/a | ('', NS(foo=42, bar=42, baz=42, version=42)), |
---|
1361 | n/a | ('a', NS(foo='a', bar=42, baz=42, version=42)), |
---|
1362 | n/a | ('a b', NS(foo='a', bar=['b'], baz=42, version=42)), |
---|
1363 | n/a | ('--baz', NS(foo=42, bar=42, baz=True, version=42)), |
---|
1364 | n/a | ('a --baz', NS(foo='a', bar=42, baz=True, version=42)), |
---|
1365 | n/a | ('--baz a b', NS(foo='a', bar=['b'], baz=True, version=42)), |
---|
1366 | n/a | ] |
---|
1367 | n/a | |
---|
1368 | n/a | |
---|
1369 | n/a | class TestArgumentsFromFile(TempDirMixin, ParserTestCase): |
---|
1370 | n/a | """Test reading arguments from a file""" |
---|
1371 | n/a | |
---|
1372 | n/a | def setUp(self): |
---|
1373 | n/a | super(TestArgumentsFromFile, self).setUp() |
---|
1374 | n/a | file_texts = [ |
---|
1375 | n/a | ('hello', 'hello world!\n'), |
---|
1376 | n/a | ('recursive', '-a\n' |
---|
1377 | n/a | 'A\n' |
---|
1378 | n/a | '@hello'), |
---|
1379 | n/a | ('invalid', '@no-such-path\n'), |
---|
1380 | n/a | ] |
---|
1381 | n/a | for path, text in file_texts: |
---|
1382 | n/a | file = open(path, 'w') |
---|
1383 | n/a | file.write(text) |
---|
1384 | n/a | file.close() |
---|
1385 | n/a | |
---|
1386 | n/a | parser_signature = Sig(fromfile_prefix_chars='@') |
---|
1387 | n/a | argument_signatures = [ |
---|
1388 | n/a | Sig('-a'), |
---|
1389 | n/a | Sig('x'), |
---|
1390 | n/a | Sig('y', nargs='+'), |
---|
1391 | n/a | ] |
---|
1392 | n/a | failures = ['', '-b', 'X', '@invalid', '@missing'] |
---|
1393 | n/a | successes = [ |
---|
1394 | n/a | ('X Y', NS(a=None, x='X', y=['Y'])), |
---|
1395 | n/a | ('X -a A Y Z', NS(a='A', x='X', y=['Y', 'Z'])), |
---|
1396 | n/a | ('@hello X', NS(a=None, x='hello world!', y=['X'])), |
---|
1397 | n/a | ('X @hello', NS(a=None, x='X', y=['hello world!'])), |
---|
1398 | n/a | ('-a B @recursive Y Z', NS(a='A', x='hello world!', y=['Y', 'Z'])), |
---|
1399 | n/a | ('X @recursive Z -a B', NS(a='B', x='X', y=['hello world!', 'Z'])), |
---|
1400 | n/a | (["-a", "", "X", "Y"], NS(a='', x='X', y=['Y'])), |
---|
1401 | n/a | ] |
---|
1402 | n/a | |
---|
1403 | n/a | |
---|
1404 | n/a | class TestArgumentsFromFileConverter(TempDirMixin, ParserTestCase): |
---|
1405 | n/a | """Test reading arguments from a file""" |
---|
1406 | n/a | |
---|
1407 | n/a | def setUp(self): |
---|
1408 | n/a | super(TestArgumentsFromFileConverter, self).setUp() |
---|
1409 | n/a | file_texts = [ |
---|
1410 | n/a | ('hello', 'hello world!\n'), |
---|
1411 | n/a | ] |
---|
1412 | n/a | for path, text in file_texts: |
---|
1413 | n/a | file = open(path, 'w') |
---|
1414 | n/a | file.write(text) |
---|
1415 | n/a | file.close() |
---|
1416 | n/a | |
---|
1417 | n/a | class FromFileConverterArgumentParser(ErrorRaisingArgumentParser): |
---|
1418 | n/a | |
---|
1419 | n/a | def convert_arg_line_to_args(self, arg_line): |
---|
1420 | n/a | for arg in arg_line.split(): |
---|
1421 | n/a | if not arg.strip(): |
---|
1422 | n/a | continue |
---|
1423 | n/a | yield arg |
---|
1424 | n/a | parser_class = FromFileConverterArgumentParser |
---|
1425 | n/a | parser_signature = Sig(fromfile_prefix_chars='@') |
---|
1426 | n/a | argument_signatures = [ |
---|
1427 | n/a | Sig('y', nargs='+'), |
---|
1428 | n/a | ] |
---|
1429 | n/a | failures = [] |
---|
1430 | n/a | successes = [ |
---|
1431 | n/a | ('@hello X', NS(y=['hello', 'world!', 'X'])), |
---|
1432 | n/a | ] |
---|
1433 | n/a | |
---|
1434 | n/a | |
---|
1435 | n/a | # ===================== |
---|
1436 | n/a | # Type conversion tests |
---|
1437 | n/a | # ===================== |
---|
1438 | n/a | |
---|
1439 | n/a | class TestFileTypeRepr(TestCase): |
---|
1440 | n/a | |
---|
1441 | n/a | def test_r(self): |
---|
1442 | n/a | type = argparse.FileType('r') |
---|
1443 | n/a | self.assertEqual("FileType('r')", repr(type)) |
---|
1444 | n/a | |
---|
1445 | n/a | def test_wb_1(self): |
---|
1446 | n/a | type = argparse.FileType('wb', 1) |
---|
1447 | n/a | self.assertEqual("FileType('wb', 1)", repr(type)) |
---|
1448 | n/a | |
---|
1449 | n/a | def test_r_latin(self): |
---|
1450 | n/a | type = argparse.FileType('r', encoding='latin_1') |
---|
1451 | n/a | self.assertEqual("FileType('r', encoding='latin_1')", repr(type)) |
---|
1452 | n/a | |
---|
1453 | n/a | def test_w_big5_ignore(self): |
---|
1454 | n/a | type = argparse.FileType('w', encoding='big5', errors='ignore') |
---|
1455 | n/a | self.assertEqual("FileType('w', encoding='big5', errors='ignore')", |
---|
1456 | n/a | repr(type)) |
---|
1457 | n/a | |
---|
1458 | n/a | def test_r_1_replace(self): |
---|
1459 | n/a | type = argparse.FileType('r', 1, errors='replace') |
---|
1460 | n/a | self.assertEqual("FileType('r', 1, errors='replace')", repr(type)) |
---|
1461 | n/a | |
---|
1462 | n/a | |
---|
1463 | n/a | class RFile(object): |
---|
1464 | n/a | seen = {} |
---|
1465 | n/a | |
---|
1466 | n/a | def __init__(self, name): |
---|
1467 | n/a | self.name = name |
---|
1468 | n/a | |
---|
1469 | n/a | def __eq__(self, other): |
---|
1470 | n/a | if other in self.seen: |
---|
1471 | n/a | text = self.seen[other] |
---|
1472 | n/a | else: |
---|
1473 | n/a | text = self.seen[other] = other.read() |
---|
1474 | n/a | other.close() |
---|
1475 | n/a | if not isinstance(text, str): |
---|
1476 | n/a | text = text.decode('ascii') |
---|
1477 | n/a | return self.name == other.name == text |
---|
1478 | n/a | |
---|
1479 | n/a | |
---|
1480 | n/a | class TestFileTypeR(TempDirMixin, ParserTestCase): |
---|
1481 | n/a | """Test the FileType option/argument type for reading files""" |
---|
1482 | n/a | |
---|
1483 | n/a | def setUp(self): |
---|
1484 | n/a | super(TestFileTypeR, self).setUp() |
---|
1485 | n/a | for file_name in ['foo', 'bar']: |
---|
1486 | n/a | file = open(os.path.join(self.temp_dir, file_name), 'w') |
---|
1487 | n/a | file.write(file_name) |
---|
1488 | n/a | file.close() |
---|
1489 | n/a | self.create_readonly_file('readonly') |
---|
1490 | n/a | |
---|
1491 | n/a | argument_signatures = [ |
---|
1492 | n/a | Sig('-x', type=argparse.FileType()), |
---|
1493 | n/a | Sig('spam', type=argparse.FileType('r')), |
---|
1494 | n/a | ] |
---|
1495 | n/a | failures = ['-x', '', 'non-existent-file.txt'] |
---|
1496 | n/a | successes = [ |
---|
1497 | n/a | ('foo', NS(x=None, spam=RFile('foo'))), |
---|
1498 | n/a | ('-x foo bar', NS(x=RFile('foo'), spam=RFile('bar'))), |
---|
1499 | n/a | ('bar -x foo', NS(x=RFile('foo'), spam=RFile('bar'))), |
---|
1500 | n/a | ('-x - -', NS(x=sys.stdin, spam=sys.stdin)), |
---|
1501 | n/a | ('readonly', NS(x=None, spam=RFile('readonly'))), |
---|
1502 | n/a | ] |
---|
1503 | n/a | |
---|
1504 | n/a | class TestFileTypeDefaults(TempDirMixin, ParserTestCase): |
---|
1505 | n/a | """Test that a file is not created unless the default is needed""" |
---|
1506 | n/a | def setUp(self): |
---|
1507 | n/a | super(TestFileTypeDefaults, self).setUp() |
---|
1508 | n/a | file = open(os.path.join(self.temp_dir, 'good'), 'w') |
---|
1509 | n/a | file.write('good') |
---|
1510 | n/a | file.close() |
---|
1511 | n/a | |
---|
1512 | n/a | argument_signatures = [ |
---|
1513 | n/a | Sig('-c', type=argparse.FileType('r'), default='no-file.txt'), |
---|
1514 | n/a | ] |
---|
1515 | n/a | # should provoke no such file error |
---|
1516 | n/a | failures = [''] |
---|
1517 | n/a | # should not provoke error because default file is created |
---|
1518 | n/a | successes = [('-c good', NS(c=RFile('good')))] |
---|
1519 | n/a | |
---|
1520 | n/a | |
---|
1521 | n/a | class TestFileTypeRB(TempDirMixin, ParserTestCase): |
---|
1522 | n/a | """Test the FileType option/argument type for reading files""" |
---|
1523 | n/a | |
---|
1524 | n/a | def setUp(self): |
---|
1525 | n/a | super(TestFileTypeRB, self).setUp() |
---|
1526 | n/a | for file_name in ['foo', 'bar']: |
---|
1527 | n/a | file = open(os.path.join(self.temp_dir, file_name), 'w') |
---|
1528 | n/a | file.write(file_name) |
---|
1529 | n/a | file.close() |
---|
1530 | n/a | |
---|
1531 | n/a | argument_signatures = [ |
---|
1532 | n/a | Sig('-x', type=argparse.FileType('rb')), |
---|
1533 | n/a | Sig('spam', type=argparse.FileType('rb')), |
---|
1534 | n/a | ] |
---|
1535 | n/a | failures = ['-x', ''] |
---|
1536 | n/a | successes = [ |
---|
1537 | n/a | ('foo', NS(x=None, spam=RFile('foo'))), |
---|
1538 | n/a | ('-x foo bar', NS(x=RFile('foo'), spam=RFile('bar'))), |
---|
1539 | n/a | ('bar -x foo', NS(x=RFile('foo'), spam=RFile('bar'))), |
---|
1540 | n/a | ('-x - -', NS(x=sys.stdin, spam=sys.stdin)), |
---|
1541 | n/a | ] |
---|
1542 | n/a | |
---|
1543 | n/a | |
---|
1544 | n/a | class WFile(object): |
---|
1545 | n/a | seen = set() |
---|
1546 | n/a | |
---|
1547 | n/a | def __init__(self, name): |
---|
1548 | n/a | self.name = name |
---|
1549 | n/a | |
---|
1550 | n/a | def __eq__(self, other): |
---|
1551 | n/a | if other not in self.seen: |
---|
1552 | n/a | text = 'Check that file is writable.' |
---|
1553 | n/a | if 'b' in other.mode: |
---|
1554 | n/a | text = text.encode('ascii') |
---|
1555 | n/a | other.write(text) |
---|
1556 | n/a | other.close() |
---|
1557 | n/a | self.seen.add(other) |
---|
1558 | n/a | return self.name == other.name |
---|
1559 | n/a | |
---|
1560 | n/a | |
---|
1561 | n/a | @unittest.skipIf(hasattr(os, 'geteuid') and os.geteuid() == 0, |
---|
1562 | n/a | "non-root user required") |
---|
1563 | n/a | class TestFileTypeW(TempDirMixin, ParserTestCase): |
---|
1564 | n/a | """Test the FileType option/argument type for writing files""" |
---|
1565 | n/a | |
---|
1566 | n/a | def setUp(self): |
---|
1567 | n/a | super(TestFileTypeW, self).setUp() |
---|
1568 | n/a | self.create_readonly_file('readonly') |
---|
1569 | n/a | |
---|
1570 | n/a | argument_signatures = [ |
---|
1571 | n/a | Sig('-x', type=argparse.FileType('w')), |
---|
1572 | n/a | Sig('spam', type=argparse.FileType('w')), |
---|
1573 | n/a | ] |
---|
1574 | n/a | failures = ['-x', '', 'readonly'] |
---|
1575 | n/a | successes = [ |
---|
1576 | n/a | ('foo', NS(x=None, spam=WFile('foo'))), |
---|
1577 | n/a | ('-x foo bar', NS(x=WFile('foo'), spam=WFile('bar'))), |
---|
1578 | n/a | ('bar -x foo', NS(x=WFile('foo'), spam=WFile('bar'))), |
---|
1579 | n/a | ('-x - -', NS(x=sys.stdout, spam=sys.stdout)), |
---|
1580 | n/a | ] |
---|
1581 | n/a | |
---|
1582 | n/a | |
---|
1583 | n/a | class TestFileTypeWB(TempDirMixin, ParserTestCase): |
---|
1584 | n/a | |
---|
1585 | n/a | argument_signatures = [ |
---|
1586 | n/a | Sig('-x', type=argparse.FileType('wb')), |
---|
1587 | n/a | Sig('spam', type=argparse.FileType('wb')), |
---|
1588 | n/a | ] |
---|
1589 | n/a | failures = ['-x', ''] |
---|
1590 | n/a | successes = [ |
---|
1591 | n/a | ('foo', NS(x=None, spam=WFile('foo'))), |
---|
1592 | n/a | ('-x foo bar', NS(x=WFile('foo'), spam=WFile('bar'))), |
---|
1593 | n/a | ('bar -x foo', NS(x=WFile('foo'), spam=WFile('bar'))), |
---|
1594 | n/a | ('-x - -', NS(x=sys.stdout, spam=sys.stdout)), |
---|
1595 | n/a | ] |
---|
1596 | n/a | |
---|
1597 | n/a | |
---|
1598 | n/a | class TestFileTypeOpenArgs(TestCase): |
---|
1599 | n/a | """Test that open (the builtin) is correctly called""" |
---|
1600 | n/a | |
---|
1601 | n/a | def test_open_args(self): |
---|
1602 | n/a | FT = argparse.FileType |
---|
1603 | n/a | cases = [ |
---|
1604 | n/a | (FT('rb'), ('rb', -1, None, None)), |
---|
1605 | n/a | (FT('w', 1), ('w', 1, None, None)), |
---|
1606 | n/a | (FT('w', errors='replace'), ('w', -1, None, 'replace')), |
---|
1607 | n/a | (FT('wb', encoding='big5'), ('wb', -1, 'big5', None)), |
---|
1608 | n/a | (FT('w', 0, 'l1', 'strict'), ('w', 0, 'l1', 'strict')), |
---|
1609 | n/a | ] |
---|
1610 | n/a | with mock.patch('builtins.open') as m: |
---|
1611 | n/a | for type, args in cases: |
---|
1612 | n/a | type('foo') |
---|
1613 | n/a | m.assert_called_with('foo', *args) |
---|
1614 | n/a | |
---|
1615 | n/a | |
---|
1616 | n/a | class TestTypeCallable(ParserTestCase): |
---|
1617 | n/a | """Test some callables as option/argument types""" |
---|
1618 | n/a | |
---|
1619 | n/a | argument_signatures = [ |
---|
1620 | n/a | Sig('--eggs', type=complex), |
---|
1621 | n/a | Sig('spam', type=float), |
---|
1622 | n/a | ] |
---|
1623 | n/a | failures = ['a', '42j', '--eggs a', '--eggs 2i'] |
---|
1624 | n/a | successes = [ |
---|
1625 | n/a | ('--eggs=42 42', NS(eggs=42, spam=42.0)), |
---|
1626 | n/a | ('--eggs 2j -- -1.5', NS(eggs=2j, spam=-1.5)), |
---|
1627 | n/a | ('1024.675', NS(eggs=None, spam=1024.675)), |
---|
1628 | n/a | ] |
---|
1629 | n/a | |
---|
1630 | n/a | |
---|
1631 | n/a | class TestTypeUserDefined(ParserTestCase): |
---|
1632 | n/a | """Test a user-defined option/argument type""" |
---|
1633 | n/a | |
---|
1634 | n/a | class MyType(TestCase): |
---|
1635 | n/a | |
---|
1636 | n/a | def __init__(self, value): |
---|
1637 | n/a | self.value = value |
---|
1638 | n/a | |
---|
1639 | n/a | def __eq__(self, other): |
---|
1640 | n/a | return (type(self), self.value) == (type(other), other.value) |
---|
1641 | n/a | |
---|
1642 | n/a | argument_signatures = [ |
---|
1643 | n/a | Sig('-x', type=MyType), |
---|
1644 | n/a | Sig('spam', type=MyType), |
---|
1645 | n/a | ] |
---|
1646 | n/a | failures = [] |
---|
1647 | n/a | successes = [ |
---|
1648 | n/a | ('a -x b', NS(x=MyType('b'), spam=MyType('a'))), |
---|
1649 | n/a | ('-xf g', NS(x=MyType('f'), spam=MyType('g'))), |
---|
1650 | n/a | ] |
---|
1651 | n/a | |
---|
1652 | n/a | |
---|
1653 | n/a | class TestTypeClassicClass(ParserTestCase): |
---|
1654 | n/a | """Test a classic class type""" |
---|
1655 | n/a | |
---|
1656 | n/a | class C: |
---|
1657 | n/a | |
---|
1658 | n/a | def __init__(self, value): |
---|
1659 | n/a | self.value = value |
---|
1660 | n/a | |
---|
1661 | n/a | def __eq__(self, other): |
---|
1662 | n/a | return (type(self), self.value) == (type(other), other.value) |
---|
1663 | n/a | |
---|
1664 | n/a | argument_signatures = [ |
---|
1665 | n/a | Sig('-x', type=C), |
---|
1666 | n/a | Sig('spam', type=C), |
---|
1667 | n/a | ] |
---|
1668 | n/a | failures = [] |
---|
1669 | n/a | successes = [ |
---|
1670 | n/a | ('a -x b', NS(x=C('b'), spam=C('a'))), |
---|
1671 | n/a | ('-xf g', NS(x=C('f'), spam=C('g'))), |
---|
1672 | n/a | ] |
---|
1673 | n/a | |
---|
1674 | n/a | |
---|
1675 | n/a | class TestTypeRegistration(TestCase): |
---|
1676 | n/a | """Test a user-defined type by registering it""" |
---|
1677 | n/a | |
---|
1678 | n/a | def test(self): |
---|
1679 | n/a | |
---|
1680 | n/a | def get_my_type(string): |
---|
1681 | n/a | return 'my_type{%s}' % string |
---|
1682 | n/a | |
---|
1683 | n/a | parser = argparse.ArgumentParser() |
---|
1684 | n/a | parser.register('type', 'my_type', get_my_type) |
---|
1685 | n/a | parser.add_argument('-x', type='my_type') |
---|
1686 | n/a | parser.add_argument('y', type='my_type') |
---|
1687 | n/a | |
---|
1688 | n/a | self.assertEqual(parser.parse_args('1'.split()), |
---|
1689 | n/a | NS(x=None, y='my_type{1}')) |
---|
1690 | n/a | self.assertEqual(parser.parse_args('-x 1 42'.split()), |
---|
1691 | n/a | NS(x='my_type{1}', y='my_type{42}')) |
---|
1692 | n/a | |
---|
1693 | n/a | |
---|
1694 | n/a | # ============ |
---|
1695 | n/a | # Action tests |
---|
1696 | n/a | # ============ |
---|
1697 | n/a | |
---|
1698 | n/a | class TestActionUserDefined(ParserTestCase): |
---|
1699 | n/a | """Test a user-defined option/argument action""" |
---|
1700 | n/a | |
---|
1701 | n/a | class OptionalAction(argparse.Action): |
---|
1702 | n/a | |
---|
1703 | n/a | def __call__(self, parser, namespace, value, option_string=None): |
---|
1704 | n/a | try: |
---|
1705 | n/a | # check destination and option string |
---|
1706 | n/a | assert self.dest == 'spam', 'dest: %s' % self.dest |
---|
1707 | n/a | assert option_string == '-s', 'flag: %s' % option_string |
---|
1708 | n/a | # when option is before argument, badger=2, and when |
---|
1709 | n/a | # option is after argument, badger=<whatever was set> |
---|
1710 | n/a | expected_ns = NS(spam=0.25) |
---|
1711 | n/a | if value in [0.125, 0.625]: |
---|
1712 | n/a | expected_ns.badger = 2 |
---|
1713 | n/a | elif value in [2.0]: |
---|
1714 | n/a | expected_ns.badger = 84 |
---|
1715 | n/a | else: |
---|
1716 | n/a | raise AssertionError('value: %s' % value) |
---|
1717 | n/a | assert expected_ns == namespace, ('expected %s, got %s' % |
---|
1718 | n/a | (expected_ns, namespace)) |
---|
1719 | n/a | except AssertionError: |
---|
1720 | n/a | e = sys.exc_info()[1] |
---|
1721 | n/a | raise ArgumentParserError('opt_action failed: %s' % e) |
---|
1722 | n/a | setattr(namespace, 'spam', value) |
---|
1723 | n/a | |
---|
1724 | n/a | class PositionalAction(argparse.Action): |
---|
1725 | n/a | |
---|
1726 | n/a | def __call__(self, parser, namespace, value, option_string=None): |
---|
1727 | n/a | try: |
---|
1728 | n/a | assert option_string is None, ('option_string: %s' % |
---|
1729 | n/a | option_string) |
---|
1730 | n/a | # check destination |
---|
1731 | n/a | assert self.dest == 'badger', 'dest: %s' % self.dest |
---|
1732 | n/a | # when argument is before option, spam=0.25, and when |
---|
1733 | n/a | # option is after argument, spam=<whatever was set> |
---|
1734 | n/a | expected_ns = NS(badger=2) |
---|
1735 | n/a | if value in [42, 84]: |
---|
1736 | n/a | expected_ns.spam = 0.25 |
---|
1737 | n/a | elif value in [1]: |
---|
1738 | n/a | expected_ns.spam = 0.625 |
---|
1739 | n/a | elif value in [2]: |
---|
1740 | n/a | expected_ns.spam = 0.125 |
---|
1741 | n/a | else: |
---|
1742 | n/a | raise AssertionError('value: %s' % value) |
---|
1743 | n/a | assert expected_ns == namespace, ('expected %s, got %s' % |
---|
1744 | n/a | (expected_ns, namespace)) |
---|
1745 | n/a | except AssertionError: |
---|
1746 | n/a | e = sys.exc_info()[1] |
---|
1747 | n/a | raise ArgumentParserError('arg_action failed: %s' % e) |
---|
1748 | n/a | setattr(namespace, 'badger', value) |
---|
1749 | n/a | |
---|
1750 | n/a | argument_signatures = [ |
---|
1751 | n/a | Sig('-s', dest='spam', action=OptionalAction, |
---|
1752 | n/a | type=float, default=0.25), |
---|
1753 | n/a | Sig('badger', action=PositionalAction, |
---|
1754 | n/a | type=int, nargs='?', default=2), |
---|
1755 | n/a | ] |
---|
1756 | n/a | failures = [] |
---|
1757 | n/a | successes = [ |
---|
1758 | n/a | ('-s0.125', NS(spam=0.125, badger=2)), |
---|
1759 | n/a | ('42', NS(spam=0.25, badger=42)), |
---|
1760 | n/a | ('-s 0.625 1', NS(spam=0.625, badger=1)), |
---|
1761 | n/a | ('84 -s2', NS(spam=2.0, badger=84)), |
---|
1762 | n/a | ] |
---|
1763 | n/a | |
---|
1764 | n/a | |
---|
1765 | n/a | class TestActionRegistration(TestCase): |
---|
1766 | n/a | """Test a user-defined action supplied by registering it""" |
---|
1767 | n/a | |
---|
1768 | n/a | class MyAction(argparse.Action): |
---|
1769 | n/a | |
---|
1770 | n/a | def __call__(self, parser, namespace, values, option_string=None): |
---|
1771 | n/a | setattr(namespace, self.dest, 'foo[%s]' % values) |
---|
1772 | n/a | |
---|
1773 | n/a | def test(self): |
---|
1774 | n/a | |
---|
1775 | n/a | parser = argparse.ArgumentParser() |
---|
1776 | n/a | parser.register('action', 'my_action', self.MyAction) |
---|
1777 | n/a | parser.add_argument('badger', action='my_action') |
---|
1778 | n/a | |
---|
1779 | n/a | self.assertEqual(parser.parse_args(['1']), NS(badger='foo[1]')) |
---|
1780 | n/a | self.assertEqual(parser.parse_args(['42']), NS(badger='foo[42]')) |
---|
1781 | n/a | |
---|
1782 | n/a | |
---|
1783 | n/a | # ================ |
---|
1784 | n/a | # Subparsers tests |
---|
1785 | n/a | # ================ |
---|
1786 | n/a | |
---|
1787 | n/a | class TestAddSubparsers(TestCase): |
---|
1788 | n/a | """Test the add_subparsers method""" |
---|
1789 | n/a | |
---|
1790 | n/a | def assertArgumentParserError(self, *args, **kwargs): |
---|
1791 | n/a | self.assertRaises(ArgumentParserError, *args, **kwargs) |
---|
1792 | n/a | |
---|
1793 | n/a | def _get_parser(self, subparser_help=False, prefix_chars=None, |
---|
1794 | n/a | aliases=False): |
---|
1795 | n/a | # create a parser with a subparsers argument |
---|
1796 | n/a | if prefix_chars: |
---|
1797 | n/a | parser = ErrorRaisingArgumentParser( |
---|
1798 | n/a | prog='PROG', description='main description', prefix_chars=prefix_chars) |
---|
1799 | n/a | parser.add_argument( |
---|
1800 | n/a | prefix_chars[0] * 2 + 'foo', action='store_true', help='foo help') |
---|
1801 | n/a | else: |
---|
1802 | n/a | parser = ErrorRaisingArgumentParser( |
---|
1803 | n/a | prog='PROG', description='main description') |
---|
1804 | n/a | parser.add_argument( |
---|
1805 | n/a | '--foo', action='store_true', help='foo help') |
---|
1806 | n/a | parser.add_argument( |
---|
1807 | n/a | 'bar', type=float, help='bar help') |
---|
1808 | n/a | |
---|
1809 | n/a | # check that only one subparsers argument can be added |
---|
1810 | n/a | subparsers_kwargs = {} |
---|
1811 | n/a | if aliases: |
---|
1812 | n/a | subparsers_kwargs['metavar'] = 'COMMAND' |
---|
1813 | n/a | subparsers_kwargs['title'] = 'commands' |
---|
1814 | n/a | else: |
---|
1815 | n/a | subparsers_kwargs['help'] = 'command help' |
---|
1816 | n/a | subparsers = parser.add_subparsers(**subparsers_kwargs) |
---|
1817 | n/a | self.assertArgumentParserError(parser.add_subparsers) |
---|
1818 | n/a | |
---|
1819 | n/a | # add first sub-parser |
---|
1820 | n/a | parser1_kwargs = dict(description='1 description') |
---|
1821 | n/a | if subparser_help: |
---|
1822 | n/a | parser1_kwargs['help'] = '1 help' |
---|
1823 | n/a | if aliases: |
---|
1824 | n/a | parser1_kwargs['aliases'] = ['1alias1', '1alias2'] |
---|
1825 | n/a | parser1 = subparsers.add_parser('1', **parser1_kwargs) |
---|
1826 | n/a | parser1.add_argument('-w', type=int, help='w help') |
---|
1827 | n/a | parser1.add_argument('x', choices='abc', help='x help') |
---|
1828 | n/a | |
---|
1829 | n/a | # add second sub-parser |
---|
1830 | n/a | parser2_kwargs = dict(description='2 description') |
---|
1831 | n/a | if subparser_help: |
---|
1832 | n/a | parser2_kwargs['help'] = '2 help' |
---|
1833 | n/a | parser2 = subparsers.add_parser('2', **parser2_kwargs) |
---|
1834 | n/a | parser2.add_argument('-y', choices='123', help='y help') |
---|
1835 | n/a | parser2.add_argument('z', type=complex, nargs='*', help='z help') |
---|
1836 | n/a | |
---|
1837 | n/a | # add third sub-parser |
---|
1838 | n/a | parser3_kwargs = dict(description='3 description') |
---|
1839 | n/a | if subparser_help: |
---|
1840 | n/a | parser3_kwargs['help'] = '3 help' |
---|
1841 | n/a | parser3 = subparsers.add_parser('3', **parser3_kwargs) |
---|
1842 | n/a | parser3.add_argument('t', type=int, help='t help') |
---|
1843 | n/a | parser3.add_argument('u', nargs='...', help='u help') |
---|
1844 | n/a | |
---|
1845 | n/a | # return the main parser |
---|
1846 | n/a | return parser |
---|
1847 | n/a | |
---|
1848 | n/a | def setUp(self): |
---|
1849 | n/a | super().setUp() |
---|
1850 | n/a | self.parser = self._get_parser() |
---|
1851 | n/a | self.command_help_parser = self._get_parser(subparser_help=True) |
---|
1852 | n/a | |
---|
1853 | n/a | def test_parse_args_failures(self): |
---|
1854 | n/a | # check some failure cases: |
---|
1855 | n/a | for args_str in ['', 'a', 'a a', '0.5 a', '0.5 1', |
---|
1856 | n/a | '0.5 1 -y', '0.5 2 -w']: |
---|
1857 | n/a | args = args_str.split() |
---|
1858 | n/a | self.assertArgumentParserError(self.parser.parse_args, args) |
---|
1859 | n/a | |
---|
1860 | n/a | def test_parse_args(self): |
---|
1861 | n/a | # check some non-failure cases: |
---|
1862 | n/a | self.assertEqual( |
---|
1863 | n/a | self.parser.parse_args('0.5 1 b -w 7'.split()), |
---|
1864 | n/a | NS(foo=False, bar=0.5, w=7, x='b'), |
---|
1865 | n/a | ) |
---|
1866 | n/a | self.assertEqual( |
---|
1867 | n/a | self.parser.parse_args('0.25 --foo 2 -y 2 3j -- -1j'.split()), |
---|
1868 | n/a | NS(foo=True, bar=0.25, y='2', z=[3j, -1j]), |
---|
1869 | n/a | ) |
---|
1870 | n/a | self.assertEqual( |
---|
1871 | n/a | self.parser.parse_args('--foo 0.125 1 c'.split()), |
---|
1872 | n/a | NS(foo=True, bar=0.125, w=None, x='c'), |
---|
1873 | n/a | ) |
---|
1874 | n/a | self.assertEqual( |
---|
1875 | n/a | self.parser.parse_args('-1.5 3 11 -- a --foo 7 -- b'.split()), |
---|
1876 | n/a | NS(foo=False, bar=-1.5, t=11, u=['a', '--foo', '7', '--', 'b']), |
---|
1877 | n/a | ) |
---|
1878 | n/a | |
---|
1879 | n/a | def test_parse_known_args(self): |
---|
1880 | n/a | self.assertEqual( |
---|
1881 | n/a | self.parser.parse_known_args('0.5 1 b -w 7'.split()), |
---|
1882 | n/a | (NS(foo=False, bar=0.5, w=7, x='b'), []), |
---|
1883 | n/a | ) |
---|
1884 | n/a | self.assertEqual( |
---|
1885 | n/a | self.parser.parse_known_args('0.5 -p 1 b -w 7'.split()), |
---|
1886 | n/a | (NS(foo=False, bar=0.5, w=7, x='b'), ['-p']), |
---|
1887 | n/a | ) |
---|
1888 | n/a | self.assertEqual( |
---|
1889 | n/a | self.parser.parse_known_args('0.5 1 b -w 7 -p'.split()), |
---|
1890 | n/a | (NS(foo=False, bar=0.5, w=7, x='b'), ['-p']), |
---|
1891 | n/a | ) |
---|
1892 | n/a | self.assertEqual( |
---|
1893 | n/a | self.parser.parse_known_args('0.5 1 b -q -rs -w 7'.split()), |
---|
1894 | n/a | (NS(foo=False, bar=0.5, w=7, x='b'), ['-q', '-rs']), |
---|
1895 | n/a | ) |
---|
1896 | n/a | self.assertEqual( |
---|
1897 | n/a | self.parser.parse_known_args('0.5 -W 1 b -X Y -w 7 Z'.split()), |
---|
1898 | n/a | (NS(foo=False, bar=0.5, w=7, x='b'), ['-W', '-X', 'Y', 'Z']), |
---|
1899 | n/a | ) |
---|
1900 | n/a | |
---|
1901 | n/a | def test_dest(self): |
---|
1902 | n/a | parser = ErrorRaisingArgumentParser() |
---|
1903 | n/a | parser.add_argument('--foo', action='store_true') |
---|
1904 | n/a | subparsers = parser.add_subparsers(dest='bar') |
---|
1905 | n/a | parser1 = subparsers.add_parser('1') |
---|
1906 | n/a | parser1.add_argument('baz') |
---|
1907 | n/a | self.assertEqual(NS(foo=False, bar='1', baz='2'), |
---|
1908 | n/a | parser.parse_args('1 2'.split())) |
---|
1909 | n/a | |
---|
1910 | n/a | def test_help(self): |
---|
1911 | n/a | self.assertEqual(self.parser.format_usage(), |
---|
1912 | n/a | 'usage: PROG [-h] [--foo] bar {1,2,3} ...\n') |
---|
1913 | n/a | self.assertEqual(self.parser.format_help(), textwrap.dedent('''\ |
---|
1914 | n/a | usage: PROG [-h] [--foo] bar {1,2,3} ... |
---|
1915 | n/a | |
---|
1916 | n/a | main description |
---|
1917 | n/a | |
---|
1918 | n/a | positional arguments: |
---|
1919 | n/a | bar bar help |
---|
1920 | n/a | {1,2,3} command help |
---|
1921 | n/a | |
---|
1922 | n/a | optional arguments: |
---|
1923 | n/a | -h, --help show this help message and exit |
---|
1924 | n/a | --foo foo help |
---|
1925 | n/a | ''')) |
---|
1926 | n/a | |
---|
1927 | n/a | def test_help_extra_prefix_chars(self): |
---|
1928 | n/a | # Make sure - is still used for help if it is a non-first prefix char |
---|
1929 | n/a | parser = self._get_parser(prefix_chars='+:-') |
---|
1930 | n/a | self.assertEqual(parser.format_usage(), |
---|
1931 | n/a | 'usage: PROG [-h] [++foo] bar {1,2,3} ...\n') |
---|
1932 | n/a | self.assertEqual(parser.format_help(), textwrap.dedent('''\ |
---|
1933 | n/a | usage: PROG [-h] [++foo] bar {1,2,3} ... |
---|
1934 | n/a | |
---|
1935 | n/a | main description |
---|
1936 | n/a | |
---|
1937 | n/a | positional arguments: |
---|
1938 | n/a | bar bar help |
---|
1939 | n/a | {1,2,3} command help |
---|
1940 | n/a | |
---|
1941 | n/a | optional arguments: |
---|
1942 | n/a | -h, --help show this help message and exit |
---|
1943 | n/a | ++foo foo help |
---|
1944 | n/a | ''')) |
---|
1945 | n/a | |
---|
1946 | n/a | def test_help_non_breaking_spaces(self): |
---|
1947 | n/a | parser = ErrorRaisingArgumentParser( |
---|
1948 | n/a | prog='PROG', description='main description') |
---|
1949 | n/a | parser.add_argument( |
---|
1950 | n/a | "--non-breaking", action='store_false', |
---|
1951 | n/a | help='help message containing non-breaking spaces shall not ' |
---|
1952 | n/a | 'wrap\N{NO-BREAK SPACE}at non-breaking spaces') |
---|
1953 | n/a | self.assertEqual(parser.format_help(), textwrap.dedent('''\ |
---|
1954 | n/a | usage: PROG [-h] [--non-breaking] |
---|
1955 | n/a | |
---|
1956 | n/a | main description |
---|
1957 | n/a | |
---|
1958 | n/a | optional arguments: |
---|
1959 | n/a | -h, --help show this help message and exit |
---|
1960 | n/a | --non-breaking help message containing non-breaking spaces shall not |
---|
1961 | n/a | wrap\N{NO-BREAK SPACE}at non-breaking spaces |
---|
1962 | n/a | ''')) |
---|
1963 | n/a | |
---|
1964 | n/a | def test_help_alternate_prefix_chars(self): |
---|
1965 | n/a | parser = self._get_parser(prefix_chars='+:/') |
---|
1966 | n/a | self.assertEqual(parser.format_usage(), |
---|
1967 | n/a | 'usage: PROG [+h] [++foo] bar {1,2,3} ...\n') |
---|
1968 | n/a | self.assertEqual(parser.format_help(), textwrap.dedent('''\ |
---|
1969 | n/a | usage: PROG [+h] [++foo] bar {1,2,3} ... |
---|
1970 | n/a | |
---|
1971 | n/a | main description |
---|
1972 | n/a | |
---|
1973 | n/a | positional arguments: |
---|
1974 | n/a | bar bar help |
---|
1975 | n/a | {1,2,3} command help |
---|
1976 | n/a | |
---|
1977 | n/a | optional arguments: |
---|
1978 | n/a | +h, ++help show this help message and exit |
---|
1979 | n/a | ++foo foo help |
---|
1980 | n/a | ''')) |
---|
1981 | n/a | |
---|
1982 | n/a | def test_parser_command_help(self): |
---|
1983 | n/a | self.assertEqual(self.command_help_parser.format_usage(), |
---|
1984 | n/a | 'usage: PROG [-h] [--foo] bar {1,2,3} ...\n') |
---|
1985 | n/a | self.assertEqual(self.command_help_parser.format_help(), |
---|
1986 | n/a | textwrap.dedent('''\ |
---|
1987 | n/a | usage: PROG [-h] [--foo] bar {1,2,3} ... |
---|
1988 | n/a | |
---|
1989 | n/a | main description |
---|
1990 | n/a | |
---|
1991 | n/a | positional arguments: |
---|
1992 | n/a | bar bar help |
---|
1993 | n/a | {1,2,3} command help |
---|
1994 | n/a | 1 1 help |
---|
1995 | n/a | 2 2 help |
---|
1996 | n/a | 3 3 help |
---|
1997 | n/a | |
---|
1998 | n/a | optional arguments: |
---|
1999 | n/a | -h, --help show this help message and exit |
---|
2000 | n/a | --foo foo help |
---|
2001 | n/a | ''')) |
---|
2002 | n/a | |
---|
2003 | n/a | def test_subparser_title_help(self): |
---|
2004 | n/a | parser = ErrorRaisingArgumentParser(prog='PROG', |
---|
2005 | n/a | description='main description') |
---|
2006 | n/a | parser.add_argument('--foo', action='store_true', help='foo help') |
---|
2007 | n/a | parser.add_argument('bar', help='bar help') |
---|
2008 | n/a | subparsers = parser.add_subparsers(title='subcommands', |
---|
2009 | n/a | description='command help', |
---|
2010 | n/a | help='additional text') |
---|
2011 | n/a | parser1 = subparsers.add_parser('1') |
---|
2012 | n/a | parser2 = subparsers.add_parser('2') |
---|
2013 | n/a | self.assertEqual(parser.format_usage(), |
---|
2014 | n/a | 'usage: PROG [-h] [--foo] bar {1,2} ...\n') |
---|
2015 | n/a | self.assertEqual(parser.format_help(), textwrap.dedent('''\ |
---|
2016 | n/a | usage: PROG [-h] [--foo] bar {1,2} ... |
---|
2017 | n/a | |
---|
2018 | n/a | main description |
---|
2019 | n/a | |
---|
2020 | n/a | positional arguments: |
---|
2021 | n/a | bar bar help |
---|
2022 | n/a | |
---|
2023 | n/a | optional arguments: |
---|
2024 | n/a | -h, --help show this help message and exit |
---|
2025 | n/a | --foo foo help |
---|
2026 | n/a | |
---|
2027 | n/a | subcommands: |
---|
2028 | n/a | command help |
---|
2029 | n/a | |
---|
2030 | n/a | {1,2} additional text |
---|
2031 | n/a | ''')) |
---|
2032 | n/a | |
---|
2033 | n/a | def _test_subparser_help(self, args_str, expected_help): |
---|
2034 | n/a | with self.assertRaises(ArgumentParserError) as cm: |
---|
2035 | n/a | self.parser.parse_args(args_str.split()) |
---|
2036 | n/a | self.assertEqual(expected_help, cm.exception.stdout) |
---|
2037 | n/a | |
---|
2038 | n/a | def test_subparser1_help(self): |
---|
2039 | n/a | self._test_subparser_help('5.0 1 -h', textwrap.dedent('''\ |
---|
2040 | n/a | usage: PROG bar 1 [-h] [-w W] {a,b,c} |
---|
2041 | n/a | |
---|
2042 | n/a | 1 description |
---|
2043 | n/a | |
---|
2044 | n/a | positional arguments: |
---|
2045 | n/a | {a,b,c} x help |
---|
2046 | n/a | |
---|
2047 | n/a | optional arguments: |
---|
2048 | n/a | -h, --help show this help message and exit |
---|
2049 | n/a | -w W w help |
---|
2050 | n/a | ''')) |
---|
2051 | n/a | |
---|
2052 | n/a | def test_subparser2_help(self): |
---|
2053 | n/a | self._test_subparser_help('5.0 2 -h', textwrap.dedent('''\ |
---|
2054 | n/a | usage: PROG bar 2 [-h] [-y {1,2,3}] [z [z ...]] |
---|
2055 | n/a | |
---|
2056 | n/a | 2 description |
---|
2057 | n/a | |
---|
2058 | n/a | positional arguments: |
---|
2059 | n/a | z z help |
---|
2060 | n/a | |
---|
2061 | n/a | optional arguments: |
---|
2062 | n/a | -h, --help show this help message and exit |
---|
2063 | n/a | -y {1,2,3} y help |
---|
2064 | n/a | ''')) |
---|
2065 | n/a | |
---|
2066 | n/a | def test_alias_invocation(self): |
---|
2067 | n/a | parser = self._get_parser(aliases=True) |
---|
2068 | n/a | self.assertEqual( |
---|
2069 | n/a | parser.parse_known_args('0.5 1alias1 b'.split()), |
---|
2070 | n/a | (NS(foo=False, bar=0.5, w=None, x='b'), []), |
---|
2071 | n/a | ) |
---|
2072 | n/a | self.assertEqual( |
---|
2073 | n/a | parser.parse_known_args('0.5 1alias2 b'.split()), |
---|
2074 | n/a | (NS(foo=False, bar=0.5, w=None, x='b'), []), |
---|
2075 | n/a | ) |
---|
2076 | n/a | |
---|
2077 | n/a | def test_error_alias_invocation(self): |
---|
2078 | n/a | parser = self._get_parser(aliases=True) |
---|
2079 | n/a | self.assertArgumentParserError(parser.parse_args, |
---|
2080 | n/a | '0.5 1alias3 b'.split()) |
---|
2081 | n/a | |
---|
2082 | n/a | def test_alias_help(self): |
---|
2083 | n/a | parser = self._get_parser(aliases=True, subparser_help=True) |
---|
2084 | n/a | self.maxDiff = None |
---|
2085 | n/a | self.assertEqual(parser.format_help(), textwrap.dedent("""\ |
---|
2086 | n/a | usage: PROG [-h] [--foo] bar COMMAND ... |
---|
2087 | n/a | |
---|
2088 | n/a | main description |
---|
2089 | n/a | |
---|
2090 | n/a | positional arguments: |
---|
2091 | n/a | bar bar help |
---|
2092 | n/a | |
---|
2093 | n/a | optional arguments: |
---|
2094 | n/a | -h, --help show this help message and exit |
---|
2095 | n/a | --foo foo help |
---|
2096 | n/a | |
---|
2097 | n/a | commands: |
---|
2098 | n/a | COMMAND |
---|
2099 | n/a | 1 (1alias1, 1alias2) |
---|
2100 | n/a | 1 help |
---|
2101 | n/a | 2 2 help |
---|
2102 | n/a | 3 3 help |
---|
2103 | n/a | """)) |
---|
2104 | n/a | |
---|
2105 | n/a | # ============ |
---|
2106 | n/a | # Groups tests |
---|
2107 | n/a | # ============ |
---|
2108 | n/a | |
---|
2109 | n/a | class TestPositionalsGroups(TestCase): |
---|
2110 | n/a | """Tests that order of group positionals matches construction order""" |
---|
2111 | n/a | |
---|
2112 | n/a | def test_nongroup_first(self): |
---|
2113 | n/a | parser = ErrorRaisingArgumentParser() |
---|
2114 | n/a | parser.add_argument('foo') |
---|
2115 | n/a | group = parser.add_argument_group('g') |
---|
2116 | n/a | group.add_argument('bar') |
---|
2117 | n/a | parser.add_argument('baz') |
---|
2118 | n/a | expected = NS(foo='1', bar='2', baz='3') |
---|
2119 | n/a | result = parser.parse_args('1 2 3'.split()) |
---|
2120 | n/a | self.assertEqual(expected, result) |
---|
2121 | n/a | |
---|
2122 | n/a | def test_group_first(self): |
---|
2123 | n/a | parser = ErrorRaisingArgumentParser() |
---|
2124 | n/a | group = parser.add_argument_group('xxx') |
---|
2125 | n/a | group.add_argument('foo') |
---|
2126 | n/a | parser.add_argument('bar') |
---|
2127 | n/a | parser.add_argument('baz') |
---|
2128 | n/a | expected = NS(foo='1', bar='2', baz='3') |
---|
2129 | n/a | result = parser.parse_args('1 2 3'.split()) |
---|
2130 | n/a | self.assertEqual(expected, result) |
---|
2131 | n/a | |
---|
2132 | n/a | def test_interleaved_groups(self): |
---|
2133 | n/a | parser = ErrorRaisingArgumentParser() |
---|
2134 | n/a | group = parser.add_argument_group('xxx') |
---|
2135 | n/a | parser.add_argument('foo') |
---|
2136 | n/a | group.add_argument('bar') |
---|
2137 | n/a | parser.add_argument('baz') |
---|
2138 | n/a | group = parser.add_argument_group('yyy') |
---|
2139 | n/a | group.add_argument('frell') |
---|
2140 | n/a | expected = NS(foo='1', bar='2', baz='3', frell='4') |
---|
2141 | n/a | result = parser.parse_args('1 2 3 4'.split()) |
---|
2142 | n/a | self.assertEqual(expected, result) |
---|
2143 | n/a | |
---|
2144 | n/a | # =================== |
---|
2145 | n/a | # Parent parser tests |
---|
2146 | n/a | # =================== |
---|
2147 | n/a | |
---|
2148 | n/a | class TestParentParsers(TestCase): |
---|
2149 | n/a | """Tests that parsers can be created with parent parsers""" |
---|
2150 | n/a | |
---|
2151 | n/a | def assertArgumentParserError(self, *args, **kwargs): |
---|
2152 | n/a | self.assertRaises(ArgumentParserError, *args, **kwargs) |
---|
2153 | n/a | |
---|
2154 | n/a | def setUp(self): |
---|
2155 | n/a | super().setUp() |
---|
2156 | n/a | self.wxyz_parent = ErrorRaisingArgumentParser(add_help=False) |
---|
2157 | n/a | self.wxyz_parent.add_argument('--w') |
---|
2158 | n/a | x_group = self.wxyz_parent.add_argument_group('x') |
---|
2159 | n/a | x_group.add_argument('-y') |
---|
2160 | n/a | self.wxyz_parent.add_argument('z') |
---|
2161 | n/a | |
---|
2162 | n/a | self.abcd_parent = ErrorRaisingArgumentParser(add_help=False) |
---|
2163 | n/a | self.abcd_parent.add_argument('a') |
---|
2164 | n/a | self.abcd_parent.add_argument('-b') |
---|
2165 | n/a | c_group = self.abcd_parent.add_argument_group('c') |
---|
2166 | n/a | c_group.add_argument('--d') |
---|
2167 | n/a | |
---|
2168 | n/a | self.w_parent = ErrorRaisingArgumentParser(add_help=False) |
---|
2169 | n/a | self.w_parent.add_argument('--w') |
---|
2170 | n/a | |
---|
2171 | n/a | self.z_parent = ErrorRaisingArgumentParser(add_help=False) |
---|
2172 | n/a | self.z_parent.add_argument('z') |
---|
2173 | n/a | |
---|
2174 | n/a | # parents with mutually exclusive groups |
---|
2175 | n/a | self.ab_mutex_parent = ErrorRaisingArgumentParser(add_help=False) |
---|
2176 | n/a | group = self.ab_mutex_parent.add_mutually_exclusive_group() |
---|
2177 | n/a | group.add_argument('-a', action='store_true') |
---|
2178 | n/a | group.add_argument('-b', action='store_true') |
---|
2179 | n/a | |
---|
2180 | n/a | self.main_program = os.path.basename(sys.argv[0]) |
---|
2181 | n/a | |
---|
2182 | n/a | def test_single_parent(self): |
---|
2183 | n/a | parser = ErrorRaisingArgumentParser(parents=[self.wxyz_parent]) |
---|
2184 | n/a | self.assertEqual(parser.parse_args('-y 1 2 --w 3'.split()), |
---|
2185 | n/a | NS(w='3', y='1', z='2')) |
---|
2186 | n/a | |
---|
2187 | n/a | def test_single_parent_mutex(self): |
---|
2188 | n/a | self._test_mutex_ab(self.ab_mutex_parent.parse_args) |
---|
2189 | n/a | parser = ErrorRaisingArgumentParser(parents=[self.ab_mutex_parent]) |
---|
2190 | n/a | self._test_mutex_ab(parser.parse_args) |
---|
2191 | n/a | |
---|
2192 | n/a | def test_single_granparent_mutex(self): |
---|
2193 | n/a | parents = [self.ab_mutex_parent] |
---|
2194 | n/a | parser = ErrorRaisingArgumentParser(add_help=False, parents=parents) |
---|
2195 | n/a | parser = ErrorRaisingArgumentParser(parents=[parser]) |
---|
2196 | n/a | self._test_mutex_ab(parser.parse_args) |
---|
2197 | n/a | |
---|
2198 | n/a | def _test_mutex_ab(self, parse_args): |
---|
2199 | n/a | self.assertEqual(parse_args([]), NS(a=False, b=False)) |
---|
2200 | n/a | self.assertEqual(parse_args(['-a']), NS(a=True, b=False)) |
---|
2201 | n/a | self.assertEqual(parse_args(['-b']), NS(a=False, b=True)) |
---|
2202 | n/a | self.assertArgumentParserError(parse_args, ['-a', '-b']) |
---|
2203 | n/a | self.assertArgumentParserError(parse_args, ['-b', '-a']) |
---|
2204 | n/a | self.assertArgumentParserError(parse_args, ['-c']) |
---|
2205 | n/a | self.assertArgumentParserError(parse_args, ['-a', '-c']) |
---|
2206 | n/a | self.assertArgumentParserError(parse_args, ['-b', '-c']) |
---|
2207 | n/a | |
---|
2208 | n/a | def test_multiple_parents(self): |
---|
2209 | n/a | parents = [self.abcd_parent, self.wxyz_parent] |
---|
2210 | n/a | parser = ErrorRaisingArgumentParser(parents=parents) |
---|
2211 | n/a | self.assertEqual(parser.parse_args('--d 1 --w 2 3 4'.split()), |
---|
2212 | n/a | NS(a='3', b=None, d='1', w='2', y=None, z='4')) |
---|
2213 | n/a | |
---|
2214 | n/a | def test_multiple_parents_mutex(self): |
---|
2215 | n/a | parents = [self.ab_mutex_parent, self.wxyz_parent] |
---|
2216 | n/a | parser = ErrorRaisingArgumentParser(parents=parents) |
---|
2217 | n/a | self.assertEqual(parser.parse_args('-a --w 2 3'.split()), |
---|
2218 | n/a | NS(a=True, b=False, w='2', y=None, z='3')) |
---|
2219 | n/a | self.assertArgumentParserError( |
---|
2220 | n/a | parser.parse_args, '-a --w 2 3 -b'.split()) |
---|
2221 | n/a | self.assertArgumentParserError( |
---|
2222 | n/a | parser.parse_args, '-a -b --w 2 3'.split()) |
---|
2223 | n/a | |
---|
2224 | n/a | def test_conflicting_parents(self): |
---|
2225 | n/a | self.assertRaises( |
---|
2226 | n/a | argparse.ArgumentError, |
---|
2227 | n/a | argparse.ArgumentParser, |
---|
2228 | n/a | parents=[self.w_parent, self.wxyz_parent]) |
---|
2229 | n/a | |
---|
2230 | n/a | def test_conflicting_parents_mutex(self): |
---|
2231 | n/a | self.assertRaises( |
---|
2232 | n/a | argparse.ArgumentError, |
---|
2233 | n/a | argparse.ArgumentParser, |
---|
2234 | n/a | parents=[self.abcd_parent, self.ab_mutex_parent]) |
---|
2235 | n/a | |
---|
2236 | n/a | def test_same_argument_name_parents(self): |
---|
2237 | n/a | parents = [self.wxyz_parent, self.z_parent] |
---|
2238 | n/a | parser = ErrorRaisingArgumentParser(parents=parents) |
---|
2239 | n/a | self.assertEqual(parser.parse_args('1 2'.split()), |
---|
2240 | n/a | NS(w=None, y=None, z='2')) |
---|
2241 | n/a | |
---|
2242 | n/a | def test_subparser_parents(self): |
---|
2243 | n/a | parser = ErrorRaisingArgumentParser() |
---|
2244 | n/a | subparsers = parser.add_subparsers() |
---|
2245 | n/a | abcde_parser = subparsers.add_parser('bar', parents=[self.abcd_parent]) |
---|
2246 | n/a | abcde_parser.add_argument('e') |
---|
2247 | n/a | self.assertEqual(parser.parse_args('bar -b 1 --d 2 3 4'.split()), |
---|
2248 | n/a | NS(a='3', b='1', d='2', e='4')) |
---|
2249 | n/a | |
---|
2250 | n/a | def test_subparser_parents_mutex(self): |
---|
2251 | n/a | parser = ErrorRaisingArgumentParser() |
---|
2252 | n/a | subparsers = parser.add_subparsers() |
---|
2253 | n/a | parents = [self.ab_mutex_parent] |
---|
2254 | n/a | abc_parser = subparsers.add_parser('foo', parents=parents) |
---|
2255 | n/a | c_group = abc_parser.add_argument_group('c_group') |
---|
2256 | n/a | c_group.add_argument('c') |
---|
2257 | n/a | parents = [self.wxyz_parent, self.ab_mutex_parent] |
---|
2258 | n/a | wxyzabe_parser = subparsers.add_parser('bar', parents=parents) |
---|
2259 | n/a | wxyzabe_parser.add_argument('e') |
---|
2260 | n/a | self.assertEqual(parser.parse_args('foo -a 4'.split()), |
---|
2261 | n/a | NS(a=True, b=False, c='4')) |
---|
2262 | n/a | self.assertEqual(parser.parse_args('bar -b --w 2 3 4'.split()), |
---|
2263 | n/a | NS(a=False, b=True, w='2', y=None, z='3', e='4')) |
---|
2264 | n/a | self.assertArgumentParserError( |
---|
2265 | n/a | parser.parse_args, 'foo -a -b 4'.split()) |
---|
2266 | n/a | self.assertArgumentParserError( |
---|
2267 | n/a | parser.parse_args, 'bar -b -a 4'.split()) |
---|
2268 | n/a | |
---|
2269 | n/a | def test_parent_help(self): |
---|
2270 | n/a | parents = [self.abcd_parent, self.wxyz_parent] |
---|
2271 | n/a | parser = ErrorRaisingArgumentParser(parents=parents) |
---|
2272 | n/a | parser_help = parser.format_help() |
---|
2273 | n/a | progname = self.main_program |
---|
2274 | n/a | self.assertEqual(parser_help, textwrap.dedent('''\ |
---|
2275 | n/a | usage: {}{}[-h] [-b B] [--d D] [--w W] [-y Y] a z |
---|
2276 | n/a | |
---|
2277 | n/a | positional arguments: |
---|
2278 | n/a | a |
---|
2279 | n/a | z |
---|
2280 | n/a | |
---|
2281 | n/a | optional arguments: |
---|
2282 | n/a | -h, --help show this help message and exit |
---|
2283 | n/a | -b B |
---|
2284 | n/a | --w W |
---|
2285 | n/a | |
---|
2286 | n/a | c: |
---|
2287 | n/a | --d D |
---|
2288 | n/a | |
---|
2289 | n/a | x: |
---|
2290 | n/a | -y Y |
---|
2291 | n/a | '''.format(progname, ' ' if progname else '' ))) |
---|
2292 | n/a | |
---|
2293 | n/a | def test_groups_parents(self): |
---|
2294 | n/a | parent = ErrorRaisingArgumentParser(add_help=False) |
---|
2295 | n/a | g = parent.add_argument_group(title='g', description='gd') |
---|
2296 | n/a | g.add_argument('-w') |
---|
2297 | n/a | g.add_argument('-x') |
---|
2298 | n/a | m = parent.add_mutually_exclusive_group() |
---|
2299 | n/a | m.add_argument('-y') |
---|
2300 | n/a | m.add_argument('-z') |
---|
2301 | n/a | parser = ErrorRaisingArgumentParser(parents=[parent]) |
---|
2302 | n/a | |
---|
2303 | n/a | self.assertRaises(ArgumentParserError, parser.parse_args, |
---|
2304 | n/a | ['-y', 'Y', '-z', 'Z']) |
---|
2305 | n/a | |
---|
2306 | n/a | parser_help = parser.format_help() |
---|
2307 | n/a | progname = self.main_program |
---|
2308 | n/a | self.assertEqual(parser_help, textwrap.dedent('''\ |
---|
2309 | n/a | usage: {}{}[-h] [-w W] [-x X] [-y Y | -z Z] |
---|
2310 | n/a | |
---|
2311 | n/a | optional arguments: |
---|
2312 | n/a | -h, --help show this help message and exit |
---|
2313 | n/a | -y Y |
---|
2314 | n/a | -z Z |
---|
2315 | n/a | |
---|
2316 | n/a | g: |
---|
2317 | n/a | gd |
---|
2318 | n/a | |
---|
2319 | n/a | -w W |
---|
2320 | n/a | -x X |
---|
2321 | n/a | '''.format(progname, ' ' if progname else '' ))) |
---|
2322 | n/a | |
---|
2323 | n/a | # ============================== |
---|
2324 | n/a | # Mutually exclusive group tests |
---|
2325 | n/a | # ============================== |
---|
2326 | n/a | |
---|
2327 | n/a | class TestMutuallyExclusiveGroupErrors(TestCase): |
---|
2328 | n/a | |
---|
2329 | n/a | def test_invalid_add_argument_group(self): |
---|
2330 | n/a | parser = ErrorRaisingArgumentParser() |
---|
2331 | n/a | raises = self.assertRaises |
---|
2332 | n/a | raises(TypeError, parser.add_mutually_exclusive_group, title='foo') |
---|
2333 | n/a | |
---|
2334 | n/a | def test_invalid_add_argument(self): |
---|
2335 | n/a | parser = ErrorRaisingArgumentParser() |
---|
2336 | n/a | group = parser.add_mutually_exclusive_group() |
---|
2337 | n/a | add_argument = group.add_argument |
---|
2338 | n/a | raises = self.assertRaises |
---|
2339 | n/a | raises(ValueError, add_argument, '--foo', required=True) |
---|
2340 | n/a | raises(ValueError, add_argument, 'bar') |
---|
2341 | n/a | raises(ValueError, add_argument, 'bar', nargs='+') |
---|
2342 | n/a | raises(ValueError, add_argument, 'bar', nargs=1) |
---|
2343 | n/a | raises(ValueError, add_argument, 'bar', nargs=argparse.PARSER) |
---|
2344 | n/a | |
---|
2345 | n/a | def test_help(self): |
---|
2346 | n/a | parser = ErrorRaisingArgumentParser(prog='PROG') |
---|
2347 | n/a | group1 = parser.add_mutually_exclusive_group() |
---|
2348 | n/a | group1.add_argument('--foo', action='store_true') |
---|
2349 | n/a | group1.add_argument('--bar', action='store_false') |
---|
2350 | n/a | group2 = parser.add_mutually_exclusive_group() |
---|
2351 | n/a | group2.add_argument('--soup', action='store_true') |
---|
2352 | n/a | group2.add_argument('--nuts', action='store_false') |
---|
2353 | n/a | expected = '''\ |
---|
2354 | n/a | usage: PROG [-h] [--foo | --bar] [--soup | --nuts] |
---|
2355 | n/a | |
---|
2356 | n/a | optional arguments: |
---|
2357 | n/a | -h, --help show this help message and exit |
---|
2358 | n/a | --foo |
---|
2359 | n/a | --bar |
---|
2360 | n/a | --soup |
---|
2361 | n/a | --nuts |
---|
2362 | n/a | ''' |
---|
2363 | n/a | self.assertEqual(parser.format_help(), textwrap.dedent(expected)) |
---|
2364 | n/a | |
---|
2365 | n/a | class MEMixin(object): |
---|
2366 | n/a | |
---|
2367 | n/a | def test_failures_when_not_required(self): |
---|
2368 | n/a | parse_args = self.get_parser(required=False).parse_args |
---|
2369 | n/a | error = ArgumentParserError |
---|
2370 | n/a | for args_string in self.failures: |
---|
2371 | n/a | self.assertRaises(error, parse_args, args_string.split()) |
---|
2372 | n/a | |
---|
2373 | n/a | def test_failures_when_required(self): |
---|
2374 | n/a | parse_args = self.get_parser(required=True).parse_args |
---|
2375 | n/a | error = ArgumentParserError |
---|
2376 | n/a | for args_string in self.failures + ['']: |
---|
2377 | n/a | self.assertRaises(error, parse_args, args_string.split()) |
---|
2378 | n/a | |
---|
2379 | n/a | def test_successes_when_not_required(self): |
---|
2380 | n/a | parse_args = self.get_parser(required=False).parse_args |
---|
2381 | n/a | successes = self.successes + self.successes_when_not_required |
---|
2382 | n/a | for args_string, expected_ns in successes: |
---|
2383 | n/a | actual_ns = parse_args(args_string.split()) |
---|
2384 | n/a | self.assertEqual(actual_ns, expected_ns) |
---|
2385 | n/a | |
---|
2386 | n/a | def test_successes_when_required(self): |
---|
2387 | n/a | parse_args = self.get_parser(required=True).parse_args |
---|
2388 | n/a | for args_string, expected_ns in self.successes: |
---|
2389 | n/a | actual_ns = parse_args(args_string.split()) |
---|
2390 | n/a | self.assertEqual(actual_ns, expected_ns) |
---|
2391 | n/a | |
---|
2392 | n/a | def test_usage_when_not_required(self): |
---|
2393 | n/a | format_usage = self.get_parser(required=False).format_usage |
---|
2394 | n/a | expected_usage = self.usage_when_not_required |
---|
2395 | n/a | self.assertEqual(format_usage(), textwrap.dedent(expected_usage)) |
---|
2396 | n/a | |
---|
2397 | n/a | def test_usage_when_required(self): |
---|
2398 | n/a | format_usage = self.get_parser(required=True).format_usage |
---|
2399 | n/a | expected_usage = self.usage_when_required |
---|
2400 | n/a | self.assertEqual(format_usage(), textwrap.dedent(expected_usage)) |
---|
2401 | n/a | |
---|
2402 | n/a | def test_help_when_not_required(self): |
---|
2403 | n/a | format_help = self.get_parser(required=False).format_help |
---|
2404 | n/a | help = self.usage_when_not_required + self.help |
---|
2405 | n/a | self.assertEqual(format_help(), textwrap.dedent(help)) |
---|
2406 | n/a | |
---|
2407 | n/a | def test_help_when_required(self): |
---|
2408 | n/a | format_help = self.get_parser(required=True).format_help |
---|
2409 | n/a | help = self.usage_when_required + self.help |
---|
2410 | n/a | self.assertEqual(format_help(), textwrap.dedent(help)) |
---|
2411 | n/a | |
---|
2412 | n/a | |
---|
2413 | n/a | class TestMutuallyExclusiveSimple(MEMixin, TestCase): |
---|
2414 | n/a | |
---|
2415 | n/a | def get_parser(self, required=None): |
---|
2416 | n/a | parser = ErrorRaisingArgumentParser(prog='PROG') |
---|
2417 | n/a | group = parser.add_mutually_exclusive_group(required=required) |
---|
2418 | n/a | group.add_argument('--bar', help='bar help') |
---|
2419 | n/a | group.add_argument('--baz', nargs='?', const='Z', help='baz help') |
---|
2420 | n/a | return parser |
---|
2421 | n/a | |
---|
2422 | n/a | failures = ['--bar X --baz Y', '--bar X --baz'] |
---|
2423 | n/a | successes = [ |
---|
2424 | n/a | ('--bar X', NS(bar='X', baz=None)), |
---|
2425 | n/a | ('--bar X --bar Z', NS(bar='Z', baz=None)), |
---|
2426 | n/a | ('--baz Y', NS(bar=None, baz='Y')), |
---|
2427 | n/a | ('--baz', NS(bar=None, baz='Z')), |
---|
2428 | n/a | ] |
---|
2429 | n/a | successes_when_not_required = [ |
---|
2430 | n/a | ('', NS(bar=None, baz=None)), |
---|
2431 | n/a | ] |
---|
2432 | n/a | |
---|
2433 | n/a | usage_when_not_required = '''\ |
---|
2434 | n/a | usage: PROG [-h] [--bar BAR | --baz [BAZ]] |
---|
2435 | n/a | ''' |
---|
2436 | n/a | usage_when_required = '''\ |
---|
2437 | n/a | usage: PROG [-h] (--bar BAR | --baz [BAZ]) |
---|
2438 | n/a | ''' |
---|
2439 | n/a | help = '''\ |
---|
2440 | n/a | |
---|
2441 | n/a | optional arguments: |
---|
2442 | n/a | -h, --help show this help message and exit |
---|
2443 | n/a | --bar BAR bar help |
---|
2444 | n/a | --baz [BAZ] baz help |
---|
2445 | n/a | ''' |
---|
2446 | n/a | |
---|
2447 | n/a | |
---|
2448 | n/a | class TestMutuallyExclusiveLong(MEMixin, TestCase): |
---|
2449 | n/a | |
---|
2450 | n/a | def get_parser(self, required=None): |
---|
2451 | n/a | parser = ErrorRaisingArgumentParser(prog='PROG') |
---|
2452 | n/a | parser.add_argument('--abcde', help='abcde help') |
---|
2453 | n/a | parser.add_argument('--fghij', help='fghij help') |
---|
2454 | n/a | group = parser.add_mutually_exclusive_group(required=required) |
---|
2455 | n/a | group.add_argument('--klmno', help='klmno help') |
---|
2456 | n/a | group.add_argument('--pqrst', help='pqrst help') |
---|
2457 | n/a | return parser |
---|
2458 | n/a | |
---|
2459 | n/a | failures = ['--klmno X --pqrst Y'] |
---|
2460 | n/a | successes = [ |
---|
2461 | n/a | ('--klmno X', NS(abcde=None, fghij=None, klmno='X', pqrst=None)), |
---|
2462 | n/a | ('--abcde Y --klmno X', |
---|
2463 | n/a | NS(abcde='Y', fghij=None, klmno='X', pqrst=None)), |
---|
2464 | n/a | ('--pqrst X', NS(abcde=None, fghij=None, klmno=None, pqrst='X')), |
---|
2465 | n/a | ('--pqrst X --fghij Y', |
---|
2466 | n/a | NS(abcde=None, fghij='Y', klmno=None, pqrst='X')), |
---|
2467 | n/a | ] |
---|
2468 | n/a | successes_when_not_required = [ |
---|
2469 | n/a | ('', NS(abcde=None, fghij=None, klmno=None, pqrst=None)), |
---|
2470 | n/a | ] |
---|
2471 | n/a | |
---|
2472 | n/a | usage_when_not_required = '''\ |
---|
2473 | n/a | usage: PROG [-h] [--abcde ABCDE] [--fghij FGHIJ] |
---|
2474 | n/a | [--klmno KLMNO | --pqrst PQRST] |
---|
2475 | n/a | ''' |
---|
2476 | n/a | usage_when_required = '''\ |
---|
2477 | n/a | usage: PROG [-h] [--abcde ABCDE] [--fghij FGHIJ] |
---|
2478 | n/a | (--klmno KLMNO | --pqrst PQRST) |
---|
2479 | n/a | ''' |
---|
2480 | n/a | help = '''\ |
---|
2481 | n/a | |
---|
2482 | n/a | optional arguments: |
---|
2483 | n/a | -h, --help show this help message and exit |
---|
2484 | n/a | --abcde ABCDE abcde help |
---|
2485 | n/a | --fghij FGHIJ fghij help |
---|
2486 | n/a | --klmno KLMNO klmno help |
---|
2487 | n/a | --pqrst PQRST pqrst help |
---|
2488 | n/a | ''' |
---|
2489 | n/a | |
---|
2490 | n/a | |
---|
2491 | n/a | class TestMutuallyExclusiveFirstSuppressed(MEMixin, TestCase): |
---|
2492 | n/a | |
---|
2493 | n/a | def get_parser(self, required): |
---|
2494 | n/a | parser = ErrorRaisingArgumentParser(prog='PROG') |
---|
2495 | n/a | group = parser.add_mutually_exclusive_group(required=required) |
---|
2496 | n/a | group.add_argument('-x', help=argparse.SUPPRESS) |
---|
2497 | n/a | group.add_argument('-y', action='store_false', help='y help') |
---|
2498 | n/a | return parser |
---|
2499 | n/a | |
---|
2500 | n/a | failures = ['-x X -y'] |
---|
2501 | n/a | successes = [ |
---|
2502 | n/a | ('-x X', NS(x='X', y=True)), |
---|
2503 | n/a | ('-x X -x Y', NS(x='Y', y=True)), |
---|
2504 | n/a | ('-y', NS(x=None, y=False)), |
---|
2505 | n/a | ] |
---|
2506 | n/a | successes_when_not_required = [ |
---|
2507 | n/a | ('', NS(x=None, y=True)), |
---|
2508 | n/a | ] |
---|
2509 | n/a | |
---|
2510 | n/a | usage_when_not_required = '''\ |
---|
2511 | n/a | usage: PROG [-h] [-y] |
---|
2512 | n/a | ''' |
---|
2513 | n/a | usage_when_required = '''\ |
---|
2514 | n/a | usage: PROG [-h] -y |
---|
2515 | n/a | ''' |
---|
2516 | n/a | help = '''\ |
---|
2517 | n/a | |
---|
2518 | n/a | optional arguments: |
---|
2519 | n/a | -h, --help show this help message and exit |
---|
2520 | n/a | -y y help |
---|
2521 | n/a | ''' |
---|
2522 | n/a | |
---|
2523 | n/a | |
---|
2524 | n/a | class TestMutuallyExclusiveManySuppressed(MEMixin, TestCase): |
---|
2525 | n/a | |
---|
2526 | n/a | def get_parser(self, required): |
---|
2527 | n/a | parser = ErrorRaisingArgumentParser(prog='PROG') |
---|
2528 | n/a | group = parser.add_mutually_exclusive_group(required=required) |
---|
2529 | n/a | add = group.add_argument |
---|
2530 | n/a | add('--spam', action='store_true', help=argparse.SUPPRESS) |
---|
2531 | n/a | add('--badger', action='store_false', help=argparse.SUPPRESS) |
---|
2532 | n/a | add('--bladder', help=argparse.SUPPRESS) |
---|
2533 | n/a | return parser |
---|
2534 | n/a | |
---|
2535 | n/a | failures = [ |
---|
2536 | n/a | '--spam --badger', |
---|
2537 | n/a | '--badger --bladder B', |
---|
2538 | n/a | '--bladder B --spam', |
---|
2539 | n/a | ] |
---|
2540 | n/a | successes = [ |
---|
2541 | n/a | ('--spam', NS(spam=True, badger=True, bladder=None)), |
---|
2542 | n/a | ('--badger', NS(spam=False, badger=False, bladder=None)), |
---|
2543 | n/a | ('--bladder B', NS(spam=False, badger=True, bladder='B')), |
---|
2544 | n/a | ('--spam --spam', NS(spam=True, badger=True, bladder=None)), |
---|
2545 | n/a | ] |
---|
2546 | n/a | successes_when_not_required = [ |
---|
2547 | n/a | ('', NS(spam=False, badger=True, bladder=None)), |
---|
2548 | n/a | ] |
---|
2549 | n/a | |
---|
2550 | n/a | usage_when_required = usage_when_not_required = '''\ |
---|
2551 | n/a | usage: PROG [-h] |
---|
2552 | n/a | ''' |
---|
2553 | n/a | help = '''\ |
---|
2554 | n/a | |
---|
2555 | n/a | optional arguments: |
---|
2556 | n/a | -h, --help show this help message and exit |
---|
2557 | n/a | ''' |
---|
2558 | n/a | |
---|
2559 | n/a | |
---|
2560 | n/a | class TestMutuallyExclusiveOptionalAndPositional(MEMixin, TestCase): |
---|
2561 | n/a | |
---|
2562 | n/a | def get_parser(self, required): |
---|
2563 | n/a | parser = ErrorRaisingArgumentParser(prog='PROG') |
---|
2564 | n/a | group = parser.add_mutually_exclusive_group(required=required) |
---|
2565 | n/a | group.add_argument('--foo', action='store_true', help='FOO') |
---|
2566 | n/a | group.add_argument('--spam', help='SPAM') |
---|
2567 | n/a | group.add_argument('badger', nargs='*', default='X', help='BADGER') |
---|
2568 | n/a | return parser |
---|
2569 | n/a | |
---|
2570 | n/a | failures = [ |
---|
2571 | n/a | '--foo --spam S', |
---|
2572 | n/a | '--spam S X', |
---|
2573 | n/a | 'X --foo', |
---|
2574 | n/a | 'X Y Z --spam S', |
---|
2575 | n/a | '--foo X Y', |
---|
2576 | n/a | ] |
---|
2577 | n/a | successes = [ |
---|
2578 | n/a | ('--foo', NS(foo=True, spam=None, badger='X')), |
---|
2579 | n/a | ('--spam S', NS(foo=False, spam='S', badger='X')), |
---|
2580 | n/a | ('X', NS(foo=False, spam=None, badger=['X'])), |
---|
2581 | n/a | ('X Y Z', NS(foo=False, spam=None, badger=['X', 'Y', 'Z'])), |
---|
2582 | n/a | ] |
---|
2583 | n/a | successes_when_not_required = [ |
---|
2584 | n/a | ('', NS(foo=False, spam=None, badger='X')), |
---|
2585 | n/a | ] |
---|
2586 | n/a | |
---|
2587 | n/a | usage_when_not_required = '''\ |
---|
2588 | n/a | usage: PROG [-h] [--foo | --spam SPAM | badger [badger ...]] |
---|
2589 | n/a | ''' |
---|
2590 | n/a | usage_when_required = '''\ |
---|
2591 | n/a | usage: PROG [-h] (--foo | --spam SPAM | badger [badger ...]) |
---|
2592 | n/a | ''' |
---|
2593 | n/a | help = '''\ |
---|
2594 | n/a | |
---|
2595 | n/a | positional arguments: |
---|
2596 | n/a | badger BADGER |
---|
2597 | n/a | |
---|
2598 | n/a | optional arguments: |
---|
2599 | n/a | -h, --help show this help message and exit |
---|
2600 | n/a | --foo FOO |
---|
2601 | n/a | --spam SPAM SPAM |
---|
2602 | n/a | ''' |
---|
2603 | n/a | |
---|
2604 | n/a | |
---|
2605 | n/a | class TestMutuallyExclusiveOptionalsMixed(MEMixin, TestCase): |
---|
2606 | n/a | |
---|
2607 | n/a | def get_parser(self, required): |
---|
2608 | n/a | parser = ErrorRaisingArgumentParser(prog='PROG') |
---|
2609 | n/a | parser.add_argument('-x', action='store_true', help='x help') |
---|
2610 | n/a | group = parser.add_mutually_exclusive_group(required=required) |
---|
2611 | n/a | group.add_argument('-a', action='store_true', help='a help') |
---|
2612 | n/a | group.add_argument('-b', action='store_true', help='b help') |
---|
2613 | n/a | parser.add_argument('-y', action='store_true', help='y help') |
---|
2614 | n/a | group.add_argument('-c', action='store_true', help='c help') |
---|
2615 | n/a | return parser |
---|
2616 | n/a | |
---|
2617 | n/a | failures = ['-a -b', '-b -c', '-a -c', '-a -b -c'] |
---|
2618 | n/a | successes = [ |
---|
2619 | n/a | ('-a', NS(a=True, b=False, c=False, x=False, y=False)), |
---|
2620 | n/a | ('-b', NS(a=False, b=True, c=False, x=False, y=False)), |
---|
2621 | n/a | ('-c', NS(a=False, b=False, c=True, x=False, y=False)), |
---|
2622 | n/a | ('-a -x', NS(a=True, b=False, c=False, x=True, y=False)), |
---|
2623 | n/a | ('-y -b', NS(a=False, b=True, c=False, x=False, y=True)), |
---|
2624 | n/a | ('-x -y -c', NS(a=False, b=False, c=True, x=True, y=True)), |
---|
2625 | n/a | ] |
---|
2626 | n/a | successes_when_not_required = [ |
---|
2627 | n/a | ('', NS(a=False, b=False, c=False, x=False, y=False)), |
---|
2628 | n/a | ('-x', NS(a=False, b=False, c=False, x=True, y=False)), |
---|
2629 | n/a | ('-y', NS(a=False, b=False, c=False, x=False, y=True)), |
---|
2630 | n/a | ] |
---|
2631 | n/a | |
---|
2632 | n/a | usage_when_required = usage_when_not_required = '''\ |
---|
2633 | n/a | usage: PROG [-h] [-x] [-a] [-b] [-y] [-c] |
---|
2634 | n/a | ''' |
---|
2635 | n/a | help = '''\ |
---|
2636 | n/a | |
---|
2637 | n/a | optional arguments: |
---|
2638 | n/a | -h, --help show this help message and exit |
---|
2639 | n/a | -x x help |
---|
2640 | n/a | -a a help |
---|
2641 | n/a | -b b help |
---|
2642 | n/a | -y y help |
---|
2643 | n/a | -c c help |
---|
2644 | n/a | ''' |
---|
2645 | n/a | |
---|
2646 | n/a | |
---|
2647 | n/a | class TestMutuallyExclusiveInGroup(MEMixin, TestCase): |
---|
2648 | n/a | |
---|
2649 | n/a | def get_parser(self, required=None): |
---|
2650 | n/a | parser = ErrorRaisingArgumentParser(prog='PROG') |
---|
2651 | n/a | titled_group = parser.add_argument_group( |
---|
2652 | n/a | title='Titled group', description='Group description') |
---|
2653 | n/a | mutex_group = \ |
---|
2654 | n/a | titled_group.add_mutually_exclusive_group(required=required) |
---|
2655 | n/a | mutex_group.add_argument('--bar', help='bar help') |
---|
2656 | n/a | mutex_group.add_argument('--baz', help='baz help') |
---|
2657 | n/a | return parser |
---|
2658 | n/a | |
---|
2659 | n/a | failures = ['--bar X --baz Y', '--baz X --bar Y'] |
---|
2660 | n/a | successes = [ |
---|
2661 | n/a | ('--bar X', NS(bar='X', baz=None)), |
---|
2662 | n/a | ('--baz Y', NS(bar=None, baz='Y')), |
---|
2663 | n/a | ] |
---|
2664 | n/a | successes_when_not_required = [ |
---|
2665 | n/a | ('', NS(bar=None, baz=None)), |
---|
2666 | n/a | ] |
---|
2667 | n/a | |
---|
2668 | n/a | usage_when_not_required = '''\ |
---|
2669 | n/a | usage: PROG [-h] [--bar BAR | --baz BAZ] |
---|
2670 | n/a | ''' |
---|
2671 | n/a | usage_when_required = '''\ |
---|
2672 | n/a | usage: PROG [-h] (--bar BAR | --baz BAZ) |
---|
2673 | n/a | ''' |
---|
2674 | n/a | help = '''\ |
---|
2675 | n/a | |
---|
2676 | n/a | optional arguments: |
---|
2677 | n/a | -h, --help show this help message and exit |
---|
2678 | n/a | |
---|
2679 | n/a | Titled group: |
---|
2680 | n/a | Group description |
---|
2681 | n/a | |
---|
2682 | n/a | --bar BAR bar help |
---|
2683 | n/a | --baz BAZ baz help |
---|
2684 | n/a | ''' |
---|
2685 | n/a | |
---|
2686 | n/a | |
---|
2687 | n/a | class TestMutuallyExclusiveOptionalsAndPositionalsMixed(MEMixin, TestCase): |
---|
2688 | n/a | |
---|
2689 | n/a | def get_parser(self, required): |
---|
2690 | n/a | parser = ErrorRaisingArgumentParser(prog='PROG') |
---|
2691 | n/a | parser.add_argument('x', help='x help') |
---|
2692 | n/a | parser.add_argument('-y', action='store_true', help='y help') |
---|
2693 | n/a | group = parser.add_mutually_exclusive_group(required=required) |
---|
2694 | n/a | group.add_argument('a', nargs='?', help='a help') |
---|
2695 | n/a | group.add_argument('-b', action='store_true', help='b help') |
---|
2696 | n/a | group.add_argument('-c', action='store_true', help='c help') |
---|
2697 | n/a | return parser |
---|
2698 | n/a | |
---|
2699 | n/a | failures = ['X A -b', '-b -c', '-c X A'] |
---|
2700 | n/a | successes = [ |
---|
2701 | n/a | ('X A', NS(a='A', b=False, c=False, x='X', y=False)), |
---|
2702 | n/a | ('X -b', NS(a=None, b=True, c=False, x='X', y=False)), |
---|
2703 | n/a | ('X -c', NS(a=None, b=False, c=True, x='X', y=False)), |
---|
2704 | n/a | ('X A -y', NS(a='A', b=False, c=False, x='X', y=True)), |
---|
2705 | n/a | ('X -y -b', NS(a=None, b=True, c=False, x='X', y=True)), |
---|
2706 | n/a | ] |
---|
2707 | n/a | successes_when_not_required = [ |
---|
2708 | n/a | ('X', NS(a=None, b=False, c=False, x='X', y=False)), |
---|
2709 | n/a | ('X -y', NS(a=None, b=False, c=False, x='X', y=True)), |
---|
2710 | n/a | ] |
---|
2711 | n/a | |
---|
2712 | n/a | usage_when_required = usage_when_not_required = '''\ |
---|
2713 | n/a | usage: PROG [-h] [-y] [-b] [-c] x [a] |
---|
2714 | n/a | ''' |
---|
2715 | n/a | help = '''\ |
---|
2716 | n/a | |
---|
2717 | n/a | positional arguments: |
---|
2718 | n/a | x x help |
---|
2719 | n/a | a a help |
---|
2720 | n/a | |
---|
2721 | n/a | optional arguments: |
---|
2722 | n/a | -h, --help show this help message and exit |
---|
2723 | n/a | -y y help |
---|
2724 | n/a | -b b help |
---|
2725 | n/a | -c c help |
---|
2726 | n/a | ''' |
---|
2727 | n/a | |
---|
2728 | n/a | # ================================================= |
---|
2729 | n/a | # Mutually exclusive group in parent parser tests |
---|
2730 | n/a | # ================================================= |
---|
2731 | n/a | |
---|
2732 | n/a | class MEPBase(object): |
---|
2733 | n/a | |
---|
2734 | n/a | def get_parser(self, required=None): |
---|
2735 | n/a | parent = super(MEPBase, self).get_parser(required=required) |
---|
2736 | n/a | parser = ErrorRaisingArgumentParser( |
---|
2737 | n/a | prog=parent.prog, add_help=False, parents=[parent]) |
---|
2738 | n/a | return parser |
---|
2739 | n/a | |
---|
2740 | n/a | |
---|
2741 | n/a | class TestMutuallyExclusiveGroupErrorsParent( |
---|
2742 | n/a | MEPBase, TestMutuallyExclusiveGroupErrors): |
---|
2743 | n/a | pass |
---|
2744 | n/a | |
---|
2745 | n/a | |
---|
2746 | n/a | class TestMutuallyExclusiveSimpleParent( |
---|
2747 | n/a | MEPBase, TestMutuallyExclusiveSimple): |
---|
2748 | n/a | pass |
---|
2749 | n/a | |
---|
2750 | n/a | |
---|
2751 | n/a | class TestMutuallyExclusiveLongParent( |
---|
2752 | n/a | MEPBase, TestMutuallyExclusiveLong): |
---|
2753 | n/a | pass |
---|
2754 | n/a | |
---|
2755 | n/a | |
---|
2756 | n/a | class TestMutuallyExclusiveFirstSuppressedParent( |
---|
2757 | n/a | MEPBase, TestMutuallyExclusiveFirstSuppressed): |
---|
2758 | n/a | pass |
---|
2759 | n/a | |
---|
2760 | n/a | |
---|
2761 | n/a | class TestMutuallyExclusiveManySuppressedParent( |
---|
2762 | n/a | MEPBase, TestMutuallyExclusiveManySuppressed): |
---|
2763 | n/a | pass |
---|
2764 | n/a | |
---|
2765 | n/a | |
---|
2766 | n/a | class TestMutuallyExclusiveOptionalAndPositionalParent( |
---|
2767 | n/a | MEPBase, TestMutuallyExclusiveOptionalAndPositional): |
---|
2768 | n/a | pass |
---|
2769 | n/a | |
---|
2770 | n/a | |
---|
2771 | n/a | class TestMutuallyExclusiveOptionalsMixedParent( |
---|
2772 | n/a | MEPBase, TestMutuallyExclusiveOptionalsMixed): |
---|
2773 | n/a | pass |
---|
2774 | n/a | |
---|
2775 | n/a | |
---|
2776 | n/a | class TestMutuallyExclusiveOptionalsAndPositionalsMixedParent( |
---|
2777 | n/a | MEPBase, TestMutuallyExclusiveOptionalsAndPositionalsMixed): |
---|
2778 | n/a | pass |
---|
2779 | n/a | |
---|
2780 | n/a | # ================= |
---|
2781 | n/a | # Set default tests |
---|
2782 | n/a | # ================= |
---|
2783 | n/a | |
---|
2784 | n/a | class TestSetDefaults(TestCase): |
---|
2785 | n/a | |
---|
2786 | n/a | def test_set_defaults_no_args(self): |
---|
2787 | n/a | parser = ErrorRaisingArgumentParser() |
---|
2788 | n/a | parser.set_defaults(x='foo') |
---|
2789 | n/a | parser.set_defaults(y='bar', z=1) |
---|
2790 | n/a | self.assertEqual(NS(x='foo', y='bar', z=1), |
---|
2791 | n/a | parser.parse_args([])) |
---|
2792 | n/a | self.assertEqual(NS(x='foo', y='bar', z=1), |
---|
2793 | n/a | parser.parse_args([], NS())) |
---|
2794 | n/a | self.assertEqual(NS(x='baz', y='bar', z=1), |
---|
2795 | n/a | parser.parse_args([], NS(x='baz'))) |
---|
2796 | n/a | self.assertEqual(NS(x='baz', y='bar', z=2), |
---|
2797 | n/a | parser.parse_args([], NS(x='baz', z=2))) |
---|
2798 | n/a | |
---|
2799 | n/a | def test_set_defaults_with_args(self): |
---|
2800 | n/a | parser = ErrorRaisingArgumentParser() |
---|
2801 | n/a | parser.set_defaults(x='foo', y='bar') |
---|
2802 | n/a | parser.add_argument('-x', default='xfoox') |
---|
2803 | n/a | self.assertEqual(NS(x='xfoox', y='bar'), |
---|
2804 | n/a | parser.parse_args([])) |
---|
2805 | n/a | self.assertEqual(NS(x='xfoox', y='bar'), |
---|
2806 | n/a | parser.parse_args([], NS())) |
---|
2807 | n/a | self.assertEqual(NS(x='baz', y='bar'), |
---|
2808 | n/a | parser.parse_args([], NS(x='baz'))) |
---|
2809 | n/a | self.assertEqual(NS(x='1', y='bar'), |
---|
2810 | n/a | parser.parse_args('-x 1'.split())) |
---|
2811 | n/a | self.assertEqual(NS(x='1', y='bar'), |
---|
2812 | n/a | parser.parse_args('-x 1'.split(), NS())) |
---|
2813 | n/a | self.assertEqual(NS(x='1', y='bar'), |
---|
2814 | n/a | parser.parse_args('-x 1'.split(), NS(x='baz'))) |
---|
2815 | n/a | |
---|
2816 | n/a | def test_set_defaults_subparsers(self): |
---|
2817 | n/a | parser = ErrorRaisingArgumentParser() |
---|
2818 | n/a | parser.set_defaults(x='foo') |
---|
2819 | n/a | subparsers = parser.add_subparsers() |
---|
2820 | n/a | parser_a = subparsers.add_parser('a') |
---|
2821 | n/a | parser_a.set_defaults(y='bar') |
---|
2822 | n/a | self.assertEqual(NS(x='foo', y='bar'), |
---|
2823 | n/a | parser.parse_args('a'.split())) |
---|
2824 | n/a | |
---|
2825 | n/a | def test_set_defaults_parents(self): |
---|
2826 | n/a | parent = ErrorRaisingArgumentParser(add_help=False) |
---|
2827 | n/a | parent.set_defaults(x='foo') |
---|
2828 | n/a | parser = ErrorRaisingArgumentParser(parents=[parent]) |
---|
2829 | n/a | self.assertEqual(NS(x='foo'), parser.parse_args([])) |
---|
2830 | n/a | |
---|
2831 | n/a | def test_set_defaults_on_parent_and_subparser(self): |
---|
2832 | n/a | parser = argparse.ArgumentParser() |
---|
2833 | n/a | xparser = parser.add_subparsers().add_parser('X') |
---|
2834 | n/a | parser.set_defaults(foo=1) |
---|
2835 | n/a | xparser.set_defaults(foo=2) |
---|
2836 | n/a | self.assertEqual(NS(foo=2), parser.parse_args(['X'])) |
---|
2837 | n/a | |
---|
2838 | n/a | def test_set_defaults_same_as_add_argument(self): |
---|
2839 | n/a | parser = ErrorRaisingArgumentParser() |
---|
2840 | n/a | parser.set_defaults(w='W', x='X', y='Y', z='Z') |
---|
2841 | n/a | parser.add_argument('-w') |
---|
2842 | n/a | parser.add_argument('-x', default='XX') |
---|
2843 | n/a | parser.add_argument('y', nargs='?') |
---|
2844 | n/a | parser.add_argument('z', nargs='?', default='ZZ') |
---|
2845 | n/a | |
---|
2846 | n/a | # defaults set previously |
---|
2847 | n/a | self.assertEqual(NS(w='W', x='XX', y='Y', z='ZZ'), |
---|
2848 | n/a | parser.parse_args([])) |
---|
2849 | n/a | |
---|
2850 | n/a | # reset defaults |
---|
2851 | n/a | parser.set_defaults(w='WW', x='X', y='YY', z='Z') |
---|
2852 | n/a | self.assertEqual(NS(w='WW', x='X', y='YY', z='Z'), |
---|
2853 | n/a | parser.parse_args([])) |
---|
2854 | n/a | |
---|
2855 | n/a | def test_set_defaults_same_as_add_argument_group(self): |
---|
2856 | n/a | parser = ErrorRaisingArgumentParser() |
---|
2857 | n/a | parser.set_defaults(w='W', x='X', y='Y', z='Z') |
---|
2858 | n/a | group = parser.add_argument_group('foo') |
---|
2859 | n/a | group.add_argument('-w') |
---|
2860 | n/a | group.add_argument('-x', default='XX') |
---|
2861 | n/a | group.add_argument('y', nargs='?') |
---|
2862 | n/a | group.add_argument('z', nargs='?', default='ZZ') |
---|
2863 | n/a | |
---|
2864 | n/a | |
---|
2865 | n/a | # defaults set previously |
---|
2866 | n/a | self.assertEqual(NS(w='W', x='XX', y='Y', z='ZZ'), |
---|
2867 | n/a | parser.parse_args([])) |
---|
2868 | n/a | |
---|
2869 | n/a | # reset defaults |
---|
2870 | n/a | parser.set_defaults(w='WW', x='X', y='YY', z='Z') |
---|
2871 | n/a | self.assertEqual(NS(w='WW', x='X', y='YY', z='Z'), |
---|
2872 | n/a | parser.parse_args([])) |
---|
2873 | n/a | |
---|
2874 | n/a | # ================= |
---|
2875 | n/a | # Get default tests |
---|
2876 | n/a | # ================= |
---|
2877 | n/a | |
---|
2878 | n/a | class TestGetDefault(TestCase): |
---|
2879 | n/a | |
---|
2880 | n/a | def test_get_default(self): |
---|
2881 | n/a | parser = ErrorRaisingArgumentParser() |
---|
2882 | n/a | self.assertIsNone(parser.get_default("foo")) |
---|
2883 | n/a | self.assertIsNone(parser.get_default("bar")) |
---|
2884 | n/a | |
---|
2885 | n/a | parser.add_argument("--foo") |
---|
2886 | n/a | self.assertIsNone(parser.get_default("foo")) |
---|
2887 | n/a | self.assertIsNone(parser.get_default("bar")) |
---|
2888 | n/a | |
---|
2889 | n/a | parser.add_argument("--bar", type=int, default=42) |
---|
2890 | n/a | self.assertIsNone(parser.get_default("foo")) |
---|
2891 | n/a | self.assertEqual(42, parser.get_default("bar")) |
---|
2892 | n/a | |
---|
2893 | n/a | parser.set_defaults(foo="badger") |
---|
2894 | n/a | self.assertEqual("badger", parser.get_default("foo")) |
---|
2895 | n/a | self.assertEqual(42, parser.get_default("bar")) |
---|
2896 | n/a | |
---|
2897 | n/a | # ========================== |
---|
2898 | n/a | # Namespace 'contains' tests |
---|
2899 | n/a | # ========================== |
---|
2900 | n/a | |
---|
2901 | n/a | class TestNamespaceContainsSimple(TestCase): |
---|
2902 | n/a | |
---|
2903 | n/a | def test_empty(self): |
---|
2904 | n/a | ns = argparse.Namespace() |
---|
2905 | n/a | self.assertNotIn('', ns) |
---|
2906 | n/a | self.assertNotIn('x', ns) |
---|
2907 | n/a | |
---|
2908 | n/a | def test_non_empty(self): |
---|
2909 | n/a | ns = argparse.Namespace(x=1, y=2) |
---|
2910 | n/a | self.assertNotIn('', ns) |
---|
2911 | n/a | self.assertIn('x', ns) |
---|
2912 | n/a | self.assertIn('y', ns) |
---|
2913 | n/a | self.assertNotIn('xx', ns) |
---|
2914 | n/a | self.assertNotIn('z', ns) |
---|
2915 | n/a | |
---|
2916 | n/a | # ===================== |
---|
2917 | n/a | # Help formatting tests |
---|
2918 | n/a | # ===================== |
---|
2919 | n/a | |
---|
2920 | n/a | class TestHelpFormattingMetaclass(type): |
---|
2921 | n/a | |
---|
2922 | n/a | def __init__(cls, name, bases, bodydict): |
---|
2923 | n/a | if name == 'HelpTestCase': |
---|
2924 | n/a | return |
---|
2925 | n/a | |
---|
2926 | n/a | class AddTests(object): |
---|
2927 | n/a | |
---|
2928 | n/a | def __init__(self, test_class, func_suffix, std_name): |
---|
2929 | n/a | self.func_suffix = func_suffix |
---|
2930 | n/a | self.std_name = std_name |
---|
2931 | n/a | |
---|
2932 | n/a | for test_func in [self.test_format, |
---|
2933 | n/a | self.test_print, |
---|
2934 | n/a | self.test_print_file]: |
---|
2935 | n/a | test_name = '%s_%s' % (test_func.__name__, func_suffix) |
---|
2936 | n/a | |
---|
2937 | n/a | def test_wrapper(self, test_func=test_func): |
---|
2938 | n/a | test_func(self) |
---|
2939 | n/a | try: |
---|
2940 | n/a | test_wrapper.__name__ = test_name |
---|
2941 | n/a | except TypeError: |
---|
2942 | n/a | pass |
---|
2943 | n/a | setattr(test_class, test_name, test_wrapper) |
---|
2944 | n/a | |
---|
2945 | n/a | def _get_parser(self, tester): |
---|
2946 | n/a | parser = argparse.ArgumentParser( |
---|
2947 | n/a | *tester.parser_signature.args, |
---|
2948 | n/a | **tester.parser_signature.kwargs) |
---|
2949 | n/a | for argument_sig in getattr(tester, 'argument_signatures', []): |
---|
2950 | n/a | parser.add_argument(*argument_sig.args, |
---|
2951 | n/a | **argument_sig.kwargs) |
---|
2952 | n/a | group_sigs = getattr(tester, 'argument_group_signatures', []) |
---|
2953 | n/a | for group_sig, argument_sigs in group_sigs: |
---|
2954 | n/a | group = parser.add_argument_group(*group_sig.args, |
---|
2955 | n/a | **group_sig.kwargs) |
---|
2956 | n/a | for argument_sig in argument_sigs: |
---|
2957 | n/a | group.add_argument(*argument_sig.args, |
---|
2958 | n/a | **argument_sig.kwargs) |
---|
2959 | n/a | subparsers_sigs = getattr(tester, 'subparsers_signatures', []) |
---|
2960 | n/a | if subparsers_sigs: |
---|
2961 | n/a | subparsers = parser.add_subparsers() |
---|
2962 | n/a | for subparser_sig in subparsers_sigs: |
---|
2963 | n/a | subparsers.add_parser(*subparser_sig.args, |
---|
2964 | n/a | **subparser_sig.kwargs) |
---|
2965 | n/a | return parser |
---|
2966 | n/a | |
---|
2967 | n/a | def _test(self, tester, parser_text): |
---|
2968 | n/a | expected_text = getattr(tester, self.func_suffix) |
---|
2969 | n/a | expected_text = textwrap.dedent(expected_text) |
---|
2970 | n/a | tester.assertEqual(expected_text, parser_text) |
---|
2971 | n/a | |
---|
2972 | n/a | def test_format(self, tester): |
---|
2973 | n/a | parser = self._get_parser(tester) |
---|
2974 | n/a | format = getattr(parser, 'format_%s' % self.func_suffix) |
---|
2975 | n/a | self._test(tester, format()) |
---|
2976 | n/a | |
---|
2977 | n/a | def test_print(self, tester): |
---|
2978 | n/a | parser = self._get_parser(tester) |
---|
2979 | n/a | print_ = getattr(parser, 'print_%s' % self.func_suffix) |
---|
2980 | n/a | old_stream = getattr(sys, self.std_name) |
---|
2981 | n/a | setattr(sys, self.std_name, StdIOBuffer()) |
---|
2982 | n/a | try: |
---|
2983 | n/a | print_() |
---|
2984 | n/a | parser_text = getattr(sys, self.std_name).getvalue() |
---|
2985 | n/a | finally: |
---|
2986 | n/a | setattr(sys, self.std_name, old_stream) |
---|
2987 | n/a | self._test(tester, parser_text) |
---|
2988 | n/a | |
---|
2989 | n/a | def test_print_file(self, tester): |
---|
2990 | n/a | parser = self._get_parser(tester) |
---|
2991 | n/a | print_ = getattr(parser, 'print_%s' % self.func_suffix) |
---|
2992 | n/a | sfile = StdIOBuffer() |
---|
2993 | n/a | print_(sfile) |
---|
2994 | n/a | parser_text = sfile.getvalue() |
---|
2995 | n/a | self._test(tester, parser_text) |
---|
2996 | n/a | |
---|
2997 | n/a | # add tests for {format,print}_{usage,help} |
---|
2998 | n/a | for func_suffix, std_name in [('usage', 'stdout'), |
---|
2999 | n/a | ('help', 'stdout')]: |
---|
3000 | n/a | AddTests(cls, func_suffix, std_name) |
---|
3001 | n/a | |
---|
3002 | n/a | bases = TestCase, |
---|
3003 | n/a | HelpTestCase = TestHelpFormattingMetaclass('HelpTestCase', bases, {}) |
---|
3004 | n/a | |
---|
3005 | n/a | |
---|
3006 | n/a | class TestHelpBiggerOptionals(HelpTestCase): |
---|
3007 | n/a | """Make sure that argument help aligns when options are longer""" |
---|
3008 | n/a | |
---|
3009 | n/a | parser_signature = Sig(prog='PROG', description='DESCRIPTION', |
---|
3010 | n/a | epilog='EPILOG') |
---|
3011 | n/a | argument_signatures = [ |
---|
3012 | n/a | Sig('-v', '--version', action='version', version='0.1'), |
---|
3013 | n/a | Sig('-x', action='store_true', help='X HELP'), |
---|
3014 | n/a | Sig('--y', help='Y HELP'), |
---|
3015 | n/a | Sig('foo', help='FOO HELP'), |
---|
3016 | n/a | Sig('bar', help='BAR HELP'), |
---|
3017 | n/a | ] |
---|
3018 | n/a | argument_group_signatures = [] |
---|
3019 | n/a | usage = '''\ |
---|
3020 | n/a | usage: PROG [-h] [-v] [-x] [--y Y] foo bar |
---|
3021 | n/a | ''' |
---|
3022 | n/a | help = usage + '''\ |
---|
3023 | n/a | |
---|
3024 | n/a | DESCRIPTION |
---|
3025 | n/a | |
---|
3026 | n/a | positional arguments: |
---|
3027 | n/a | foo FOO HELP |
---|
3028 | n/a | bar BAR HELP |
---|
3029 | n/a | |
---|
3030 | n/a | optional arguments: |
---|
3031 | n/a | -h, --help show this help message and exit |
---|
3032 | n/a | -v, --version show program's version number and exit |
---|
3033 | n/a | -x X HELP |
---|
3034 | n/a | --y Y Y HELP |
---|
3035 | n/a | |
---|
3036 | n/a | EPILOG |
---|
3037 | n/a | ''' |
---|
3038 | n/a | version = '''\ |
---|
3039 | n/a | 0.1 |
---|
3040 | n/a | ''' |
---|
3041 | n/a | |
---|
3042 | n/a | class TestShortColumns(HelpTestCase): |
---|
3043 | n/a | '''Test extremely small number of columns. |
---|
3044 | n/a | |
---|
3045 | n/a | TestCase prevents "COLUMNS" from being too small in the tests themselves, |
---|
3046 | n/a | but we don't want any exceptions thrown in such cases. Only ugly representation. |
---|
3047 | n/a | ''' |
---|
3048 | n/a | def setUp(self): |
---|
3049 | n/a | env = support.EnvironmentVarGuard() |
---|
3050 | n/a | env.set("COLUMNS", '15') |
---|
3051 | n/a | self.addCleanup(env.__exit__) |
---|
3052 | n/a | |
---|
3053 | n/a | parser_signature = TestHelpBiggerOptionals.parser_signature |
---|
3054 | n/a | argument_signatures = TestHelpBiggerOptionals.argument_signatures |
---|
3055 | n/a | argument_group_signatures = TestHelpBiggerOptionals.argument_group_signatures |
---|
3056 | n/a | usage = '''\ |
---|
3057 | n/a | usage: PROG |
---|
3058 | n/a | [-h] |
---|
3059 | n/a | [-v] |
---|
3060 | n/a | [-x] |
---|
3061 | n/a | [--y Y] |
---|
3062 | n/a | foo |
---|
3063 | n/a | bar |
---|
3064 | n/a | ''' |
---|
3065 | n/a | help = usage + '''\ |
---|
3066 | n/a | |
---|
3067 | n/a | DESCRIPTION |
---|
3068 | n/a | |
---|
3069 | n/a | positional arguments: |
---|
3070 | n/a | foo |
---|
3071 | n/a | FOO HELP |
---|
3072 | n/a | bar |
---|
3073 | n/a | BAR HELP |
---|
3074 | n/a | |
---|
3075 | n/a | optional arguments: |
---|
3076 | n/a | -h, --help |
---|
3077 | n/a | show this |
---|
3078 | n/a | help |
---|
3079 | n/a | message and |
---|
3080 | n/a | exit |
---|
3081 | n/a | -v, --version |
---|
3082 | n/a | show |
---|
3083 | n/a | program's |
---|
3084 | n/a | version |
---|
3085 | n/a | number and |
---|
3086 | n/a | exit |
---|
3087 | n/a | -x |
---|
3088 | n/a | X HELP |
---|
3089 | n/a | --y Y |
---|
3090 | n/a | Y HELP |
---|
3091 | n/a | |
---|
3092 | n/a | EPILOG |
---|
3093 | n/a | ''' |
---|
3094 | n/a | version = TestHelpBiggerOptionals.version |
---|
3095 | n/a | |
---|
3096 | n/a | |
---|
3097 | n/a | class TestHelpBiggerOptionalGroups(HelpTestCase): |
---|
3098 | n/a | """Make sure that argument help aligns when options are longer""" |
---|
3099 | n/a | |
---|
3100 | n/a | parser_signature = Sig(prog='PROG', description='DESCRIPTION', |
---|
3101 | n/a | epilog='EPILOG') |
---|
3102 | n/a | argument_signatures = [ |
---|
3103 | n/a | Sig('-v', '--version', action='version', version='0.1'), |
---|
3104 | n/a | Sig('-x', action='store_true', help='X HELP'), |
---|
3105 | n/a | Sig('--y', help='Y HELP'), |
---|
3106 | n/a | Sig('foo', help='FOO HELP'), |
---|
3107 | n/a | Sig('bar', help='BAR HELP'), |
---|
3108 | n/a | ] |
---|
3109 | n/a | argument_group_signatures = [ |
---|
3110 | n/a | (Sig('GROUP TITLE', description='GROUP DESCRIPTION'), [ |
---|
3111 | n/a | Sig('baz', help='BAZ HELP'), |
---|
3112 | n/a | Sig('-z', nargs='+', help='Z HELP')]), |
---|
3113 | n/a | ] |
---|
3114 | n/a | usage = '''\ |
---|
3115 | n/a | usage: PROG [-h] [-v] [-x] [--y Y] [-z Z [Z ...]] foo bar baz |
---|
3116 | n/a | ''' |
---|
3117 | n/a | help = usage + '''\ |
---|
3118 | n/a | |
---|
3119 | n/a | DESCRIPTION |
---|
3120 | n/a | |
---|
3121 | n/a | positional arguments: |
---|
3122 | n/a | foo FOO HELP |
---|
3123 | n/a | bar BAR HELP |
---|
3124 | n/a | |
---|
3125 | n/a | optional arguments: |
---|
3126 | n/a | -h, --help show this help message and exit |
---|
3127 | n/a | -v, --version show program's version number and exit |
---|
3128 | n/a | -x X HELP |
---|
3129 | n/a | --y Y Y HELP |
---|
3130 | n/a | |
---|
3131 | n/a | GROUP TITLE: |
---|
3132 | n/a | GROUP DESCRIPTION |
---|
3133 | n/a | |
---|
3134 | n/a | baz BAZ HELP |
---|
3135 | n/a | -z Z [Z ...] Z HELP |
---|
3136 | n/a | |
---|
3137 | n/a | EPILOG |
---|
3138 | n/a | ''' |
---|
3139 | n/a | version = '''\ |
---|
3140 | n/a | 0.1 |
---|
3141 | n/a | ''' |
---|
3142 | n/a | |
---|
3143 | n/a | |
---|
3144 | n/a | class TestHelpBiggerPositionals(HelpTestCase): |
---|
3145 | n/a | """Make sure that help aligns when arguments are longer""" |
---|
3146 | n/a | |
---|
3147 | n/a | parser_signature = Sig(usage='USAGE', description='DESCRIPTION') |
---|
3148 | n/a | argument_signatures = [ |
---|
3149 | n/a | Sig('-x', action='store_true', help='X HELP'), |
---|
3150 | n/a | Sig('--y', help='Y HELP'), |
---|
3151 | n/a | Sig('ekiekiekifekang', help='EKI HELP'), |
---|
3152 | n/a | Sig('bar', help='BAR HELP'), |
---|
3153 | n/a | ] |
---|
3154 | n/a | argument_group_signatures = [] |
---|
3155 | n/a | usage = '''\ |
---|
3156 | n/a | usage: USAGE |
---|
3157 | n/a | ''' |
---|
3158 | n/a | help = usage + '''\ |
---|
3159 | n/a | |
---|
3160 | n/a | DESCRIPTION |
---|
3161 | n/a | |
---|
3162 | n/a | positional arguments: |
---|
3163 | n/a | ekiekiekifekang EKI HELP |
---|
3164 | n/a | bar BAR HELP |
---|
3165 | n/a | |
---|
3166 | n/a | optional arguments: |
---|
3167 | n/a | -h, --help show this help message and exit |
---|
3168 | n/a | -x X HELP |
---|
3169 | n/a | --y Y Y HELP |
---|
3170 | n/a | ''' |
---|
3171 | n/a | |
---|
3172 | n/a | version = '' |
---|
3173 | n/a | |
---|
3174 | n/a | |
---|
3175 | n/a | class TestHelpReformatting(HelpTestCase): |
---|
3176 | n/a | """Make sure that text after short names starts on the first line""" |
---|
3177 | n/a | |
---|
3178 | n/a | parser_signature = Sig( |
---|
3179 | n/a | prog='PROG', |
---|
3180 | n/a | description=' oddly formatted\n' |
---|
3181 | n/a | 'description\n' |
---|
3182 | n/a | '\n' |
---|
3183 | n/a | 'that is so long that it should go onto multiple ' |
---|
3184 | n/a | 'lines when wrapped') |
---|
3185 | n/a | argument_signatures = [ |
---|
3186 | n/a | Sig('-x', metavar='XX', help='oddly\n' |
---|
3187 | n/a | ' formatted -x help'), |
---|
3188 | n/a | Sig('y', metavar='yyy', help='normal y help'), |
---|
3189 | n/a | ] |
---|
3190 | n/a | argument_group_signatures = [ |
---|
3191 | n/a | (Sig('title', description='\n' |
---|
3192 | n/a | ' oddly formatted group\n' |
---|
3193 | n/a | '\n' |
---|
3194 | n/a | 'description'), |
---|
3195 | n/a | [Sig('-a', action='store_true', |
---|
3196 | n/a | help=' oddly \n' |
---|
3197 | n/a | 'formatted -a help \n' |
---|
3198 | n/a | ' again, so long that it should be wrapped over ' |
---|
3199 | n/a | 'multiple lines')]), |
---|
3200 | n/a | ] |
---|
3201 | n/a | usage = '''\ |
---|
3202 | n/a | usage: PROG [-h] [-x XX] [-a] yyy |
---|
3203 | n/a | ''' |
---|
3204 | n/a | help = usage + '''\ |
---|
3205 | n/a | |
---|
3206 | n/a | oddly formatted description that is so long that it should go onto \ |
---|
3207 | n/a | multiple |
---|
3208 | n/a | lines when wrapped |
---|
3209 | n/a | |
---|
3210 | n/a | positional arguments: |
---|
3211 | n/a | yyy normal y help |
---|
3212 | n/a | |
---|
3213 | n/a | optional arguments: |
---|
3214 | n/a | -h, --help show this help message and exit |
---|
3215 | n/a | -x XX oddly formatted -x help |
---|
3216 | n/a | |
---|
3217 | n/a | title: |
---|
3218 | n/a | oddly formatted group description |
---|
3219 | n/a | |
---|
3220 | n/a | -a oddly formatted -a help again, so long that it should \ |
---|
3221 | n/a | be wrapped |
---|
3222 | n/a | over multiple lines |
---|
3223 | n/a | ''' |
---|
3224 | n/a | version = '' |
---|
3225 | n/a | |
---|
3226 | n/a | |
---|
3227 | n/a | class TestHelpWrappingShortNames(HelpTestCase): |
---|
3228 | n/a | """Make sure that text after short names starts on the first line""" |
---|
3229 | n/a | |
---|
3230 | n/a | parser_signature = Sig(prog='PROG', description= 'D\nD' * 30) |
---|
3231 | n/a | argument_signatures = [ |
---|
3232 | n/a | Sig('-x', metavar='XX', help='XHH HX' * 20), |
---|
3233 | n/a | Sig('y', metavar='yyy', help='YH YH' * 20), |
---|
3234 | n/a | ] |
---|
3235 | n/a | argument_group_signatures = [ |
---|
3236 | n/a | (Sig('ALPHAS'), [ |
---|
3237 | n/a | Sig('-a', action='store_true', help='AHHH HHA' * 10)]), |
---|
3238 | n/a | ] |
---|
3239 | n/a | usage = '''\ |
---|
3240 | n/a | usage: PROG [-h] [-x XX] [-a] yyy |
---|
3241 | n/a | ''' |
---|
3242 | n/a | help = usage + '''\ |
---|
3243 | n/a | |
---|
3244 | n/a | D DD DD DD DD DD DD DD DD DD DD DD DD DD DD DD DD DD DD DD DD DD DD \ |
---|
3245 | n/a | DD DD DD |
---|
3246 | n/a | DD DD DD DD D |
---|
3247 | n/a | |
---|
3248 | n/a | positional arguments: |
---|
3249 | n/a | yyy YH YHYH YHYH YHYH YHYH YHYH YHYH YHYH YHYH YHYH YHYH \ |
---|
3250 | n/a | YHYH YHYH |
---|
3251 | n/a | YHYH YHYH YHYH YHYH YHYH YHYH YHYH YH |
---|
3252 | n/a | |
---|
3253 | n/a | optional arguments: |
---|
3254 | n/a | -h, --help show this help message and exit |
---|
3255 | n/a | -x XX XHH HXXHH HXXHH HXXHH HXXHH HXXHH HXXHH HXXHH HXXHH \ |
---|
3256 | n/a | HXXHH HXXHH |
---|
3257 | n/a | HXXHH HXXHH HXXHH HXXHH HXXHH HXXHH HXXHH HXXHH HXXHH HX |
---|
3258 | n/a | |
---|
3259 | n/a | ALPHAS: |
---|
3260 | n/a | -a AHHH HHAAHHH HHAAHHH HHAAHHH HHAAHHH HHAAHHH HHAAHHH \ |
---|
3261 | n/a | HHAAHHH |
---|
3262 | n/a | HHAAHHH HHAAHHH HHA |
---|
3263 | n/a | ''' |
---|
3264 | n/a | version = '' |
---|
3265 | n/a | |
---|
3266 | n/a | |
---|
3267 | n/a | class TestHelpWrappingLongNames(HelpTestCase): |
---|
3268 | n/a | """Make sure that text after long names starts on the next line""" |
---|
3269 | n/a | |
---|
3270 | n/a | parser_signature = Sig(usage='USAGE', description= 'D D' * 30) |
---|
3271 | n/a | argument_signatures = [ |
---|
3272 | n/a | Sig('-v', '--version', action='version', version='V V' * 30), |
---|
3273 | n/a | Sig('-x', metavar='X' * 25, help='XH XH' * 20), |
---|
3274 | n/a | Sig('y', metavar='y' * 25, help='YH YH' * 20), |
---|
3275 | n/a | ] |
---|
3276 | n/a | argument_group_signatures = [ |
---|
3277 | n/a | (Sig('ALPHAS'), [ |
---|
3278 | n/a | Sig('-a', metavar='A' * 25, help='AH AH' * 20), |
---|
3279 | n/a | Sig('z', metavar='z' * 25, help='ZH ZH' * 20)]), |
---|
3280 | n/a | ] |
---|
3281 | n/a | usage = '''\ |
---|
3282 | n/a | usage: USAGE |
---|
3283 | n/a | ''' |
---|
3284 | n/a | help = usage + '''\ |
---|
3285 | n/a | |
---|
3286 | n/a | D DD DD DD DD DD DD DD DD DD DD DD DD DD DD DD DD DD DD DD DD DD DD \ |
---|
3287 | n/a | DD DD DD |
---|
3288 | n/a | DD DD DD DD D |
---|
3289 | n/a | |
---|
3290 | n/a | positional arguments: |
---|
3291 | n/a | yyyyyyyyyyyyyyyyyyyyyyyyy |
---|
3292 | n/a | YH YHYH YHYH YHYH YHYH YHYH YHYH YHYH YHYH \ |
---|
3293 | n/a | YHYH YHYH |
---|
3294 | n/a | YHYH YHYH YHYH YHYH YHYH YHYH YHYH YHYH YHYH YH |
---|
3295 | n/a | |
---|
3296 | n/a | optional arguments: |
---|
3297 | n/a | -h, --help show this help message and exit |
---|
3298 | n/a | -v, --version show program's version number and exit |
---|
3299 | n/a | -x XXXXXXXXXXXXXXXXXXXXXXXXX |
---|
3300 | n/a | XH XHXH XHXH XHXH XHXH XHXH XHXH XHXH XHXH \ |
---|
3301 | n/a | XHXH XHXH |
---|
3302 | n/a | XHXH XHXH XHXH XHXH XHXH XHXH XHXH XHXH XHXH XH |
---|
3303 | n/a | |
---|
3304 | n/a | ALPHAS: |
---|
3305 | n/a | -a AAAAAAAAAAAAAAAAAAAAAAAAA |
---|
3306 | n/a | AH AHAH AHAH AHAH AHAH AHAH AHAH AHAH AHAH \ |
---|
3307 | n/a | AHAH AHAH |
---|
3308 | n/a | AHAH AHAH AHAH AHAH AHAH AHAH AHAH AHAH AHAH AH |
---|
3309 | n/a | zzzzzzzzzzzzzzzzzzzzzzzzz |
---|
3310 | n/a | ZH ZHZH ZHZH ZHZH ZHZH ZHZH ZHZH ZHZH ZHZH \ |
---|
3311 | n/a | ZHZH ZHZH |
---|
3312 | n/a | ZHZH ZHZH ZHZH ZHZH ZHZH ZHZH ZHZH ZHZH ZHZH ZH |
---|
3313 | n/a | ''' |
---|
3314 | n/a | version = '''\ |
---|
3315 | n/a | V VV VV VV VV VV VV VV VV VV VV VV VV VV VV VV VV VV VV VV VV VV VV \ |
---|
3316 | n/a | VV VV VV |
---|
3317 | n/a | VV VV VV VV V |
---|
3318 | n/a | ''' |
---|
3319 | n/a | |
---|
3320 | n/a | |
---|
3321 | n/a | class TestHelpUsage(HelpTestCase): |
---|
3322 | n/a | """Test basic usage messages""" |
---|
3323 | n/a | |
---|
3324 | n/a | parser_signature = Sig(prog='PROG') |
---|
3325 | n/a | argument_signatures = [ |
---|
3326 | n/a | Sig('-w', nargs='+', help='w'), |
---|
3327 | n/a | Sig('-x', nargs='*', help='x'), |
---|
3328 | n/a | Sig('a', help='a'), |
---|
3329 | n/a | Sig('b', help='b', nargs=2), |
---|
3330 | n/a | Sig('c', help='c', nargs='?'), |
---|
3331 | n/a | ] |
---|
3332 | n/a | argument_group_signatures = [ |
---|
3333 | n/a | (Sig('group'), [ |
---|
3334 | n/a | Sig('-y', nargs='?', help='y'), |
---|
3335 | n/a | Sig('-z', nargs=3, help='z'), |
---|
3336 | n/a | Sig('d', help='d', nargs='*'), |
---|
3337 | n/a | Sig('e', help='e', nargs='+'), |
---|
3338 | n/a | ]) |
---|
3339 | n/a | ] |
---|
3340 | n/a | usage = '''\ |
---|
3341 | n/a | usage: PROG [-h] [-w W [W ...]] [-x [X [X ...]]] [-y [Y]] [-z Z Z Z] |
---|
3342 | n/a | a b b [c] [d [d ...]] e [e ...] |
---|
3343 | n/a | ''' |
---|
3344 | n/a | help = usage + '''\ |
---|
3345 | n/a | |
---|
3346 | n/a | positional arguments: |
---|
3347 | n/a | a a |
---|
3348 | n/a | b b |
---|
3349 | n/a | c c |
---|
3350 | n/a | |
---|
3351 | n/a | optional arguments: |
---|
3352 | n/a | -h, --help show this help message and exit |
---|
3353 | n/a | -w W [W ...] w |
---|
3354 | n/a | -x [X [X ...]] x |
---|
3355 | n/a | |
---|
3356 | n/a | group: |
---|
3357 | n/a | -y [Y] y |
---|
3358 | n/a | -z Z Z Z z |
---|
3359 | n/a | d d |
---|
3360 | n/a | e e |
---|
3361 | n/a | ''' |
---|
3362 | n/a | version = '' |
---|
3363 | n/a | |
---|
3364 | n/a | |
---|
3365 | n/a | class TestHelpOnlyUserGroups(HelpTestCase): |
---|
3366 | n/a | """Test basic usage messages""" |
---|
3367 | n/a | |
---|
3368 | n/a | parser_signature = Sig(prog='PROG', add_help=False) |
---|
3369 | n/a | argument_signatures = [] |
---|
3370 | n/a | argument_group_signatures = [ |
---|
3371 | n/a | (Sig('xxxx'), [ |
---|
3372 | n/a | Sig('-x', help='x'), |
---|
3373 | n/a | Sig('a', help='a'), |
---|
3374 | n/a | ]), |
---|
3375 | n/a | (Sig('yyyy'), [ |
---|
3376 | n/a | Sig('b', help='b'), |
---|
3377 | n/a | Sig('-y', help='y'), |
---|
3378 | n/a | ]), |
---|
3379 | n/a | ] |
---|
3380 | n/a | usage = '''\ |
---|
3381 | n/a | usage: PROG [-x X] [-y Y] a b |
---|
3382 | n/a | ''' |
---|
3383 | n/a | help = usage + '''\ |
---|
3384 | n/a | |
---|
3385 | n/a | xxxx: |
---|
3386 | n/a | -x X x |
---|
3387 | n/a | a a |
---|
3388 | n/a | |
---|
3389 | n/a | yyyy: |
---|
3390 | n/a | b b |
---|
3391 | n/a | -y Y y |
---|
3392 | n/a | ''' |
---|
3393 | n/a | version = '' |
---|
3394 | n/a | |
---|
3395 | n/a | |
---|
3396 | n/a | class TestHelpUsageLongProg(HelpTestCase): |
---|
3397 | n/a | """Test usage messages where the prog is long""" |
---|
3398 | n/a | |
---|
3399 | n/a | parser_signature = Sig(prog='P' * 60) |
---|
3400 | n/a | argument_signatures = [ |
---|
3401 | n/a | Sig('-w', metavar='W'), |
---|
3402 | n/a | Sig('-x', metavar='X'), |
---|
3403 | n/a | Sig('a'), |
---|
3404 | n/a | Sig('b'), |
---|
3405 | n/a | ] |
---|
3406 | n/a | argument_group_signatures = [] |
---|
3407 | n/a | usage = '''\ |
---|
3408 | n/a | usage: PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP |
---|
3409 | n/a | [-h] [-w W] [-x X] a b |
---|
3410 | n/a | ''' |
---|
3411 | n/a | help = usage + '''\ |
---|
3412 | n/a | |
---|
3413 | n/a | positional arguments: |
---|
3414 | n/a | a |
---|
3415 | n/a | b |
---|
3416 | n/a | |
---|
3417 | n/a | optional arguments: |
---|
3418 | n/a | -h, --help show this help message and exit |
---|
3419 | n/a | -w W |
---|
3420 | n/a | -x X |
---|
3421 | n/a | ''' |
---|
3422 | n/a | version = '' |
---|
3423 | n/a | |
---|
3424 | n/a | |
---|
3425 | n/a | class TestHelpUsageLongProgOptionsWrap(HelpTestCase): |
---|
3426 | n/a | """Test usage messages where the prog is long and the optionals wrap""" |
---|
3427 | n/a | |
---|
3428 | n/a | parser_signature = Sig(prog='P' * 60) |
---|
3429 | n/a | argument_signatures = [ |
---|
3430 | n/a | Sig('-w', metavar='W' * 25), |
---|
3431 | n/a | Sig('-x', metavar='X' * 25), |
---|
3432 | n/a | Sig('-y', metavar='Y' * 25), |
---|
3433 | n/a | Sig('-z', metavar='Z' * 25), |
---|
3434 | n/a | Sig('a'), |
---|
3435 | n/a | Sig('b'), |
---|
3436 | n/a | ] |
---|
3437 | n/a | argument_group_signatures = [] |
---|
3438 | n/a | usage = '''\ |
---|
3439 | n/a | usage: PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP |
---|
3440 | n/a | [-h] [-w WWWWWWWWWWWWWWWWWWWWWWWWW] \ |
---|
3441 | n/a | [-x XXXXXXXXXXXXXXXXXXXXXXXXX] |
---|
3442 | n/a | [-y YYYYYYYYYYYYYYYYYYYYYYYYY] [-z ZZZZZZZZZZZZZZZZZZZZZZZZZ] |
---|
3443 | n/a | a b |
---|
3444 | n/a | ''' |
---|
3445 | n/a | help = usage + '''\ |
---|
3446 | n/a | |
---|
3447 | n/a | positional arguments: |
---|
3448 | n/a | a |
---|
3449 | n/a | b |
---|
3450 | n/a | |
---|
3451 | n/a | optional arguments: |
---|
3452 | n/a | -h, --help show this help message and exit |
---|
3453 | n/a | -w WWWWWWWWWWWWWWWWWWWWWWWWW |
---|
3454 | n/a | -x XXXXXXXXXXXXXXXXXXXXXXXXX |
---|
3455 | n/a | -y YYYYYYYYYYYYYYYYYYYYYYYYY |
---|
3456 | n/a | -z ZZZZZZZZZZZZZZZZZZZZZZZZZ |
---|
3457 | n/a | ''' |
---|
3458 | n/a | version = '' |
---|
3459 | n/a | |
---|
3460 | n/a | |
---|
3461 | n/a | class TestHelpUsageLongProgPositionalsWrap(HelpTestCase): |
---|
3462 | n/a | """Test usage messages where the prog is long and the positionals wrap""" |
---|
3463 | n/a | |
---|
3464 | n/a | parser_signature = Sig(prog='P' * 60, add_help=False) |
---|
3465 | n/a | argument_signatures = [ |
---|
3466 | n/a | Sig('a' * 25), |
---|
3467 | n/a | Sig('b' * 25), |
---|
3468 | n/a | Sig('c' * 25), |
---|
3469 | n/a | ] |
---|
3470 | n/a | argument_group_signatures = [] |
---|
3471 | n/a | usage = '''\ |
---|
3472 | n/a | usage: PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP |
---|
3473 | n/a | aaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbb |
---|
3474 | n/a | ccccccccccccccccccccccccc |
---|
3475 | n/a | ''' |
---|
3476 | n/a | help = usage + '''\ |
---|
3477 | n/a | |
---|
3478 | n/a | positional arguments: |
---|
3479 | n/a | aaaaaaaaaaaaaaaaaaaaaaaaa |
---|
3480 | n/a | bbbbbbbbbbbbbbbbbbbbbbbbb |
---|
3481 | n/a | ccccccccccccccccccccccccc |
---|
3482 | n/a | ''' |
---|
3483 | n/a | version = '' |
---|
3484 | n/a | |
---|
3485 | n/a | |
---|
3486 | n/a | class TestHelpUsageOptionalsWrap(HelpTestCase): |
---|
3487 | n/a | """Test usage messages where the optionals wrap""" |
---|
3488 | n/a | |
---|
3489 | n/a | parser_signature = Sig(prog='PROG') |
---|
3490 | n/a | argument_signatures = [ |
---|
3491 | n/a | Sig('-w', metavar='W' * 25), |
---|
3492 | n/a | Sig('-x', metavar='X' * 25), |
---|
3493 | n/a | Sig('-y', metavar='Y' * 25), |
---|
3494 | n/a | Sig('-z', metavar='Z' * 25), |
---|
3495 | n/a | Sig('a'), |
---|
3496 | n/a | Sig('b'), |
---|
3497 | n/a | Sig('c'), |
---|
3498 | n/a | ] |
---|
3499 | n/a | argument_group_signatures = [] |
---|
3500 | n/a | usage = '''\ |
---|
3501 | n/a | usage: PROG [-h] [-w WWWWWWWWWWWWWWWWWWWWWWWWW] \ |
---|
3502 | n/a | [-x XXXXXXXXXXXXXXXXXXXXXXXXX] |
---|
3503 | n/a | [-y YYYYYYYYYYYYYYYYYYYYYYYYY] \ |
---|
3504 | n/a | [-z ZZZZZZZZZZZZZZZZZZZZZZZZZ] |
---|
3505 | n/a | a b c |
---|
3506 | n/a | ''' |
---|
3507 | n/a | help = usage + '''\ |
---|
3508 | n/a | |
---|
3509 | n/a | positional arguments: |
---|
3510 | n/a | a |
---|
3511 | n/a | b |
---|
3512 | n/a | c |
---|
3513 | n/a | |
---|
3514 | n/a | optional arguments: |
---|
3515 | n/a | -h, --help show this help message and exit |
---|
3516 | n/a | -w WWWWWWWWWWWWWWWWWWWWWWWWW |
---|
3517 | n/a | -x XXXXXXXXXXXXXXXXXXXXXXXXX |
---|
3518 | n/a | -y YYYYYYYYYYYYYYYYYYYYYYYYY |
---|
3519 | n/a | -z ZZZZZZZZZZZZZZZZZZZZZZZZZ |
---|
3520 | n/a | ''' |
---|
3521 | n/a | version = '' |
---|
3522 | n/a | |
---|
3523 | n/a | |
---|
3524 | n/a | class TestHelpUsagePositionalsWrap(HelpTestCase): |
---|
3525 | n/a | """Test usage messages where the positionals wrap""" |
---|
3526 | n/a | |
---|
3527 | n/a | parser_signature = Sig(prog='PROG') |
---|
3528 | n/a | argument_signatures = [ |
---|
3529 | n/a | Sig('-x'), |
---|
3530 | n/a | Sig('-y'), |
---|
3531 | n/a | Sig('-z'), |
---|
3532 | n/a | Sig('a' * 25), |
---|
3533 | n/a | Sig('b' * 25), |
---|
3534 | n/a | Sig('c' * 25), |
---|
3535 | n/a | ] |
---|
3536 | n/a | argument_group_signatures = [] |
---|
3537 | n/a | usage = '''\ |
---|
3538 | n/a | usage: PROG [-h] [-x X] [-y Y] [-z Z] |
---|
3539 | n/a | aaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbb |
---|
3540 | n/a | ccccccccccccccccccccccccc |
---|
3541 | n/a | ''' |
---|
3542 | n/a | help = usage + '''\ |
---|
3543 | n/a | |
---|
3544 | n/a | positional arguments: |
---|
3545 | n/a | aaaaaaaaaaaaaaaaaaaaaaaaa |
---|
3546 | n/a | bbbbbbbbbbbbbbbbbbbbbbbbb |
---|
3547 | n/a | ccccccccccccccccccccccccc |
---|
3548 | n/a | |
---|
3549 | n/a | optional arguments: |
---|
3550 | n/a | -h, --help show this help message and exit |
---|
3551 | n/a | -x X |
---|
3552 | n/a | -y Y |
---|
3553 | n/a | -z Z |
---|
3554 | n/a | ''' |
---|
3555 | n/a | version = '' |
---|
3556 | n/a | |
---|
3557 | n/a | |
---|
3558 | n/a | class TestHelpUsageOptionalsPositionalsWrap(HelpTestCase): |
---|
3559 | n/a | """Test usage messages where the optionals and positionals wrap""" |
---|
3560 | n/a | |
---|
3561 | n/a | parser_signature = Sig(prog='PROG') |
---|
3562 | n/a | argument_signatures = [ |
---|
3563 | n/a | Sig('-x', metavar='X' * 25), |
---|
3564 | n/a | Sig('-y', metavar='Y' * 25), |
---|
3565 | n/a | Sig('-z', metavar='Z' * 25), |
---|
3566 | n/a | Sig('a' * 25), |
---|
3567 | n/a | Sig('b' * 25), |
---|
3568 | n/a | Sig('c' * 25), |
---|
3569 | n/a | ] |
---|
3570 | n/a | argument_group_signatures = [] |
---|
3571 | n/a | usage = '''\ |
---|
3572 | n/a | usage: PROG [-h] [-x XXXXXXXXXXXXXXXXXXXXXXXXX] \ |
---|
3573 | n/a | [-y YYYYYYYYYYYYYYYYYYYYYYYYY] |
---|
3574 | n/a | [-z ZZZZZZZZZZZZZZZZZZZZZZZZZ] |
---|
3575 | n/a | aaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbb |
---|
3576 | n/a | ccccccccccccccccccccccccc |
---|
3577 | n/a | ''' |
---|
3578 | n/a | help = usage + '''\ |
---|
3579 | n/a | |
---|
3580 | n/a | positional arguments: |
---|
3581 | n/a | aaaaaaaaaaaaaaaaaaaaaaaaa |
---|
3582 | n/a | bbbbbbbbbbbbbbbbbbbbbbbbb |
---|
3583 | n/a | ccccccccccccccccccccccccc |
---|
3584 | n/a | |
---|
3585 | n/a | optional arguments: |
---|
3586 | n/a | -h, --help show this help message and exit |
---|
3587 | n/a | -x XXXXXXXXXXXXXXXXXXXXXXXXX |
---|
3588 | n/a | -y YYYYYYYYYYYYYYYYYYYYYYYYY |
---|
3589 | n/a | -z ZZZZZZZZZZZZZZZZZZZZZZZZZ |
---|
3590 | n/a | ''' |
---|
3591 | n/a | version = '' |
---|
3592 | n/a | |
---|
3593 | n/a | |
---|
3594 | n/a | class TestHelpUsageOptionalsOnlyWrap(HelpTestCase): |
---|
3595 | n/a | """Test usage messages where there are only optionals and they wrap""" |
---|
3596 | n/a | |
---|
3597 | n/a | parser_signature = Sig(prog='PROG') |
---|
3598 | n/a | argument_signatures = [ |
---|
3599 | n/a | Sig('-x', metavar='X' * 25), |
---|
3600 | n/a | Sig('-y', metavar='Y' * 25), |
---|
3601 | n/a | Sig('-z', metavar='Z' * 25), |
---|
3602 | n/a | ] |
---|
3603 | n/a | argument_group_signatures = [] |
---|
3604 | n/a | usage = '''\ |
---|
3605 | n/a | usage: PROG [-h] [-x XXXXXXXXXXXXXXXXXXXXXXXXX] \ |
---|
3606 | n/a | [-y YYYYYYYYYYYYYYYYYYYYYYYYY] |
---|
3607 | n/a | [-z ZZZZZZZZZZZZZZZZZZZZZZZZZ] |
---|
3608 | n/a | ''' |
---|
3609 | n/a | help = usage + '''\ |
---|
3610 | n/a | |
---|
3611 | n/a | optional arguments: |
---|
3612 | n/a | -h, --help show this help message and exit |
---|
3613 | n/a | -x XXXXXXXXXXXXXXXXXXXXXXXXX |
---|
3614 | n/a | -y YYYYYYYYYYYYYYYYYYYYYYYYY |
---|
3615 | n/a | -z ZZZZZZZZZZZZZZZZZZZZZZZZZ |
---|
3616 | n/a | ''' |
---|
3617 | n/a | version = '' |
---|
3618 | n/a | |
---|
3619 | n/a | |
---|
3620 | n/a | class TestHelpUsagePositionalsOnlyWrap(HelpTestCase): |
---|
3621 | n/a | """Test usage messages where there are only positionals and they wrap""" |
---|
3622 | n/a | |
---|
3623 | n/a | parser_signature = Sig(prog='PROG', add_help=False) |
---|
3624 | n/a | argument_signatures = [ |
---|
3625 | n/a | Sig('a' * 25), |
---|
3626 | n/a | Sig('b' * 25), |
---|
3627 | n/a | Sig('c' * 25), |
---|
3628 | n/a | ] |
---|
3629 | n/a | argument_group_signatures = [] |
---|
3630 | n/a | usage = '''\ |
---|
3631 | n/a | usage: PROG aaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbb |
---|
3632 | n/a | ccccccccccccccccccccccccc |
---|
3633 | n/a | ''' |
---|
3634 | n/a | help = usage + '''\ |
---|
3635 | n/a | |
---|
3636 | n/a | positional arguments: |
---|
3637 | n/a | aaaaaaaaaaaaaaaaaaaaaaaaa |
---|
3638 | n/a | bbbbbbbbbbbbbbbbbbbbbbbbb |
---|
3639 | n/a | ccccccccccccccccccccccccc |
---|
3640 | n/a | ''' |
---|
3641 | n/a | version = '' |
---|
3642 | n/a | |
---|
3643 | n/a | |
---|
3644 | n/a | class TestHelpVariableExpansion(HelpTestCase): |
---|
3645 | n/a | """Test that variables are expanded properly in help messages""" |
---|
3646 | n/a | |
---|
3647 | n/a | parser_signature = Sig(prog='PROG') |
---|
3648 | n/a | argument_signatures = [ |
---|
3649 | n/a | Sig('-x', type=int, |
---|
3650 | n/a | help='x %(prog)s %(default)s %(type)s %%'), |
---|
3651 | n/a | Sig('-y', action='store_const', default=42, const='XXX', |
---|
3652 | n/a | help='y %(prog)s %(default)s %(const)s'), |
---|
3653 | n/a | Sig('--foo', choices='abc', |
---|
3654 | n/a | help='foo %(prog)s %(default)s %(choices)s'), |
---|
3655 | n/a | Sig('--bar', default='baz', choices=[1, 2], metavar='BBB', |
---|
3656 | n/a | help='bar %(prog)s %(default)s %(dest)s'), |
---|
3657 | n/a | Sig('spam', help='spam %(prog)s %(default)s'), |
---|
3658 | n/a | Sig('badger', default=0.5, help='badger %(prog)s %(default)s'), |
---|
3659 | n/a | ] |
---|
3660 | n/a | argument_group_signatures = [ |
---|
3661 | n/a | (Sig('group'), [ |
---|
3662 | n/a | Sig('-a', help='a %(prog)s %(default)s'), |
---|
3663 | n/a | Sig('-b', default=-1, help='b %(prog)s %(default)s'), |
---|
3664 | n/a | ]) |
---|
3665 | n/a | ] |
---|
3666 | n/a | usage = ('''\ |
---|
3667 | n/a | usage: PROG [-h] [-x X] [-y] [--foo {a,b,c}] [--bar BBB] [-a A] [-b B] |
---|
3668 | n/a | spam badger |
---|
3669 | n/a | ''') |
---|
3670 | n/a | help = usage + '''\ |
---|
3671 | n/a | |
---|
3672 | n/a | positional arguments: |
---|
3673 | n/a | spam spam PROG None |
---|
3674 | n/a | badger badger PROG 0.5 |
---|
3675 | n/a | |
---|
3676 | n/a | optional arguments: |
---|
3677 | n/a | -h, --help show this help message and exit |
---|
3678 | n/a | -x X x PROG None int % |
---|
3679 | n/a | -y y PROG 42 XXX |
---|
3680 | n/a | --foo {a,b,c} foo PROG None a, b, c |
---|
3681 | n/a | --bar BBB bar PROG baz bar |
---|
3682 | n/a | |
---|
3683 | n/a | group: |
---|
3684 | n/a | -a A a PROG None |
---|
3685 | n/a | -b B b PROG -1 |
---|
3686 | n/a | ''' |
---|
3687 | n/a | version = '' |
---|
3688 | n/a | |
---|
3689 | n/a | |
---|
3690 | n/a | class TestHelpVariableExpansionUsageSupplied(HelpTestCase): |
---|
3691 | n/a | """Test that variables are expanded properly when usage= is present""" |
---|
3692 | n/a | |
---|
3693 | n/a | parser_signature = Sig(prog='PROG', usage='%(prog)s FOO') |
---|
3694 | n/a | argument_signatures = [] |
---|
3695 | n/a | argument_group_signatures = [] |
---|
3696 | n/a | usage = ('''\ |
---|
3697 | n/a | usage: PROG FOO |
---|
3698 | n/a | ''') |
---|
3699 | n/a | help = usage + '''\ |
---|
3700 | n/a | |
---|
3701 | n/a | optional arguments: |
---|
3702 | n/a | -h, --help show this help message and exit |
---|
3703 | n/a | ''' |
---|
3704 | n/a | version = '' |
---|
3705 | n/a | |
---|
3706 | n/a | |
---|
3707 | n/a | class TestHelpVariableExpansionNoArguments(HelpTestCase): |
---|
3708 | n/a | """Test that variables are expanded properly with no arguments""" |
---|
3709 | n/a | |
---|
3710 | n/a | parser_signature = Sig(prog='PROG', add_help=False) |
---|
3711 | n/a | argument_signatures = [] |
---|
3712 | n/a | argument_group_signatures = [] |
---|
3713 | n/a | usage = ('''\ |
---|
3714 | n/a | usage: PROG |
---|
3715 | n/a | ''') |
---|
3716 | n/a | help = usage |
---|
3717 | n/a | version = '' |
---|
3718 | n/a | |
---|
3719 | n/a | |
---|
3720 | n/a | class TestHelpSuppressUsage(HelpTestCase): |
---|
3721 | n/a | """Test that items can be suppressed in usage messages""" |
---|
3722 | n/a | |
---|
3723 | n/a | parser_signature = Sig(prog='PROG', usage=argparse.SUPPRESS) |
---|
3724 | n/a | argument_signatures = [ |
---|
3725 | n/a | Sig('--foo', help='foo help'), |
---|
3726 | n/a | Sig('spam', help='spam help'), |
---|
3727 | n/a | ] |
---|
3728 | n/a | argument_group_signatures = [] |
---|
3729 | n/a | help = '''\ |
---|
3730 | n/a | positional arguments: |
---|
3731 | n/a | spam spam help |
---|
3732 | n/a | |
---|
3733 | n/a | optional arguments: |
---|
3734 | n/a | -h, --help show this help message and exit |
---|
3735 | n/a | --foo FOO foo help |
---|
3736 | n/a | ''' |
---|
3737 | n/a | usage = '' |
---|
3738 | n/a | version = '' |
---|
3739 | n/a | |
---|
3740 | n/a | |
---|
3741 | n/a | class TestHelpSuppressOptional(HelpTestCase): |
---|
3742 | n/a | """Test that optional arguments can be suppressed in help messages""" |
---|
3743 | n/a | |
---|
3744 | n/a | parser_signature = Sig(prog='PROG', add_help=False) |
---|
3745 | n/a | argument_signatures = [ |
---|
3746 | n/a | Sig('--foo', help=argparse.SUPPRESS), |
---|
3747 | n/a | Sig('spam', help='spam help'), |
---|
3748 | n/a | ] |
---|
3749 | n/a | argument_group_signatures = [] |
---|
3750 | n/a | usage = '''\ |
---|
3751 | n/a | usage: PROG spam |
---|
3752 | n/a | ''' |
---|
3753 | n/a | help = usage + '''\ |
---|
3754 | n/a | |
---|
3755 | n/a | positional arguments: |
---|
3756 | n/a | spam spam help |
---|
3757 | n/a | ''' |
---|
3758 | n/a | version = '' |
---|
3759 | n/a | |
---|
3760 | n/a | |
---|
3761 | n/a | class TestHelpSuppressOptionalGroup(HelpTestCase): |
---|
3762 | n/a | """Test that optional groups can be suppressed in help messages""" |
---|
3763 | n/a | |
---|
3764 | n/a | parser_signature = Sig(prog='PROG') |
---|
3765 | n/a | argument_signatures = [ |
---|
3766 | n/a | Sig('--foo', help='foo help'), |
---|
3767 | n/a | Sig('spam', help='spam help'), |
---|
3768 | n/a | ] |
---|
3769 | n/a | argument_group_signatures = [ |
---|
3770 | n/a | (Sig('group'), [Sig('--bar', help=argparse.SUPPRESS)]), |
---|
3771 | n/a | ] |
---|
3772 | n/a | usage = '''\ |
---|
3773 | n/a | usage: PROG [-h] [--foo FOO] spam |
---|
3774 | n/a | ''' |
---|
3775 | n/a | help = usage + '''\ |
---|
3776 | n/a | |
---|
3777 | n/a | positional arguments: |
---|
3778 | n/a | spam spam help |
---|
3779 | n/a | |
---|
3780 | n/a | optional arguments: |
---|
3781 | n/a | -h, --help show this help message and exit |
---|
3782 | n/a | --foo FOO foo help |
---|
3783 | n/a | ''' |
---|
3784 | n/a | version = '' |
---|
3785 | n/a | |
---|
3786 | n/a | |
---|
3787 | n/a | class TestHelpSuppressPositional(HelpTestCase): |
---|
3788 | n/a | """Test that positional arguments can be suppressed in help messages""" |
---|
3789 | n/a | |
---|
3790 | n/a | parser_signature = Sig(prog='PROG') |
---|
3791 | n/a | argument_signatures = [ |
---|
3792 | n/a | Sig('--foo', help='foo help'), |
---|
3793 | n/a | Sig('spam', help=argparse.SUPPRESS), |
---|
3794 | n/a | ] |
---|
3795 | n/a | argument_group_signatures = [] |
---|
3796 | n/a | usage = '''\ |
---|
3797 | n/a | usage: PROG [-h] [--foo FOO] |
---|
3798 | n/a | ''' |
---|
3799 | n/a | help = usage + '''\ |
---|
3800 | n/a | |
---|
3801 | n/a | optional arguments: |
---|
3802 | n/a | -h, --help show this help message and exit |
---|
3803 | n/a | --foo FOO foo help |
---|
3804 | n/a | ''' |
---|
3805 | n/a | version = '' |
---|
3806 | n/a | |
---|
3807 | n/a | |
---|
3808 | n/a | class TestHelpRequiredOptional(HelpTestCase): |
---|
3809 | n/a | """Test that required options don't look optional""" |
---|
3810 | n/a | |
---|
3811 | n/a | parser_signature = Sig(prog='PROG') |
---|
3812 | n/a | argument_signatures = [ |
---|
3813 | n/a | Sig('--foo', required=True, help='foo help'), |
---|
3814 | n/a | ] |
---|
3815 | n/a | argument_group_signatures = [] |
---|
3816 | n/a | usage = '''\ |
---|
3817 | n/a | usage: PROG [-h] --foo FOO |
---|
3818 | n/a | ''' |
---|
3819 | n/a | help = usage + '''\ |
---|
3820 | n/a | |
---|
3821 | n/a | optional arguments: |
---|
3822 | n/a | -h, --help show this help message and exit |
---|
3823 | n/a | --foo FOO foo help |
---|
3824 | n/a | ''' |
---|
3825 | n/a | version = '' |
---|
3826 | n/a | |
---|
3827 | n/a | |
---|
3828 | n/a | class TestHelpAlternatePrefixChars(HelpTestCase): |
---|
3829 | n/a | """Test that options display with different prefix characters""" |
---|
3830 | n/a | |
---|
3831 | n/a | parser_signature = Sig(prog='PROG', prefix_chars='^;', add_help=False) |
---|
3832 | n/a | argument_signatures = [ |
---|
3833 | n/a | Sig('^^foo', action='store_true', help='foo help'), |
---|
3834 | n/a | Sig(';b', ';;bar', help='bar help'), |
---|
3835 | n/a | ] |
---|
3836 | n/a | argument_group_signatures = [] |
---|
3837 | n/a | usage = '''\ |
---|
3838 | n/a | usage: PROG [^^foo] [;b BAR] |
---|
3839 | n/a | ''' |
---|
3840 | n/a | help = usage + '''\ |
---|
3841 | n/a | |
---|
3842 | n/a | optional arguments: |
---|
3843 | n/a | ^^foo foo help |
---|
3844 | n/a | ;b BAR, ;;bar BAR bar help |
---|
3845 | n/a | ''' |
---|
3846 | n/a | version = '' |
---|
3847 | n/a | |
---|
3848 | n/a | |
---|
3849 | n/a | class TestHelpNoHelpOptional(HelpTestCase): |
---|
3850 | n/a | """Test that the --help argument can be suppressed help messages""" |
---|
3851 | n/a | |
---|
3852 | n/a | parser_signature = Sig(prog='PROG', add_help=False) |
---|
3853 | n/a | argument_signatures = [ |
---|
3854 | n/a | Sig('--foo', help='foo help'), |
---|
3855 | n/a | Sig('spam', help='spam help'), |
---|
3856 | n/a | ] |
---|
3857 | n/a | argument_group_signatures = [] |
---|
3858 | n/a | usage = '''\ |
---|
3859 | n/a | usage: PROG [--foo FOO] spam |
---|
3860 | n/a | ''' |
---|
3861 | n/a | help = usage + '''\ |
---|
3862 | n/a | |
---|
3863 | n/a | positional arguments: |
---|
3864 | n/a | spam spam help |
---|
3865 | n/a | |
---|
3866 | n/a | optional arguments: |
---|
3867 | n/a | --foo FOO foo help |
---|
3868 | n/a | ''' |
---|
3869 | n/a | version = '' |
---|
3870 | n/a | |
---|
3871 | n/a | |
---|
3872 | n/a | class TestHelpNone(HelpTestCase): |
---|
3873 | n/a | """Test that no errors occur if no help is specified""" |
---|
3874 | n/a | |
---|
3875 | n/a | parser_signature = Sig(prog='PROG') |
---|
3876 | n/a | argument_signatures = [ |
---|
3877 | n/a | Sig('--foo'), |
---|
3878 | n/a | Sig('spam'), |
---|
3879 | n/a | ] |
---|
3880 | n/a | argument_group_signatures = [] |
---|
3881 | n/a | usage = '''\ |
---|
3882 | n/a | usage: PROG [-h] [--foo FOO] spam |
---|
3883 | n/a | ''' |
---|
3884 | n/a | help = usage + '''\ |
---|
3885 | n/a | |
---|
3886 | n/a | positional arguments: |
---|
3887 | n/a | spam |
---|
3888 | n/a | |
---|
3889 | n/a | optional arguments: |
---|
3890 | n/a | -h, --help show this help message and exit |
---|
3891 | n/a | --foo FOO |
---|
3892 | n/a | ''' |
---|
3893 | n/a | version = '' |
---|
3894 | n/a | |
---|
3895 | n/a | |
---|
3896 | n/a | class TestHelpTupleMetavar(HelpTestCase): |
---|
3897 | n/a | """Test specifying metavar as a tuple""" |
---|
3898 | n/a | |
---|
3899 | n/a | parser_signature = Sig(prog='PROG') |
---|
3900 | n/a | argument_signatures = [ |
---|
3901 | n/a | Sig('-w', help='w', nargs='+', metavar=('W1', 'W2')), |
---|
3902 | n/a | Sig('-x', help='x', nargs='*', metavar=('X1', 'X2')), |
---|
3903 | n/a | Sig('-y', help='y', nargs=3, metavar=('Y1', 'Y2', 'Y3')), |
---|
3904 | n/a | Sig('-z', help='z', nargs='?', metavar=('Z1', )), |
---|
3905 | n/a | ] |
---|
3906 | n/a | argument_group_signatures = [] |
---|
3907 | n/a | usage = '''\ |
---|
3908 | n/a | usage: PROG [-h] [-w W1 [W2 ...]] [-x [X1 [X2 ...]]] [-y Y1 Y2 Y3] \ |
---|
3909 | n/a | [-z [Z1]] |
---|
3910 | n/a | ''' |
---|
3911 | n/a | help = usage + '''\ |
---|
3912 | n/a | |
---|
3913 | n/a | optional arguments: |
---|
3914 | n/a | -h, --help show this help message and exit |
---|
3915 | n/a | -w W1 [W2 ...] w |
---|
3916 | n/a | -x [X1 [X2 ...]] x |
---|
3917 | n/a | -y Y1 Y2 Y3 y |
---|
3918 | n/a | -z [Z1] z |
---|
3919 | n/a | ''' |
---|
3920 | n/a | version = '' |
---|
3921 | n/a | |
---|
3922 | n/a | |
---|
3923 | n/a | class TestHelpRawText(HelpTestCase): |
---|
3924 | n/a | """Test the RawTextHelpFormatter""" |
---|
3925 | n/a | |
---|
3926 | n/a | parser_signature = Sig( |
---|
3927 | n/a | prog='PROG', formatter_class=argparse.RawTextHelpFormatter, |
---|
3928 | n/a | description='Keep the formatting\n' |
---|
3929 | n/a | ' exactly as it is written\n' |
---|
3930 | n/a | '\n' |
---|
3931 | n/a | 'here\n') |
---|
3932 | n/a | |
---|
3933 | n/a | argument_signatures = [ |
---|
3934 | n/a | Sig('--foo', help=' foo help should also\n' |
---|
3935 | n/a | 'appear as given here'), |
---|
3936 | n/a | Sig('spam', help='spam help'), |
---|
3937 | n/a | ] |
---|
3938 | n/a | argument_group_signatures = [ |
---|
3939 | n/a | (Sig('title', description=' This text\n' |
---|
3940 | n/a | ' should be indented\n' |
---|
3941 | n/a | ' exactly like it is here\n'), |
---|
3942 | n/a | [Sig('--bar', help='bar help')]), |
---|
3943 | n/a | ] |
---|
3944 | n/a | usage = '''\ |
---|
3945 | n/a | usage: PROG [-h] [--foo FOO] [--bar BAR] spam |
---|
3946 | n/a | ''' |
---|
3947 | n/a | help = usage + '''\ |
---|
3948 | n/a | |
---|
3949 | n/a | Keep the formatting |
---|
3950 | n/a | exactly as it is written |
---|
3951 | n/a | |
---|
3952 | n/a | here |
---|
3953 | n/a | |
---|
3954 | n/a | positional arguments: |
---|
3955 | n/a | spam spam help |
---|
3956 | n/a | |
---|
3957 | n/a | optional arguments: |
---|
3958 | n/a | -h, --help show this help message and exit |
---|
3959 | n/a | --foo FOO foo help should also |
---|
3960 | n/a | appear as given here |
---|
3961 | n/a | |
---|
3962 | n/a | title: |
---|
3963 | n/a | This text |
---|
3964 | n/a | should be indented |
---|
3965 | n/a | exactly like it is here |
---|
3966 | n/a | |
---|
3967 | n/a | --bar BAR bar help |
---|
3968 | n/a | ''' |
---|
3969 | n/a | version = '' |
---|
3970 | n/a | |
---|
3971 | n/a | |
---|
3972 | n/a | class TestHelpRawDescription(HelpTestCase): |
---|
3973 | n/a | """Test the RawTextHelpFormatter""" |
---|
3974 | n/a | |
---|
3975 | n/a | parser_signature = Sig( |
---|
3976 | n/a | prog='PROG', formatter_class=argparse.RawDescriptionHelpFormatter, |
---|
3977 | n/a | description='Keep the formatting\n' |
---|
3978 | n/a | ' exactly as it is written\n' |
---|
3979 | n/a | '\n' |
---|
3980 | n/a | 'here\n') |
---|
3981 | n/a | |
---|
3982 | n/a | argument_signatures = [ |
---|
3983 | n/a | Sig('--foo', help=' foo help should not\n' |
---|
3984 | n/a | ' retain this odd formatting'), |
---|
3985 | n/a | Sig('spam', help='spam help'), |
---|
3986 | n/a | ] |
---|
3987 | n/a | argument_group_signatures = [ |
---|
3988 | n/a | (Sig('title', description=' This text\n' |
---|
3989 | n/a | ' should be indented\n' |
---|
3990 | n/a | ' exactly like it is here\n'), |
---|
3991 | n/a | [Sig('--bar', help='bar help')]), |
---|
3992 | n/a | ] |
---|
3993 | n/a | usage = '''\ |
---|
3994 | n/a | usage: PROG [-h] [--foo FOO] [--bar BAR] spam |
---|
3995 | n/a | ''' |
---|
3996 | n/a | help = usage + '''\ |
---|
3997 | n/a | |
---|
3998 | n/a | Keep the formatting |
---|
3999 | n/a | exactly as it is written |
---|
4000 | n/a | |
---|
4001 | n/a | here |
---|
4002 | n/a | |
---|
4003 | n/a | positional arguments: |
---|
4004 | n/a | spam spam help |
---|
4005 | n/a | |
---|
4006 | n/a | optional arguments: |
---|
4007 | n/a | -h, --help show this help message and exit |
---|
4008 | n/a | --foo FOO foo help should not retain this odd formatting |
---|
4009 | n/a | |
---|
4010 | n/a | title: |
---|
4011 | n/a | This text |
---|
4012 | n/a | should be indented |
---|
4013 | n/a | exactly like it is here |
---|
4014 | n/a | |
---|
4015 | n/a | --bar BAR bar help |
---|
4016 | n/a | ''' |
---|
4017 | n/a | version = '' |
---|
4018 | n/a | |
---|
4019 | n/a | |
---|
4020 | n/a | class TestHelpArgumentDefaults(HelpTestCase): |
---|
4021 | n/a | """Test the ArgumentDefaultsHelpFormatter""" |
---|
4022 | n/a | |
---|
4023 | n/a | parser_signature = Sig( |
---|
4024 | n/a | prog='PROG', formatter_class=argparse.ArgumentDefaultsHelpFormatter, |
---|
4025 | n/a | description='description') |
---|
4026 | n/a | |
---|
4027 | n/a | argument_signatures = [ |
---|
4028 | n/a | Sig('--foo', help='foo help - oh and by the way, %(default)s'), |
---|
4029 | n/a | Sig('--bar', action='store_true', help='bar help'), |
---|
4030 | n/a | Sig('spam', help='spam help'), |
---|
4031 | n/a | Sig('badger', nargs='?', default='wooden', help='badger help'), |
---|
4032 | n/a | ] |
---|
4033 | n/a | argument_group_signatures = [ |
---|
4034 | n/a | (Sig('title', description='description'), |
---|
4035 | n/a | [Sig('--baz', type=int, default=42, help='baz help')]), |
---|
4036 | n/a | ] |
---|
4037 | n/a | usage = '''\ |
---|
4038 | n/a | usage: PROG [-h] [--foo FOO] [--bar] [--baz BAZ] spam [badger] |
---|
4039 | n/a | ''' |
---|
4040 | n/a | help = usage + '''\ |
---|
4041 | n/a | |
---|
4042 | n/a | description |
---|
4043 | n/a | |
---|
4044 | n/a | positional arguments: |
---|
4045 | n/a | spam spam help |
---|
4046 | n/a | badger badger help (default: wooden) |
---|
4047 | n/a | |
---|
4048 | n/a | optional arguments: |
---|
4049 | n/a | -h, --help show this help message and exit |
---|
4050 | n/a | --foo FOO foo help - oh and by the way, None |
---|
4051 | n/a | --bar bar help (default: False) |
---|
4052 | n/a | |
---|
4053 | n/a | title: |
---|
4054 | n/a | description |
---|
4055 | n/a | |
---|
4056 | n/a | --baz BAZ baz help (default: 42) |
---|
4057 | n/a | ''' |
---|
4058 | n/a | version = '' |
---|
4059 | n/a | |
---|
4060 | n/a | class TestHelpVersionAction(HelpTestCase): |
---|
4061 | n/a | """Test the default help for the version action""" |
---|
4062 | n/a | |
---|
4063 | n/a | parser_signature = Sig(prog='PROG', description='description') |
---|
4064 | n/a | argument_signatures = [Sig('-V', '--version', action='version', version='3.6')] |
---|
4065 | n/a | argument_group_signatures = [] |
---|
4066 | n/a | usage = '''\ |
---|
4067 | n/a | usage: PROG [-h] [-V] |
---|
4068 | n/a | ''' |
---|
4069 | n/a | help = usage + '''\ |
---|
4070 | n/a | |
---|
4071 | n/a | description |
---|
4072 | n/a | |
---|
4073 | n/a | optional arguments: |
---|
4074 | n/a | -h, --help show this help message and exit |
---|
4075 | n/a | -V, --version show program's version number and exit |
---|
4076 | n/a | ''' |
---|
4077 | n/a | version = '' |
---|
4078 | n/a | |
---|
4079 | n/a | |
---|
4080 | n/a | class TestHelpVersionActionSuppress(HelpTestCase): |
---|
4081 | n/a | """Test that the --version argument can be suppressed in help messages""" |
---|
4082 | n/a | |
---|
4083 | n/a | parser_signature = Sig(prog='PROG') |
---|
4084 | n/a | argument_signatures = [ |
---|
4085 | n/a | Sig('-v', '--version', action='version', version='1.0', |
---|
4086 | n/a | help=argparse.SUPPRESS), |
---|
4087 | n/a | Sig('--foo', help='foo help'), |
---|
4088 | n/a | Sig('spam', help='spam help'), |
---|
4089 | n/a | ] |
---|
4090 | n/a | argument_group_signatures = [] |
---|
4091 | n/a | usage = '''\ |
---|
4092 | n/a | usage: PROG [-h] [--foo FOO] spam |
---|
4093 | n/a | ''' |
---|
4094 | n/a | help = usage + '''\ |
---|
4095 | n/a | |
---|
4096 | n/a | positional arguments: |
---|
4097 | n/a | spam spam help |
---|
4098 | n/a | |
---|
4099 | n/a | optional arguments: |
---|
4100 | n/a | -h, --help show this help message and exit |
---|
4101 | n/a | --foo FOO foo help |
---|
4102 | n/a | ''' |
---|
4103 | n/a | |
---|
4104 | n/a | |
---|
4105 | n/a | class TestHelpSubparsersOrdering(HelpTestCase): |
---|
4106 | n/a | """Test ordering of subcommands in help matches the code""" |
---|
4107 | n/a | parser_signature = Sig(prog='PROG', |
---|
4108 | n/a | description='display some subcommands') |
---|
4109 | n/a | argument_signatures = [Sig('-v', '--version', action='version', version='0.1')] |
---|
4110 | n/a | |
---|
4111 | n/a | subparsers_signatures = [Sig(name=name) |
---|
4112 | n/a | for name in ('a', 'b', 'c', 'd', 'e')] |
---|
4113 | n/a | |
---|
4114 | n/a | usage = '''\ |
---|
4115 | n/a | usage: PROG [-h] [-v] {a,b,c,d,e} ... |
---|
4116 | n/a | ''' |
---|
4117 | n/a | |
---|
4118 | n/a | help = usage + '''\ |
---|
4119 | n/a | |
---|
4120 | n/a | display some subcommands |
---|
4121 | n/a | |
---|
4122 | n/a | positional arguments: |
---|
4123 | n/a | {a,b,c,d,e} |
---|
4124 | n/a | |
---|
4125 | n/a | optional arguments: |
---|
4126 | n/a | -h, --help show this help message and exit |
---|
4127 | n/a | -v, --version show program's version number and exit |
---|
4128 | n/a | ''' |
---|
4129 | n/a | |
---|
4130 | n/a | version = '''\ |
---|
4131 | n/a | 0.1 |
---|
4132 | n/a | ''' |
---|
4133 | n/a | |
---|
4134 | n/a | class TestHelpSubparsersWithHelpOrdering(HelpTestCase): |
---|
4135 | n/a | """Test ordering of subcommands in help matches the code""" |
---|
4136 | n/a | parser_signature = Sig(prog='PROG', |
---|
4137 | n/a | description='display some subcommands') |
---|
4138 | n/a | argument_signatures = [Sig('-v', '--version', action='version', version='0.1')] |
---|
4139 | n/a | |
---|
4140 | n/a | subcommand_data = (('a', 'a subcommand help'), |
---|
4141 | n/a | ('b', 'b subcommand help'), |
---|
4142 | n/a | ('c', 'c subcommand help'), |
---|
4143 | n/a | ('d', 'd subcommand help'), |
---|
4144 | n/a | ('e', 'e subcommand help'), |
---|
4145 | n/a | ) |
---|
4146 | n/a | |
---|
4147 | n/a | subparsers_signatures = [Sig(name=name, help=help) |
---|
4148 | n/a | for name, help in subcommand_data] |
---|
4149 | n/a | |
---|
4150 | n/a | usage = '''\ |
---|
4151 | n/a | usage: PROG [-h] [-v] {a,b,c,d,e} ... |
---|
4152 | n/a | ''' |
---|
4153 | n/a | |
---|
4154 | n/a | help = usage + '''\ |
---|
4155 | n/a | |
---|
4156 | n/a | display some subcommands |
---|
4157 | n/a | |
---|
4158 | n/a | positional arguments: |
---|
4159 | n/a | {a,b,c,d,e} |
---|
4160 | n/a | a a subcommand help |
---|
4161 | n/a | b b subcommand help |
---|
4162 | n/a | c c subcommand help |
---|
4163 | n/a | d d subcommand help |
---|
4164 | n/a | e e subcommand help |
---|
4165 | n/a | |
---|
4166 | n/a | optional arguments: |
---|
4167 | n/a | -h, --help show this help message and exit |
---|
4168 | n/a | -v, --version show program's version number and exit |
---|
4169 | n/a | ''' |
---|
4170 | n/a | |
---|
4171 | n/a | version = '''\ |
---|
4172 | n/a | 0.1 |
---|
4173 | n/a | ''' |
---|
4174 | n/a | |
---|
4175 | n/a | |
---|
4176 | n/a | |
---|
4177 | n/a | class TestHelpMetavarTypeFormatter(HelpTestCase): |
---|
4178 | n/a | """""" |
---|
4179 | n/a | |
---|
4180 | n/a | def custom_type(string): |
---|
4181 | n/a | return string |
---|
4182 | n/a | |
---|
4183 | n/a | parser_signature = Sig(prog='PROG', description='description', |
---|
4184 | n/a | formatter_class=argparse.MetavarTypeHelpFormatter) |
---|
4185 | n/a | argument_signatures = [Sig('a', type=int), |
---|
4186 | n/a | Sig('-b', type=custom_type), |
---|
4187 | n/a | Sig('-c', type=float, metavar='SOME FLOAT')] |
---|
4188 | n/a | argument_group_signatures = [] |
---|
4189 | n/a | usage = '''\ |
---|
4190 | n/a | usage: PROG [-h] [-b custom_type] [-c SOME FLOAT] int |
---|
4191 | n/a | ''' |
---|
4192 | n/a | help = usage + '''\ |
---|
4193 | n/a | |
---|
4194 | n/a | description |
---|
4195 | n/a | |
---|
4196 | n/a | positional arguments: |
---|
4197 | n/a | int |
---|
4198 | n/a | |
---|
4199 | n/a | optional arguments: |
---|
4200 | n/a | -h, --help show this help message and exit |
---|
4201 | n/a | -b custom_type |
---|
4202 | n/a | -c SOME FLOAT |
---|
4203 | n/a | ''' |
---|
4204 | n/a | version = '' |
---|
4205 | n/a | |
---|
4206 | n/a | |
---|
4207 | n/a | # ===================================== |
---|
4208 | n/a | # Optional/Positional constructor tests |
---|
4209 | n/a | # ===================================== |
---|
4210 | n/a | |
---|
4211 | n/a | class TestInvalidArgumentConstructors(TestCase): |
---|
4212 | n/a | """Test a bunch of invalid Argument constructors""" |
---|
4213 | n/a | |
---|
4214 | n/a | def assertTypeError(self, *args, **kwargs): |
---|
4215 | n/a | parser = argparse.ArgumentParser() |
---|
4216 | n/a | self.assertRaises(TypeError, parser.add_argument, |
---|
4217 | n/a | *args, **kwargs) |
---|
4218 | n/a | |
---|
4219 | n/a | def assertValueError(self, *args, **kwargs): |
---|
4220 | n/a | parser = argparse.ArgumentParser() |
---|
4221 | n/a | self.assertRaises(ValueError, parser.add_argument, |
---|
4222 | n/a | *args, **kwargs) |
---|
4223 | n/a | |
---|
4224 | n/a | def test_invalid_keyword_arguments(self): |
---|
4225 | n/a | self.assertTypeError('-x', bar=None) |
---|
4226 | n/a | self.assertTypeError('-y', callback='foo') |
---|
4227 | n/a | self.assertTypeError('-y', callback_args=()) |
---|
4228 | n/a | self.assertTypeError('-y', callback_kwargs={}) |
---|
4229 | n/a | |
---|
4230 | n/a | def test_missing_destination(self): |
---|
4231 | n/a | self.assertTypeError() |
---|
4232 | n/a | for action in ['append', 'store']: |
---|
4233 | n/a | self.assertTypeError(action=action) |
---|
4234 | n/a | |
---|
4235 | n/a | def test_invalid_option_strings(self): |
---|
4236 | n/a | self.assertValueError('--') |
---|
4237 | n/a | self.assertValueError('---') |
---|
4238 | n/a | |
---|
4239 | n/a | def test_invalid_type(self): |
---|
4240 | n/a | self.assertValueError('--foo', type='int') |
---|
4241 | n/a | self.assertValueError('--foo', type=(int, float)) |
---|
4242 | n/a | |
---|
4243 | n/a | def test_invalid_action(self): |
---|
4244 | n/a | self.assertValueError('-x', action='foo') |
---|
4245 | n/a | self.assertValueError('foo', action='baz') |
---|
4246 | n/a | self.assertValueError('--foo', action=('store', 'append')) |
---|
4247 | n/a | parser = argparse.ArgumentParser() |
---|
4248 | n/a | with self.assertRaises(ValueError) as cm: |
---|
4249 | n/a | parser.add_argument("--foo", action="store-true") |
---|
4250 | n/a | self.assertIn('unknown action', str(cm.exception)) |
---|
4251 | n/a | |
---|
4252 | n/a | def test_multiple_dest(self): |
---|
4253 | n/a | parser = argparse.ArgumentParser() |
---|
4254 | n/a | parser.add_argument(dest='foo') |
---|
4255 | n/a | with self.assertRaises(ValueError) as cm: |
---|
4256 | n/a | parser.add_argument('bar', dest='baz') |
---|
4257 | n/a | self.assertIn('dest supplied twice for positional argument', |
---|
4258 | n/a | str(cm.exception)) |
---|
4259 | n/a | |
---|
4260 | n/a | def test_no_argument_actions(self): |
---|
4261 | n/a | for action in ['store_const', 'store_true', 'store_false', |
---|
4262 | n/a | 'append_const', 'count']: |
---|
4263 | n/a | for attrs in [dict(type=int), dict(nargs='+'), |
---|
4264 | n/a | dict(choices='ab')]: |
---|
4265 | n/a | self.assertTypeError('-x', action=action, **attrs) |
---|
4266 | n/a | |
---|
4267 | n/a | def test_no_argument_no_const_actions(self): |
---|
4268 | n/a | # options with zero arguments |
---|
4269 | n/a | for action in ['store_true', 'store_false', 'count']: |
---|
4270 | n/a | |
---|
4271 | n/a | # const is always disallowed |
---|
4272 | n/a | self.assertTypeError('-x', const='foo', action=action) |
---|
4273 | n/a | |
---|
4274 | n/a | # nargs is always disallowed |
---|
4275 | n/a | self.assertTypeError('-x', nargs='*', action=action) |
---|
4276 | n/a | |
---|
4277 | n/a | def test_more_than_one_argument_actions(self): |
---|
4278 | n/a | for action in ['store', 'append']: |
---|
4279 | n/a | |
---|
4280 | n/a | # nargs=0 is disallowed |
---|
4281 | n/a | self.assertValueError('-x', nargs=0, action=action) |
---|
4282 | n/a | self.assertValueError('spam', nargs=0, action=action) |
---|
4283 | n/a | |
---|
4284 | n/a | # const is disallowed with non-optional arguments |
---|
4285 | n/a | for nargs in [1, '*', '+']: |
---|
4286 | n/a | self.assertValueError('-x', const='foo', |
---|
4287 | n/a | nargs=nargs, action=action) |
---|
4288 | n/a | self.assertValueError('spam', const='foo', |
---|
4289 | n/a | nargs=nargs, action=action) |
---|
4290 | n/a | |
---|
4291 | n/a | def test_required_const_actions(self): |
---|
4292 | n/a | for action in ['store_const', 'append_const']: |
---|
4293 | n/a | |
---|
4294 | n/a | # nargs is always disallowed |
---|
4295 | n/a | self.assertTypeError('-x', nargs='+', action=action) |
---|
4296 | n/a | |
---|
4297 | n/a | def test_parsers_action_missing_params(self): |
---|
4298 | n/a | self.assertTypeError('command', action='parsers') |
---|
4299 | n/a | self.assertTypeError('command', action='parsers', prog='PROG') |
---|
4300 | n/a | self.assertTypeError('command', action='parsers', |
---|
4301 | n/a | parser_class=argparse.ArgumentParser) |
---|
4302 | n/a | |
---|
4303 | n/a | def test_required_positional(self): |
---|
4304 | n/a | self.assertTypeError('foo', required=True) |
---|
4305 | n/a | |
---|
4306 | n/a | def test_user_defined_action(self): |
---|
4307 | n/a | |
---|
4308 | n/a | class Success(Exception): |
---|
4309 | n/a | pass |
---|
4310 | n/a | |
---|
4311 | n/a | class Action(object): |
---|
4312 | n/a | |
---|
4313 | n/a | def __init__(self, |
---|
4314 | n/a | option_strings, |
---|
4315 | n/a | dest, |
---|
4316 | n/a | const, |
---|
4317 | n/a | default, |
---|
4318 | n/a | required=False): |
---|
4319 | n/a | if dest == 'spam': |
---|
4320 | n/a | if const is Success: |
---|
4321 | n/a | if default is Success: |
---|
4322 | n/a | raise Success() |
---|
4323 | n/a | |
---|
4324 | n/a | def __call__(self, *args, **kwargs): |
---|
4325 | n/a | pass |
---|
4326 | n/a | |
---|
4327 | n/a | parser = argparse.ArgumentParser() |
---|
4328 | n/a | self.assertRaises(Success, parser.add_argument, '--spam', |
---|
4329 | n/a | action=Action, default=Success, const=Success) |
---|
4330 | n/a | self.assertRaises(Success, parser.add_argument, 'spam', |
---|
4331 | n/a | action=Action, default=Success, const=Success) |
---|
4332 | n/a | |
---|
4333 | n/a | # ================================ |
---|
4334 | n/a | # Actions returned by add_argument |
---|
4335 | n/a | # ================================ |
---|
4336 | n/a | |
---|
4337 | n/a | class TestActionsReturned(TestCase): |
---|
4338 | n/a | |
---|
4339 | n/a | def test_dest(self): |
---|
4340 | n/a | parser = argparse.ArgumentParser() |
---|
4341 | n/a | action = parser.add_argument('--foo') |
---|
4342 | n/a | self.assertEqual(action.dest, 'foo') |
---|
4343 | n/a | action = parser.add_argument('-b', '--bar') |
---|
4344 | n/a | self.assertEqual(action.dest, 'bar') |
---|
4345 | n/a | action = parser.add_argument('-x', '-y') |
---|
4346 | n/a | self.assertEqual(action.dest, 'x') |
---|
4347 | n/a | |
---|
4348 | n/a | def test_misc(self): |
---|
4349 | n/a | parser = argparse.ArgumentParser() |
---|
4350 | n/a | action = parser.add_argument('--foo', nargs='?', const=42, |
---|
4351 | n/a | default=84, type=int, choices=[1, 2], |
---|
4352 | n/a | help='FOO', metavar='BAR', dest='baz') |
---|
4353 | n/a | self.assertEqual(action.nargs, '?') |
---|
4354 | n/a | self.assertEqual(action.const, 42) |
---|
4355 | n/a | self.assertEqual(action.default, 84) |
---|
4356 | n/a | self.assertEqual(action.type, int) |
---|
4357 | n/a | self.assertEqual(action.choices, [1, 2]) |
---|
4358 | n/a | self.assertEqual(action.help, 'FOO') |
---|
4359 | n/a | self.assertEqual(action.metavar, 'BAR') |
---|
4360 | n/a | self.assertEqual(action.dest, 'baz') |
---|
4361 | n/a | |
---|
4362 | n/a | |
---|
4363 | n/a | # ================================ |
---|
4364 | n/a | # Argument conflict handling tests |
---|
4365 | n/a | # ================================ |
---|
4366 | n/a | |
---|
4367 | n/a | class TestConflictHandling(TestCase): |
---|
4368 | n/a | |
---|
4369 | n/a | def test_bad_type(self): |
---|
4370 | n/a | self.assertRaises(ValueError, argparse.ArgumentParser, |
---|
4371 | n/a | conflict_handler='foo') |
---|
4372 | n/a | |
---|
4373 | n/a | def test_conflict_error(self): |
---|
4374 | n/a | parser = argparse.ArgumentParser() |
---|
4375 | n/a | parser.add_argument('-x') |
---|
4376 | n/a | self.assertRaises(argparse.ArgumentError, |
---|
4377 | n/a | parser.add_argument, '-x') |
---|
4378 | n/a | parser.add_argument('--spam') |
---|
4379 | n/a | self.assertRaises(argparse.ArgumentError, |
---|
4380 | n/a | parser.add_argument, '--spam') |
---|
4381 | n/a | |
---|
4382 | n/a | def test_resolve_error(self): |
---|
4383 | n/a | get_parser = argparse.ArgumentParser |
---|
4384 | n/a | parser = get_parser(prog='PROG', conflict_handler='resolve') |
---|
4385 | n/a | |
---|
4386 | n/a | parser.add_argument('-x', help='OLD X') |
---|
4387 | n/a | parser.add_argument('-x', help='NEW X') |
---|
4388 | n/a | self.assertEqual(parser.format_help(), textwrap.dedent('''\ |
---|
4389 | n/a | usage: PROG [-h] [-x X] |
---|
4390 | n/a | |
---|
4391 | n/a | optional arguments: |
---|
4392 | n/a | -h, --help show this help message and exit |
---|
4393 | n/a | -x X NEW X |
---|
4394 | n/a | ''')) |
---|
4395 | n/a | |
---|
4396 | n/a | parser.add_argument('--spam', metavar='OLD_SPAM') |
---|
4397 | n/a | parser.add_argument('--spam', metavar='NEW_SPAM') |
---|
4398 | n/a | self.assertEqual(parser.format_help(), textwrap.dedent('''\ |
---|
4399 | n/a | usage: PROG [-h] [-x X] [--spam NEW_SPAM] |
---|
4400 | n/a | |
---|
4401 | n/a | optional arguments: |
---|
4402 | n/a | -h, --help show this help message and exit |
---|
4403 | n/a | -x X NEW X |
---|
4404 | n/a | --spam NEW_SPAM |
---|
4405 | n/a | ''')) |
---|
4406 | n/a | |
---|
4407 | n/a | |
---|
4408 | n/a | # ============================= |
---|
4409 | n/a | # Help and Version option tests |
---|
4410 | n/a | # ============================= |
---|
4411 | n/a | |
---|
4412 | n/a | class TestOptionalsHelpVersionActions(TestCase): |
---|
4413 | n/a | """Test the help and version actions""" |
---|
4414 | n/a | |
---|
4415 | n/a | def assertPrintHelpExit(self, parser, args_str): |
---|
4416 | n/a | with self.assertRaises(ArgumentParserError) as cm: |
---|
4417 | n/a | parser.parse_args(args_str.split()) |
---|
4418 | n/a | self.assertEqual(parser.format_help(), cm.exception.stdout) |
---|
4419 | n/a | |
---|
4420 | n/a | def assertArgumentParserError(self, parser, *args): |
---|
4421 | n/a | self.assertRaises(ArgumentParserError, parser.parse_args, args) |
---|
4422 | n/a | |
---|
4423 | n/a | def test_version(self): |
---|
4424 | n/a | parser = ErrorRaisingArgumentParser() |
---|
4425 | n/a | parser.add_argument('-v', '--version', action='version', version='1.0') |
---|
4426 | n/a | self.assertPrintHelpExit(parser, '-h') |
---|
4427 | n/a | self.assertPrintHelpExit(parser, '--help') |
---|
4428 | n/a | self.assertRaises(AttributeError, getattr, parser, 'format_version') |
---|
4429 | n/a | |
---|
4430 | n/a | def test_version_format(self): |
---|
4431 | n/a | parser = ErrorRaisingArgumentParser(prog='PPP') |
---|
4432 | n/a | parser.add_argument('-v', '--version', action='version', version='%(prog)s 3.5') |
---|
4433 | n/a | with self.assertRaises(ArgumentParserError) as cm: |
---|
4434 | n/a | parser.parse_args(['-v']) |
---|
4435 | n/a | self.assertEqual('PPP 3.5\n', cm.exception.stdout) |
---|
4436 | n/a | |
---|
4437 | n/a | def test_version_no_help(self): |
---|
4438 | n/a | parser = ErrorRaisingArgumentParser(add_help=False) |
---|
4439 | n/a | parser.add_argument('-v', '--version', action='version', version='1.0') |
---|
4440 | n/a | self.assertArgumentParserError(parser, '-h') |
---|
4441 | n/a | self.assertArgumentParserError(parser, '--help') |
---|
4442 | n/a | self.assertRaises(AttributeError, getattr, parser, 'format_version') |
---|
4443 | n/a | |
---|
4444 | n/a | def test_version_action(self): |
---|
4445 | n/a | parser = ErrorRaisingArgumentParser(prog='XXX') |
---|
4446 | n/a | parser.add_argument('-V', action='version', version='%(prog)s 3.7') |
---|
4447 | n/a | with self.assertRaises(ArgumentParserError) as cm: |
---|
4448 | n/a | parser.parse_args(['-V']) |
---|
4449 | n/a | self.assertEqual('XXX 3.7\n', cm.exception.stdout) |
---|
4450 | n/a | |
---|
4451 | n/a | def test_no_help(self): |
---|
4452 | n/a | parser = ErrorRaisingArgumentParser(add_help=False) |
---|
4453 | n/a | self.assertArgumentParserError(parser, '-h') |
---|
4454 | n/a | self.assertArgumentParserError(parser, '--help') |
---|
4455 | n/a | self.assertArgumentParserError(parser, '-v') |
---|
4456 | n/a | self.assertArgumentParserError(parser, '--version') |
---|
4457 | n/a | |
---|
4458 | n/a | def test_alternate_help_version(self): |
---|
4459 | n/a | parser = ErrorRaisingArgumentParser() |
---|
4460 | n/a | parser.add_argument('-x', action='help') |
---|
4461 | n/a | parser.add_argument('-y', action='version') |
---|
4462 | n/a | self.assertPrintHelpExit(parser, '-x') |
---|
4463 | n/a | self.assertArgumentParserError(parser, '-v') |
---|
4464 | n/a | self.assertArgumentParserError(parser, '--version') |
---|
4465 | n/a | self.assertRaises(AttributeError, getattr, parser, 'format_version') |
---|
4466 | n/a | |
---|
4467 | n/a | def test_help_version_extra_arguments(self): |
---|
4468 | n/a | parser = ErrorRaisingArgumentParser() |
---|
4469 | n/a | parser.add_argument('--version', action='version', version='1.0') |
---|
4470 | n/a | parser.add_argument('-x', action='store_true') |
---|
4471 | n/a | parser.add_argument('y') |
---|
4472 | n/a | |
---|
4473 | n/a | # try all combinations of valid prefixes and suffixes |
---|
4474 | n/a | valid_prefixes = ['', '-x', 'foo', '-x bar', 'baz -x'] |
---|
4475 | n/a | valid_suffixes = valid_prefixes + ['--bad-option', 'foo bar baz'] |
---|
4476 | n/a | for prefix in valid_prefixes: |
---|
4477 | n/a | for suffix in valid_suffixes: |
---|
4478 | n/a | format = '%s %%s %s' % (prefix, suffix) |
---|
4479 | n/a | self.assertPrintHelpExit(parser, format % '-h') |
---|
4480 | n/a | self.assertPrintHelpExit(parser, format % '--help') |
---|
4481 | n/a | self.assertRaises(AttributeError, getattr, parser, 'format_version') |
---|
4482 | n/a | |
---|
4483 | n/a | |
---|
4484 | n/a | # ====================== |
---|
4485 | n/a | # str() and repr() tests |
---|
4486 | n/a | # ====================== |
---|
4487 | n/a | |
---|
4488 | n/a | class TestStrings(TestCase): |
---|
4489 | n/a | """Test str() and repr() on Optionals and Positionals""" |
---|
4490 | n/a | |
---|
4491 | n/a | def assertStringEqual(self, obj, result_string): |
---|
4492 | n/a | for func in [str, repr]: |
---|
4493 | n/a | self.assertEqual(func(obj), result_string) |
---|
4494 | n/a | |
---|
4495 | n/a | def test_optional(self): |
---|
4496 | n/a | option = argparse.Action( |
---|
4497 | n/a | option_strings=['--foo', '-a', '-b'], |
---|
4498 | n/a | dest='b', |
---|
4499 | n/a | type='int', |
---|
4500 | n/a | nargs='+', |
---|
4501 | n/a | default=42, |
---|
4502 | n/a | choices=[1, 2, 3], |
---|
4503 | n/a | help='HELP', |
---|
4504 | n/a | metavar='METAVAR') |
---|
4505 | n/a | string = ( |
---|
4506 | n/a | "Action(option_strings=['--foo', '-a', '-b'], dest='b', " |
---|
4507 | n/a | "nargs='+', const=None, default=42, type='int', " |
---|
4508 | n/a | "choices=[1, 2, 3], help='HELP', metavar='METAVAR')") |
---|
4509 | n/a | self.assertStringEqual(option, string) |
---|
4510 | n/a | |
---|
4511 | n/a | def test_argument(self): |
---|
4512 | n/a | argument = argparse.Action( |
---|
4513 | n/a | option_strings=[], |
---|
4514 | n/a | dest='x', |
---|
4515 | n/a | type=float, |
---|
4516 | n/a | nargs='?', |
---|
4517 | n/a | default=2.5, |
---|
4518 | n/a | choices=[0.5, 1.5, 2.5], |
---|
4519 | n/a | help='H HH H', |
---|
4520 | n/a | metavar='MV MV MV') |
---|
4521 | n/a | string = ( |
---|
4522 | n/a | "Action(option_strings=[], dest='x', nargs='?', " |
---|
4523 | n/a | "const=None, default=2.5, type=%r, choices=[0.5, 1.5, 2.5], " |
---|
4524 | n/a | "help='H HH H', metavar='MV MV MV')" % float) |
---|
4525 | n/a | self.assertStringEqual(argument, string) |
---|
4526 | n/a | |
---|
4527 | n/a | def test_namespace(self): |
---|
4528 | n/a | ns = argparse.Namespace(foo=42, bar='spam') |
---|
4529 | n/a | string = "Namespace(bar='spam', foo=42)" |
---|
4530 | n/a | self.assertStringEqual(ns, string) |
---|
4531 | n/a | |
---|
4532 | n/a | def test_namespace_starkwargs_notidentifier(self): |
---|
4533 | n/a | ns = argparse.Namespace(**{'"': 'quote'}) |
---|
4534 | n/a | string = """Namespace(**{'"': 'quote'})""" |
---|
4535 | n/a | self.assertStringEqual(ns, string) |
---|
4536 | n/a | |
---|
4537 | n/a | def test_namespace_kwargs_and_starkwargs_notidentifier(self): |
---|
4538 | n/a | ns = argparse.Namespace(a=1, **{'"': 'quote'}) |
---|
4539 | n/a | string = """Namespace(a=1, **{'"': 'quote'})""" |
---|
4540 | n/a | self.assertStringEqual(ns, string) |
---|
4541 | n/a | |
---|
4542 | n/a | def test_namespace_starkwargs_identifier(self): |
---|
4543 | n/a | ns = argparse.Namespace(**{'valid': True}) |
---|
4544 | n/a | string = "Namespace(valid=True)" |
---|
4545 | n/a | self.assertStringEqual(ns, string) |
---|
4546 | n/a | |
---|
4547 | n/a | def test_parser(self): |
---|
4548 | n/a | parser = argparse.ArgumentParser(prog='PROG') |
---|
4549 | n/a | string = ( |
---|
4550 | n/a | "ArgumentParser(prog='PROG', usage=None, description=None, " |
---|
4551 | n/a | "formatter_class=%r, conflict_handler='error', " |
---|
4552 | n/a | "add_help=True)" % argparse.HelpFormatter) |
---|
4553 | n/a | self.assertStringEqual(parser, string) |
---|
4554 | n/a | |
---|
4555 | n/a | # =============== |
---|
4556 | n/a | # Namespace tests |
---|
4557 | n/a | # =============== |
---|
4558 | n/a | |
---|
4559 | n/a | class TestNamespace(TestCase): |
---|
4560 | n/a | |
---|
4561 | n/a | def test_constructor(self): |
---|
4562 | n/a | ns = argparse.Namespace() |
---|
4563 | n/a | self.assertRaises(AttributeError, getattr, ns, 'x') |
---|
4564 | n/a | |
---|
4565 | n/a | ns = argparse.Namespace(a=42, b='spam') |
---|
4566 | n/a | self.assertEqual(ns.a, 42) |
---|
4567 | n/a | self.assertEqual(ns.b, 'spam') |
---|
4568 | n/a | |
---|
4569 | n/a | def test_equality(self): |
---|
4570 | n/a | ns1 = argparse.Namespace(a=1, b=2) |
---|
4571 | n/a | ns2 = argparse.Namespace(b=2, a=1) |
---|
4572 | n/a | ns3 = argparse.Namespace(a=1) |
---|
4573 | n/a | ns4 = argparse.Namespace(b=2) |
---|
4574 | n/a | |
---|
4575 | n/a | self.assertEqual(ns1, ns2) |
---|
4576 | n/a | self.assertNotEqual(ns1, ns3) |
---|
4577 | n/a | self.assertNotEqual(ns1, ns4) |
---|
4578 | n/a | self.assertNotEqual(ns2, ns3) |
---|
4579 | n/a | self.assertNotEqual(ns2, ns4) |
---|
4580 | n/a | self.assertTrue(ns1 != ns3) |
---|
4581 | n/a | self.assertTrue(ns1 != ns4) |
---|
4582 | n/a | self.assertTrue(ns2 != ns3) |
---|
4583 | n/a | self.assertTrue(ns2 != ns4) |
---|
4584 | n/a | |
---|
4585 | n/a | def test_equality_returns_notimplemented(self): |
---|
4586 | n/a | # See issue 21481 |
---|
4587 | n/a | ns = argparse.Namespace(a=1, b=2) |
---|
4588 | n/a | self.assertIs(ns.__eq__(None), NotImplemented) |
---|
4589 | n/a | self.assertIs(ns.__ne__(None), NotImplemented) |
---|
4590 | n/a | |
---|
4591 | n/a | |
---|
4592 | n/a | # =================== |
---|
4593 | n/a | # File encoding tests |
---|
4594 | n/a | # =================== |
---|
4595 | n/a | |
---|
4596 | n/a | class TestEncoding(TestCase): |
---|
4597 | n/a | |
---|
4598 | n/a | def _test_module_encoding(self, path): |
---|
4599 | n/a | path, _ = os.path.splitext(path) |
---|
4600 | n/a | path += ".py" |
---|
4601 | n/a | with codecs.open(path, 'r', 'utf-8') as f: |
---|
4602 | n/a | f.read() |
---|
4603 | n/a | |
---|
4604 | n/a | def test_argparse_module_encoding(self): |
---|
4605 | n/a | self._test_module_encoding(argparse.__file__) |
---|
4606 | n/a | |
---|
4607 | n/a | def test_test_argparse_module_encoding(self): |
---|
4608 | n/a | self._test_module_encoding(__file__) |
---|
4609 | n/a | |
---|
4610 | n/a | # =================== |
---|
4611 | n/a | # ArgumentError tests |
---|
4612 | n/a | # =================== |
---|
4613 | n/a | |
---|
4614 | n/a | class TestArgumentError(TestCase): |
---|
4615 | n/a | |
---|
4616 | n/a | def test_argument_error(self): |
---|
4617 | n/a | msg = "my error here" |
---|
4618 | n/a | error = argparse.ArgumentError(None, msg) |
---|
4619 | n/a | self.assertEqual(str(error), msg) |
---|
4620 | n/a | |
---|
4621 | n/a | # ======================= |
---|
4622 | n/a | # ArgumentTypeError tests |
---|
4623 | n/a | # ======================= |
---|
4624 | n/a | |
---|
4625 | n/a | class TestArgumentTypeError(TestCase): |
---|
4626 | n/a | |
---|
4627 | n/a | def test_argument_type_error(self): |
---|
4628 | n/a | |
---|
4629 | n/a | def spam(string): |
---|
4630 | n/a | raise argparse.ArgumentTypeError('spam!') |
---|
4631 | n/a | |
---|
4632 | n/a | parser = ErrorRaisingArgumentParser(prog='PROG', add_help=False) |
---|
4633 | n/a | parser.add_argument('x', type=spam) |
---|
4634 | n/a | with self.assertRaises(ArgumentParserError) as cm: |
---|
4635 | n/a | parser.parse_args(['XXX']) |
---|
4636 | n/a | self.assertEqual('usage: PROG x\nPROG: error: argument x: spam!\n', |
---|
4637 | n/a | cm.exception.stderr) |
---|
4638 | n/a | |
---|
4639 | n/a | # ========================= |
---|
4640 | n/a | # MessageContentError tests |
---|
4641 | n/a | # ========================= |
---|
4642 | n/a | |
---|
4643 | n/a | class TestMessageContentError(TestCase): |
---|
4644 | n/a | |
---|
4645 | n/a | def test_missing_argument_name_in_message(self): |
---|
4646 | n/a | parser = ErrorRaisingArgumentParser(prog='PROG', usage='') |
---|
4647 | n/a | parser.add_argument('req_pos', type=str) |
---|
4648 | n/a | parser.add_argument('-req_opt', type=int, required=True) |
---|
4649 | n/a | parser.add_argument('need_one', type=str, nargs='+') |
---|
4650 | n/a | |
---|
4651 | n/a | with self.assertRaises(ArgumentParserError) as cm: |
---|
4652 | n/a | parser.parse_args([]) |
---|
4653 | n/a | msg = str(cm.exception) |
---|
4654 | n/a | self.assertRegex(msg, 'req_pos') |
---|
4655 | n/a | self.assertRegex(msg, 'req_opt') |
---|
4656 | n/a | self.assertRegex(msg, 'need_one') |
---|
4657 | n/a | with self.assertRaises(ArgumentParserError) as cm: |
---|
4658 | n/a | parser.parse_args(['myXargument']) |
---|
4659 | n/a | msg = str(cm.exception) |
---|
4660 | n/a | self.assertNotIn(msg, 'req_pos') |
---|
4661 | n/a | self.assertRegex(msg, 'req_opt') |
---|
4662 | n/a | self.assertRegex(msg, 'need_one') |
---|
4663 | n/a | with self.assertRaises(ArgumentParserError) as cm: |
---|
4664 | n/a | parser.parse_args(['myXargument', '-req_opt=1']) |
---|
4665 | n/a | msg = str(cm.exception) |
---|
4666 | n/a | self.assertNotIn(msg, 'req_pos') |
---|
4667 | n/a | self.assertNotIn(msg, 'req_opt') |
---|
4668 | n/a | self.assertRegex(msg, 'need_one') |
---|
4669 | n/a | |
---|
4670 | n/a | def test_optional_optional_not_in_message(self): |
---|
4671 | n/a | parser = ErrorRaisingArgumentParser(prog='PROG', usage='') |
---|
4672 | n/a | parser.add_argument('req_pos', type=str) |
---|
4673 | n/a | parser.add_argument('--req_opt', type=int, required=True) |
---|
4674 | n/a | parser.add_argument('--opt_opt', type=bool, nargs='?', |
---|
4675 | n/a | default=True) |
---|
4676 | n/a | with self.assertRaises(ArgumentParserError) as cm: |
---|
4677 | n/a | parser.parse_args([]) |
---|
4678 | n/a | msg = str(cm.exception) |
---|
4679 | n/a | self.assertRegex(msg, 'req_pos') |
---|
4680 | n/a | self.assertRegex(msg, 'req_opt') |
---|
4681 | n/a | self.assertNotIn(msg, 'opt_opt') |
---|
4682 | n/a | with self.assertRaises(ArgumentParserError) as cm: |
---|
4683 | n/a | parser.parse_args(['--req_opt=1']) |
---|
4684 | n/a | msg = str(cm.exception) |
---|
4685 | n/a | self.assertRegex(msg, 'req_pos') |
---|
4686 | n/a | self.assertNotIn(msg, 'req_opt') |
---|
4687 | n/a | self.assertNotIn(msg, 'opt_opt') |
---|
4688 | n/a | |
---|
4689 | n/a | def test_optional_positional_not_in_message(self): |
---|
4690 | n/a | parser = ErrorRaisingArgumentParser(prog='PROG', usage='') |
---|
4691 | n/a | parser.add_argument('req_pos') |
---|
4692 | n/a | parser.add_argument('optional_positional', nargs='?', default='eggs') |
---|
4693 | n/a | with self.assertRaises(ArgumentParserError) as cm: |
---|
4694 | n/a | parser.parse_args([]) |
---|
4695 | n/a | msg = str(cm.exception) |
---|
4696 | n/a | self.assertRegex(msg, 'req_pos') |
---|
4697 | n/a | self.assertNotIn(msg, 'optional_positional') |
---|
4698 | n/a | |
---|
4699 | n/a | |
---|
4700 | n/a | # ================================================ |
---|
4701 | n/a | # Check that the type function is called only once |
---|
4702 | n/a | # ================================================ |
---|
4703 | n/a | |
---|
4704 | n/a | class TestTypeFunctionCallOnlyOnce(TestCase): |
---|
4705 | n/a | |
---|
4706 | n/a | def test_type_function_call_only_once(self): |
---|
4707 | n/a | def spam(string_to_convert): |
---|
4708 | n/a | self.assertEqual(string_to_convert, 'spam!') |
---|
4709 | n/a | return 'foo_converted' |
---|
4710 | n/a | |
---|
4711 | n/a | parser = argparse.ArgumentParser() |
---|
4712 | n/a | parser.add_argument('--foo', type=spam, default='bar') |
---|
4713 | n/a | args = parser.parse_args('--foo spam!'.split()) |
---|
4714 | n/a | self.assertEqual(NS(foo='foo_converted'), args) |
---|
4715 | n/a | |
---|
4716 | n/a | # ================================================================== |
---|
4717 | n/a | # Check semantics regarding the default argument and type conversion |
---|
4718 | n/a | # ================================================================== |
---|
4719 | n/a | |
---|
4720 | n/a | class TestTypeFunctionCalledOnDefault(TestCase): |
---|
4721 | n/a | |
---|
4722 | n/a | def test_type_function_call_with_non_string_default(self): |
---|
4723 | n/a | def spam(int_to_convert): |
---|
4724 | n/a | self.assertEqual(int_to_convert, 0) |
---|
4725 | n/a | return 'foo_converted' |
---|
4726 | n/a | |
---|
4727 | n/a | parser = argparse.ArgumentParser() |
---|
4728 | n/a | parser.add_argument('--foo', type=spam, default=0) |
---|
4729 | n/a | args = parser.parse_args([]) |
---|
4730 | n/a | # foo should *not* be converted because its default is not a string. |
---|
4731 | n/a | self.assertEqual(NS(foo=0), args) |
---|
4732 | n/a | |
---|
4733 | n/a | def test_type_function_call_with_string_default(self): |
---|
4734 | n/a | def spam(int_to_convert): |
---|
4735 | n/a | return 'foo_converted' |
---|
4736 | n/a | |
---|
4737 | n/a | parser = argparse.ArgumentParser() |
---|
4738 | n/a | parser.add_argument('--foo', type=spam, default='0') |
---|
4739 | n/a | args = parser.parse_args([]) |
---|
4740 | n/a | # foo is converted because its default is a string. |
---|
4741 | n/a | self.assertEqual(NS(foo='foo_converted'), args) |
---|
4742 | n/a | |
---|
4743 | n/a | def test_no_double_type_conversion_of_default(self): |
---|
4744 | n/a | def extend(str_to_convert): |
---|
4745 | n/a | return str_to_convert + '*' |
---|
4746 | n/a | |
---|
4747 | n/a | parser = argparse.ArgumentParser() |
---|
4748 | n/a | parser.add_argument('--test', type=extend, default='*') |
---|
4749 | n/a | args = parser.parse_args([]) |
---|
4750 | n/a | # The test argument will be two stars, one coming from the default |
---|
4751 | n/a | # value and one coming from the type conversion being called exactly |
---|
4752 | n/a | # once. |
---|
4753 | n/a | self.assertEqual(NS(test='**'), args) |
---|
4754 | n/a | |
---|
4755 | n/a | def test_issue_15906(self): |
---|
4756 | n/a | # Issue #15906: When action='append', type=str, default=[] are |
---|
4757 | n/a | # providing, the dest value was the string representation "[]" when it |
---|
4758 | n/a | # should have been an empty list. |
---|
4759 | n/a | parser = argparse.ArgumentParser() |
---|
4760 | n/a | parser.add_argument('--test', dest='test', type=str, |
---|
4761 | n/a | default=[], action='append') |
---|
4762 | n/a | args = parser.parse_args([]) |
---|
4763 | n/a | self.assertEqual(args.test, []) |
---|
4764 | n/a | |
---|
4765 | n/a | # ====================== |
---|
4766 | n/a | # parse_known_args tests |
---|
4767 | n/a | # ====================== |
---|
4768 | n/a | |
---|
4769 | n/a | class TestParseKnownArgs(TestCase): |
---|
4770 | n/a | |
---|
4771 | n/a | def test_arguments_tuple(self): |
---|
4772 | n/a | parser = argparse.ArgumentParser() |
---|
4773 | n/a | parser.parse_args(()) |
---|
4774 | n/a | |
---|
4775 | n/a | def test_arguments_list(self): |
---|
4776 | n/a | parser = argparse.ArgumentParser() |
---|
4777 | n/a | parser.parse_args([]) |
---|
4778 | n/a | |
---|
4779 | n/a | def test_arguments_tuple_positional(self): |
---|
4780 | n/a | parser = argparse.ArgumentParser() |
---|
4781 | n/a | parser.add_argument('x') |
---|
4782 | n/a | parser.parse_args(('x',)) |
---|
4783 | n/a | |
---|
4784 | n/a | def test_arguments_list_positional(self): |
---|
4785 | n/a | parser = argparse.ArgumentParser() |
---|
4786 | n/a | parser.add_argument('x') |
---|
4787 | n/a | parser.parse_args(['x']) |
---|
4788 | n/a | |
---|
4789 | n/a | def test_optionals(self): |
---|
4790 | n/a | parser = argparse.ArgumentParser() |
---|
4791 | n/a | parser.add_argument('--foo') |
---|
4792 | n/a | args, extras = parser.parse_known_args('--foo F --bar --baz'.split()) |
---|
4793 | n/a | self.assertEqual(NS(foo='F'), args) |
---|
4794 | n/a | self.assertEqual(['--bar', '--baz'], extras) |
---|
4795 | n/a | |
---|
4796 | n/a | def test_mixed(self): |
---|
4797 | n/a | parser = argparse.ArgumentParser() |
---|
4798 | n/a | parser.add_argument('-v', nargs='?', const=1, type=int) |
---|
4799 | n/a | parser.add_argument('--spam', action='store_false') |
---|
4800 | n/a | parser.add_argument('badger') |
---|
4801 | n/a | |
---|
4802 | n/a | argv = ["B", "C", "--foo", "-v", "3", "4"] |
---|
4803 | n/a | args, extras = parser.parse_known_args(argv) |
---|
4804 | n/a | self.assertEqual(NS(v=3, spam=True, badger="B"), args) |
---|
4805 | n/a | self.assertEqual(["C", "--foo", "4"], extras) |
---|
4806 | n/a | |
---|
4807 | n/a | # ========================== |
---|
4808 | n/a | # add_argument metavar tests |
---|
4809 | n/a | # ========================== |
---|
4810 | n/a | |
---|
4811 | n/a | class TestAddArgumentMetavar(TestCase): |
---|
4812 | n/a | |
---|
4813 | n/a | EXPECTED_MESSAGE = "length of metavar tuple does not match nargs" |
---|
4814 | n/a | |
---|
4815 | n/a | def do_test_no_exception(self, nargs, metavar): |
---|
4816 | n/a | parser = argparse.ArgumentParser() |
---|
4817 | n/a | parser.add_argument("--foo", nargs=nargs, metavar=metavar) |
---|
4818 | n/a | |
---|
4819 | n/a | def do_test_exception(self, nargs, metavar): |
---|
4820 | n/a | parser = argparse.ArgumentParser() |
---|
4821 | n/a | with self.assertRaises(ValueError) as cm: |
---|
4822 | n/a | parser.add_argument("--foo", nargs=nargs, metavar=metavar) |
---|
4823 | n/a | self.assertEqual(cm.exception.args[0], self.EXPECTED_MESSAGE) |
---|
4824 | n/a | |
---|
4825 | n/a | # Unit tests for different values of metavar when nargs=None |
---|
4826 | n/a | |
---|
4827 | n/a | def test_nargs_None_metavar_string(self): |
---|
4828 | n/a | self.do_test_no_exception(nargs=None, metavar="1") |
---|
4829 | n/a | |
---|
4830 | n/a | def test_nargs_None_metavar_length0(self): |
---|
4831 | n/a | self.do_test_exception(nargs=None, metavar=tuple()) |
---|
4832 | n/a | |
---|
4833 | n/a | def test_nargs_None_metavar_length1(self): |
---|
4834 | n/a | self.do_test_no_exception(nargs=None, metavar=("1")) |
---|
4835 | n/a | |
---|
4836 | n/a | def test_nargs_None_metavar_length2(self): |
---|
4837 | n/a | self.do_test_exception(nargs=None, metavar=("1", "2")) |
---|
4838 | n/a | |
---|
4839 | n/a | def test_nargs_None_metavar_length3(self): |
---|
4840 | n/a | self.do_test_exception(nargs=None, metavar=("1", "2", "3")) |
---|
4841 | n/a | |
---|
4842 | n/a | # Unit tests for different values of metavar when nargs=? |
---|
4843 | n/a | |
---|
4844 | n/a | def test_nargs_optional_metavar_string(self): |
---|
4845 | n/a | self.do_test_no_exception(nargs="?", metavar="1") |
---|
4846 | n/a | |
---|
4847 | n/a | def test_nargs_optional_metavar_length0(self): |
---|
4848 | n/a | self.do_test_exception(nargs="?", metavar=tuple()) |
---|
4849 | n/a | |
---|
4850 | n/a | def test_nargs_optional_metavar_length1(self): |
---|
4851 | n/a | self.do_test_no_exception(nargs="?", metavar=("1")) |
---|
4852 | n/a | |
---|
4853 | n/a | def test_nargs_optional_metavar_length2(self): |
---|
4854 | n/a | self.do_test_exception(nargs="?", metavar=("1", "2")) |
---|
4855 | n/a | |
---|
4856 | n/a | def test_nargs_optional_metavar_length3(self): |
---|
4857 | n/a | self.do_test_exception(nargs="?", metavar=("1", "2", "3")) |
---|
4858 | n/a | |
---|
4859 | n/a | # Unit tests for different values of metavar when nargs=* |
---|
4860 | n/a | |
---|
4861 | n/a | def test_nargs_zeroormore_metavar_string(self): |
---|
4862 | n/a | self.do_test_no_exception(nargs="*", metavar="1") |
---|
4863 | n/a | |
---|
4864 | n/a | def test_nargs_zeroormore_metavar_length0(self): |
---|
4865 | n/a | self.do_test_exception(nargs="*", metavar=tuple()) |
---|
4866 | n/a | |
---|
4867 | n/a | def test_nargs_zeroormore_metavar_length1(self): |
---|
4868 | n/a | self.do_test_no_exception(nargs="*", metavar=("1")) |
---|
4869 | n/a | |
---|
4870 | n/a | def test_nargs_zeroormore_metavar_length2(self): |
---|
4871 | n/a | self.do_test_no_exception(nargs="*", metavar=("1", "2")) |
---|
4872 | n/a | |
---|
4873 | n/a | def test_nargs_zeroormore_metavar_length3(self): |
---|
4874 | n/a | self.do_test_exception(nargs="*", metavar=("1", "2", "3")) |
---|
4875 | n/a | |
---|
4876 | n/a | # Unit tests for different values of metavar when nargs=+ |
---|
4877 | n/a | |
---|
4878 | n/a | def test_nargs_oneormore_metavar_string(self): |
---|
4879 | n/a | self.do_test_no_exception(nargs="+", metavar="1") |
---|
4880 | n/a | |
---|
4881 | n/a | def test_nargs_oneormore_metavar_length0(self): |
---|
4882 | n/a | self.do_test_exception(nargs="+", metavar=tuple()) |
---|
4883 | n/a | |
---|
4884 | n/a | def test_nargs_oneormore_metavar_length1(self): |
---|
4885 | n/a | self.do_test_no_exception(nargs="+", metavar=("1")) |
---|
4886 | n/a | |
---|
4887 | n/a | def test_nargs_oneormore_metavar_length2(self): |
---|
4888 | n/a | self.do_test_no_exception(nargs="+", metavar=("1", "2")) |
---|
4889 | n/a | |
---|
4890 | n/a | def test_nargs_oneormore_metavar_length3(self): |
---|
4891 | n/a | self.do_test_exception(nargs="+", metavar=("1", "2", "3")) |
---|
4892 | n/a | |
---|
4893 | n/a | # Unit tests for different values of metavar when nargs=... |
---|
4894 | n/a | |
---|
4895 | n/a | def test_nargs_remainder_metavar_string(self): |
---|
4896 | n/a | self.do_test_no_exception(nargs="...", metavar="1") |
---|
4897 | n/a | |
---|
4898 | n/a | def test_nargs_remainder_metavar_length0(self): |
---|
4899 | n/a | self.do_test_no_exception(nargs="...", metavar=tuple()) |
---|
4900 | n/a | |
---|
4901 | n/a | def test_nargs_remainder_metavar_length1(self): |
---|
4902 | n/a | self.do_test_no_exception(nargs="...", metavar=("1")) |
---|
4903 | n/a | |
---|
4904 | n/a | def test_nargs_remainder_metavar_length2(self): |
---|
4905 | n/a | self.do_test_no_exception(nargs="...", metavar=("1", "2")) |
---|
4906 | n/a | |
---|
4907 | n/a | def test_nargs_remainder_metavar_length3(self): |
---|
4908 | n/a | self.do_test_no_exception(nargs="...", metavar=("1", "2", "3")) |
---|
4909 | n/a | |
---|
4910 | n/a | # Unit tests for different values of metavar when nargs=A... |
---|
4911 | n/a | |
---|
4912 | n/a | def test_nargs_parser_metavar_string(self): |
---|
4913 | n/a | self.do_test_no_exception(nargs="A...", metavar="1") |
---|
4914 | n/a | |
---|
4915 | n/a | def test_nargs_parser_metavar_length0(self): |
---|
4916 | n/a | self.do_test_exception(nargs="A...", metavar=tuple()) |
---|
4917 | n/a | |
---|
4918 | n/a | def test_nargs_parser_metavar_length1(self): |
---|
4919 | n/a | self.do_test_no_exception(nargs="A...", metavar=("1")) |
---|
4920 | n/a | |
---|
4921 | n/a | def test_nargs_parser_metavar_length2(self): |
---|
4922 | n/a | self.do_test_exception(nargs="A...", metavar=("1", "2")) |
---|
4923 | n/a | |
---|
4924 | n/a | def test_nargs_parser_metavar_length3(self): |
---|
4925 | n/a | self.do_test_exception(nargs="A...", metavar=("1", "2", "3")) |
---|
4926 | n/a | |
---|
4927 | n/a | # Unit tests for different values of metavar when nargs=1 |
---|
4928 | n/a | |
---|
4929 | n/a | def test_nargs_1_metavar_string(self): |
---|
4930 | n/a | self.do_test_no_exception(nargs=1, metavar="1") |
---|
4931 | n/a | |
---|
4932 | n/a | def test_nargs_1_metavar_length0(self): |
---|
4933 | n/a | self.do_test_exception(nargs=1, metavar=tuple()) |
---|
4934 | n/a | |
---|
4935 | n/a | def test_nargs_1_metavar_length1(self): |
---|
4936 | n/a | self.do_test_no_exception(nargs=1, metavar=("1")) |
---|
4937 | n/a | |
---|
4938 | n/a | def test_nargs_1_metavar_length2(self): |
---|
4939 | n/a | self.do_test_exception(nargs=1, metavar=("1", "2")) |
---|
4940 | n/a | |
---|
4941 | n/a | def test_nargs_1_metavar_length3(self): |
---|
4942 | n/a | self.do_test_exception(nargs=1, metavar=("1", "2", "3")) |
---|
4943 | n/a | |
---|
4944 | n/a | # Unit tests for different values of metavar when nargs=2 |
---|
4945 | n/a | |
---|
4946 | n/a | def test_nargs_2_metavar_string(self): |
---|
4947 | n/a | self.do_test_no_exception(nargs=2, metavar="1") |
---|
4948 | n/a | |
---|
4949 | n/a | def test_nargs_2_metavar_length0(self): |
---|
4950 | n/a | self.do_test_exception(nargs=2, metavar=tuple()) |
---|
4951 | n/a | |
---|
4952 | n/a | def test_nargs_2_metavar_length1(self): |
---|
4953 | n/a | self.do_test_no_exception(nargs=2, metavar=("1")) |
---|
4954 | n/a | |
---|
4955 | n/a | def test_nargs_2_metavar_length2(self): |
---|
4956 | n/a | self.do_test_no_exception(nargs=2, metavar=("1", "2")) |
---|
4957 | n/a | |
---|
4958 | n/a | def test_nargs_2_metavar_length3(self): |
---|
4959 | n/a | self.do_test_exception(nargs=2, metavar=("1", "2", "3")) |
---|
4960 | n/a | |
---|
4961 | n/a | # Unit tests for different values of metavar when nargs=3 |
---|
4962 | n/a | |
---|
4963 | n/a | def test_nargs_3_metavar_string(self): |
---|
4964 | n/a | self.do_test_no_exception(nargs=3, metavar="1") |
---|
4965 | n/a | |
---|
4966 | n/a | def test_nargs_3_metavar_length0(self): |
---|
4967 | n/a | self.do_test_exception(nargs=3, metavar=tuple()) |
---|
4968 | n/a | |
---|
4969 | n/a | def test_nargs_3_metavar_length1(self): |
---|
4970 | n/a | self.do_test_no_exception(nargs=3, metavar=("1")) |
---|
4971 | n/a | |
---|
4972 | n/a | def test_nargs_3_metavar_length2(self): |
---|
4973 | n/a | self.do_test_exception(nargs=3, metavar=("1", "2")) |
---|
4974 | n/a | |
---|
4975 | n/a | def test_nargs_3_metavar_length3(self): |
---|
4976 | n/a | self.do_test_no_exception(nargs=3, metavar=("1", "2", "3")) |
---|
4977 | n/a | |
---|
4978 | n/a | # ============================ |
---|
4979 | n/a | # from argparse import * tests |
---|
4980 | n/a | # ============================ |
---|
4981 | n/a | |
---|
4982 | n/a | class TestImportStar(TestCase): |
---|
4983 | n/a | |
---|
4984 | n/a | def test(self): |
---|
4985 | n/a | for name in argparse.__all__: |
---|
4986 | n/a | self.assertTrue(hasattr(argparse, name)) |
---|
4987 | n/a | |
---|
4988 | n/a | def test_all_exports_everything_but_modules(self): |
---|
4989 | n/a | items = [ |
---|
4990 | n/a | name |
---|
4991 | n/a | for name, value in vars(argparse).items() |
---|
4992 | n/a | if not (name.startswith("_") or name == 'ngettext') |
---|
4993 | n/a | if not inspect.ismodule(value) |
---|
4994 | n/a | ] |
---|
4995 | n/a | self.assertEqual(sorted(items), sorted(argparse.__all__)) |
---|
4996 | n/a | |
---|
4997 | n/a | def test_main(): |
---|
4998 | n/a | support.run_unittest(__name__) |
---|
4999 | n/a | # Remove global references to avoid looking like we have refleaks. |
---|
5000 | n/a | RFile.seen = {} |
---|
5001 | n/a | WFile.seen = set() |
---|
5002 | n/a | |
---|
5003 | n/a | |
---|
5004 | n/a | |
---|
5005 | n/a | if __name__ == '__main__': |
---|
5006 | n/a | test_main() |
---|