diff options
author | Toshio Kuratomi <a.badger@gmail.com> | 2022-05-03 16:38:18 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-03 16:38:18 (GMT) |
commit | 20490d5018cff4c604b37142323c531f8512b9aa (patch) | |
tree | 8b8b3717c4f24fdf4b6024f301538ddbda10c4f2 /Lib/argparse.py | |
parent | 6c25bf07e86b6d8d5e50488835f70539f382d7e0 (diff) | |
download | cpython-20490d5018cff4c604b37142323c531f8512b9aa.zip cpython-20490d5018cff4c604b37142323c531f8512b9aa.tar.gz cpython-20490d5018cff4c604b37142323c531f8512b9aa.tar.bz2 |
gh-88753: Make BooleanOptionalAction's addition of default to help more similar to other actions (#27808)
Help for other actions omit the default value if default is SUPPRESS or
already contains the special format string '%(default)'. Add those
special cases to BooleanOptionalAction's help formatting too.
Fixes https://bugs.python.org/issue44587 so that default=SUPPRESS is not
emitted.
Fixes https://bugs.python.org/issue38956 as this code will detect
whether '%(default)s' has already been specified in the help string.
Signed-off-by: Micky Yun Chan (michiboo): <chanmickyyun@gmail.com>
Co-authored-by: Micky Yun Chan <michan@redhat.com>
Diffstat (limited to 'Lib/argparse.py')
-rw-r--r-- | Lib/argparse.py | 20 |
1 files changed, 15 insertions, 5 deletions
diff --git a/Lib/argparse.py b/Lib/argparse.py index c47aeff..8d1a00a 100644 --- a/Lib/argparse.py +++ b/Lib/argparse.py @@ -153,6 +153,7 @@ def _copy_items(items): # Formatting Help # =============== + class HelpFormatter(object): """Formatter for generating usage messages and argument help strings. @@ -695,8 +696,19 @@ class ArgumentDefaultsHelpFormatter(HelpFormatter): """ def _get_help_string(self, action): + """ + Add the default value to the option help message. + + ArgumentDefaultsHelpFormatter and BooleanOptionalAction when it isn't + already present. This code will do that, detecting cornercases to + prevent duplicates or cases where it wouldn't make sense to the end + user. + """ help = action.help - if '%(default)' not in action.help: + if help is None: + help = '' + + if '%(default)' not in help: if action.default is not SUPPRESS: defaulting_nargs = [OPTIONAL, ZERO_OR_MORE] if action.option_strings or action.nargs in defaulting_nargs: @@ -704,6 +716,7 @@ class ArgumentDefaultsHelpFormatter(HelpFormatter): return help + class MetavarTypeHelpFormatter(HelpFormatter): """Help message formatter which uses the argument 'type' as the default metavar value (instead of the argument 'dest') @@ -719,7 +732,6 @@ class MetavarTypeHelpFormatter(HelpFormatter): return action.type.__name__ - # ===================== # Options and Arguments # ===================== @@ -882,9 +894,6 @@ class BooleanOptionalAction(Action): option_string = '--no-' + option_string[2:] _option_strings.append(option_string) - if help is not None and default is not None and default is not SUPPRESS: - help += " (default: %(default)s)" - super().__init__( option_strings=_option_strings, dest=dest, @@ -896,6 +905,7 @@ class BooleanOptionalAction(Action): help=help, metavar=metavar) + def __call__(self, parser, namespace, values, option_string=None): if option_string in self.option_strings: setattr(namespace, self.dest, not option_string.startswith('--no-')) |