summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_argparse.py
diff options
context:
space:
mode:
authorAnthony Sottile <asottile@umich.edu>2021-07-23 12:49:04 (GMT)
committerGitHub <noreply@github.com>2021-07-23 12:49:04 (GMT)
commit17575f73ce2cb9f3a4eb4cc416c690f9a4e7205c (patch)
treeaae5b349852e301f25d191e5408b562e7028e953 /Lib/test/test_argparse.py
parentfe13f0b0f696464dd6f283576668dbf57cb11399 (diff)
downloadcpython-17575f73ce2cb9f3a4eb4cc416c690f9a4e7205c.zip
cpython-17575f73ce2cb9f3a4eb4cc416c690f9a4e7205c.tar.gz
cpython-17575f73ce2cb9f3a4eb4cc416c690f9a4e7205c.tar.bz2
bpo-29298: Fix crash with required subparsers without dest (GH-3680)
Automerge-Triggered-By: GH:encukou
Diffstat (limited to 'Lib/test/test_argparse.py')
-rw-r--r--Lib/test/test_argparse.py24
1 files changed, 24 insertions, 0 deletions
diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py
index 4d0316f..0994e70 100644
--- a/Lib/test/test_argparse.py
+++ b/Lib/test/test_argparse.py
@@ -2060,6 +2060,30 @@ class TestAddSubparsers(TestCase):
ret = parser.parse_args(())
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):
parser = ErrorRaisingArgumentParser()
subparsers = parser.add_subparsers(dest='command', required=False)