@@ -2388,16 +2388,17 @@ def assertArgumentParserError(self, *args, **kwargs):
23882388 self .assertRaises (ArgumentParserError , * args , ** kwargs )
23892389
23902390 def _get_parser (self , subparser_help = False , prefix_chars = None ,
2391- aliases = False ):
2391+ aliases = False , usage = None ):
23922392 # create a parser with a subparsers argument
23932393 if prefix_chars :
23942394 parser = ErrorRaisingArgumentParser (
2395- prog = 'PROG' , description = 'main description' , prefix_chars = prefix_chars )
2395+ prog = 'PROG' , description = 'main description' , usage = usage ,
2396+ prefix_chars = prefix_chars )
23962397 parser .add_argument (
23972398 prefix_chars [0 ] * 2 + 'foo' , action = 'store_true' , help = 'foo help' )
23982399 else :
23992400 parser = ErrorRaisingArgumentParser (
2400- prog = 'PROG' , description = 'main description' )
2401+ prog = 'PROG' , description = 'main description' , usage = usage )
24012402 parser .add_argument (
24022403 '--foo' , action = 'store_true' , help = 'foo help' )
24032404 parser .add_argument (
@@ -2434,7 +2435,8 @@ def _get_parser(self, subparser_help=False, prefix_chars=None,
24342435 parser2 .add_argument ('z' , type = complex , nargs = '*' , help = 'z help' )
24352436
24362437 # add third sub-parser
2437- parser3_kwargs = dict (description = '3 description' )
2438+ parser3_kwargs = dict (description = '3 description' ,
2439+ usage = 'PROG --foo bar 3 t ...' )
24382440 if subparser_help :
24392441 parser3_kwargs ['help' ] = '3 help'
24402442 parser3 = subparsers .add_parser ('3' , ** parser3_kwargs )
@@ -2456,6 +2458,47 @@ def test_parse_args_failures(self):
24562458 args = args_str .split ()
24572459 self .assertArgumentParserError (self .parser .parse_args , args )
24582460
2461+ def test_parse_args_failures_details (self ):
2462+ for args_str , usage_str , error_str in [
2463+ ('' ,
2464+ 'usage: PROG [-h] [--foo] bar {1,2,3} ...' ,
2465+ 'PROG: error: the following arguments are required: bar' ),
2466+ ('0.5 1 -y' ,
2467+ 'usage: PROG bar 1 [-h] [-w W] {a,b,c}' ,
2468+ 'PROG bar 1: error: the following arguments are required: x' ),
2469+ ('0.5 3' ,
2470+ 'usage: PROG --foo bar 3 t ...' ,
2471+ 'PROG bar 3: error: the following arguments are required: t' ),
2472+ ]:
2473+ with self .subTest (args_str ):
2474+ args = args_str .split ()
2475+ with self .assertRaises (ArgumentParserError ) as cm :
2476+ self .parser .parse_args (args )
2477+ self .assertEqual (cm .exception .args [0 ], 'SystemExit' )
2478+ self .assertEqual (cm .exception .args [2 ], f'{ usage_str } \n { error_str } \n ' )
2479+
2480+ def test_parse_args_failures_details_custom_usage (self ):
2481+ parser = self ._get_parser (usage = 'PROG [--foo] bar 1 [-w W] {a,b,c}\n '
2482+ ' PROG --foo bar 3 t ...' )
2483+ for args_str , usage_str , error_str in [
2484+ ('' ,
2485+ 'usage: PROG [--foo] bar 1 [-w W] {a,b,c}\n '
2486+ ' PROG --foo bar 3 t ...' ,
2487+ 'PROG: error: the following arguments are required: bar' ),
2488+ ('0.5 1 -y' ,
2489+ 'usage: PROG bar 1 [-h] [-w W] {a,b,c}' ,
2490+ 'PROG bar 1: error: the following arguments are required: x' ),
2491+ ('0.5 3' ,
2492+ 'usage: PROG --foo bar 3 t ...' ,
2493+ 'PROG bar 3: error: the following arguments are required: t' ),
2494+ ]:
2495+ with self .subTest (args_str ):
2496+ args = args_str .split ()
2497+ with self .assertRaises (ArgumentParserError ) as cm :
2498+ parser .parse_args (args )
2499+ self .assertEqual (cm .exception .args [0 ], 'SystemExit' )
2500+ self .assertEqual (cm .exception .args [2 ], f'{ usage_str } \n { error_str } \n ' )
2501+
24592502 def test_parse_args (self ):
24602503 # check some non-failure cases:
24612504 self .assertEqual (
0 commit comments