Skip to content

Commit 024243c

Browse files
authored
argparse: Don't add default for required actions in DefaultsHelpFormatter
For actions with required=True, the ArgumentDefaultsHelpFormatter would always add a " (default: None)" to the end of the help text. Since that's a bit misleading, it is removed with this commit.
1 parent e0449b9 commit 024243c

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

Lib/argparse.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -714,11 +714,15 @@ def _get_help_string(self, action):
714714
if help is None:
715715
help = ''
716716

717-
if '%(default)' not in help:
718-
if action.default is not SUPPRESS:
719-
defaulting_nargs = [OPTIONAL, ZERO_OR_MORE]
720-
if action.option_strings or action.nargs in defaulting_nargs:
721-
help += ' (default: %(default)s)'
717+
if (
718+
'%(default)' not in help
719+
and action.default is not SUPPRESS
720+
and not action.required
721+
):
722+
defaulting_nargs = [OPTIONAL, ZERO_OR_MORE]
723+
if action.option_strings or action.nargs in defaulting_nargs:
724+
help += ' (default: %(default)s)'
725+
722726
return help
723727

724728

0 commit comments

Comments
 (0)