Message150404
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 <module>
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 |
|
Date |
User |
Action |
Args |
2011-12-30 22:29:27 | Jeff.Yurkiw | set | recipients:
+ Jeff.Yurkiw |
2011-12-30 22:29:27 | Jeff.Yurkiw | set | messageid: <1325284167.16.0.584942168313.issue13685@psf.upfronthosting.co.za> |
2011-12-30 22:29:26 | Jeff.Yurkiw | link | issue13685 messages |
2011-12-30 22:29:26 | Jeff.Yurkiw | create | |
|