Skip to content
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

Abort on ambiguous subcommand names #629

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions Sources/ArgumentParser/Parsing/CommandParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ struct CommandParser {
self.commandTree = try Tree(root: rootCommand)
} catch Tree<ParsableCommand.Type>.InitializationError.recursiveSubcommand(let command) {
fatalError("The ParsableCommand \"\(command)\" can't have itself as its own subcommand.")
} catch Tree<ParsableCommand.Type>.InitializationError.matchingCommandNames(let command, let commandName) {
fatalError("Ambiguous subcommand name \"\(commandName)\" encountered in ParsableCommand \"\(command)\"")
} catch {
fatalError("Unexpected error: \(error).")
}
Expand Down
5 changes: 5 additions & 0 deletions Sources/ArgumentParser/Utilities/Tree.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ extension Tree where Element == ParsableCommand.Type {

convenience init(root command: ParsableCommand.Type) throws {
self.init(command)
var subcommandNames = Set<String>()
for subcommand in command.configuration.subcommands {
if !subcommandNames.insert(subcommand._commandName).inserted {
throw InitializationError.matchingCommandNames(command, subcommand._commandName)
}
if subcommand == command {
throw InitializationError.recursiveSubcommand(subcommand)
}
Expand All @@ -100,5 +104,6 @@ extension Tree where Element == ParsableCommand.Type {

enum InitializationError: Error {
case recursiveSubcommand(ParsableCommand.Type)
case matchingCommandNames(ParsableCommand.Type, String)
}
}
32 changes: 32 additions & 0 deletions Tests/ArgumentParserUnitTests/TreeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,35 @@ extension TreeTests {
XCTAssertThrowsError(try Tree(root: Root.asCommand))
}
}

extension TreeTests {
struct RootWithSubs: ParsableCommand {
static let configuration = CommandConfiguration(subcommands: [Sub.self, SubTwo.self])

struct Sub: ParsableCommand {
}

struct SubTwo: ParsableCommand {
static let configuration = CommandConfiguration(commandName: "sub")
}
}

struct RootWithNestedSubs: ParsableCommand {
static let configuration = CommandConfiguration(subcommands: [Sub.self])

struct Sub: ParsableCommand {
static let configuration = CommandConfiguration(subcommands: [Nested.self, NestedTwo.self])
struct Nested: ParsableCommand {
}

struct NestedTwo: ParsableCommand {
static let configuration = CommandConfiguration(commandName: "nested")
}
}
}

func testInitializationWithMatchingCommandNames() {
XCTAssertThrowsError(try Tree(root: RootWithSubs.asCommand))
XCTAssertThrowsError(try Tree(root: RootWithNestedSubs.asCommand))
}
}