mirror of https://github.com/python/cpython
gh-81691: Fix handling of multiple "--" (double dashes) in argparse (GH-124233)
Only the first one has now been removed, all subsequent ones are now taken literally.
This commit is contained in:
parent
7a2d77c903
commit
aae126748f
|
@ -2070,6 +2070,11 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
|
|||
# and add the Positional and its args to the list
|
||||
for action, arg_count in zip(positionals, arg_counts):
|
||||
args = arg_strings[start_index: start_index + arg_count]
|
||||
# Strip out the first '--' if it is not in PARSER or REMAINDER arg.
|
||||
if (action.nargs not in [PARSER, REMAINDER]
|
||||
and arg_strings_pattern.find('-', start_index,
|
||||
start_index + arg_count) >= 0):
|
||||
args.remove('--')
|
||||
start_index += arg_count
|
||||
if args and action.deprecated and action.dest not in warned:
|
||||
self._warning(_("argument '%(argument_name)s' is deprecated") %
|
||||
|
@ -2470,13 +2475,6 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
|
|||
# Value conversion methods
|
||||
# ========================
|
||||
def _get_values(self, action, arg_strings):
|
||||
# for everything but PARSER, REMAINDER args, strip out first '--'
|
||||
if not action.option_strings and action.nargs not in [PARSER, REMAINDER]:
|
||||
try:
|
||||
arg_strings.remove('--')
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
# optional argument produces a default when not present
|
||||
if not arg_strings and action.nargs == OPTIONAL:
|
||||
if action.option_strings:
|
||||
|
|
|
@ -5721,7 +5721,30 @@ class TestParseKnownArgs(TestCase):
|
|||
self.assertEqual(NS(x=[]), args)
|
||||
|
||||
def test_double_dash(self):
|
||||
parser = argparse.ArgumentParser()
|
||||
parser = argparse.ArgumentParser(exit_on_error=False)
|
||||
parser.add_argument('-f', '--foo')
|
||||
parser.add_argument('bar', nargs='*')
|
||||
|
||||
args = parser.parse_args(['--foo=--'])
|
||||
self.assertEqual(NS(foo='--', bar=[]), args)
|
||||
self.assertRaisesRegex(argparse.ArgumentError,
|
||||
'argument -f/--foo: expected one argument',
|
||||
parser.parse_args, ['--foo', '--'])
|
||||
args = parser.parse_args(['-f--'])
|
||||
self.assertEqual(NS(foo='--', bar=[]), args)
|
||||
self.assertRaisesRegex(argparse.ArgumentError,
|
||||
'argument -f/--foo: expected one argument',
|
||||
parser.parse_args, ['-f', '--'])
|
||||
args = parser.parse_args(['--foo', 'a', '--', 'b', 'c'])
|
||||
self.assertEqual(NS(foo='a', bar=['b', 'c']), args)
|
||||
args = parser.parse_args(['a', 'b', '--foo', 'c'])
|
||||
self.assertEqual(NS(foo='c', bar=['a', 'b']), args)
|
||||
args = parser.parse_args(['a', '--', 'b', '--foo', 'c'])
|
||||
self.assertEqual(NS(foo=None, bar=['a', 'b', '--foo', 'c']), args)
|
||||
args = parser.parse_args(['a', '--', 'b', '--', 'c', '--foo', 'd'])
|
||||
self.assertEqual(NS(foo=None, bar=['a', 'b', '--', 'c', '--foo', 'd']), args)
|
||||
|
||||
parser = argparse.ArgumentParser(exit_on_error=False)
|
||||
parser.add_argument('-f', '--foo', nargs='*')
|
||||
parser.add_argument('bar', nargs='*')
|
||||
|
||||
|
@ -5735,6 +5758,41 @@ class TestParseKnownArgs(TestCase):
|
|||
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)
|
||||
args = parser.parse_args(['a', 'b', '--foo', 'c', 'd'])
|
||||
self.assertEqual(NS(foo=['c', 'd'], bar=['a', 'b']), args)
|
||||
args = parser.parse_args(['a', '--', 'b', '--foo', 'c', 'd'])
|
||||
self.assertEqual(NS(foo=None, bar=['a', 'b', '--foo', 'c', 'd']), args)
|
||||
args, argv = parser.parse_known_args(['a', 'b', '--foo', 'c', '--', 'd'])
|
||||
self.assertEqual(NS(foo=['c'], bar=['a', 'b']), args)
|
||||
self.assertEqual(argv, ['--', 'd'])
|
||||
|
||||
parser = argparse.ArgumentParser(exit_on_error=False)
|
||||
parser.add_argument('foo')
|
||||
parser.add_argument('bar', nargs='*')
|
||||
|
||||
args = parser.parse_args(['--', 'a', 'b', 'c'])
|
||||
self.assertEqual(NS(foo='a', bar=['b', 'c']), args)
|
||||
args = parser.parse_args(['a', '--', 'b', 'c'])
|
||||
self.assertEqual(NS(foo='a', bar=['b', 'c']), args)
|
||||
args = parser.parse_args(['a', 'b', '--', 'c'])
|
||||
self.assertEqual(NS(foo='a', bar=['b', 'c']), args)
|
||||
args = parser.parse_args(['a', '--', 'b', '--', 'c'])
|
||||
self.assertEqual(NS(foo='a', bar=['b', '--', 'c']), args)
|
||||
args = parser.parse_args(['--', '--', 'a', '--', 'b', 'c'])
|
||||
self.assertEqual(NS(foo='--', bar=['a', '--', 'b', 'c']), args)
|
||||
|
||||
parser = argparse.ArgumentParser(exit_on_error=False)
|
||||
parser.add_argument('foo')
|
||||
parser.add_argument('bar', nargs=argparse.REMAINDER)
|
||||
|
||||
args = parser.parse_args(['--', 'a', 'b', 'c'])
|
||||
self.assertEqual(NS(foo='a', bar=['b', 'c']), args)
|
||||
args = parser.parse_args(['a', '--', 'b', 'c'])
|
||||
self.assertEqual(NS(foo='a', bar=['b', 'c']), args)
|
||||
args = parser.parse_args(['a', 'b', '--', 'c'])
|
||||
self.assertEqual(NS(foo='a', bar=['b', '--', 'c']), args)
|
||||
args = parser.parse_args(['a', '--', 'b', '--', 'c'])
|
||||
self.assertEqual(NS(foo='a', bar=['b', '--', 'c']), args)
|
||||
|
||||
|
||||
# ===========================
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
Fix handling of multiple ``"--"`` (double dashes) in :mod:`argparse`. Only
|
||||
the first one has now been removed, all subsequent ones are now taken
|
||||
literally.
|
Loading…
Reference in New Issue