-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
gh-125355: Rewrite parse_intermixed_args() in argparse #125356
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
Changes from 1 commit
342eb90
a693da5
e305e86
dbc8a86
c538416
26840a8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6277,12 +6277,23 @@ def test_basic(self): | |
# cannot parse the '1,2,3' | ||
self.assertEqual(NS(bar='y', cmd='cmd', foo='x', rest=[1]), args) | ||
self.assertEqual(["2", "3"], extras) | ||
args, extras = parser.parse_known_intermixed_args(argv) | ||
self.assertEqual(NS(bar='y', cmd='cmd', foo='x', rest=[1, 2, 3]), args) | ||
self.assertEqual([], extras) | ||
|
||
# unknown optionals go into extras | ||
argv = 'cmd --foo x --error 1 2 --bar y 3'.split() | ||
args, extras = parser.parse_known_intermixed_args(argv) | ||
self.assertEqual(NS(bar='y', cmd='cmd', foo='x', rest=[1, 2, 3]), args) | ||
self.assertEqual(['--error'], extras) | ||
argv = 'cmd --foo x 1 --error 2 --bar y 3'.split() | ||
args, extras = parser.parse_known_intermixed_args(argv) | ||
# unknown optionals go into extras | ||
self.assertEqual(NS(bar='y', cmd='cmd', foo='x', rest=[1]), args) | ||
self.assertEqual(['--error', '2', '3'], extras) | ||
Comment on lines
-6419
to
-6420
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that was a bug. The result was different depending on the position of unknown option. |
||
self.assertEqual(NS(bar='y', cmd='cmd', foo='x', rest=[1, 2, 3]), args) | ||
self.assertEqual(['--error'], extras) | ||
argv = 'cmd --foo x 1 2 --error --bar y 3'.split() | ||
args, extras = parser.parse_known_intermixed_args(argv) | ||
self.assertEqual(NS(bar='y', cmd='cmd', foo='x', rest=[1, 2, 3]), args) | ||
self.assertEqual(['--error'], extras) | ||
|
||
# restores attributes that were temporarily changed | ||
self.assertIsNone(parser.usage) | ||
|
@@ -6327,11 +6338,6 @@ def test_invalid_args(self): | |
parser = ErrorRaisingArgumentParser(prog='PROG') | ||
self.assertRaises(ArgumentParserError, parser.parse_intermixed_args, ['a']) | ||
|
||
parser = ErrorRaisingArgumentParser(prog='PROG') | ||
parser.add_argument('--foo', nargs="*") | ||
parser.add_argument('foo') | ||
with self.assertWarns(UserWarning): | ||
parser.parse_intermixed_args(['hello', '--foo']) | ||
Comment on lines
-6465
to
-6469
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
class TestIntermixedMessageContentError(TestCase): | ||
# case where Intermixed gives different error message | ||
|
@@ -6350,7 +6356,7 @@ def test_missing_argument_name_in_message(self): | |
with self.assertRaises(ArgumentParserError) as cm: | ||
parser.parse_intermixed_args([]) | ||
msg = str(cm.exception) | ||
self.assertNotRegex(msg, 'req_pos') | ||
self.assertRegex(msg, 'req_pos') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was a bug. |
||
self.assertRegex(msg, 'req_opt') | ||
|
||
# ========================== | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not understand this code. There were no tests, and the purpose is not clear to me. @hpaulj, @bitdancer, could you shed some light on this?