From 57c1ed8d16a710ad118e5286d6d6a83c1885fd18 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Sat, 22 Feb 2020 11:54:40 +0100 Subject: [PATCH] bpo-39716: Raise on conflicting subparser names. Raise an ArgumentError when the same subparser name is added twice to an ArgumentParser. This is consistent with the (default) behavior when the same option string is added twice to an ArgumentParser. (Support for `conflict_handler="resolve"` could be considered as a followup feature, although real use cases seem even rarer than "resolve"ing option-strings.) --- Lib/argparse.py | 7 +++++++ Lib/test/test_argparse.py | 13 +++++++++++++ .../2020-02-22-12-02-11.bpo-39716.z2WhDQ.rst | 3 +++ 3 files changed, 23 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2020-02-22-12-02-11.bpo-39716.z2WhDQ.rst 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.