argparse fails to get value of argument if the value is -- #109475
Copy link
Copy link
Closed
Labels
3.11only security fixesonly security fixes3.12only security fixesonly security fixes3.13only security fixesonly security fixesstdlibStandard 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
Bug report
Bug description:
This code:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--broken")
parser.add_argument("--working")
args = parser.parse_args(["--broken=--", "--working=asdf"])
print(args)prints:
$ podman run -v $(pwd):/src python:3.11.5-bookworm python /src/delim.py
Namespace(broken=[], working='asdf')
The value of the --broken argument is not parsed, presumably because it is the standard end-of-argument-list value. But this does not seem expected when the value is introduced with =. Besides, in this case, the parsing does continue for subsequent args, so it was not actually treated as end of arguments.
CPython versions tested on:
3.11, 3.12
Operating systems tested on:
Linux
Linked PRs
- gh-109475: argument get the value is
--#110959 - gh-109475: Fix support of explicit option value "--" in argparse #114814
- [3.12] gh-109475: Fix support of explicit option value "--" in argparse (GH-114814) #115036
- [3.11] gh-109475: Fix support of explicit option value "--" in argparse (GH-114814) #115037
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
3.11only security fixesonly security fixes3.12only security fixesonly security fixes3.13only security fixesonly security fixesstdlibStandard 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
Activity
sobolevn commentedon Sep 16, 2023
--means a very specific thing in argument parsing, I don't think that it should be a value of any other argument. If you need this value somewhere in your program, you can use some alias: like--flag=doubledashSouSingh commentedon Sep 16, 2023
Hello @kushal-kumaran I try to run it on my device I get some output I hope that will help

ericvsmith commentedon Sep 18, 2023
I completely disagree with this. Imagine if you were reading the documentation of a command line utility, and it said "If you want the output file to be named '--', then use 'doubledash' instead, and this will be translated to '--' internally. It is not possible to specify an output name of 'doubledash'."
I think this is a completely valid bug report. I'm not sure it's fixable, given all of the issues with argparse and how it scans arguments. I am surprised that using
=--doesn't work. See #53580 for a case where using=was suggested as a partial workaround.pythongh-109475: Fix support of explicit option value "--" in argparse
gh-109475: Fix support of explicit option value "--" in argparse (GH-…
pythongh-109475: Fix support of explicit option value "--" in argparse (
[3.11] pythongh-109475: Fix support of explicit option value "--" in …
[3.12] gh-109475: Fix support of explicit option value "--" in argpar…
[3.11] gh-109475: Fix support of explicit option value "--" in argpar…
pythongh-109475: Fix support of explicit option value "--" in argparse (