diff --git a/Lib/argparse.py b/Lib/argparse.py index 8a12dea76687994..afae9d375cefa8b 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 isinstance(argument, _SubParsersAction): + return '{%s}' % ','.join(map(str, argument.choices)) else: return None diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py index 4d0316f73edcd29..558861fb85e13ce 100644 --- a/Lib/test/test_argparse.py +++ b/Lib/test/test_argparse.py @@ -2052,6 +2052,12 @@ def test_required_subparsers_via_kwarg(self): subparsers.add_parser('run') self._test_required_subparsers(parser) + def test_required_subparsers_via_kwarg_no_dest(self): + parser = ErrorRaisingArgumentParser() + subparsers = parser.add_subparsers(required=True) + subparsers.add_parser('run') + self.assertArgumentParserError(parser.parse_args, ()) + def test_required_subparsers_default(self): parser = ErrorRaisingArgumentParser() subparsers = parser.add_subparsers(dest='command') diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-05-21-09-18-14.bpo-33109.s97TSf.rst b/Misc/NEWS.d/next/Core and Builtins/2021-05-21-09-18-14.bpo-33109.s97TSf.rst new file mode 100644 index 000000000000000..c0835356caccc50 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2021-05-21-09-18-14.bpo-33109.s97TSf.rst @@ -0,0 +1 @@ +Fixing argparse required subparsers error handling