Message153632
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. |
|
Date |
User |
Action |
Args |
2012-02-18 05:21:49 | oxplot | set | recipients:
+ oxplot |
2012-02-18 05:21:49 | oxplot | set | messageid: <1329542509.5.0.84021889574.issue14046@psf.upfronthosting.co.za> |
2012-02-18 05:21:48 | oxplot | link | issue14046 messages |
2012-02-18 05:21:48 | oxplot | create | |
|