Issue 13685: argparse update help msg for % signs (original) (raw)

I discovered this while programming the command line interface for a python program that can take a passed argument and throw it into the 'where like' clause of a SQL expression (intended for a postgresql database).

The wildcard character for where-like statements is generally the percent sign, which is how I found this ("WHERE %s LIKE '%--value%')".

If you use any single '%' signs in an ArgumentParser.new_argument(help=)'s help description Python 3.2 will throw an error.

Workaround: You can avoid this issue by doubling up on all % signs that you want to display in your help text.

parser.add_argument(('--foo', action='store',help='%bar') throws an error. parser.add_argument(('--foo', action='store',help='%%bar') displays '--foo FOO %bar'.

Suggested fix: When assigning help strings from add_argument(), throw them through a sanitizer and replace all occurrences of '%' with '%%' behind the scenes.

Example code (argparseBug.py):

from argparse import ArgumentParser

parser = ArgumentParser() parser.add_argument('--foo', action='store', help='%bar')

args = parser.parse_args('-h'.split())

You get the following stacktrace: Traceback (most recent call last): File "/path/to/script/argparseBug.py", line 6, in args = parser.parse_args('-h'.split()) File "/usr/lib/python3.2/argparse.py", line 1701, in parse_args args, argv = self.parse_known_args(args, namespace) File "/usr/lib/python3.2/argparse.py", line 1733, in parse_known_args namespace, args = self._parse_known_args(args, namespace) File "/usr/lib/python3.2/argparse.py", line 1939, in _parse_known_args start_index = consume_optional(start_index) File "/usr/lib/python3.2/argparse.py", line 1879, in consume_optional take_action(action, args, option_string) File "/usr/lib/python3.2/argparse.py", line 1807, in take_action action(self, namespace, argument_values, option_string) File "/usr/lib/python3.2/argparse.py", line 994, in call parser.print_help() File "/usr/lib/python3.2/argparse.py", line 2331, in print_help self._print_message(self.format_help(), file) File "/usr/lib/python3.2/argparse.py", line 2305, in format_help return formatter.format_help() File "/usr/lib/python3.2/argparse.py", line 279, in format_help help = self._root_section.format_help() File "/usr/lib/python3.2/argparse.py", line 209, in format_help func(*args) File "/usr/lib/python3.2/argparse.py", line 209, in format_help func(*args) File "/usr/lib/python3.2/argparse.py", line 515, in _format_action help_text = self._expand_help(action) File "/usr/lib/python3.2/argparse.py", line 601, in _expand_help return self._get_help_string(action) % params ValueError: unsupported format character 'b' (0x62) at index 1

In case I wasn't clear, I mean that the help string supports %-formatting variable expansion, such as "%(default)s". I think it would be good if a sentence were added to the end of the "help" section, saying something like:

Because the help string supports %-formatting, if you want a literal "%" to appear in the help string, you must escape it as "%%".