Skip to content

Commit

Permalink
pythongh-81691: Fix handling of multiple "--" (double dashes) in argp…
Browse files Browse the repository at this point in the history
…arse (pythonGH-124233)

Only the first one has now been removed, all subsequent ones are now
taken literally.
  • Loading branch information
serhiy-storchaka authored Sep 20, 2024
1 parent 7a2d77c commit aae1267
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 8 deletions.
12 changes: 5 additions & 7 deletions Lib/argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -2070,6 +2070,11 @@ def consume_positionals(start_index):
# 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") %
Expand Down Expand Up @@ -2470,13 +2475,6 @@ def parse_known_intermixed_args(self, args=None, namespace=None):
# 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:
Expand Down
60 changes: 59 additions & 1 deletion Lib/test/test_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -5721,7 +5721,30 @@ def test_zero_or_more_optional(self):
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='*')

Expand All @@ -5735,6 +5758,41 @@ def test_double_dash(self):
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)


# ===========================
Expand Down
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit aae1267

Please sign in to comment.