Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Closed
15 changes: 10 additions & 5 deletions 15 Lib/argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -2470,12 +2470,17 @@ 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]:
Comment thread
savannahostrowski marked this conversation as resolved.
try:
arg_strings.remove('--')
except ValueError:
pass
if ((action.nargs in [ZERO_OR_MORE, ONE_OR_MORE] or
(isinstance(action.nargs, int) and action.nargs > 1)) and
action.type is None):
if arg_strings and arg_strings[0] == '--':
arg_strings = arg_strings[1:]
else:
try:
arg_strings.remove('--')
except ValueError:
pass

# optional argument produces a default when not present
if not arg_strings and action.nargs == OPTIONAL:
Expand Down
21 changes: 21 additions & 0 deletions 21 Lib/test/test_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -5716,6 +5716,27 @@ def test_double_dash(self):
args = parser.parse_args(['--foo', 'a', 'b', '--', 'c', 'd'])
self.assertEqual(NS(foo=['a', 'b'], bar=['c', 'd']), args)

parser2 = argparse.ArgumentParser()
parser2.add_argument('foo')
parser2.add_argument('bar', nargs='*')

args = parser2.parse_args(['foo', '--', '--bar', '--', 'com'])
self.assertEqual(NS(foo='foo', bar=['--bar', '--', 'com']), args)

parser3 = argparse.ArgumentParser()
parser3.add_argument('foo')
parser3.add_argument('bar', nargs='+')

args = parser3.parse_args(['foo', '--', '--bar', '--', 'com'])
self.assertEqual(NS(foo='foo', bar=['--bar', '--', 'com']), args)

parser4 = argparse.ArgumentParser()
parser4.add_argument('foo')
parser4.add_argument('bar', nargs=3)

args = parser4.parse_args(['foo', '--', '--bar', '--', 'com'])
self.assertEqual(NS(foo='foo', bar=['--bar', '--', 'com']), args)


# ===========================
# parse_intermixed_args tests
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a bug where :mod:`argparse` would remove "--" in cases where it delineates positional arguments in nargs.
Morty Proxy This is a proxified and sanitized view of the page, visit original site.