gh-77956: Add the words 'default' and 'version' help text localizable by eamanu · Pull Request #12711 · python/cpython (original) (raw)
This needs tests and I am not sure how to test this since argparse lacks localization tests. As noted by paul.j3 I am not sure how worthy enough it is to make this change that doesn't have tests and something the user can override by having a custom help formatter.
Sample program where I directly patch gettext so that translation just gives uppercase of the word :
python3 ../backups/bpo33775.py --help
USAGE: bpo33775.py [-h] [-a A]
OPTIONAL ARGUMENTS:
-h, --help SHOW THIS HELP MESSAGE AND EXIT
-a A Test help (default: 1)
./python.exe ../backups/bpo33775.py --help
Traceback (most recent call last):
File "../backups/bpo33775.py", line 12, in <module>
args = parser.parse_args()
File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/argparse.py", line 1746, in parse_args
args, argv = self.parse_known_args(args, namespace)
File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/argparse.py", line 1778, in parse_known_args
namespace, args = self._parse_known_args(args, namespace)
File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/argparse.py", line 1984, in _parse_known_args
start_index = consume_optional(start_index)
File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/argparse.py", line 1924, in consume_optional
take_action(action, args, option_string)
File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/argparse.py", line 1852, in take_action
action(self, namespace, argument_values, option_string)
File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/argparse.py", line 1034, in __call__
parser.print_help()
File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/argparse.py", line 2471, in print_help
self._print_message(self.format_help(), file)
File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/argparse.py", line 2455, in format_help
return formatter.format_help()
File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/argparse.py", line 281, in format_help
help = self._root_section.format_help()
File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/argparse.py", line 212, in format_help
item_help = join([func(*args) for func, args in self.items])
File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/argparse.py", line 212, in <listcomp>
item_help = join([func(*args) for func, args in self.items])
File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/argparse.py", line 212, in format_help
item_help = join([func(*args) for func, args in self.items])
File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/argparse.py", line 212, in <listcomp>
item_help = join([func(*args) for func, args in self.items])
File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/argparse.py", line 522, in _format_action
help_text = self._expand_help(action)
File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/argparse.py", line 611, in _expand_help
return self._get_help_string(action) % params
KeyError: 'DEFAULT'