Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.13] gh-81691: Fix handling of multiple "--" (double dashes) in argparse (GH-124233) #124266

Merged
merged 1 commit into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions Lib/argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -2098,6 +2098,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.
AlexWaygood marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -2498,13 +2503,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 @@ -5744,7 +5744,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 @@ -5758,6 +5781,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.
Loading