@@ -49,7 +49,7 @@ func TestCommand(t *testing.T) {
49
49
Value :clibase .BoolOf (& lower ),
50
50
},
51
51
},
52
- Handler :clibase . HandlerFunc (func (i * clibase.Invokation )error {
52
+ Handler : (func (i * clibase.Invokation )error {
53
53
i .Stdout .Write ([]byte (prefix ))
54
54
w := i .Args [0 ]
55
55
if lower {
@@ -74,32 +74,27 @@ func TestCommand(t *testing.T) {
74
74
75
75
t .Run ("SimpleOK" ,func (t * testing.T ) {
76
76
t .Parallel ()
77
- i := & clibase.Invokation {
78
- Args : []string {"toupper" ,"hello" },
79
- Command :cmd (),
80
- }
77
+ i := cmd ().Invoke ("toupper" ,"hello" )
81
78
io := clibasetest .FakeIO (i )
82
79
i .Run ()
83
80
require .Equal (t ,"HELLO" ,io .Stdout .String ())
84
81
})
85
82
86
83
t .Run ("Alias" ,func (t * testing.T ) {
87
84
t .Parallel ()
88
- i := & clibase.Invokation {
89
- Args : []string {"up" ,"hello" },
90
- Command :cmd (),
91
- }
85
+ i := cmd ().Invoke (
86
+ "up" ,"hello" ,
87
+ )
92
88
io := clibasetest .FakeIO (i )
93
89
i .Run ()
94
90
require .Equal (t ,"HELLO" ,io .Stdout .String ())
95
91
})
96
92
97
93
t .Run ("NoSubcommand" ,func (t * testing.T ) {
98
94
t .Parallel ()
99
- i := & clibase.Invokation {
100
- Args : []string {"na" },
101
- Command :cmd (),
102
- }
95
+ i := cmd ().Invoke (
96
+ "na" ,
97
+ )
103
98
io := clibasetest .FakeIO (i )
104
99
err := i .Run ()
105
100
require .Empty (t ,io .Stdout .String ())
@@ -108,10 +103,9 @@ func TestCommand(t *testing.T) {
108
103
109
104
t .Run ("BadArgs" ,func (t * testing.T ) {
110
105
t .Parallel ()
111
- i := & clibase.Invokation {
112
- Args : []string {"toupper" },
113
- Command :cmd (),
114
- }
106
+ i := cmd ().Invoke (
107
+ "toupper" ,
108
+ )
115
109
io := clibasetest .FakeIO (i )
116
110
err := i .Run ()
117
111
require .Empty (t ,io .Stdout .String ())
@@ -120,10 +114,9 @@ func TestCommand(t *testing.T) {
120
114
121
115
t .Run ("UnknownFlags" ,func (t * testing.T ) {
122
116
t .Parallel ()
123
- i := & clibase.Invokation {
124
- Args : []string {"toupper" ,"--unknown" },
125
- Command :cmd (),
126
- }
117
+ i := cmd ().Invoke (
118
+ "toupper" ,"--unknown" ,
119
+ )
127
120
io := clibasetest .FakeIO (i )
128
121
err := i .Run ()
129
122
require .Empty (t ,io .Stdout .String ())
@@ -132,65 +125,59 @@ func TestCommand(t *testing.T) {
132
125
133
126
t .Run ("Verbose" ,func (t * testing.T ) {
134
127
t .Parallel ()
135
- i := & clibase.Invokation {
136
- Args : []string {"--verbose" ,"toupper" ,"hello" },
137
- Command :cmd (),
138
- }
128
+ i := cmd ().Invoke (
129
+ "--verbose" ,"toupper" ,"hello" ,
130
+ )
139
131
io := clibasetest .FakeIO (i )
140
132
require .NoError (t ,i .Run ())
141
133
require .Equal (t ,"HELLO!!!" ,io .Stdout .String ())
142
134
})
143
135
144
136
t .Run ("Verbose=" ,func (t * testing.T ) {
145
137
t .Parallel ()
146
- i := & clibase.Invokation {
147
- Args : []string {"--verbose=true" ,"toupper" ,"hello" },
148
- Command :cmd (),
149
- }
138
+ i := cmd ().Invoke (
139
+ "--verbose=true" ,"toupper" ,"hello" ,
140
+ )
150
141
io := clibasetest .FakeIO (i )
151
142
require .NoError (t ,i .Run ())
152
143
require .Equal (t ,"HELLO!!!" ,io .Stdout .String ())
153
144
})
154
145
155
146
t .Run ("PrefixSpace" ,func (t * testing.T ) {
156
147
t .Parallel ()
157
- i := & clibase.Invokation {
158
- Args : []string {"--prefix" ,"conv: " ,"toupper" ,"hello" },
159
- Command :cmd (),
160
- }
148
+ i := cmd ().Invoke (
149
+ "--prefix" ,"conv: " ,"toupper" ,"hello" ,
150
+ )
161
151
io := clibasetest .FakeIO (i )
162
152
require .NoError (t ,i .Run ())
163
153
require .Equal (t ,"conv: HELLO" ,io .Stdout .String ())
164
154
})
165
155
166
156
t .Run ("GlobalFlagsAnywhere" ,func (t * testing.T ) {
167
157
t .Parallel ()
168
- i := & clibase.Invokation {
169
- Args : []string {"toupper" ,"--prefix" ,"conv: " ,"hello" ,"--verbose" },
170
- Command :cmd (),
171
- }
158
+ i := cmd ().Invoke (
159
+ "toupper" ,"--prefix" ,"conv: " ,"hello" ,"--verbose" ,
160
+ )
172
161
io := clibasetest .FakeIO (i )
173
162
require .NoError (t ,i .Run ())
174
163
require .Equal (t ,"conv: HELLO!!!" ,io .Stdout .String ())
175
164
})
176
165
177
166
t .Run ("LowerVerbose" ,func (t * testing.T ) {
178
167
t .Parallel ()
179
- i := & clibase.Invokation {
180
- Args : []string {"toupper" ,"--verbose" ,"hello" ,"--lower" },
181
- Command :cmd (),
182
- }
168
+ i := cmd ().Invoke (
169
+ "toupper" ,"--verbose" ,"hello" ,"--lower" ,
170
+ )
183
171
io := clibasetest .FakeIO (i )
184
172
require .NoError (t ,i .Run ())
185
173
require .Equal (t ,"hello!!!" ,io .Stdout .String ())
186
174
})
187
175
188
176
t .Run ("ParsedFlags" ,func (t * testing.T ) {
189
177
t .Parallel ()
190
- i := & clibase.Invokation {
191
- Args : []string {"toupper" ,"--verbose" ,"hello" ,"--lower" },
192
- Command :cmd (),
193
- }
178
+ i := cmd ().Invoke (
179
+ "toupper" ,"--verbose" ,"hello" ,"--lower" ,
180
+ )
194
181
_ = clibasetest .FakeIO (i )
195
182
require .NoError (t ,i .Run ())
196
183
require .Equal (t ,
@@ -201,10 +188,9 @@ func TestCommand(t *testing.T) {
201
188
202
189
t .Run ("NoDeepChild" ,func (t * testing.T ) {
203
190
t .Parallel ()
204
- i := & clibase.Invokation {
205
- Args : []string {"root" ,"level" ,"level" ,"toupper" ,"--verbose" ,"hello" ,"--lower" },
206
- Command :cmd (),
207
- }
191
+ i := cmd ().Invoke (
192
+ "root" ,"level" ,"level" ,"toupper" ,"--verbose" ,"hello" ,"--lower" ,
193
+ )
208
194
fio := clibasetest .FakeIO (i )
209
195
require .Error (t ,i .Run (),fio .Stdout .String ())
210
196
})
@@ -215,7 +201,7 @@ func TestCommand_MiddlewareOrder(t *testing.T) {
215
201
216
202
mw := func (letter string ) clibase.MiddlewareFunc {
217
203
return func (next clibase.HandlerFunc ) clibase.HandlerFunc {
218
- return clibase . HandlerFunc (func (i * clibase.Invokation )error {
204
+ return (func (i * clibase.Invokation )error {
219
205
_ ,_ = i .Stdout .Write ([]byte (letter ))
220
206
return next (i )
221
207
})
@@ -230,15 +216,14 @@ func TestCommand_MiddlewareOrder(t *testing.T) {
230
216
mw ("B" ),
231
217
mw ("C" ),
232
218
),
233
- Handler :clibase . HandlerFunc (func (i * clibase.Invokation )error {
219
+ Handler : (func (i * clibase.Invokation )error {
234
220
return nil
235
221
}),
236
222
}
237
223
238
- i := & clibase.Invokation {
239
- Args : []string {"hello" ,"world" },
240
- Command :cmd ,
241
- }
224
+ i := cmd .Invoke (
225
+ "hello" ,"world" ,
226
+ )
242
227
io := clibasetest .FakeIO (i )
243
228
require .NoError (t ,i .Run ())
244
229
require .Equal (t ,"ABC" ,io .Stdout .String ())
@@ -262,7 +247,7 @@ func TestCommand_RawArgs(t *testing.T) {
262
247
Use :"sushi <args...>" ,
263
248
Short :"Throws back raw output" ,
264
249
RawArgs :true ,
265
- Handler :clibase . HandlerFunc (func (i * clibase.Invokation )error {
250
+ Handler : (func (i * clibase.Invokation )error {
266
251
if v := i .ParsedFlags ().Lookup ("password" ).Value .String ();v != "codershack" {
267
252
return xerrors .Errorf ("password %q is wrong!" ,v )
268
253
}
@@ -278,12 +263,9 @@ func TestCommand_RawArgs(t *testing.T) {
278
263
// Flag parsed before the raw arg command should still work.
279
264
t .Parallel ()
280
265
281
- i := & clibase.Invokation {
282
- Args : []string {
283
- "--password" ,"codershack" ,"sushi" ,"hello" ,"--verbose" ,"world" ,
284
- },
285
- Command :cmd (),
286
- }
266
+ i := cmd ().Invoke (
267
+ "--password" ,"codershack" ,"sushi" ,"hello" ,"--verbose" ,"world" ,
268
+ )
287
269
io := clibasetest .FakeIO (i )
288
270
require .NoError (t ,i .Run ())
289
271
require .Equal (t ,"hello --verbose world" ,io .Stdout .String ())
@@ -293,12 +275,9 @@ func TestCommand_RawArgs(t *testing.T) {
293
275
// Verbose before the raw arg command should fail.
294
276
t .Parallel ()
295
277
296
- i := & clibase.Invokation {
297
- Args : []string {
298
- "--password" ,"codershack" ,"--verbose" ,"sushi" ,"hello" ,"world" ,
299
- },
300
- Command :cmd (),
301
- }
278
+ i := cmd ().Invoke (
279
+ "--password" ,"codershack" ,"--verbose" ,"sushi" ,"hello" ,"world" ,
280
+ )
302
281
io := clibasetest .FakeIO (i )
303
282
require .Error (t ,i .Run ())
304
283
require .Empty (t ,io .Stdout .String ())
@@ -307,10 +286,9 @@ func TestCommand_RawArgs(t *testing.T) {
307
286
t .Run ("NoPassword" ,func (t * testing.T ) {
308
287
// Flag parsed before the raw arg command should still work.
309
288
t .Parallel ()
310
- i := & clibase.Invokation {
311
- Args : []string {"sushi" ,"hello" ,"--verbose" ,"world" },
312
- Command :cmd (),
313
- }
289
+ i := cmd ().Invoke (
290
+ "sushi" ,"hello" ,"--verbose" ,"world" ,
291
+ )
314
292
_ = clibasetest .FakeIO (i )
315
293
i .Stdout = clibasetest .TestWriter (t ,"stdout: " )
316
294
require .Error (t ,i .Run ())
@@ -321,10 +299,10 @@ func TestCommand_RootRaw(t *testing.T) {
321
299
t .Parallel ()
322
300
cmd := & clibase.Cmd {
323
301
RawArgs :true ,
324
- Handler :clibase . HandlerFunc ( func (i * clibase.Invokation )error {
302
+ Handler :func (i * clibase.Invokation )error {
325
303
i .Stdout .Write ([]byte (strings .Join (i .Args ," " )))
326
304
return nil
327
- }) ,
305
+ },
328
306
}
329
307
330
308
inv ,stdio := clibasetest .Invoke (cmd ,"hello" ,"--verbose" ,"--friendly" )
@@ -337,7 +315,7 @@ func TestCommand_RootRaw(t *testing.T) {
337
315
func TestCommand_HyphenHypen (t * testing.T ) {
338
316
t .Parallel ()
339
317
cmd := & clibase.Cmd {
340
- Handler :clibase . HandlerFunc (func (i * clibase.Invokation )error {
318
+ Handler : (func (i * clibase.Invokation )error {
341
319
i .Stdout .Write ([]byte (strings .Join (i .Args ," " )))
342
320
return nil
343
321
}),