Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(6)

Delta Between Two Patch Sets: Lib/test/test_argparse.py

Issue 14191: argparse: nargs='*' doesn't get out-of-order positional parameters
Left Patch Set: Created 6 years, 11 months ago
Right Patch Set: Created 6 years, 10 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Lib/argparse.py ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 # Author: Steven J. Bethard <steven.bethard@gmail.com>. 1 # Author: Steven J. Bethard <steven.bethard@gmail.com>.
2 2
3 import codecs 3 import codecs
4 import inspect 4 import inspect
5 import os 5 import os
6 import shutil 6 import shutil
7 import stat 7 import stat
8 import sys 8 import sys
9 import textwrap 9 import textwrap
10 import tempfile 10 import tempfile
(...skipping 4736 matching lines...) Expand 10 before | Expand all | Expand 10 after
4747 self.assertEqual(bar.required, True) 4747 self.assertEqual(bar.required, True)
4748 4748
4749 def test_remainder(self): 4749 def test_remainder(self):
4750 # Intermixed and remainder are incompatible 4750 # Intermixed and remainder are incompatible
4751 parser = ErrorRaisingArgumentParser(prog='PROG') 4751 parser = ErrorRaisingArgumentParser(prog='PROG')
4752 parser.add_argument('-z') 4752 parser.add_argument('-z')
4753 parser.add_argument('x') 4753 parser.add_argument('x')
4754 parser.add_argument('y', nargs='...') 4754 parser.add_argument('y', nargs='...')
4755 argv = 'X A B -z Z'.split() 4755 argv = 'X A B -z Z'.split()
4756 # intermixed fails with '...' (also 'A...') 4756 # intermixed fails with '...' (also 'A...')
4757 # but there is a fallback mechanism 4757 # self.assertRaises(TypeError, parser.parse_intermixed_args, argv)
4758 self.assertRaises(ArgumentParserError, parser.parse_intermixed_args, arg v) 4758 with self.assertRaises(TypeError) as cm:
4759 args, extras = parser.parse_known_intermixed_args(argv, _fallback=parser .parse_known_args) 4759 parser.parse_intermixed_args(argv)
4760 self.assertEqual(NS(x='X', y=['A', 'B', '-z', 'Z'], z=None), args) 4760 self.assertRegex(str(cm.exception), '\.\.\.')
4761 4761
4762 def test_exclusive(self): 4762 def test_exclusive(self):
4763 # mutually exclusive group; intermixed works fine 4763 # mutually exclusive group; intermixed works fine
4764 parser = ErrorRaisingArgumentParser(prog='PROG') 4764 parser = ErrorRaisingArgumentParser(prog='PROG')
4765 group = parser.add_mutually_exclusive_group(required=True) 4765 group = parser.add_mutually_exclusive_group(required=True)
4766 group.add_argument('--foo', action='store_true', help='FOO') 4766 group.add_argument('--foo', action='store_true', help='FOO')
4767 group.add_argument('--spam', help='SPAM') 4767 group.add_argument('--spam', help='SPAM')
4768 parser.add_argument('badger', nargs='*', default='X', help='BADGER') 4768 parser.add_argument('badger', nargs='*', default='X', help='BADGER')
4769 args = parser.parse_intermixed_args('1 --foo 2'.split()) 4769 args = parser.parse_intermixed_args('1 --foo 2'.split())
4770 self.assertEqual(NS(badger=['1', '2'], foo=True, spam=None), args) 4770 self.assertEqual(NS(badger=['1', '2'], foo=True, spam=None), args)
4771 self.assertRaises(ArgumentParserError, parser.parse_intermixed_args, '1 2'.split()) 4771 self.assertRaises(ArgumentParserError, parser.parse_intermixed_args, '1 2'.split())
4772 self.assertEqual(group.required, True) 4772 self.assertEqual(group.required, True)
4773 4773
4774 def test_exclusive_incompatible(self): 4774 def test_exclusive_incompatible(self):
4775 # mutually exclusive group including positional - fail 4775 # mutually exclusive group including positional - fail
4776 parser = ErrorRaisingArgumentParser(prog='PROG') 4776 parser = ErrorRaisingArgumentParser(prog='PROG')
4777 group = parser.add_mutually_exclusive_group(required=True) 4777 group = parser.add_mutually_exclusive_group(required=True)
4778 group.add_argument('--foo', action='store_true', help='FOO') 4778 group.add_argument('--foo', action='store_true', help='FOO')
4779 group.add_argument('--spam', help='SPAM') 4779 group.add_argument('--spam', help='SPAM')
4780 group.add_argument('badger', nargs='*', default='X', help='BADGER') 4780 group.add_argument('badger', nargs='*', default='X', help='BADGER')
4781 self.assertRaises(ArgumentParserError, parser.parse_intermixed_args, []) 4781 self.assertRaises(TypeError, parser.parse_intermixed_args, [])
4782 # could include a fallback example
4783 self.assertEqual(group.required, True) 4782 self.assertEqual(group.required, True)
4784 4783
4785 class TestIntermixedMessageContentError(TestCase): 4784 class TestIntermixedMessageContentError(TestCase):
4786 # case where Intermixed gives different error message 4785 # case where Intermixed gives different error message
4787 # error is raised by 1st parsing step 4786 # error is raised by 1st parsing step
4788 def test_missing_argument_name_in_message(self): 4787 def test_missing_argument_name_in_message(self):
4789 parser = ErrorRaisingArgumentParser(prog='PROG', usage='') 4788 parser = ErrorRaisingArgumentParser(prog='PROG', usage='')
4790 parser.add_argument('req_pos', type=str) 4789 parser.add_argument('req_pos', type=str)
4791 parser.add_argument('-req_opt', type=int, required=True) 4790 parser.add_argument('-req_opt', type=int, required=True)
4792 4791
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after
4995 def test_main(): 4994 def test_main():
4996 support.run_unittest(__name__) 4995 support.run_unittest(__name__)
4997 # Remove global references to avoid looking like we have refleaks. 4996 # Remove global references to avoid looking like we have refleaks.
4998 RFile.seen = {} 4997 RFile.seen = {}
4999 WFile.seen = set() 4998 WFile.seen = set()
5000 4999
5001 5000
5002 5001
5003 if __name__ == '__main__': 5002 if __name__ == '__main__':
5004 test_main() 5003 test_main()
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+