Created
December 6, 2016 08:43
-
-
Save pde/817a00378d3f6ed73747dfffce323ae5 to your computer and use it in GitHub Desktop.
allow overriding of argparse.ArgumentDefaultsHelpFormatter default values
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| diff --git a/Doc/library/argparse.rst b/Doc/library/argparse.rst | |
| index c6b2bf6..15f2fe8 100644 | |
| --- a/Doc/library/argparse.rst | |
| +++ b/Doc/library/argparse.rst | |
| @@ -429,13 +429,15 @@ should not be line-wrapped:: | |
| including argument descriptions. | |
| :class:`ArgumentDefaultsHelpFormatter` automatically adds information about | |
| -default values to each of the argument help messages:: | |
| +default values to each of the argument help messages, unless they already | |
| +contain a ``(default:`` substring or a ``%(default)s`` format specifier:: | |
| >>> parser = argparse.ArgumentParser( | |
| ... prog='PROG', | |
| ... formatter_class=argparse.ArgumentDefaultsHelpFormatter) | |
| >>> parser.add_argument('--foo', type=int, default=42, help='FOO!') | |
| >>> parser.add_argument('bar', nargs='*', default=[1, 2, 3], help='BAR!') | |
| + >>> parser.add_argument('--bleep', help='bleep (default: override)') | |
| >>> parser.print_help() | |
| usage: PROG [-h] [--foo FOO] [bar [bar ...]] | |
| diff --git a/Lib/argparse.py b/Lib/argparse.py | |
| index 209b4e9..c5b2755 100644 | |
| --- a/Lib/argparse.py | |
| +++ b/Lib/argparse.py | |
| @@ -665,7 +665,7 @@ class ArgumentDefaultsHelpFormatter(HelpFormatter): | |
| def _get_help_string(self, action): | |
| help = action.help | |
| - if '%(default)' not in action.help: | |
| + if '%(default)' not in action.help and '(default:' not in action.help: | |
| if action.default is not SUPPRESS: | |
| defaulting_nargs = [OPTIONAL, ZERO_OR_MORE] | |
| if action.option_strings or action.nargs in defaulting_nargs: | |
| diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py | |
| index bc83161..f0432be 100644 | |
| --- a/Lib/test/test_argparse.py | |
| +++ b/Lib/test/test_argparse.py | |
| @@ -4010,6 +4010,9 @@ class TestHelpArgumentDefaults(HelpTestCase): | |
| argument_signatures = [ | |
| Sig('--foo', help='foo help - oh and by the way, %(default)s'), | |
| Sig('--bar', action='store_true', help='bar help'), | |
| + Sig('--quiz', action='store_true', help='should we have a quiz (default: Ask)'), | |
| + Sig('--disable-floopery', action='store_false', help='disable floopery (default: Enabled)', | |
| + dest='floopery', default=True), | |
| Sig('spam', help='spam help'), | |
| Sig('badger', nargs='?', default='wooden', help='badger help'), | |
| ] | |
| @@ -4018,25 +4021,28 @@ class TestHelpArgumentDefaults(HelpTestCase): | |
| [Sig('--baz', type=int, default=42, help='baz help')]), | |
| ] | |
| usage = '''\ | |
| - usage: PROG [-h] [--foo FOO] [--bar] [--baz BAZ] spam [badger] | |
| + usage: PROG [-h] [--foo FOO] [--bar] [--quiz] [--disable-floopery] [--baz BAZ] | |
| + spam [badger] | |
| ''' | |
| help = usage + '''\ | |
| description | |
| positional arguments: | |
| - spam spam help | |
| - badger badger help (default: wooden) | |
| + spam spam help | |
| + badger badger help (default: wooden) | |
| optional arguments: | |
| - -h, --help show this help message and exit | |
| - --foo FOO foo help - oh and by the way, None | |
| - --bar bar help (default: False) | |
| + -h, --help show this help message and exit | |
| + --foo FOO foo help - oh and by the way, None | |
| + --bar bar help (default: False) | |
| + --quiz should we have a quiz (default: Ask) | |
| + --disable-floopery disable floopery (default: Enabled) | |
| title: | |
| description | |
| - --baz BAZ baz help (default: 42) | |
| + --baz BAZ baz help (default: 42) | |
| ''' | |
| version = '' | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment