classification
Title: argparse usage should preserve () in metavars such as range(20)
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.4
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: paul.j3
Priority: normal Keywords: patch

Created on 2013-07-03 06:16 by paul.j3, last changed 2013-07-16 20:34 by paul.j3.

Files
File name Uploaded Description Edit
metaparen.patch paul.j3, 2013-07-03 06:16 review
Messages (4)
msg192222 - (view) Author: paul j3 (paul.j3) * (Python triager) Date: 2013-07-03 06:16
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)
msg192717 - (view) Author: paul j3 (paul.j3) * (Python triager) Date: 2013-07-09 03:08
I just posted a patch to http://bugs.python.org/issue16468 that uses (and tests) this fix.
msg193077 - (view) Author: paul j3 (paul.j3) * (Python triager) Date: 2013-07-15 07:15
This issue should also preserve a metavar like: '(one)two', i.e. '(' at the start.  

In http://bugs.python.org/issue10984 these _re replacements are applied to individual action strings as well as the whole usage line.  So if () are to be removed from '[-h] (-y)', they should also be removed from '(-y)'.
msg193188 - (view) Author: paul j3 (paul.j3) * (Python triager) Date: 2013-07-16 20:34
I just submitted at patch to http://bugs.python.org/issue11874 that takes care of this issue as well.

I rewrote _format_actions_usage() so it formats the parts directly, so there is no need cleanup or parse the full text string.
History
Date User Action Args
2013-07-16 20:34:49paul.j3setmessages: + msg193188
2013-07-15 07:15:36paul.j3setmessages: + msg193077
2013-07-09 03:08:49paul.j3setmessages: + msg192717
2013-07-03 06:19:39paul.j3settype: behavior
components: + Library (Lib)
versions: + Python 3.4
2013-07-03 06:16:51paul.j3create