@@ -5469,11 +5469,60 @@ def custom_type(string):
5469
5469
version = ''
5470
5470
5471
5471
5472
- class TestHelpUsageLongSubparserCommand (TestCase ):
5473
- """Test that subparser commands are formatted correctly in help"""
5472
+ class TestHelpCustomHelpFormatter (TestCase ):
5474
5473
maxDiff = None
5475
5474
5476
- def test_parent_help (self ):
5475
+ def test_custom_formatter_function (self ):
5476
+ def custom_formatter (prog ):
5477
+ return argparse .RawTextHelpFormatter (prog , indent_increment = 5 )
5478
+
5479
+ parser = argparse .ArgumentParser (
5480
+ prog = 'PROG' ,
5481
+ prefix_chars = '-+' ,
5482
+ formatter_class = custom_formatter
5483
+ )
5484
+ parser .add_argument ('+f' , '++foo' , help = "foo help" )
5485
+ parser .add_argument ('spam' , help = "spam help" )
5486
+
5487
+ parser_help = parser .format_help ()
5488
+ self .assertEqual (parser_help , textwrap .dedent ('''\
5489
+ usage: PROG [-h] [+f FOO] spam
5490
+
5491
+ positional arguments:
5492
+ spam spam help
5493
+
5494
+ options:
5495
+ -h, --help show this help message and exit
5496
+ +f, ++foo FOO foo help
5497
+ ''' ))
5498
+
5499
+ def test_custom_formatter_class (self ):
5500
+ class CustomFormatter (argparse .RawTextHelpFormatter ):
5501
+ def __init__ (self , prog ):
5502
+ super ().__init__ (prog , indent_increment = 5 )
5503
+
5504
+ parser = argparse .ArgumentParser (
5505
+ prog = 'PROG' ,
5506
+ prefix_chars = '-+' ,
5507
+ formatter_class = CustomFormatter
5508
+ )
5509
+ parser .add_argument ('+f' , '++foo' , help = "foo help" )
5510
+ parser .add_argument ('spam' , help = "spam help" )
5511
+
5512
+ parser_help = parser .format_help ()
5513
+ self .assertEqual (parser_help , textwrap .dedent ('''\
5514
+ usage: PROG [-h] [+f FOO] spam
5515
+
5516
+ positional arguments:
5517
+ spam spam help
5518
+
5519
+ options:
5520
+ -h, --help show this help message and exit
5521
+ +f, ++foo FOO foo help
5522
+ ''' ))
5523
+
5524
+ def test_usage_long_subparser_command (self ):
5525
+ """Test that subparser commands are formatted correctly in help"""
5477
5526
def custom_formatter (prog ):
5478
5527
return argparse .RawTextHelpFormatter (prog , max_help_position = 50 )
5479
5528
@@ -7053,6 +7102,7 @@ def test_translations(self):
7053
7102
7054
7103
7055
7104
class TestColorized (TestCase ):
7105
+ maxDiff = None
7056
7106
7057
7107
def setUp (self ):
7058
7108
super ().setUp ()
@@ -7211,6 +7261,79 @@ def test_argparse_color_usage(self):
7211
7261
),
7212
7262
)
7213
7263
7264
+ def test_custom_formatter_function (self ):
7265
+ def custom_formatter (prog ):
7266
+ return argparse .RawTextHelpFormatter (prog , indent_increment = 5 )
7267
+
7268
+ parser = argparse .ArgumentParser (
7269
+ prog = "PROG" ,
7270
+ prefix_chars = "-+" ,
7271
+ formatter_class = custom_formatter ,
7272
+ color = True ,
7273
+ )
7274
+ parser .add_argument ('+f' , '++foo' , help = "foo help" )
7275
+ parser .add_argument ('spam' , help = "spam help" )
7276
+
7277
+ prog = self .theme .prog
7278
+ heading = self .theme .heading
7279
+ short = self .theme .summary_short_option
7280
+ label = self .theme .summary_label
7281
+ pos = self .theme .summary_action
7282
+ long_b = self .theme .long_option
7283
+ short_b = self .theme .short_option
7284
+ label_b = self .theme .label
7285
+ pos_b = self .theme .action
7286
+ reset = self .theme .reset
7287
+
7288
+ parser_help = parser .format_help ()
7289
+ self .assertEqual (parser_help , textwrap .dedent (f'''\
7290
+ { heading } usage: { reset } { prog } PROG{ reset } [{ short } -h{ reset } ] [{ short } +f { label } FOO{ reset } ] { pos } spam{ reset }
7291
+
7292
+ { heading } positional arguments:{ reset }
7293
+ { pos_b } spam{ reset } spam help
7294
+
7295
+ { heading } options:{ reset }
7296
+ { short_b } -h{ reset } , { long_b } --help{ reset } show this help message and exit
7297
+ { short_b } +f{ reset } , { long_b } ++foo{ reset } { label_b } FOO{ reset } foo help
7298
+ ''' ))
7299
+
7300
+ def test_custom_formatter_class (self ):
7301
+ class CustomFormatter (argparse .RawTextHelpFormatter ):
7302
+ def __init__ (self , prog ):
7303
+ super ().__init__ (prog , indent_increment = 5 )
7304
+
7305
+ parser = argparse .ArgumentParser (
7306
+ prog = "PROG" ,
7307
+ prefix_chars = "-+" ,
7308
+ formatter_class = CustomFormatter ,
7309
+ color = True ,
7310
+ )
7311
+ parser .add_argument ('+f' , '++foo' , help = "foo help" )
7312
+ parser .add_argument ('spam' , help = "spam help" )
7313
+
7314
+ prog = self .theme .prog
7315
+ heading = self .theme .heading
7316
+ short = self .theme .summary_short_option
7317
+ label = self .theme .summary_label
7318
+ pos = self .theme .summary_action
7319
+ long_b = self .theme .long_option
7320
+ short_b = self .theme .short_option
7321
+ label_b = self .theme .label
7322
+ pos_b = self .theme .action
7323
+ reset = self .theme .reset
7324
+
7325
+ parser_help = parser .format_help ()
7326
+ self .assertEqual (parser_help , textwrap .dedent (f'''\
7327
+ { heading } usage: { reset } { prog } PROG{ reset } [{ short } -h{ reset } ] [{ short } +f { label } FOO{ reset } ] { pos } spam{ reset }
7328
+
7329
+ { heading } positional arguments:{ reset }
7330
+ { pos_b } spam{ reset } spam help
7331
+
7332
+ { heading } options:{ reset }
7333
+ { short_b } -h{ reset } , { long_b } --help{ reset } show this help message and exit
7334
+ { short_b } +f{ reset } , { long_b } ++foo{ reset } { label_b } FOO{ reset } foo help
7335
+ ''' ))
7336
+
7214
7337
7215
7338
def tearDownModule ():
7216
7339
# Remove global references to avoid looking like we have refleaks.
0 commit comments