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

gh-109475: Fix support of explicit option value "--" in argparse #114814

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib/argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -2489,7 +2489,7 @@ def parse_known_intermixed_args(self, args=None, namespace=None):
# ========================
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:
Expand Down
16 changes: 16 additions & 0 deletions Lib/test/test_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -5405,6 +5405,22 @@ def test_zero_or_more_optional(self):
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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix support of explicit option value "--" in :mod:`argparse` (e.g.
``--option=--``).