Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit818b6e7

Browse files
committed
Simplify Invoke calls
1 parentf6a8670 commit818b6e7

File tree

2 files changed

+54
-76
lines changed

2 files changed

+54
-76
lines changed

‎cli/clibase/cmd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ func (i *Invokation) run(state *runState) error {
222222
}
223223

224224
// Flag parse errors are irrelevant for raw args commands.
225-
if!i.Command.RawArgs&&state.flagParseErr!=nil&&state.flagParseErr!=pflag.ErrHelp {
225+
if!i.Command.RawArgs&&state.flagParseErr!=nil&&!errors.Is(state.flagParseErr,pflag.ErrHelp) {
226226
returnxerrors.Errorf(
227227
"parsing flags (%v) for %q: %w",
228228
state.allArgs,

‎cli/clibase/cmd_test.go

Lines changed: 53 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func TestCommand(t *testing.T) {
4949
Value:clibase.BoolOf(&lower),
5050
},
5151
},
52-
Handler:clibase.HandlerFunc(func(i*clibase.Invokation)error {
52+
Handler: (func(i*clibase.Invokation)error {
5353
i.Stdout.Write([]byte(prefix))
5454
w:=i.Args[0]
5555
iflower {
@@ -74,32 +74,27 @@ func TestCommand(t *testing.T) {
7474

7575
t.Run("SimpleOK",func(t*testing.T) {
7676
t.Parallel()
77-
i:=&clibase.Invokation{
78-
Args: []string{"toupper","hello"},
79-
Command:cmd(),
80-
}
77+
i:=cmd().Invoke("toupper","hello")
8178
io:=clibasetest.FakeIO(i)
8279
i.Run()
8380
require.Equal(t,"HELLO",io.Stdout.String())
8481
})
8582

8683
t.Run("Alias",func(t*testing.T) {
8784
t.Parallel()
88-
i:=&clibase.Invokation{
89-
Args: []string{"up","hello"},
90-
Command:cmd(),
91-
}
85+
i:=cmd().Invoke(
86+
"up","hello",
87+
)
9288
io:=clibasetest.FakeIO(i)
9389
i.Run()
9490
require.Equal(t,"HELLO",io.Stdout.String())
9591
})
9692

9793
t.Run("NoSubcommand",func(t*testing.T) {
9894
t.Parallel()
99-
i:=&clibase.Invokation{
100-
Args: []string{"na"},
101-
Command:cmd(),
102-
}
95+
i:=cmd().Invoke(
96+
"na",
97+
)
10398
io:=clibasetest.FakeIO(i)
10499
err:=i.Run()
105100
require.Empty(t,io.Stdout.String())
@@ -108,10 +103,9 @@ func TestCommand(t *testing.T) {
108103

109104
t.Run("BadArgs",func(t*testing.T) {
110105
t.Parallel()
111-
i:=&clibase.Invokation{
112-
Args: []string{"toupper"},
113-
Command:cmd(),
114-
}
106+
i:=cmd().Invoke(
107+
"toupper",
108+
)
115109
io:=clibasetest.FakeIO(i)
116110
err:=i.Run()
117111
require.Empty(t,io.Stdout.String())
@@ -120,10 +114,9 @@ func TestCommand(t *testing.T) {
120114

121115
t.Run("UnknownFlags",func(t*testing.T) {
122116
t.Parallel()
123-
i:=&clibase.Invokation{
124-
Args: []string{"toupper","--unknown"},
125-
Command:cmd(),
126-
}
117+
i:=cmd().Invoke(
118+
"toupper","--unknown",
119+
)
127120
io:=clibasetest.FakeIO(i)
128121
err:=i.Run()
129122
require.Empty(t,io.Stdout.String())
@@ -132,65 +125,59 @@ func TestCommand(t *testing.T) {
132125

133126
t.Run("Verbose",func(t*testing.T) {
134127
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+
)
139131
io:=clibasetest.FakeIO(i)
140132
require.NoError(t,i.Run())
141133
require.Equal(t,"HELLO!!!",io.Stdout.String())
142134
})
143135

144136
t.Run("Verbose=",func(t*testing.T) {
145137
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+
)
150141
io:=clibasetest.FakeIO(i)
151142
require.NoError(t,i.Run())
152143
require.Equal(t,"HELLO!!!",io.Stdout.String())
153144
})
154145

155146
t.Run("PrefixSpace",func(t*testing.T) {
156147
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+
)
161151
io:=clibasetest.FakeIO(i)
162152
require.NoError(t,i.Run())
163153
require.Equal(t,"conv: HELLO",io.Stdout.String())
164154
})
165155

166156
t.Run("GlobalFlagsAnywhere",func(t*testing.T) {
167157
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+
)
172161
io:=clibasetest.FakeIO(i)
173162
require.NoError(t,i.Run())
174163
require.Equal(t,"conv: HELLO!!!",io.Stdout.String())
175164
})
176165

177166
t.Run("LowerVerbose",func(t*testing.T) {
178167
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+
)
183171
io:=clibasetest.FakeIO(i)
184172
require.NoError(t,i.Run())
185173
require.Equal(t,"hello!!!",io.Stdout.String())
186174
})
187175

188176
t.Run("ParsedFlags",func(t*testing.T) {
189177
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+
)
194181
_=clibasetest.FakeIO(i)
195182
require.NoError(t,i.Run())
196183
require.Equal(t,
@@ -201,10 +188,9 @@ func TestCommand(t *testing.T) {
201188

202189
t.Run("NoDeepChild",func(t*testing.T) {
203190
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+
)
208194
fio:=clibasetest.FakeIO(i)
209195
require.Error(t,i.Run(),fio.Stdout.String())
210196
})
@@ -215,7 +201,7 @@ func TestCommand_MiddlewareOrder(t *testing.T) {
215201

216202
mw:=func(letterstring) clibase.MiddlewareFunc {
217203
returnfunc(next clibase.HandlerFunc) clibase.HandlerFunc {
218-
returnclibase.HandlerFunc(func(i*clibase.Invokation)error {
204+
return (func(i*clibase.Invokation)error {
219205
_,_=i.Stdout.Write([]byte(letter))
220206
returnnext(i)
221207
})
@@ -230,15 +216,14 @@ func TestCommand_MiddlewareOrder(t *testing.T) {
230216
mw("B"),
231217
mw("C"),
232218
),
233-
Handler:clibase.HandlerFunc(func(i*clibase.Invokation)error {
219+
Handler: (func(i*clibase.Invokation)error {
234220
returnnil
235221
}),
236222
}
237223

238-
i:=&clibase.Invokation{
239-
Args: []string{"hello","world"},
240-
Command:cmd,
241-
}
224+
i:=cmd.Invoke(
225+
"hello","world",
226+
)
242227
io:=clibasetest.FakeIO(i)
243228
require.NoError(t,i.Run())
244229
require.Equal(t,"ABC",io.Stdout.String())
@@ -262,7 +247,7 @@ func TestCommand_RawArgs(t *testing.T) {
262247
Use:"sushi <args...>",
263248
Short:"Throws back raw output",
264249
RawArgs:true,
265-
Handler:clibase.HandlerFunc(func(i*clibase.Invokation)error {
250+
Handler: (func(i*clibase.Invokation)error {
266251
ifv:=i.ParsedFlags().Lookup("password").Value.String();v!="codershack" {
267252
returnxerrors.Errorf("password %q is wrong!",v)
268253
}
@@ -278,12 +263,9 @@ func TestCommand_RawArgs(t *testing.T) {
278263
// Flag parsed before the raw arg command should still work.
279264
t.Parallel()
280265

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+
)
287269
io:=clibasetest.FakeIO(i)
288270
require.NoError(t,i.Run())
289271
require.Equal(t,"hello --verbose world",io.Stdout.String())
@@ -293,12 +275,9 @@ func TestCommand_RawArgs(t *testing.T) {
293275
// Verbose before the raw arg command should fail.
294276
t.Parallel()
295277

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+
)
302281
io:=clibasetest.FakeIO(i)
303282
require.Error(t,i.Run())
304283
require.Empty(t,io.Stdout.String())
@@ -307,10 +286,9 @@ func TestCommand_RawArgs(t *testing.T) {
307286
t.Run("NoPassword",func(t*testing.T) {
308287
// Flag parsed before the raw arg command should still work.
309288
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+
)
314292
_=clibasetest.FakeIO(i)
315293
i.Stdout=clibasetest.TestWriter(t,"stdout: ")
316294
require.Error(t,i.Run())
@@ -321,10 +299,10 @@ func TestCommand_RootRaw(t *testing.T) {
321299
t.Parallel()
322300
cmd:=&clibase.Cmd{
323301
RawArgs:true,
324-
Handler:clibase.HandlerFunc(func(i*clibase.Invokation)error {
302+
Handler:func(i*clibase.Invokation)error {
325303
i.Stdout.Write([]byte(strings.Join(i.Args," ")))
326304
returnnil
327-
}),
305+
},
328306
}
329307

330308
inv,stdio:=clibasetest.Invoke(cmd,"hello","--verbose","--friendly")
@@ -337,7 +315,7 @@ func TestCommand_RootRaw(t *testing.T) {
337315
funcTestCommand_HyphenHypen(t*testing.T) {
338316
t.Parallel()
339317
cmd:=&clibase.Cmd{
340-
Handler:clibase.HandlerFunc(func(i*clibase.Invokation)error {
318+
Handler: (func(i*clibase.Invokation)error {
341319
i.Stdout.Write([]byte(strings.Join(i.Args," ")))
342320
returnnil
343321
}),

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp