[New-bugs-announce] [issue14046] argparse: assertion failure if optional argument has square/round brackets in metavar
Mansour
report at bugs.python.org
Sat Feb 18 06:21:49 CET 2012
New submission from Mansour <mansour at oxplot.com>:
I have a web server written in python which takes an optional argument telling it what IP and port to bind to. Here's the definition:
parser.add_argument(
'-b', '--bind',
metavar="[ip]:port",
type=unicode,
help="IP and port to bind to."
)
There are several other arguments as well. When the usage is printed (due to parse failures or -h), if the command line is longer than terminal width, it is wrapped. Here is the (relevant) code in argparse.py:311 which deals with this scenario:
# wrap the usage parts if it's too long
text_width = self._width - self._current_indent
if len(prefix) + len(usage) > text_width:
# break usage into wrappable parts
part_regexp = r'\(.*?\)+|\[.*?\]+|\S+'
opt_usage = format(optionals, groups)
pos_usage = format(positionals, groups)
opt_parts = _re.findall(part_regexp, opt_usage)
pos_parts = _re.findall(part_regexp, pos_usage)
assert ' '.join(opt_parts) == opt_usage
assert ' '.join(pos_parts) == pos_usage
Note how part_regexp extracts words, and text surrounded with round/square brackets. Now the argument I defined above, when displayed in usage text, looks like this: [-b [ip]:port]
part_regexp however, will cut it into "[-b [ip]" and ":port]" and concatenated later with " ", it will have an extra space which causes the first assertion to fail.
I fiddled with part_regexp but that proved unreliable at best. I think the opt_parts should be obtained, not from the final formatted text, but from actual argument objects.
For a demonstration, see the attachment.
----------
components: Library (Lib)
files: argparse_wrap_test.py
messages: 153632
nosy: oxplot
priority: normal
severity: normal
status: open
title: argparse: assertion failure if optional argument has square/round brackets in metavar
type: crash
versions: Python 2.7
Added file: http://bugs.python.org/file24554/argparse_wrap_test.py
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue14046>
_______________________________________
More information about the New-bugs-announce
mailing list