summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2024-02-05 21:04:11 (GMT)
committerGitHub <noreply@github.com>2024-02-05 21:04:11 (GMT)
commite1976399cd08a0aadeb094cee46b4ebf468fecd9 (patch)
treeadc1c2fa5c5f487482bc2712df84762c38de5c58
parent98b2f4624a40eb374807842a7fb9d109375af99d (diff)
downloadcpython-e1976399cd08a0aadeb094cee46b4ebf468fecd9.zip
cpython-e1976399cd08a0aadeb094cee46b4ebf468fecd9.tar.gz
cpython-e1976399cd08a0aadeb094cee46b4ebf468fecd9.tar.bz2
[3.11] gh-109475: Fix support of explicit option value "--" in argparse (GH-114814) (GH-115037)
For example "--option=--". (cherry picked from commit 4aa4f0906df9fc9c6c6f6657f2c521468c6b1688)
-rw-r--r--Lib/argparse.py2
-rw-r--r--Lib/test/test_argparse.py17
-rw-r--r--Misc/NEWS.d/next/Library/2024-01-31-20-07-11.gh-issue-109475.lmTb9S.rst2
3 files changed, 20 insertions, 1 deletions
diff --git a/Lib/argparse.py b/Lib/argparse.py
index 9962e61..a999ea6 100644
--- a/Lib/argparse.py
+++ b/Lib/argparse.py
@@ -2464,7 +2464,7 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
# ========================
def _get_values(self, action, arg_strings):
# for everything but PARSER, REMAINDER args, strip out first '--'
- if action.nargs not in [PARSER, REMAINDER]:
+ if not action.option_strings and action.nargs not in [PARSER, REMAINDER]:
try:
arg_strings.remove('--')
except ValueError:
diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py
index 6322ebb..adee4f3 100644
--- a/Lib/test/test_argparse.py
+++ b/Lib/test/test_argparse.py
@@ -5272,6 +5272,23 @@ class TestParseKnownArgs(TestCase):
self.assertEqual(NS(v=3, spam=True, badger="B"), args)
self.assertEqual(["C", "--foo", "4"], extras)
+ def test_double_dash(self):
+ parser = argparse.ArgumentParser()
+ parser.add_argument('-f', '--foo', nargs='*')
+ parser.add_argument('bar', nargs='*')
+
+ args = parser.parse_args(['--foo=--'])
+ self.assertEqual(NS(foo=['--'], bar=[]), args)
+ args = parser.parse_args(['--foo', '--'])
+ self.assertEqual(NS(foo=[], bar=[]), args)
+ args = parser.parse_args(['-f--'])
+ self.assertEqual(NS(foo=['--'], bar=[]), args)
+ args = parser.parse_args(['-f', '--'])
+ self.assertEqual(NS(foo=[], bar=[]), args)
+ args = parser.parse_args(['--foo', 'a', 'b', '--', 'c', 'd'])
+ self.assertEqual(NS(foo=['a', 'b'], bar=['c', 'd']), args)
+
+
# ===========================
# parse_intermixed_args tests
# ===========================
diff --git a/Misc/NEWS.d/next/Library/2024-01-31-20-07-11.gh-issue-109475.lmTb9S.rst b/Misc/NEWS.d/next/Library/2024-01-31-20-07-11.gh-issue-109475.lmTb9S.rst
new file mode 100644
index 0000000..7582cb2
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2024-01-31-20-07-11.gh-issue-109475.lmTb9S.rst
@@ -0,0 +1,2 @@
+Fix support of explicit option value "--" in :mod:`argparse` (e.g.
+``--option=--``).