@@ -588,17 +588,20 @@ def _format_args(self, action, default_metavar):
588
588
return result
589
589
590
590
def _expand_help (self , action ):
591
+ help_string = self ._get_help_string (action )
592
+ if '%' not in help_string :
593
+ return help_string
591
594
params = dict (vars (action ), prog = self ._prog )
592
595
for name in list (params ):
593
- if params [name ] is SUPPRESS :
596
+ value = params [name ]
597
+ if value is SUPPRESS :
594
598
del params [name ]
595
- for name in list (params ):
596
- if hasattr (params [name ], '__name__' ):
597
- params [name ] = params [name ].__name__
599
+ elif hasattr (value , '__name__' ):
600
+ params [name ] = value .__name__
598
601
if params .get ('choices' ) is not None :
599
602
choices_str = ', ' .join ([str (c ) for c in params ['choices' ]])
600
603
params ['choices' ] = choices_str
601
- return self . _get_help_string ( action ) % params
604
+ return help_string % params
602
605
603
606
def _iter_indented_subactions (self , action ):
604
607
try :
@@ -1180,9 +1183,13 @@ def add_parser(self, name, *, deprecated=False, **kwargs):
1180
1183
help = kwargs .pop ('help' )
1181
1184
choice_action = self ._ChoicesPseudoAction (name , aliases , help )
1182
1185
self ._choices_actions .append (choice_action )
1186
+ else :
1187
+ choice_action = None
1183
1188
1184
1189
# create the parser and add it to the map
1185
1190
parser = self ._parser_class (** kwargs )
1191
+ if choice_action is not None :
1192
+ parser ._check_help (choice_action )
1186
1193
self ._name_parser_map [name ] = parser
1187
1194
1188
1195
# make parser available under aliases also
@@ -1449,11 +1456,12 @@ def add_argument(self, *args, **kwargs):
1449
1456
1450
1457
# raise an error if the metavar does not match the type
1451
1458
if hasattr (self , "_get_formatter" ):
1459
+ formatter = self ._get_formatter ()
1452
1460
try :
1453
- self . _get_formatter () ._format_args (action , None )
1461
+ formatter ._format_args (action , None )
1454
1462
except TypeError :
1455
1463
raise ValueError ("length of metavar tuple does not match nargs" )
1456
-
1464
+ self . _check_help ( action )
1457
1465
return self ._add_action (action )
1458
1466
1459
1467
def add_argument_group (self , * args , ** kwargs ):
@@ -1635,6 +1643,14 @@ def _handle_conflict_resolve(self, action, conflicting_actions):
1635
1643
if not action .option_strings :
1636
1644
action .container ._remove_action (action )
1637
1645
1646
+ def _check_help (self , action ):
1647
+ if action .help and hasattr (self , "_get_formatter" ):
1648
+ formatter = self ._get_formatter ()
1649
+ try :
1650
+ formatter ._expand_help (action )
1651
+ except (ValueError , TypeError , KeyError ) as exc :
1652
+ raise ValueError ('badly formed help string' ) from exc
1653
+
1638
1654
1639
1655
class _ArgumentGroup (_ActionsContainer ):
1640
1656
@@ -1852,6 +1868,7 @@ def add_subparsers(self, **kwargs):
1852
1868
# create the parsers action and add it to the positionals list
1853
1869
parsers_class = self ._pop_action_class (kwargs , 'parsers' )
1854
1870
action = parsers_class (option_strings = [], ** kwargs )
1871
+ self ._check_help (action )
1855
1872
self ._subparsers ._add_action (action )
1856
1873
1857
1874
# return the created parsers action
0 commit comments