Repository navigation
argparse usage should preserve () in metavars such as range(20) #62549
Description
Activity
As discussed in bpo-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)
- addedstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on Jul 3, 2013 I just posted a patch to http://bugs.python.org/issue16468 that uses (and tests) this fix.
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)'.
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.
Reproduced on 3.11:
>>> import argparse >>> parser = argparse.ArgumentParser() >>> parser.add_argument('foo', type=int, choices=range(20), metavar='range(0,20)') _StoreAction(option_strings=[], dest='foo', nargs=None, const=None, default=None, type=<class 'int'>, choices=range(0, 20), help=None, metavar='range(0,20)') >>> parser.format_usage() 'usage: [-h] range0,20\n'
- added3.9 (EOL)end of lifeend of life3.10 (EOL)end of lifeend of life3.11only security fixesonly security fixes
on Dec 10, 2021 - added a commit that references this issue
on May 28, 2023 This was fixed in #102318.
Metadata
Metadata
Assignees
Labels
Projects
- StatusShow more project fieldsDoc issues
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields: