7171 'ArgumentDefaultsHelpFormatter' ,
7272 'RawDescriptionHelpFormatter' ,
7373 'RawTextHelpFormatter' ,
74+ 'MetavarTypeHelpFormatter' ,
7475 'Namespace' ,
7576 'Action' ,
7677 'ONE_OR_MORE' ,
@@ -422,7 +423,8 @@ def _format_actions_usage(self, actions, groups):
422423
423424 # produce all arg strings
424425 elif not action .option_strings :
425- part = self ._format_args (action , action .dest )
426+ default = self ._get_default_metavar_for_positional (action )
427+ part = self ._format_args (action , default )
426428
427429 # if it's in a group, strip the outer []
428430 if action in group_actions :
@@ -444,7 +446,7 @@ def _format_actions_usage(self, actions, groups):
444446 # if the Optional takes a value, format is:
445447 # -s ARGS or --long ARGS
446448 else :
447- default = action . dest . upper ( )
449+ default = self . _get_default_metavar_for_optional ( action )
448450 args_string = self ._format_args (action , default )
449451 part = '%s %s' % (option_string , args_string )
450452
@@ -530,7 +532,8 @@ def _format_action(self, action):
530532
531533 def _format_action_invocation (self , action ):
532534 if not action .option_strings :
533- metavar , = self ._metavar_formatter (action , action .dest )(1 )
535+ default = self ._get_default_metavar_for_positional (action )
536+ metavar , = self ._metavar_formatter (action , default )(1 )
534537 return metavar
535538
536539 else :
@@ -544,7 +547,7 @@ def _format_action_invocation(self, action):
544547 # if the Optional takes a value, format is:
545548 # -s ARGS, --long ARGS
546549 else :
547- default = action . dest . upper ( )
550+ default = self . _get_default_metavar_for_optional ( action )
548551 args_string = self ._format_args (action , default )
549552 for option_string in action .option_strings :
550553 parts .append ('%s %s' % (option_string , args_string ))
@@ -622,6 +625,12 @@ def _fill_text(self, text, width, indent):
622625 def _get_help_string (self , action ):
623626 return action .help
624627
628+ def _get_default_metavar_for_optional (self , action ):
629+ return action .dest .upper ()
630+
631+ def _get_default_metavar_for_positional (self , action ):
632+ return action .dest
633+
625634
626635class RawDescriptionHelpFormatter (HelpFormatter ):
627636 """Help message formatter which retains any formatting in descriptions.
@@ -662,6 +671,22 @@ def _get_help_string(self, action):
662671 return help
663672
664673
674+ class MetavarTypeHelpFormatter (HelpFormatter ):
675+ """Help message formatter which uses the argument 'type' as the default
676+ metavar value (instead of the argument 'dest')
677+
678+ Only the name of this class is considered a public API. All the methods
679+ provided by the class are considered an implementation detail.
680+ """
681+
682+ def _get_default_metavar_for_optional (self , action ):
683+ return action .type .__name__
684+
685+ def _get_default_metavar_for_positional (self , action ):
686+ return action .type .__name__
687+
688+
689+
665690# =====================
666691# Options and Arguments
667692# =====================
0 commit comments