Vapor文档学习十三:Commands

2017-04-20  本文已影响44人  Supremodeamor

Vapor自定义控制台指令很轻松。

Example

想要自定义控制台指令必须创建一个新的.swift文件,然后引入VaporConsole模块,然后实现Command协议。

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

创建完Custom Command文件之后,我们切换到main.swift文件,将自定义指令添加到droplet中

drop.commands.append(MyCustomCommand(console: drop.console))

这将允许Vapor访问我们的自定义指令,并且在--help中显示其信息。
在编译应用程序之后可以输入下面的指令运行我们的指令:

.build/debug/App command

<b>总结:</b>自定义指令方便我们进行个性化的调试,当然也可以方便我们对程序进行管理。

上一篇 下一篇

猜你喜欢

热点阅读