mirror of https://github.com/python/cpython
gh-109475: Fix support of explicit option value "--" in argparse (GH-114814)
For example "--option=--".
This commit is contained in:
parent
750489cc77
commit
4aa4f0906d
|
@ -2485,7 +2485,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:
|
||||
|
|
|
@ -5405,6 +5405,22 @@ class TestParseKnownArgs(TestCase):
|
|||
args = parser.parse_args([])
|
||||
self.assertEqual(NS(x=[]), args)
|
||||
|
||||
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
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
Fix support of explicit option value "--" in :mod:`argparse` (e.g.
|
||||
``--option=--``).
|
Loading…
Reference in New Issue