Skip to content

Commit 94ad682

Browse files
[3.12] gh-109475: Fix support of explicit option value "--" in argparse (GH-114814) (GH-115036)
For example "--option=--". (cherry picked from commit 4aa4f09) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent 21f06a2 commit 94ad682

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

Lib/argparse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2488,7 +2488,7 @@ def parse_known_intermixed_args(self, args=None, namespace=None):
24882488
# ========================
24892489
def _get_values(self, action, arg_strings):
24902490
# for everything but PARSER, REMAINDER args, strip out first '--'
2491-
if action.nargs not in [PARSER, REMAINDER]:
2491+
if not action.option_strings and action.nargs not in [PARSER, REMAINDER]:
24922492
try:
24932493
arg_strings.remove('--')
24942494
except ValueError:

Lib/test/test_argparse.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5332,6 +5332,22 @@ def test_zero_or_more_optional(self):
53325332
args = parser.parse_args([])
53335333
self.assertEqual(NS(x=[]), args)
53345334

5335+
def test_double_dash(self):
5336+
parser = argparse.ArgumentParser()
5337+
parser.add_argument('-f', '--foo', nargs='*')
5338+
parser.add_argument('bar', nargs='*')
5339+
5340+
args = parser.parse_args(['--foo=--'])
5341+
self.assertEqual(NS(foo=['--'], bar=[]), args)
5342+
args = parser.parse_args(['--foo', '--'])
5343+
self.assertEqual(NS(foo=[], bar=[]), args)
5344+
args = parser.parse_args(['-f--'])
5345+
self.assertEqual(NS(foo=['--'], bar=[]), args)
5346+
args = parser.parse_args(['-f', '--'])
5347+
self.assertEqual(NS(foo=[], bar=[]), args)
5348+
args = parser.parse_args(['--foo', 'a', 'b', '--', 'c', 'd'])
5349+
self.assertEqual(NS(foo=['a', 'b'], bar=['c', 'd']), args)
5350+
53355351

53365352
# ===========================
53375353
# parse_intermixed_args tests
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix support of explicit option value "--" in :mod:`argparse` (e.g.
2+
``--option=--``).

0 commit comments

Comments
 (0)