mirror of https://github.com/vapor/docs.git
Update Command docs to use new AsyncCommand api (#953)
This commit is contained in:
parent
ed51309ce3
commit
32e917855b
|
|
@ -1,10 +1,10 @@
|
|||
# Commands
|
||||
|
||||
Vapor's Command API allows you to build custom command-line functions and interact with the terminal. It is what Vapor's default commands like `serve`, `routes`, and `migrate` are built on.
|
||||
Vapor's Command API allows you to build custom command-line functions and interact with the terminal. It is what Vapor's default commands like `serve`, `routes`, and `migrate` are built on.
|
||||
|
||||
## Default Commands
|
||||
|
||||
You can learn more about Vapor's default commands using the `--help` option.
|
||||
You can learn more about Vapor's default commands using the `--help` option.
|
||||
|
||||
```sh
|
||||
swift run App --help
|
||||
|
|
@ -18,7 +18,7 @@ swift run App serve --help
|
|||
|
||||
### Xcode
|
||||
|
||||
You can run commands in Xcode by adding arguments to the `App` scheme. To do this, follow these steps:
|
||||
You can run commands in Xcode by adding arguments to the `App` scheme. To do this, follow these steps:
|
||||
|
||||
- Choose `App` scheme (to the right of play/stop buttons)
|
||||
- Click "Edit Scheme"
|
||||
|
|
@ -28,35 +28,35 @@ You can run commands in Xcode by adding arguments to the `App` scheme. To do thi
|
|||
|
||||
## Custom Commands
|
||||
|
||||
You can create your own commands by creating types conforming to `Command`.
|
||||
You can create your own commands by creating types conforming to `AsyncCommand`.
|
||||
|
||||
```swift
|
||||
import Vapor
|
||||
|
||||
struct HelloCommand: Command {
|
||||
struct HelloCommand: AsyncCommand {
|
||||
...
|
||||
}
|
||||
```
|
||||
|
||||
Adding the custom command to `app.commands` will make it available via `swift run`.
|
||||
Adding the custom command to `app.asyncCommands` will make it available via `swift run`.
|
||||
|
||||
```swift
|
||||
app.commands.use(HelloCommand(), as: "hello")
|
||||
app.asyncCommands.use(HelloCommand(), as: "hello")
|
||||
```
|
||||
|
||||
To conform to `Command`, you must implement the `run` method. This requires declaring a `Signature`. You must also provide default help text.
|
||||
To conform to `AsyncCommand`, you must implement the `run` method. This requires declaring a `Signature`. You must also provide default help text.
|
||||
|
||||
```swift
|
||||
import Vapor
|
||||
|
||||
struct HelloCommand: Command {
|
||||
struct HelloCommand: AsyncCommand {
|
||||
struct Signature: CommandSignature { }
|
||||
|
||||
var help: String {
|
||||
"Says hello"
|
||||
}
|
||||
|
||||
func run(using context: CommandContext, signature: Signature) throws {
|
||||
func run(using context: CommandContext, signature: Signature) async throws {
|
||||
context.console.print("Hello, world!")
|
||||
}
|
||||
}
|
||||
|
|
@ -64,7 +64,7 @@ struct HelloCommand: Command {
|
|||
|
||||
This simple command example has no arguments or options, so leave the signature empty.
|
||||
|
||||
You can get access to the current console via the supplied context. Console has many helpful methods for prompting user input, output formatting, and more.
|
||||
You can get access to the current console via the supplied context. Console has many helpful methods for prompting user input, output formatting, and more.
|
||||
|
||||
```swift
|
||||
let name = context.console.ask("What is your \("name", color: .blue)?")
|
||||
|
|
@ -84,7 +84,7 @@ Take a look at this re-creation of the famous [`cowsay`](https://en.wikipedia.or
|
|||
```swift
|
||||
import Vapor
|
||||
|
||||
struct Cowsay: Command {
|
||||
struct Cowsay: AsyncCommand {
|
||||
struct Signature: CommandSignature {
|
||||
@Argument(name: "message")
|
||||
var message: String
|
||||
|
|
@ -100,7 +100,7 @@ struct Cowsay: Command {
|
|||
"Generates ASCII picture of a cow with a message."
|
||||
}
|
||||
|
||||
func run(using context: CommandContext, signature: Signature) throws {
|
||||
func run(using context: CommandContext, signature: Signature) async throws {
|
||||
let eyes = signature.eyes ?? "oo"
|
||||
let tongue = signature.tongue ?? " "
|
||||
let cow = #"""
|
||||
|
|
@ -121,7 +121,7 @@ struct Cowsay: Command {
|
|||
Try adding this to your application and running it.
|
||||
|
||||
```swift
|
||||
app.commands.use(Cowsay(), as: "cowsay")
|
||||
app.asyncCommands.use(Cowsay(), as: "cowsay")
|
||||
```
|
||||
|
||||
```sh
|
||||
|
|
|
|||
|
|
@ -1,10 +1,10 @@
|
|||
# Commando's
|
||||
|
||||
Vapor's Command API staat u toe om aangepaste command-line functies te bouwen en te communiceren met de terminal. Het is waar Vapor's standaard commando's zoals `serve`, `routes`, en `migrate` op gebouwd zijn.
|
||||
Vapor's Command API staat u toe om aangepaste command-line functies te bouwen en te communiceren met de terminal. Het is waar Vapor's standaard commando's zoals `serve`, `routes`, en `migrate` op gebouwd zijn.
|
||||
|
||||
## Standaard Commando's
|
||||
|
||||
U kunt meer te weten komen over de standaard commando's van Vapor door de `--help` optie te gebruiken.
|
||||
U kunt meer te weten komen over de standaard commando's van Vapor door de `--help` optie te gebruiken.
|
||||
|
||||
```sh
|
||||
swift run App --help
|
||||
|
|
@ -18,7 +18,7 @@ swift run App serve --help
|
|||
|
||||
### Xcode
|
||||
|
||||
U kunt in Xcode commando's uitvoeren door argumenten toe te voegen aan het `App` schema. Om dit te doen volgt u deze stappen:
|
||||
U kunt in Xcode commando's uitvoeren door argumenten toe te voegen aan het `App` schema. Om dit te doen volgt u deze stappen:
|
||||
|
||||
- Kies `App` schema (rechts van de play/stop knoppen)
|
||||
- Klik op "Edit Scheme"
|
||||
|
|
@ -28,35 +28,35 @@ U kunt in Xcode commando's uitvoeren door argumenten toe te voegen aan het `App`
|
|||
|
||||
## Aangepaste Commando's
|
||||
|
||||
Je kunt je eigen commando's maken door types te maken die voldoen aan `Command`.
|
||||
Je kunt je eigen commando's maken door types te maken die voldoen aan `AsyncCommand`.
|
||||
|
||||
```swift
|
||||
import Vapor
|
||||
|
||||
struct HelloCommand: Command {
|
||||
struct HelloCommand: AsyncCommand {
|
||||
...
|
||||
}
|
||||
```
|
||||
|
||||
Het toevoegen van het aangepaste commando aan `app.commands` maakt het beschikbaar via `swift run`.
|
||||
Het toevoegen van het aangepaste commando aan `app.asyncCommands` maakt het beschikbaar via `swift run`.
|
||||
|
||||
```swift
|
||||
app.commands.use(HelloCommand(), as: "hello")
|
||||
app.asyncCommands.use(HelloCommand(), as: "hello")
|
||||
```
|
||||
|
||||
Om te voldoen aan `Command`, moet je de `run` methode implementeren. Dit vereist het declareren van een `Signature`. Je moet ook een standaard helptekst opgeven.
|
||||
Om te voldoen aan `AsyncCommand`, moet je de `run` methode implementeren. Dit vereist het declareren van een `Signature`. Je moet ook een standaard helptekst opgeven.
|
||||
|
||||
```swift
|
||||
import Vapor
|
||||
|
||||
struct HelloCommand: Command {
|
||||
struct HelloCommand: AsyncCommand {
|
||||
struct Signature: CommandSignature { }
|
||||
|
||||
var help: String {
|
||||
"Says hello"
|
||||
}
|
||||
|
||||
func run(using context: CommandContext, signature: Signature) throws {
|
||||
func run(using context: CommandContext, signature: Signature) async throws {
|
||||
context.console.print("Hello, world!")
|
||||
}
|
||||
}
|
||||
|
|
@ -64,7 +64,7 @@ struct HelloCommand: Command {
|
|||
|
||||
Dit eenvoudige commando voorbeeld heeft geen argumenten of opties, dus laat de handtekening leeg.
|
||||
|
||||
Je kunt toegang krijgen tot de huidige console via de meegeleverde context. Console heeft veel handige methodes voor het vragen van gebruikersinvoer, uitvoeropmaak, en meer.
|
||||
Je kunt toegang krijgen tot de huidige console via de meegeleverde context. Console heeft veel handige methodes voor het vragen van gebruikersinvoer, uitvoeropmaak, en meer.
|
||||
|
||||
```swift
|
||||
let name = context.console.ask("What is your \("name", color: .blue)?")
|
||||
|
|
@ -84,7 +84,7 @@ Kijk eens naar deze re-creatie van het beroemde [`cowsay`](https://en.wikipedia.
|
|||
```swift
|
||||
import Vapor
|
||||
|
||||
struct Cowsay: Command {
|
||||
struct Cowsay: AsyncCommand {
|
||||
struct Signature: CommandSignature {
|
||||
@Argument(name: "message")
|
||||
var message: String
|
||||
|
|
@ -100,7 +100,7 @@ struct Cowsay: Command {
|
|||
"Generates ASCII picture of a cow with a message."
|
||||
}
|
||||
|
||||
func run(using context: CommandContext, signature: Signature) throws {
|
||||
func run(using context: CommandContext, signature: Signature) async throws {
|
||||
let eyes = signature.eyes ?? "oo"
|
||||
let tongue = signature.tongue ?? " "
|
||||
let cow = #"""
|
||||
|
|
@ -121,7 +121,7 @@ struct Cowsay: Command {
|
|||
Probeer dit toe te voegen aan je applicatie en het uit te voeren.
|
||||
|
||||
```swift
|
||||
app.commands.use(Cowsay(), as: "cowsay")
|
||||
app.asyncCommands.use(Cowsay(), as: "cowsay")
|
||||
```
|
||||
|
||||
```sh
|
||||
|
|
|
|||
|
|
@ -28,35 +28,35 @@ swift run App serve --help
|
|||
|
||||
## 自定义指令
|
||||
|
||||
你可以通过一个符合 `Command` 协议的类型创建你自己的命令
|
||||
你可以通过一个符合 `AsyncCommand` 协议的类型创建你自己的命令
|
||||
|
||||
```swift
|
||||
import Vapor
|
||||
|
||||
struct HelloCommand: Command {
|
||||
struct HelloCommand: AsyncCommand {
|
||||
...
|
||||
}
|
||||
```
|
||||
|
||||
将自定义指令加入到 `app.commands` 将允许你使用这个指令通过 `swift run`。
|
||||
将自定义指令加入到 `app.asyncCommands` 将允许你使用这个指令通过 `swift run`。
|
||||
|
||||
```swift
|
||||
app.commands.use(HelloCommand(), as: "hello")
|
||||
app.asyncCommands.use(HelloCommand(), as: "hello")
|
||||
```
|
||||
|
||||
为了符合 `Command` ,你必须实现 `run` 方法。这个方法需要你定义一个 `Signature` 。你还需要提供一个默认的帮助文本。
|
||||
为了符合 `AsyncCommand` ,你必须实现 `run` 方法。这个方法需要你定义一个 `Signature` 。你还需要提供一个默认的帮助文本。
|
||||
|
||||
```swift
|
||||
import Vapor
|
||||
|
||||
struct HelloCommand: Command {
|
||||
struct HelloCommand: AsyncCommand {
|
||||
struct Signature: CommandSignature { }
|
||||
|
||||
var help: String {
|
||||
"Says hello"
|
||||
}
|
||||
|
||||
func run(using context: CommandContext, signature: Signature) throws {
|
||||
func run(using context: CommandContext, signature: Signature) async throws {
|
||||
context.console.print("Hello, world!")
|
||||
}
|
||||
}
|
||||
|
|
@ -84,7 +84,7 @@ swift run App hello
|
|||
```swift
|
||||
import Vapor
|
||||
|
||||
struct Cowsay: Command {
|
||||
struct Cowsay: AsyncCommand {
|
||||
struct Signature: CommandSignature {
|
||||
@Argument(name: "message")
|
||||
var message: String
|
||||
|
|
@ -100,7 +100,7 @@ struct Cowsay: Command {
|
|||
"Generates ASCII picture of a cow with a message."
|
||||
}
|
||||
|
||||
func run(using context: CommandContext, signature: Signature) throws {
|
||||
func run(using context: CommandContext, signature: Signature) async throws {
|
||||
let eyes = signature.eyes ?? "oo"
|
||||
let tongue = signature.tongue ?? " "
|
||||
let cow = #"""
|
||||
|
|
@ -121,7 +121,7 @@ struct Cowsay: Command {
|
|||
尝试将这个指令加入到程序然后运行它。
|
||||
|
||||
```swift
|
||||
app.commands.use(Cowsay(), as: "cowsay")
|
||||
app.asyncCommands.use(Cowsay(), as: "cowsay")
|
||||
```
|
||||
|
||||
```sh
|
||||
|
|
|
|||
Loading…
Reference in New Issue