Skip to content

Commit

Permalink
Fixed nesting of subcommands.
Browse files Browse the repository at this point in the history
  • Loading branch information
memfrag committed Sep 30, 2019
1 parent 7d326df commit 3621d5a
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
6 changes: 5 additions & 1 deletion Sources/CLIKit/Command Line/Command.swift
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,11 @@ internal class InternalNamedCommands: InternalCommands, Commands {
parentCommands = parents
internalCommands = []
internalCommands = originalCommands.namedCommands.map {
InternalNamedCommand(name: $0.name, command: $0.command, parents: parents + [name])
if let subcommands = $0.command as? Commands {
return InternalNamedCommands(name: $0.name, commands: subcommands, parents: parents + [name])
} else {
return InternalNamedCommand(name: $0.name, command: $0.command, parents: parents + [name])
}
}
}

Expand Down
31 changes: 29 additions & 2 deletions Tests/CLIKitTests/CLIKitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ final class CLIKitTests: XCTestCase {

XCTAssertNoThrow(try runCommand())
}

func testUnrecognizedCommand() {

func runCommand() throws {
Expand Down Expand Up @@ -121,7 +121,32 @@ final class CLIKitTests: XCTestCase {

XCTAssertNoThrow(try runCommand())
}


func testNestedCommands() {

func runCommand() throws {
let parser = CommandLineParser()

let arguments: [String] = [
"bot",
"branch",
"list"
]

let parsedCommand = try parser.parseArguments(arguments, command: BotCommands(), expectedRootCommand: "bot")
try parsedCommand.run()
}

do {
try runCommand()
} catch {
print(error.localizedDescription)
XCTAssertTrue(false, error.localizedDescription)
}

XCTAssertTrue(true)
}

func testCommandBuildBot() {

func runCommand() throws {
Expand All @@ -141,6 +166,7 @@ final class CLIKitTests: XCTestCase {
try runCommand()
} catch {
print(error.localizedDescription)
XCTAssertTrue(false)
}

XCTAssertTrue(true)
Expand All @@ -167,6 +193,7 @@ final class CLIKitTests: XCTestCase {
("testOptionalInput", testOptionalInput),
("testOptionalInput2", testOptionalInput2),
("testCommandBuildBot", testCommandBuildBot),
("testNestedCommands", testNestedCommands),
("testMainframeCommand", testMainframeCommand),
]
}
21 changes: 21 additions & 0 deletions Tests/CLIKitTests/Helpers/BotCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,30 @@ class BuildCommand: Command {
}
}

class ListBranchesCommand: Command {

let description = "List branches"

var action: ((ListBranchesCommand) -> Void)?

func run() {
print("Requested a branch list")
action?(self)
}
}


class BranchCommand: Commands {

let description = "Branch commands"

let list = ListBranchesCommand()
}

class BotCommands: Commands {

let description = "BuildBot commands"

let build = BuildCommand()
let branch = BranchCommand()
}

0 comments on commit 3621d5a

Please sign in to comment.