mirror of
https://github.com/python/cpython.git
synced 2025-09-15 05:06:12 +00:00
[3.11] gh-109475: Fix support of explicit option value "--" in argparse (GH-114814) (GH-115037)
For example "--option=--".
(cherry picked from commit 4aa4f0906d
)
This commit is contained in:
parent
98b2f4624a
commit
e1976399cd
3 changed files with 20 additions and 1 deletions
|
@ -2464,7 +2464,7 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
|
||||||
# ========================
|
# ========================
|
||||||
def _get_values(self, action, arg_strings):
|
def _get_values(self, action, arg_strings):
|
||||||
# for everything but PARSER, REMAINDER args, strip out first '--'
|
# 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:
|
try:
|
||||||
arg_strings.remove('--')
|
arg_strings.remove('--')
|
||||||
except ValueError:
|
except ValueError:
|
||||||
|
|
|
@ -5272,6 +5272,23 @@ class TestParseKnownArgs(TestCase):
|
||||||
self.assertEqual(NS(v=3, spam=True, badger="B"), args)
|
self.assertEqual(NS(v=3, spam=True, badger="B"), args)
|
||||||
self.assertEqual(["C", "--foo", "4"], extras)
|
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
|
# parse_intermixed_args tests
|
||||||
# ===========================
|
# ===========================
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Fix support of explicit option value "--" in :mod:`argparse` (e.g.
|
||||||
|
``--option=--``).
|
Loading…
Add table
Add a link
Reference in a new issue