8000 gh-83897: Raise on conflicting subparser names. by anntzer · Pull Request #18605 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-83897: Raise on conflicting subparser names. #18605

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Lib/argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
13 changes: 13 additions & 0 deletions Lib/test/test_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Raise an ArgumentError when the same subparser name is added twice to an
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about using added twice or more to replace added twice?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You won't be able to add it more than twice if it errors out on the second addition...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make sense.

@rhettinger Hi, raymond. Pls review this PR if you have free time.

`argparse.ArgumentParser`. This is consistent with the (default) behavior
when the same option string is added twice to an ArgumentParser.
0