Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Command docs to use new AsyncCommand api #953

Merged
merged 1 commit into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions docs/advanced/commands.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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"
Expand All @@ -28,43 +28,43 @@ 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!")
}
}
```

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)?")
Expand All @@ -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
Expand All @@ -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 = #"""
Expand All @@ -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
Expand Down
28 changes: 14 additions & 14 deletions docs/advanced/commands.nl.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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"
Expand All @@ -28,43 +28,43 @@ 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!")
}
}
```

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)?")
Expand All @@ -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
Expand All @@ -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 = #"""
Expand All @@ -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
Expand Down
20 changes: 10 additions & 10 deletions docs/advanced/commands.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!")
}
}
Expand Down Expand Up @@ -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
Expand All @@ -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 = #"""
Expand All @@ -121,7 +121,7 @@ struct Cowsay: Command {
尝试将这个指令加入到程序然后运行它。

```swift
app.commands.use(Cowsay(), as: "cowsay")
app.asyncCommands.use(Cowsay(), as: "cowsay")
```

```sh
Expand Down