diff --git a/Lib/argparse.py b/Lib/argparse.py index 9c710cef5b6aaa..032d958bb4daf2 100644 --- a/Lib/argparse.py +++ b/Lib/argparse.py @@ -1162,6 +1162,13 @@ def add_parser(self, name, **kwargs): aliases = kwargs.pop('aliases', ()) + if name in self._name_parser_map: + raise ArgumentError(self, _('conflicting subparser: %s') % name) + for alias in aliases: + if alias in self._name_parser_map: + raise ArgumentError( + self, _('conflicting subparser alias: %s') % alias) + # create a pseudo-action to hold the choice help if 'help' in kwargs: help = kwargs.pop('help') diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py index 9899a53d6d1974..9ce3cc913423d1 100644 --- a/Lib/test/test_argparse.py +++ b/Lib/test/test_argparse.py @@ -4603,6 +4603,19 @@ def test_resolve_error(self): --spam NEW_SPAM ''')) + def test_subparser_conflict(self): + parser = argparse.ArgumentParser() + sp = parser.add_subparsers() + sp.add_parser('fullname', aliases=['alias']) + self.assertRaises(argparse.ArgumentError, + sp.add_parser, 'fullname') + self.assertRaises(argparse.ArgumentError, + sp.add_parser, 'alias') + self.assertRaises(argparse.ArgumentError, + sp.add_parser, 'other', aliases=['fullname']) + self.assertRaises(argparse.ArgumentError, + sp.add_parser, 'other', aliases=['alias']) + # ============================= # Help and Version option tests diff --git a/Misc/NEWS.d/next/Library/2020-02-22-12-02-11.bpo-39716.z2WhDQ.rst b/Misc/NEWS.d/next/Library/2020-02-22-12-02-11.bpo-39716.z2WhDQ.rst new file mode 100644 index 00000000000000..f122811e6bfe3b --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-02-22-12-02-11.bpo-39716.z2WhDQ.rst @@ -0,0 +1,3 @@ +Raise an ArgumentError when the same subparser name is added twice to an +`argparse.ArgumentParser`. This is consistent with the (default) behavior +when the same option string is added twice to an ArgumentParser.