Protocol
Command
A command that can be run through a
Console.protocol Command : AnyCommand
Overview
Both Command and CommandGroup conform to AnyCommand which provides the basic requirements all command-like types need. In addition to those types, a Command requires zero or more CommandArguments.
Below is a sample command that generates ASCII picture of a cow with a message.
struct CowsayCommand: Command {
public struct Signature: CommandSignature {
@Argument(name: "message")
var message: String
@Option(name: "eyes", short: "e")
var eyes: String?
@Option(name: "tongue", short: "t")
var tongue: String?
public init() { }
}
var help: String {
"Generates ASCII picture of a cow with a message."
}
public init() { }
public func run(using context: CommandContext, signature: Signature) throws {
let eyes = signature.eyes ?? "oo"
let tongue = signature.tongue ?? " "
let padding = String(repeating: "-", count: message.count)
let text: String = """
\(padding)
< \(message) >
\(padding)
\\ ^__^
\\ (\(eyes)\\_______
(__)\\ )\\/\\
\(tongue) ||----w |
|| ||
"""
context.console.print(text)
}
}
Meanwhile you can use the Command in an executable target like:
let console: Console = Terminal()
var input = CommandInput(arguments: CommandLine.arguments)
var context = CommandContext(console: console, input: input)
try console.run(CoswayCommand(), with: context)
Use Commands to register commands and create a CommandGroup.
Note
You can also use console.run(...) to run an AnyCommand manually.
Here is a simple example of the command in action, assuming it has been registered as "cowsay".
swift run cowsay Hello
-----
< Hello >
-----
\ ^__^
\ (oo\_______
(__)\ )\/\
||----w |
|| ||
And an example with flags:
swift run cowsay "I'm a dead cow" -e xx -t U
--------------
< I'm a dead cow >
--------------
\ ^__^
\ (xx\_______
(__)\ )\/\
U ||----w |
|| ||
Topics
Associated Types
Instance Methods
Relationships
Inherits From
AnyAsyncCommandAnyCommandSwift.SendableSwift.SendableMetatype