Skip to content

Commit b6a18bb

Browse files
committed
bpo-22433: do not consider "--foo='bar baz'" to be a positional argument
Ref: https://bugs.python.org/issue22433
1 parent c4862e3 commit b6a18bb

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

Lib/argparse.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2223,8 +2223,8 @@ def _parse_optional(self, arg_string):
22232223
if not self._has_negative_number_optionals:
22242224
return None
22252225

2226-
# if it contains a space, it was meant to be a positional
2227-
if ' ' in arg_string:
2226+
# if it contains a space (before any equal sign), it was meant to be a positional
2227+
if ' ' in arg_string.split("=", 1)[0]:
22282228
return None
22292229

22302230
# it was meant to be an optional but there is no such option

Lib/test/test_argparse.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2018,6 +2018,15 @@ def test_parse_known_args(self):
20182018
self.parser.parse_known_args('0.5 -W 1 b -X Y -w 7 Z'.split()),
20192019
(NS(foo=False, bar=0.5, w=7, x='b'), ['-W', '-X', 'Y', 'Z']),
20202020
)
2021+
self.assertEqual(
2022+
self.parser.parse_known_args(['0.5', '--opt="with space"']),
2023+
(NS(foo=False, bar=0.5), ['--opt="with space"']),
2024+
)
2025+
self.assertRaisesRegexp(
2026+
ArgumentParserError,
2027+
"invalid choice: '--opt with space'",
2028+
self.parser.parse_known_args,
2029+
['0.5', '--opt with space'])
20212030

20222031
def test_dest(self):
20232032
parser = ErrorRaisingArgumentParser()

0 commit comments

Comments
 (0)