@@ -2879,26 +2879,30 @@ def test_failures_when_not_required(self):
2879
2879
parse_args = self .get_parser (required = False ).parse_args
2880
2880
error = ArgumentParserError
2881
2881
for args_string in self .failures :
2882
- self .assertRaises (error , parse_args , args_string .split ())
2882
+ with self .subTest (args = args_string ):
2883
+ self .assertRaises (error , parse_args , args_string .split ())
2883
2884
2884
2885
def test_failures_when_required (self ):
2885
2886
parse_args = self .get_parser (required = True ).parse_args
2886
2887
error = ArgumentParserError
2887
2888
for args_string in self .failures + ['' ]:
2888
- self .assertRaises (error , parse_args , args_string .split ())
2889
+ with self .subTest (args = args_string ):
2890
+ self .assertRaises (error , parse_args , args_string .split ())
2889
2891
2890
2892
def test_successes_when_not_required (self ):
2891
2893
parse_args = self .get_parser (required = False ).parse_args
2892
2894
successes = self .successes + self .successes_when_not_required
2893
2895
for args_string , expected_ns in successes :
2894
- actual_ns = parse_args (args_string .split ())
2895
- self .assertEqual (actual_ns , expected_ns )
2896
+ with self .subTest (args = args_string ):
2897
+ actual_ns = parse_args (args_string .split ())
2898
+ self .assertEqual (actual_ns , expected_ns )
2896
2899
2897
2900
def test_successes_when_required (self ):
2898
2901
parse_args = self .get_parser (required = True ).parse_args
2899
2902
for args_string , expected_ns in self .successes :
2900
- actual_ns = parse_args (args_string .split ())
2901
- self .assertEqual (actual_ns , expected_ns )
2903
+ with self .subTest (args = args_string ):
2904
+ actual_ns = parse_args (args_string .split ())
2905
+ self .assertEqual (actual_ns , expected_ns )
2902
2906
2903
2907
def test_usage_when_not_required (self ):
2904
2908
format_usage = self .get_parser (required = False ).format_usage
@@ -3285,6 +3289,111 @@ def get_parser(self, required):
3285
3289
test_successes_when_not_required = None
3286
3290
test_successes_when_required = None
3287
3291
3292
+
3293
+ class TestMutuallyExclusiveOptionalOptional (MEMixin , TestCase ):
3294
+ def get_parser (self , required = None ):
3295
+ parser = ErrorRaisingArgumentParser (prog = 'PROG' )
3296
+ group = parser .add_mutually_exclusive_group (required = required )
3297
+ group .add_argument ('--foo' )
3298
+ group .add_argument ('--bar' , nargs = '?' )
3299
+ return parser
3300
+
3301
+ failures = [
3302
+ '--foo X --bar Y' ,
3303
+ '--foo X --bar' ,
3304
+ ]
3305
+ successes = [
3306
+ ('--foo X' , NS (foo = 'X' , bar = None )),
3307
+ ('--bar X' , NS (foo = None , bar = 'X' )),
3308
+ ('--bar' , NS (foo = None , bar = None )),
3309
+ ]
3310
+ successes_when_not_required = [
3311
+ ('' , NS (foo = None , bar = None )),
3312
+ ]
3313
+ usage_when_required = '''\
3314
+ usage: PROG [-h] (--foo FOO | --bar [BAR])
3315
+ '''
3316
+ usage_when_not_required = '''\
3317
+ usage: PROG [-h] [--foo FOO | --bar [BAR]]
3318
+ '''
3319
+ help = '''\
3320
+
3321
+ options:
3322
+ -h, --help show this help message and exit
3323
+ --foo FOO
3324
+ --bar [BAR]
3325
+ '''
3326
+
3327
+
3328
+ class TestMutuallyExclusiveOptionalWithDefault (MEMixin , TestCase ):
3329
+ def get_parser (self , required = None ):
3330
+ parser = ErrorRaisingArgumentParser (prog = 'PROG' )
3331
+ group = parser .add_mutually_exclusive_group (required = required )
3332
+ group .add_argument ('--foo' )
3333
+ group .add_argument ('--bar' , type = bool , default = True )
3334
+ return parser
3335
+
3336
+ failures = [
3337
+ '--foo X --bar Y' ,
3338
+ '--foo X --bar=' ,
3339
+ ]
3340
+ successes = [
3341
+ ('--foo X' , NS (foo = 'X' , bar = True )),
3342
+ ('--bar X' , NS (foo = None , bar = True )),
3343
+ ('--bar=' , NS (foo = None , bar = False )),
3344
+ ]
3345
+ successes_when_not_required = [
3346
+ ('' , NS (foo = None , bar = True )),
3347
+ ]
3348
+ usage_when_required = '''\
3349
+ usage: PROG [-h] (--foo FOO | --bar BAR)
3350
+ '''
3351
+ usage_when_not_required = '''\
3352
+ usage: PROG [-h] [--foo FOO | --bar BAR]
3353
+ '''
3354
+ help = '''\
3355
+
3356
+ options:
3357
+ -h, --help show this help message and exit
3358
+ --foo FOO
3359
+ --bar BAR
3360
+ '''
3361
+
3362
+
3363
+ class TestMutuallyExclusivePositionalWithDefault (MEMixin , TestCase ):
3364
+ def get_parser (self , required = None ):
3365
+ parser = ErrorRaisingArgumentParser (prog = 'PROG' )
3366
+ group = parser .add_mutually_exclusive_group (required = required )
3367
+ group .add_argument ('--foo' )
3368
+ group .add_argument ('bar' , nargs = '?' , type = bool , default = True )
3369
+ return parser
3370
+
3371
+ failures = [
3372
+ '--foo X Y' ,
3373
+ ]
3374
+ successes = [
3375
+ ('--foo X' , NS (foo = 'X' , bar = True )),
3376
+ ('X' , NS (foo = None , bar = True )),
3377
+ ]
3378
+ successes_when_not_required = [
3379
+ ('' , NS (foo = None , bar = True )),
3380
+ ]
3381
+ usage_when_required = '''\
3382
+ usage: PROG [-h] (--foo FOO | bar)
3383
+ '''
3384
+ usage_when_not_required = '''\
3385
+ usage: PROG [-h] [--foo FOO | bar]
3386
+ '''
3387
+ help = '''\
3388
+
3389
+ positional arguments:
3390
+ bar
3391
+
3392
+ options:
3393
+ -h, --help show this help message and exit
3394
+ --foo FOO
3395
+ '''
3396
+
3288
3397
# =================================================
3289
3398
# Mutually exclusive group in parent parser tests
3290
3399
# =================================================
0 commit comments