@@ -2409,16 +2409,17 @@ def assertArgumentParserError(self, *args, **kwargs):
2409
2409
self .assertRaises (ArgumentParserError , * args , ** kwargs )
2410
2410
2411
2411
def _get_parser (self , subparser_help = False , prefix_chars = None ,
2412
- aliases = False ):
2412
+ aliases = False , usage = None ):
2413
2413
# create a parser with a subparsers argument
2414
2414
if prefix_chars :
2415
2415
parser = ErrorRaisingArgumentParser (
2416
- prog = 'PROG' , description = 'main description' , prefix_chars = prefix_chars )
2416
+ prog = 'PROG' , description = 'main description' , usage = usage ,
2417
+ prefix_chars = prefix_chars )
2417
2418
parser .add_argument (
2418
2419
prefix_chars [0 ] * 2 + 'foo' , action = 'store_true' , help = 'foo help' )
2419
2420
else :
2420
2421
parser = ErrorRaisingArgumentParser (
2421
- prog = 'PROG' , description = 'main description' )
2422
+ prog = 'PROG' , description = 'main description' , usage = usage )
2422
2423
parser .add_argument (
2423
2424
'--foo' , action = 'store_true' , help = 'foo help' )
2424
2425
parser .add_argument (
@@ -2455,7 +2456,8 @@ def _get_parser(self, subparser_help=False, prefix_chars=None,
2455
2456
parser2 .add_argument ('z' , type = complex , nargs = '*' , help = 'z help' )
2456
2457
2457
2458
# add third sub-parser
2458
- parser3_kwargs = dict (description = '3 description' )
2459
+ parser3_kwargs = dict (description = '3 description' ,
2460
+ usage = 'PROG --foo bar 3 t ...' )
2459
2461
if subparser_help :
2460
2462
parser3_kwargs ['help' ] = '3 help'
2461
2463
parser3 = subparsers .add_parser ('3' , ** parser3_kwargs )
@@ -2477,6 +2479,47 @@ def test_parse_args_failures(self):
2477
2479
args = args_str .split ()
2478
2480
self .assertArgumentParserError (self .parser .parse_args , args )
2479
2481
2482
+ def test_parse_args_failures_details (self ):
2483
+ for args_str , usage_str , error_str in [
2484
+ ('' ,
2485
+ 'usage: PROG [-h] [--foo] bar {1,2,3} ...' ,
2486
+ 'PROG: error: the following arguments are required: bar' ),
2487
+ ('0.5 1 -y' ,
2488
+ 'usage: PROG bar 1 [-h] [-w W] {a,b,c}' ,
2489
+ 'PROG bar 1: error: the following arguments are required: x' ),
2490
+ ('0.5 3' ,
2491
+ 'usage: PROG --foo bar 3 t ...' ,
2492
+ 'PROG bar 3: error: the following arguments are required: t' ),
2493
+ ]:
2494
+ with self .subTest (args_str ):
2495
+ args = args_str .split ()
2496
+ with self .assertRaises (ArgumentParserError ) as cm :
2497
+ self .parser .parse_args (args )
2498
+ self .assertEqual (cm .exception .args [0 ], 'SystemExit' )
2499
+ self .assertEqual (cm .exception .args [2 ], f'{ usage_str } \n { error_str } \n ' )
2500
+
2501
+ def test_parse_args_failures_details_custom_usage (self ):
2502
+ parser = self ._get_parser (usage = 'PROG [--foo] bar 1 [-w W] {a,b,c}\n '
2503
+ ' PROG --foo bar 3 t ...' )
2504
+ for args_str , usage_str , error_str in [
2505
+ ('' ,
2506
+ 'usage: PROG [--foo] bar 1 [-w W] {a,b,c}\n '
2507
+ ' PROG --foo bar 3 t ...' ,
2508
+ 'PROG: error: the following arguments are required: bar' ),
2509
+ ('0.5 1 -y' ,
2510
+ 'usage: PROG bar 1 [-h] [-w W] {a,b,c}' ,
2511
+ 'PROG bar 1: error: the following arguments are required: x' ),
2512
+ ('0.5 3' ,
2513
+ 'usage: PROG --foo bar 3 t ...' ,
2514
+ 'PROG bar 3: error: the following arguments are required: t' ),
2515
+ ]:
2516
+ with self .subTest (args_str ):
2517
+ args = args_str .split ()
2518
+ with self .assertRaises (ArgumentParserError ) as cm :
2519
+ parser .parse_args (args )
2520
+ self .assertEqual (cm .exception .args [0 ], 'SystemExit' )
2521
+ self .assertEqual (cm .exception .args [2 ], f'{ usage_str } \n { error_str } \n ' )
2522
+
2480
2523
def test_parse_args (self ):
2481
2524
# check some non-failure cases:
2482
2525
self .assertEqual (
0 commit comments