Link to example code.

This commit is contained in:
Alec Thomas
2018-06-23 15:21:14 +10:00
parent e756d995c7
commit f7acb2b389
+10 -4
View File
@@ -8,8 +8,8 @@
1. [Introduction](#introduction)
1. [Help](#help)
1. [Command handling](#command-handling)
1. [1. Switch on the command string](#1-switch-on-the-command-string)
1. [2. Attach a `Run(...) error` method to each command](#2-attach-a-run-error-method-to-each-command)
1. [Switch on the command string](#switch-on-the-command-string)
1. [Attach a `Run(...) error` method to each command](#attach-a-run-error-method-to-each-command)
1. [Flags](#flags)
1. [Commands and sub-commands](#commands-and-sub-commands)
1. [Branching positional arguments](#branching-positional-arguments)
@@ -115,10 +115,14 @@ eg.
There are two ways to handle commands in Kong.
### 1. Switch on the command string
### Switch on the command string
When you call `kong.Parse()` it will return a unique string representation of the command. Each command branch in the hierarchy will be a bare word and each branching argument or required positional argument will be the name surrounded by angle brackets. Here's an example:
There's an example of this pattern [here](https://github.com/alecthomas/kong/blob/master/_examples/shell/main.go).
eg.
```go
package main
@@ -150,7 +154,7 @@ func main() {
This has the advantage that it is convenient, but the downside that if you modify your CLI structure, the strings may change. This can be fragile.
### 2. Attach a `Run(...) error` method to each command
### Attach a `Run(...) error` method to each command
A more robust approach is to break each command out into their own structs:
@@ -159,6 +163,8 @@ A more robust approach is to break each command out into their own structs:
3. Call `kong.Kong.Parse()` to obtain a `kong.Context`.
4. Call `kong.Context.Run(params...)` to call the selected parsed command.
There's a full example emulating part of the Docker CLI [here](https://github.com/alecthomas/kong/tree/master/_examples/docker).
eg.
```go