-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_test.go
More file actions
63 lines (56 loc) · 1.52 KB
/
command_test.go
File metadata and controls
63 lines (56 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package cli
import (
"context"
"fmt"
"os"
)
func ExampleNewCommand() {
command, _ := NewCommand("server", "An http server.",
SetVersion("v0.1.0"),
AddVersionFlag(AddFlagShort('V')),
AddHelpFlag(AddFlagShort('h')),
AddFlag("port", "Port to run server on.",
AddFlagShort('p'),
SetFlagDefault(3000),
SetFlagDefaultEnv("PORT"),
),
AddFlag("auth-required", "Whether to require authentication.",
SetFlagDefault(true),
),
AddSubCmd("proxy", "Proxy requests to another server.",
AddArg("target", "Target server to proxy requests to.",
SetArgParser(URLParser),
),
),
)
command.renderHelp(os.Stdout)
// Output:
// server v0.1.0: An http server.
//
// Usage:
// server [flags] [sub-command]
//
// Sub-commands:
// proxy: Proxy requests to another server.
//
// Flags:
// --version -V Print version. (type: bool, default: "false")
// --help -h Print help. (type: bool, default: "false")
// --port -p Port to run server on. (type: int, default: $PORT, "3000")
// --auth-required Whether to require authentication. (type: bool, default: "true")
}
func ExampleRun() {
oldArgs := os.Args
os.Args = []string{"echo", "hello"}
defer func() { os.Args = oldArgs }()
Run("echo", "Echo the arguments.",
AddArg("arg", "The argument to echo."),
SetHandler(func(ctx context.Context) error {
arg, _ := ArgValue[string](ctx, "arg")
fmt.Println(arg)
return nil
}),
)
// Output:
// hello
}