@@ -4480,6 +4480,67 @@ def spam(string):
4480
4480
else :
4481
4481
self .fail ()
4482
4482
4483
+ # =========================
4484
+ # MessageContentError tests
4485
+ # =========================
4486
+
4487
+ class TestMessageContentError (TestCase ):
4488
+
4489
+ def test_missing_argument_name_in_message (self ):
4490
+ parser = ErrorRaisingArgumentParser (prog = 'PROG' , usage = '' )
4491
+ parser .add_argument ('req_pos' , type = str )
4492
+ parser .add_argument ('-req_opt' , type = int , required = True )
4493
+ parser .add_argument ('need_one' , type = str , nargs = '+' )
4494
+
4495
+ with self .assertRaises (ArgumentParserError ) as cm :
4496
+ parser .parse_args ([])
4497
+ msg = str (cm .exception )
4498
+ self .assertRegex (msg , 'req_pos' )
4499
+ self .assertRegex (msg , 'req_opt' )
4500
+ self .assertRegex (msg , 'need_one' )
4501
+ with self .assertRaises (ArgumentParserError ) as cm :
4502
+ parser .parse_args (['myXargument' ])
4503
+ msg = str (cm .exception )
4504
+ self .assertNotIn (msg , 'req_pos' )
4505
+ self .assertRegex (msg , 'req_opt' )
4506
+ self .assertRegex (msg , 'need_one' )
4507
+ with self .assertRaises (ArgumentParserError ) as cm :
4508
+ parser .parse_args (['myXargument' , '-req_opt=1' ])
4509
+ msg = str (cm .exception )
4510
+ self .assertNotIn (msg , 'req_pos' )
4511
+ self .assertNotIn (msg , 'req_opt' )
4512
+ self .assertRegex (msg , 'need_one' )
4513
+
4514
+ def test_optional_optional_not_in_message (self ):
4515
+ parser = ErrorRaisingArgumentParser (prog = 'PROG' , usage = '' )
4516
+ parser .add_argument ('req_pos' , type = str )
4517
+ parser .add_argument ('--req_opt' , type = int , required = True )
4518
+ parser .add_argument ('--opt_opt' , type = bool , nargs = '?' ,
4519
+ default = True )
4520
+ with self .assertRaises (ArgumentParserError ) as cm :
4521
+ parser .parse_args ([])
4522
+ msg = str (cm .exception )
4523
+ self .assertRegex (msg , 'req_pos' )
4524
+ self .assertRegex (msg , 'req_opt' )
4525
+ self .assertNotIn (msg , 'opt_opt' )
4526
+ with self .assertRaises (ArgumentParserError ) as cm :
4527
+ parser .parse_args (['--req_opt=1' ])
4528
+ msg = str (cm .exception )
4529
+ self .assertRegex (msg , 'req_pos' )
4530
+ self .assertNotIn (msg , 'req_opt' )
4531
+ self .assertNotIn (msg , 'opt_opt' )
4532
+
4533
+ def test_optional_positional_not_in_message (self ):
4534
+ parser = ErrorRaisingArgumentParser (prog = 'PROG' , usage = '' )
4535
+ parser .add_argument ('req_pos' )
4536
+ parser .add_argument ('optional_positional' , nargs = '?' , default = 'eggs' )
4537
+ with self .assertRaises (ArgumentParserError ) as cm :
4538
+ parser .parse_args ([])
4539
+ msg = str (cm .exception )
4540
+ self .assertRegex (msg , 'req_pos' )
4541
+ self .assertNotIn (msg , 'optional_positional' )
4542
+
4543
+
4483
4544
# ======================
4484
4545
# parse_known_args tests
4485
4546
# ======================
0 commit comments