From f21f0e04ee2f6ff1766adde66da81292bde65c43 Mon Sep 17 00:00:00 2001 From: Stephen Seaton-Blanchard Date: Sun, 25 Sep 2016 19:44:26 -0500 Subject: [PATCH 1/2] Update couscous.yml adding console.html to couscous.yml --- couscous.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/couscous.yml b/couscous.yml index bd7b26c7..7fa38870 100644 --- a/couscous.yml +++ b/couscous.yml @@ -62,6 +62,9 @@ menu: guide-hash: text: Hash relativeUrl: guide/hash.html + guide-commands: + text: Commands + relativeUrl: guide/commands.html routing: name: Routing items: From 30c64b9b6275cb54df871e0fc901e67c0c6321de Mon Sep 17 00:00:00 2001 From: Stephen Seaton-Blanchard Date: Sun, 25 Sep 2016 20:07:35 -0500 Subject: [PATCH 2/2] Create commands.md Writing Command documentation. --- guide/commands.md | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 guide/commands.md diff --git a/guide/commands.md b/guide/commands.md new file mode 100644 index 00000000..84befd6f --- /dev/null +++ b/guide/commands.md @@ -0,0 +1,45 @@ +--- +currentMenu: guide-commands +--- + +# Commands +Custom console commands on vapor are a breeze. + +## Example +To make a custom console command we must first create a new .swift file, import Vapor and Console, and implement the Command protocol. + +```swift +import Vapor +import Console + +final class MyCustomCommand: Command { + public let id = "command" + public let help = ["This command does things, like foo, and bar."] + public let console: ConsoleProtocol + + public init(console: ConsoleProtocol) { + self.console = console + } + + public func run(arguments: [String]) throws { + console.print("running custom command...") + } +} +``` + + - The **id** property is the string you will type in the console to access the command. `.build/debug/App command` will run the Custom Command. + - The **help** property is the help message that will give your custom command's users some idea as to how to access it. + - The **console** property is the object passed to your custom command that adheres to the console protocol, allowing manipulation of the console. + - The **run** method is where you put the logic relating to your command. + +After we work our magic in the Custom Command file, we switch over to our main.swift file and add the custom command to the droplet like so. +```swift +drop.commands.append(MyCustomCommand(console: drop.console)) +``` +This allows vapor access to our custom command and lets it know to display it in the --help section of the program. + +After compiling the application we can run our custom command like so + +``` +.build/debug/App command +```