[New-bugs-announce] [issue18349] argparse usage should preserve () in metavars such as range(20)

paul j3 report at bugs.python.org
Wed Jul 3 08:16:51 CEST 2013


New submission from paul j3:

As discussed in issue 16468, a metavar may be used to provide an alternative representation of a choices option.  However if a metvar like 'range(20)' is used, usage formatter strips off the '()'.

    >>> parser.add_argument('foo', type=int, 
        choices=range(20), metavar='range(0,20)')
    >>> parser.format_usage()
    # expect: 'usage: PROG [-h] range(0,20)\n'
    # actual: 'usage: PROG [-h] range0,20\n'

This is done by a line in the help formater that removes excess mutually exclusive group notation:

    HelpFormatter._format_actions_usage
       ...
       text = _re.sub(r'\(([^|]*)\)', r'\1', text)

A solution is to change this line to distinguish between a case like ' (...)' and 'range(...)'

    text = _re.sub(r'( )\(([^|]*)\)', r'\1\2', text)

----------
files: metaparen.patch
keywords: patch
messages: 192222
nosy: paul.j3
priority: normal
severity: normal
status: open
title: argparse usage should preserve () in metavars such as range(20)
Added file: http://bugs.python.org/file30754/metaparen.patch

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue18349>
_______________________________________


More information about the New-bugs-announce mailing list