-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands_examples_test.go
290 lines (246 loc) · 6.94 KB
/
commands_examples_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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package cli_test
import (
"fmt"
"math"
"github.com/mailund/cli"
)
func ExampleCommand() {
cmd := cli.NewCommand(
cli.CommandSpec{
Name: "first",
Short: "my first command",
Long: "The first command I ever made",
})
cmd.Run([]string{})
// Output:
}
func ExampleCommand_second() {
cmd := cli.NewCommand(
cli.CommandSpec{
Name: "hello",
Short: "prints hello world",
Action: func(_ interface{}) { fmt.Println("hello, world!") },
})
cmd.Run([]string{})
// Output: hello, world!
}
func ExampleCommand_third() {
type AddArgs struct {
X int `pos:"x" descr:"first addition argument"`
Y int `pos:"y" descr:"second addition argument"`
}
add := cli.NewCommand(
cli.CommandSpec{
Name: "add",
Short: "adds two floating point arguments",
Long: "<long description of how addition works>",
Init: func() interface{} { return new(AddArgs) },
Action: func(args interface{}) {
// Get the argumens through casting the args
argv, _ := args.(*AddArgs)
// Then we can do our calculations with the arguments
fmt.Printf("Result: %d\n", argv.X+argv.Y)
},
})
add.Run([]string{"2", "2"})
// Output: Result: 4
}
func ExampleCommand_fourth() {
type NumArgs struct {
Round bool `flag:"round" descr:"should result be rounded to nearest integer"`
Args []float64 `pos:"args" descr:"numerical arguments to command"`
}
cmd := cli.NewCommand(
cli.CommandSpec{
Name: "sum",
Short: "adds floating point arguments",
Long: "<long description of how addition works>",
Init: func() interface{} { return new(NumArgs) },
Action: func(args interface{}) {
argv, _ := args.(*NumArgs)
res := 0.0
for _, x := range argv.Args {
res += x
}
if argv.Round {
res = math.Round(res)
}
fmt.Printf("Result: %f\n", res)
},
})
// Don't round off the result
cmd.Run([]string{"0.42", "3.14", "0.3"})
// Round off the result
cmd.Run([]string{"--round", "0.42", "3.14", "0.3"})
// Output:
// Result: 3.860000
// Result: 4.000000
}
func ExampleCommand_fifth() {
type NumArgs struct {
Round bool `flag:"round"`
Args []float64 `pos:"args"`
}
cmd := cli.NewCommand(
cli.CommandSpec{
Name: "sum",
Short: "adds floating point arguments",
Long: "<long description of how addition works>",
Init: func() interface{} {
return &NumArgs{Round: true}
},
Action: func(args interface{}) {
argv, _ := args.(*NumArgs)
res := 0.0
for _, x := range argv.Args {
res += x
}
if argv.Round {
res = math.Round(res)
}
fmt.Printf("Result: %f\n", res)
},
})
// This will round off the result
cmd.Run([]string{"0.42", "3.14", "0.3"})
// Turn off rounding
cmd.Run([]string{"--round=false", "0.42", "3.14", "0.3"})
// Output:
// Result: 4.000000
// Result: 3.860000
}
func ExampleCommand_sixth() {
type CalcArgs struct {
X int `pos:"x" descr:"first argument"`
Y int `pos:"y" descr:"second argument"`
}
add := cli.NewCommand(
cli.CommandSpec{
Name: "add",
Short: "adds two floating point arguments",
Long: "<long description of how addition works>",
Init: func() interface{} { return new(CalcArgs) },
Action: func(args interface{}) {
fmt.Printf("Result: %d\n", args.(*CalcArgs).X+args.(*CalcArgs).Y)
},
})
mult := cli.NewCommand(
cli.CommandSpec{
Name: "mult",
Short: "multiplies two floating point arguments",
Long: "<long description of how multiplication works>",
Init: func() interface{} { return new(CalcArgs) },
Action: func(args interface{}) {
fmt.Printf("Result: %d\n", args.(*CalcArgs).X*args.(*CalcArgs).Y)
},
})
calc := cli.NewCommand(
cli.CommandSpec{
Name: "calc",
Short: "does calculations",
Long: "<long explanation of arithmetic>",
Subcommands: []*cli.Command{add, mult},
})
calc.Run([]string{"add", "2", "3"})
calc.Run([]string{"mult", "2", "3"})
// Output:
// Result: 5
// Result: 6
}
func ExampleCommand_seventh() {
type CalcArgs struct {
X int `pos:"x" descr:"first argument"`
Y int `pos:"y" descr:"second argument"`
}
add := cli.NewCommand(
cli.CommandSpec{
Name: "add",
Short: "adds two floating point arguments",
Long: "<long description of how addition works>",
Init: func() interface{} { return new(CalcArgs) },
Action: func(args interface{}) {
fmt.Printf("Result: %d\n", args.(*CalcArgs).X+args.(*CalcArgs).Y)
},
})
mult := cli.NewCommand(
cli.CommandSpec{
Name: "mult",
Short: "multiplies two floating point arguments",
Long: "<long description of how multiplication works>",
Init: func() interface{} { return new(CalcArgs) },
Action: func(args interface{}) {
fmt.Printf("Result: %d\n", args.(*CalcArgs).X*args.(*CalcArgs).Y)
},
})
calc := cli.NewCommand(
cli.CommandSpec{
Name: "calc",
Short: "does calculations",
Long: "<long explanation of arithmetic>",
Action: func(_ interface{}) {
fmt.Println("Hello from calc")
},
Subcommands: []*cli.Command{add, mult},
})
calc.Run([]string{"add", "2", "3"})
calc.Run([]string{"mult", "2", "3"})
// Output:
// Hello from calc
// Result: 5
// Hello from calc
// Result: 6
}
func ExampleNewMenu() {
type CalcArgs struct {
X int `pos:"x" descr:"first argument"`
Y int `pos:"y" descr:"second argument"`
}
add := cli.NewCommand(
cli.CommandSpec{
Name: "add",
Short: "adds two floating point arguments",
Long: "<long description of how addition works>",
Init: func() interface{} { return new(CalcArgs) },
Action: func(args interface{}) {
fmt.Printf("Result: %d\n", args.(*CalcArgs).X+args.(*CalcArgs).Y)
},
})
mult := cli.NewCommand(
cli.CommandSpec{
Name: "mult",
Short: "multiplies two floating point arguments",
Long: "<long description of how multiplication works>",
Init: func() interface{} { return new(CalcArgs) },
Action: func(args interface{}) {
fmt.Printf("Result: %d\n", args.(*CalcArgs).X*args.(*CalcArgs).Y)
},
})
calc := cli.NewMenu(
"calc", "does calculations", "<long explanation of arithmetic>",
add, mult)
calc.Run([]string{"add", "2", "3"})
calc.Run([]string{"mult", "2", "3"})
// Output:
// Result: 5
// Result: 6
}
func ExampleNewMenu_second() {
say := func(x string) func(interface{}) {
return func(argv interface{}) { fmt.Println(x) }
}
menu := cli.NewMenu("menu", "", "",
cli.NewMenu("foo", "", "",
cli.NewCommand(cli.CommandSpec{Name: "x", Action: say("menu/foo/x")}),
cli.NewCommand(cli.CommandSpec{Name: "y", Action: say("menu/foo/y")})),
cli.NewMenu("bar", "", "",
cli.NewCommand(cli.CommandSpec{Name: "x", Action: say("menu/bar/x")}),
cli.NewCommand(cli.CommandSpec{Name: "y", Action: say("menu/bar/y")})),
cli.NewCommand(cli.CommandSpec{Name: "baz", Action: say("menu/baz")}))
menu.Run([]string{"baz"}) // will say menu/baz
menu.Run([]string{"foo", "x"}) // will say menu/foo/x
menu.Run([]string{"bar", "y"}) // will say menu/bar/y
// Output:
// menu/baz
// menu/foo/x
// menu/bar/y
}