Skip to content

Fail on duplicate subcommand names or aliases - #993

Open
awaisdev5765 wants to merge 1 commit into
apple:mainfrom
awaisdev5765:fix-duplicate-subcommands
Open

awaisdev5765 wants to merge 1 commit into
apple:mainfrom
awaisdev5765:fix-duplicate-subcommands

Conversation

@awaisdev5765

Copy link
Copy Markdown

Fixes #976

Problem

Duplicate subcommand names were silently ignored: firstChild(withName:) resolves to the first match, so with two subcommands sharing a commandName, only the first was invokable while the second remained listed in --help but could never run. This is the behavior reported in #976.

Fix

Tree.init(root:) now throws a new InitializationError.duplicateSubcommand when a sibling subcommand's name (or alias) collides with an already-registered name/alias at that level. CommandParser.init catches it via configurationFailure, following the established pattern for recursiveSubcommand and aliasMatchingCommand.

The check also catches alias collisions across sibling subcommands, which would otherwise shadow each other in the same way.

Example failure:

Error: The command "duplicate" can't be used as a subcommand more than once.

Testing

  • initializationWithDuplicateSubcommands: two subcommands named "duplicate" → throws (the issue's exact scenario)
  • initializationWithConflictingAliases: two subcommands sharing an alias → throws
  • initializationWithDistinctSubcommandsSucceeds: distinct name and alias → no false positive
  • Full suite: 575 tests in 57 suites, all passing

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Duplicate subcommands are silently ignored by ArgumentParser

2 participants