diff options
author | R David Murray <rdmurray@bitdance.com> | 2011-06-09 16:34:07 (GMT) |
---|---|---|
committer | R David Murray <rdmurray@bitdance.com> | 2011-06-09 16:34:07 (GMT) |
commit | f97c59aaba2d93e48cbc6d25f7ff9f9c87f8d0b2 (patch) | |
tree | 44fd2f7567db3c58d2bceb724defb040701bce61 /Lib/test/test_argparse.py | |
parent | 8dd8d582e3c6d1dd80f5b958284fcb7260209bf3 (diff) | |
download | cpython-f97c59aaba2d93e48cbc6d25f7ff9f9c87f8d0b2.zip cpython-f97c59aaba2d93e48cbc6d25f7ff9f9c87f8d0b2.tar.gz cpython-f97c59aaba2d93e48cbc6d25f7ff9f9c87f8d0b2.tar.bz2 |
#10424: argument names are now included in the missing argument message
Fix and initial test patch by Michele OrrĂ¹.
Diffstat (limited to 'Lib/test/test_argparse.py')
-rw-r--r-- | Lib/test/test_argparse.py | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py index 2836e7e..b0e9a03 100644 --- a/Lib/test/test_argparse.py +++ b/Lib/test/test_argparse.py @@ -4480,6 +4480,67 @@ class TestArgumentTypeError(TestCase): else: self.fail() +# ========================= +# MessageContentError tests +# ========================= + +class TestMessageContentError(TestCase): + + def test_missing_argument_name_in_message(self): + parser = ErrorRaisingArgumentParser(prog='PROG', usage='') + parser.add_argument('req_pos', type=str) + parser.add_argument('-req_opt', type=int, required=True) + parser.add_argument('need_one', type=str, nargs='+') + + with self.assertRaises(ArgumentParserError) as cm: + parser.parse_args([]) + msg = str(cm.exception) + self.assertRegex(msg, 'req_pos') + self.assertRegex(msg, 'req_opt') + self.assertRegex(msg, 'need_one') + with self.assertRaises(ArgumentParserError) as cm: + parser.parse_args(['myXargument']) + msg = str(cm.exception) + self.assertNotIn(msg, 'req_pos') + self.assertRegex(msg, 'req_opt') + self.assertRegex(msg, 'need_one') + with self.assertRaises(ArgumentParserError) as cm: + parser.parse_args(['myXargument', '-req_opt=1']) + msg = str(cm.exception) + self.assertNotIn(msg, 'req_pos') + self.assertNotIn(msg, 'req_opt') + self.assertRegex(msg, 'need_one') + + def test_optional_optional_not_in_message(self): + parser = ErrorRaisingArgumentParser(prog='PROG', usage='') + parser.add_argument('req_pos', type=str) + parser.add_argument('--req_opt', type=int, required=True) + parser.add_argument('--opt_opt', type=bool, nargs='?', + default=True) + with self.assertRaises(ArgumentParserError) as cm: + parser.parse_args([]) + msg = str(cm.exception) + self.assertRegex(msg, 'req_pos') + self.assertRegex(msg, 'req_opt') + self.assertNotIn(msg, 'opt_opt') + with self.assertRaises(ArgumentParserError) as cm: + parser.parse_args(['--req_opt=1']) + msg = str(cm.exception) + self.assertRegex(msg, 'req_pos') + self.assertNotIn(msg, 'req_opt') + self.assertNotIn(msg, 'opt_opt') + + def test_optional_positional_not_in_message(self): + parser = ErrorRaisingArgumentParser(prog='PROG', usage='') + parser.add_argument('req_pos') + parser.add_argument('optional_positional', nargs='?', default='eggs') + with self.assertRaises(ArgumentParserError) as cm: + parser.parse_args([]) + msg = str(cm.exception) + self.assertRegex(msg, 'req_pos') + self.assertNotIn(msg, 'optional_positional') + + # ====================== # parse_known_args tests # ====================== |