diff options
author | R. David Murray <rdmurray@bitdance.com> | 2010-08-03 17:56:09 (GMT) |
---|---|---|
committer | R. David Murray <rdmurray@bitdance.com> | 2010-08-03 17:56:09 (GMT) |
commit | 88c49fe32054e28627eb41134656da606e9f9595 (patch) | |
tree | dab77fd4ece6d10f22bcba57ced3b824f2df69d2 /Lib/argparse.py | |
parent | f767f08e290d0caa42148083940a08e8425b18f2 (diff) | |
download | cpython-88c49fe32054e28627eb41134656da606e9f9595.zip cpython-88c49fe32054e28627eb41134656da606e9f9595.tar.gz cpython-88c49fe32054e28627eb41134656da606e9f9595.tar.bz2 |
#9444: use first of prefix_chars for help opt instead of raising error
An argparse option parser created with a prefix_chars that did not
include a '-' would happily add -h and --help options, and then throw
an error when it tried to format the help because the - was an invalid
prefix character. This patch makes it use the first character of
prefix_chars as the character for the help options if and only if '-'
is not one of the valid prefix_chars.
Fix by Theodore Turocy, unit tests by Catherine Devlin.
Diffstat (limited to 'Lib/argparse.py')
-rw-r--r-- | Lib/argparse.py | 7 |
1 files changed, 5 insertions, 2 deletions
diff --git a/Lib/argparse.py b/Lib/argparse.py index c12a16d..d556621 100644 --- a/Lib/argparse.py +++ b/Lib/argparse.py @@ -1561,13 +1561,16 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer): # add help and version arguments if necessary # (using explicit default to override global argument_default) + default_prefix = '-' if '-' in prefix_chars else prefix_chars[0] if self.add_help: self.add_argument( - '-h', '--help', action='help', default=SUPPRESS, + default_prefix+'h', default_prefix*2+'help', + action='help', default=SUPPRESS, help=_('show this help message and exit')) if self.version: self.add_argument( - '-v', '--version', action='version', default=SUPPRESS, + default_prefix+'v', default_prefix*2+'version', + action='version', default=SUPPRESS, version=self.version, help=_("show program's version number and exit")) |