summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
Diffstat (limited to 'Lib')
-rw-r--r--Lib/argparse.py2
-rw-r--r--Lib/test/test_argparse.py24
2 files changed, 26 insertions, 0 deletions
diff --git a/Lib/argparse.py b/Lib/argparse.py
index 8a12dea..e3a49e7 100644
--- a/Lib/argparse.py
+++ b/Lib/argparse.py
@@ -727,6 +727,8 @@ def _get_action_name(argument):
return argument.metavar
elif argument.dest not in (None, SUPPRESS):
return argument.dest
+ elif argument.choices:
+ return '{' + ','.join(argument.choices) + '}'
else:
return None
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)