✎ Edit on GitHub

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.

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...")
    }
}

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.

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