-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
Closed
Labels
stdlibStandard 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
Description
Parentheses in argparse metavars are dropped on the usage line but appear as expected in the argument list. In particular, the very first and very last parentheses disappear. It looks from other issues (e.g. #56083) that braces, brackets and parentheses in metavars are problematic, so this may be part of a wider and more difficult set of puzzlers to fix.
Illustrative code:
#!/usr/bin/python3
"""Demonstrate parenthesis drops in argparse metavar."""
import argparse
# The very first and last parentheses disappear in the usage line, but are
# present in the argument list.
parser = argparse.ArgumentParser(
description='Demonstrate parenthesis drops in argument meta-descriptions')
parser.add_argument(
'positional',
help='positional argument',
metavar='(example) positional')
parser.add_argument(
'-p',
'--optional',
help='optional argument',
type=int,
choices=[1, 2],
metavar='{1 (option A), 2 (option B)}',
default=1)
arguments = parser.parse_args()
print(arguments)
When this is run with -h, the help text is rendered as shown below. Note the parentheses are missing before option A and after example:
usage: parens.py [-h] [-p {1 option A), 2 (option B)}] (example positional
Demonstrate parenthesis drops in argument meta-descriptions
positional arguments:
(example) positional positional argument
optional arguments:
-h, --help show this help message and exit
-p {1 (option A), 2 (option B)}, --optional {1 (option A), 2 (option B)}
optional argument
I've tried this on Python 3.8.10 and Python 3.10.6.
Linked PRs
- gh-101979: fix a bug that parentheses in metavar argument of add_argument() were dropped #102318
- [3.11] gh-101979: argparse: fix a bug where parentheses in metavar argument of add_argument() were dropped (GH-102318) #102438
- [3.10] gh-101979: argparse: fix a bug where parentheses in metavar argument of add_argument() were dropped (GH-102318) #102439
Metadata
Metadata
Assignees
Labels
stdlibStandard 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
Projects
Status
Doc issues