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
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: 8 additions & 4 deletions 12 Lib/argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -2097,11 +2097,15 @@ 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,
# Strip out the first '--' if it is not in REMAINDER arg.
if action.nargs == PARSER:
if arg_strings_pattern[start_index] == '-':
assert args[0] == '--'
args.remove('--')
elif action.nargs != REMAINDER:
if (arg_strings_pattern.find('-', start_index,
start_index + arg_count) >= 0):
args.remove('--')
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
14 changes: 14 additions & 0 deletions 14 Lib/test/test_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -6106,6 +6106,20 @@ def test_subparser(self):
"invalid choice: '--'",
parser.parse_args, ['--', 'x', '--', 'run', 'a', 'b'])

def test_subparser_after_multiple_argument_option(self):
parser = argparse.ArgumentParser(exit_on_error=False)
parser.add_argument('--foo', nargs='*')
subparsers = parser.add_subparsers()
parser1 = subparsers.add_parser('run')
parser1.add_argument('-f')
parser1.add_argument('bar', nargs='*')

args = parser.parse_args(['--foo', 'x', 'y', '--', 'run', 'a', 'b', '-f', 'c'])
self.assertEqual(NS(foo=['x', 'y'], f='c', bar=['a', 'b']), args)
self.assertRaisesRegex(argparse.ArgumentError,
"invalid choice: '--'",
parser.parse_args, ['--foo', 'x', '--', '--', 'run', 'a', 'b'])


# ===========================
# parse_intermixed_args tests
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:mod:`argparse` now ignores the first ``"--"`` (double dash) between an option and command.
Morty Proxy This is a proxified and sanitized view of the page, visit original site.