-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication_test.go
106 lines (97 loc) · 2.92 KB
/
application_test.go
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package clif_test
import (
"context"
"fmt"
"impractical.co/clif"
)
type funcCommandHandler func(ctx context.Context, resp *clif.Response)
func (f funcCommandHandler) Build(_ context.Context, _ clif.FlagSet, _ []string, _ *clif.Response) clif.Handler { //nolint:ireturn // filling an interface
return f
}
func (f funcCommandHandler) Handle(ctx context.Context, resp *clif.Response) {
f(ctx, resp)
}
type flagCommandHandler struct {
flags clif.FlagSet
args []string
f func(ctx context.Context, flags clif.FlagSet, args []string, resp *clif.Response)
}
func (f flagCommandHandler) Build(_ context.Context, flags clif.FlagSet, args []string, _ *clif.Response) clif.Handler { //nolint:ireturn // filling an interface
f.flags = flags
f.args = args
return f
}
func (f flagCommandHandler) Handle(ctx context.Context, resp *clif.Response) {
f.f(ctx, f.flags, f.args, resp)
}
//nolint:errcheck // several places we're not checking errors because they can't fail
func ExampleApplication() {
app := clif.Application{
Commands: []clif.Command{
{
Name: "help",
Description: "Displays help information about this program.",
Handler: funcCommandHandler(func(_ context.Context, resp *clif.Response) {
resp.Output.Write([]byte("this is help information\n"))
}),
},
{
Name: "foo",
Subcommands: []clif.Command{
{
Name: "bar",
Flags: []clif.FlagDef{
{
Name: "--quux",
},
},
Handler: flagCommandHandler{
f: func(_ context.Context, flags clif.FlagSet, args []string, resp *clif.Response) {
fmt.Fprintln(resp.Output, flags, args)
},
},
},
},
},
},
Flags: []clif.FlagDef{
{
Name: "--baaz",
IsToggle: true,
},
},
}
res := app.Run(context.Background(), clif.WithArgs([]string{"help"}))
fmt.Println(res)
res = app.Run(context.Background(), clif.WithArgs([]string{"foo", "bar", "--quux", "hello"}))
fmt.Println(res)
res = app.Run(context.Background(), clif.WithArgs([]string{"foo", "--quux", "hello", "bar"}))
fmt.Println(res)
res = app.Run(context.Background(), clif.WithArgs([]string{"--quux", "hello", "foo", "bar"}))
fmt.Println(res)
res = app.Run(context.Background(), clif.WithArgs([]string{"foo", "bar", "--quux=hello"}))
fmt.Println(res)
res = app.Run(context.Background(), clif.WithArgs([]string{"foo", "--quux=hello", "bar"}))
fmt.Println(res)
res = app.Run(context.Background(), clif.WithArgs([]string{"--quux=hello", "foo", "bar"}))
fmt.Println(res)
res = app.Run(context.Background(), clif.WithArgs([]string{"--baaz", "foo", "bar", "--quux", "hello"}))
fmt.Println(res)
// output:
// this is help information
// 0
// map[quux:[{true hello}]] []
// 0
// map[quux:[{true hello}]] []
// 0
// map[quux:[{true hello}]] []
// 0
// map[quux:[{true hello}]] []
// 0
// map[quux:[{true hello}]] []
// 0
// map[quux:[{true hello}]] []
// 0
// map[baaz:[{false }] quux:[{true hello}]] []
// 0
}