summaryrefslogtreecommitdiffstats
path: root/Lib/argparse.py
diff options
context:
space:
mode:
authorAntony Lee <anntzer.lee@gmail.com>2022-05-01 06:04:50 (GMT)
committerGitHub <noreply@github.com>2022-05-01 06:04:50 (GMT)
commitad5e8520f3e117f45481513014548a39879d30d2 (patch)
treee967814e58ce8cd84ad3debc8f6ff91781991ef8 /Lib/argparse.py
parent9588f880a286a8cc5597188f6ab44108c8f18761 (diff)
downloadcpython-ad5e8520f3e117f45481513014548a39879d30d2.zip
cpython-ad5e8520f3e117f45481513014548a39879d30d2.tar.gz
cpython-ad5e8520f3e117f45481513014548a39879d30d2.tar.bz2
bpo-39716: Raise on conflicting subparser names. (GH-18605)
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.) Automerge-Triggered-By: GH:rhettinger
Diffstat (limited to 'Lib/argparse.py')
-rw-r--r--Lib/argparse.py7
1 files changed, 7 insertions, 0 deletions
diff --git a/Lib/argparse.py b/Lib/argparse.py
index 668e1d4..c47aeff 100644
--- a/Lib/argparse.py
+++ b/Lib/argparse.py
@@ -1171,6 +1171,13 @@ class _SubParsersAction(Action):
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')