Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitad5e852

Browse files
authored
bpo-39716: Raise on conflicting subparser names. (GH-18605)
Raise an ArgumentError when the same subparser name is added twice to anArgumentParser. This is consistent with the (default) behavior when thesame option string is added twice to an ArgumentParser.(Support for `conflict_handler="resolve"` could be considered as afollowup feature, although real use cases seem even rarer than"resolve"ing option-strings.)Automerge-Triggered-By: GH:rhettinger
1 parent9588f88 commitad5e852

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

‎Lib/argparse.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,6 +1171,13 @@ def add_parser(self, name, **kwargs):
11711171

11721172
aliases=kwargs.pop('aliases', ())
11731173

1174+
ifnameinself._name_parser_map:
1175+
raiseArgumentError(self,_('conflicting subparser: %s')%name)
1176+
foraliasinaliases:
1177+
ifaliasinself._name_parser_map:
1178+
raiseArgumentError(
1179+
self,_('conflicting subparser alias: %s')%alias)
1180+
11741181
# create a pseudo-action to hold the choice help
11751182
if'help'inkwargs:
11761183
help=kwargs.pop('help')

‎Lib/test/test_argparse.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4804,6 +4804,19 @@ def test_resolve_error(self):
48044804
--spam NEW_SPAM
48054805
'''))
48064806

4807+
deftest_subparser_conflict(self):
4808+
parser=argparse.ArgumentParser()
4809+
sp=parser.add_subparsers()
4810+
sp.add_parser('fullname',aliases=['alias'])
4811+
self.assertRaises(argparse.ArgumentError,
4812+
sp.add_parser,'fullname')
4813+
self.assertRaises(argparse.ArgumentError,
4814+
sp.add_parser,'alias')
4815+
self.assertRaises(argparse.ArgumentError,
4816+
sp.add_parser,'other',aliases=['fullname'])
4817+
self.assertRaises(argparse.ArgumentError,
4818+
sp.add_parser,'other',aliases=['alias'])
4819+
48074820

48084821
# =============================
48094822
# Help and Version option tests
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Raise an ArgumentError when the same subparser name is added twice to an
2+
`argparse.ArgumentParser`. This is consistent with the (default) behavior
3+
when the same option string is added twice to an ArgumentParser.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp