-
-
Notifications
You must be signed in to change notification settings - Fork 30.2k
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-125355: Rewrite parse_intermixed_args() in argparse #125356
Changes from all commits
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 |
---|---|---|
|
@@ -6412,12 +6412,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) | ||
|
@@ -6436,37 +6447,48 @@ def test_remainder(self): | |
parser.parse_intermixed_args(argv) | ||
self.assertRegex(str(cm.exception), r'\.\.\.') | ||
|
||
def test_exclusive(self): | ||
# mutually exclusive group; intermixed works fine | ||
parser = ErrorRaisingArgumentParser(prog='PROG') | ||
def test_required_exclusive(self): | ||
# required mutually exclusive group; intermixed works fine | ||
parser = argparse.ArgumentParser(prog='PROG', exit_on_error=False) | ||
group = parser.add_mutually_exclusive_group(required=True) | ||
group.add_argument('--foo', action='store_true', help='FOO') | ||
group.add_argument('--spam', help='SPAM') | ||
parser.add_argument('badger', nargs='*', default='X', help='BADGER') | ||
args = parser.parse_intermixed_args('--foo 1 2'.split()) | ||
self.assertEqual(NS(badger=['1', '2'], foo=True, spam=None), args) | ||
args = parser.parse_intermixed_args('1 --foo 2'.split()) | ||
self.assertEqual(NS(badger=['1', '2'], foo=True, spam=None), args) | ||
self.assertRaises(ArgumentParserError, parser.parse_intermixed_args, '1 2'.split()) | ||
self.assertRaisesRegex(argparse.ArgumentError, | ||
'one of the arguments --foo --spam is required', | ||
parser.parse_intermixed_args, '1 2'.split()) | ||
self.assertEqual(group.required, True) | ||
|
||
def test_exclusive_incompatible(self): | ||
# mutually exclusive group including positional - fail | ||
parser = ErrorRaisingArgumentParser(prog='PROG') | ||
def test_required_exclusive_with_positional(self): | ||
# required mutually exclusive group with positional argument | ||
parser = argparse.ArgumentParser(prog='PROG', exit_on_error=False) | ||
group = parser.add_mutually_exclusive_group(required=True) | ||
group.add_argument('--foo', action='store_true', help='FOO') | ||
group.add_argument('--spam', help='SPAM') | ||
group.add_argument('badger', nargs='*', default='X', help='BADGER') | ||
self.assertRaises(TypeError, parser.parse_intermixed_args, []) | ||
args = parser.parse_intermixed_args(['--foo']) | ||
self.assertEqual(NS(foo=True, spam=None, badger='X'), args) | ||
args = parser.parse_intermixed_args(['a', 'b']) | ||
self.assertEqual(NS(foo=False, spam=None, badger=['a', 'b']), args) | ||
self.assertRaisesRegex(argparse.ArgumentError, | ||
'one of the arguments --foo --spam badger is required', | ||
parser.parse_intermixed_args, []) | ||
self.assertRaisesRegex(argparse.ArgumentError, | ||
'argument badger: not allowed with argument --foo', | ||
parser.parse_intermixed_args, ['--foo', 'a', 'b']) | ||
self.assertRaisesRegex(argparse.ArgumentError, | ||
'argument badger: not allowed with argument --foo', | ||
parser.parse_intermixed_args, ['a', '--foo', 'b']) | ||
self.assertEqual(group.required, True) | ||
|
||
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 | ||
|
@@ -6485,7 +6507,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') | ||
|
||
# ========================== | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Fix several bugs in :meth:`argparse.ArgumentParser.parse_intermixed_args`. | ||
|
||
* The parser no longer changes temporarily during parsing. | ||
* Default values are not processed twice. | ||
* Required mutually exclusive groups containing positional arguments are now supported. | ||
* The missing arguments report now includes the names of all required optional and positional arguments. | ||
* Unknown options can be intermixed with positional arguments in parse_known_intermixed_args(). |
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?