mirror of
https://github.com/python/cpython.git
synced 2025-09-14 20:56:06 +00:00
bpo-29298: Fix crash with required subparsers without dest (GH-3680) (GH-27303)
(cherry picked from commit 17575f73ce
)
Co-authored-by: Anthony Sottile <asottile@umich.edu>
This commit is contained in:
parent
21db59fc75
commit
c589992e09
3 changed files with 28 additions and 0 deletions
|
@ -727,6 +727,8 @@ def _get_action_name(argument):
|
||||||
return argument.metavar
|
return argument.metavar
|
||||||
elif argument.dest not in (None, SUPPRESS):
|
elif argument.dest not in (None, SUPPRESS):
|
||||||
return argument.dest
|
return argument.dest
|
||||||
|
elif argument.choices:
|
||||||
|
return '{' + ','.join(argument.choices) + '}'
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
|
@ -2060,6 +2060,30 @@ class TestAddSubparsers(TestCase):
|
||||||
ret = parser.parse_args(())
|
ret = parser.parse_args(())
|
||||||
self.assertIsNone(ret.command)
|
self.assertIsNone(ret.command)
|
||||||
|
|
||||||
|
def test_required_subparsers_no_destination_error(self):
|
||||||
|
parser = ErrorRaisingArgumentParser()
|
||||||
|
subparsers = parser.add_subparsers(required=True)
|
||||||
|
subparsers.add_parser('foo')
|
||||||
|
subparsers.add_parser('bar')
|
||||||
|
with self.assertRaises(ArgumentParserError) as excinfo:
|
||||||
|
parser.parse_args(())
|
||||||
|
self.assertRegex(
|
||||||
|
excinfo.exception.stderr,
|
||||||
|
'error: the following arguments are required: {foo,bar}\n$'
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_wrong_argument_subparsers_no_destination_error(self):
|
||||||
|
parser = ErrorRaisingArgumentParser()
|
||||||
|
subparsers = parser.add_subparsers(required=True)
|
||||||
|
subparsers.add_parser('foo')
|
||||||
|
subparsers.add_parser('bar')
|
||||||
|
with self.assertRaises(ArgumentParserError) as excinfo:
|
||||||
|
parser.parse_args(('baz',))
|
||||||
|
self.assertRegex(
|
||||||
|
excinfo.exception.stderr,
|
||||||
|
r"error: argument {foo,bar}: invalid choice: 'baz' \(choose from 'foo', 'bar'\)\n$"
|
||||||
|
)
|
||||||
|
|
||||||
def test_optional_subparsers(self):
|
def test_optional_subparsers(self):
|
||||||
parser = ErrorRaisingArgumentParser()
|
parser = ErrorRaisingArgumentParser()
|
||||||
subparsers = parser.add_subparsers(dest='command', required=False)
|
subparsers = parser.add_subparsers(dest='command', required=False)
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Fix ``TypeError`` when required subparsers without ``dest`` do not receive
|
||||||
|
arguments. Patch by Anthony Sottile.
|
Loading…
Add table
Add a link
Reference in a new issue