Skip to content

Commit ea09457

Browse files
authored
fix(os/gcmd): argument index calculating error in multilevel command (#3807)
1 parent 66ee52c commit ea09457

File tree

4 files changed

+79
-6
lines changed

4 files changed

+79
-6
lines changed

frame/g/g.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
// If a copy of the MIT was not distributed with this file,
55
// You can obtain one at https://github.com/gogf/gf.
66

7-
// Package g provides commonly used type/function defines and coupled calling for creating commonly used objects.
7+
// Package g provides commonly used type/function defines and coupled calling for creating commonly-used objects.
8+
//
9+
// Note that, using package g might make the compiled binary a little bit bigger, as it imports a few frequently-used
10+
// packages whatever you use them or not.
811
package g
912

1013
import (

os/gcmd/gcmd_command_object.go

-4
Original file line numberDiff line numberDiff line change
@@ -267,10 +267,6 @@ func newCommandFromMethod(
267267
)
268268
if value := ctx.Value(CtxKeyArgumentsIndex); value != nil {
269269
argIndex = value.(int)
270-
// Use the left args to assign to input struct object.
271-
if argIndex < len(arguments) {
272-
arguments = arguments[argIndex:]
273-
}
274270
}
275271
if data == nil {
276272
data = map[string]interface{}{}

os/gcmd/gcmd_command_run.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ func (c *Command) RunWithSpecificArgs(ctx context.Context, args []string) (value
9393
parsedArgs = parsedArgs[1:]
9494

9595
// Find the matched command and run it.
96-
lastCmd, foundCmd, newCtx := c.searchCommand(ctx, parsedArgs, 0)
96+
// It here `fromArgIndex` set to 1 to calculate the argument index in to `newCtx`.
97+
lastCmd, foundCmd, newCtx := c.searchCommand(ctx, parsedArgs, 1)
9798
if foundCmd != nil {
9899
return foundCmd.doRun(newCtx, args, parser)
99100
}

os/gcmd/gcmd_z_unit_issue_test.go

+73
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,76 @@ func Test_Issue3417(t *testing.T) {
232232
)
233233
})
234234
}
235+
236+
// https://github.com/gogf/gf/issues/3670
237+
type (
238+
Issue3670FirstCommand struct {
239+
*gcmd.Command
240+
}
241+
242+
Issue3670First struct {
243+
g.Meta `name:"first"`
244+
}
245+
246+
Issue3670Second struct {
247+
g.Meta `name:"second"`
248+
}
249+
250+
Issue3670Third struct {
251+
g.Meta `name:"third"`
252+
Issue3670Last
253+
}
254+
255+
Issue3670Last struct {
256+
g.Meta `name:"last"`
257+
}
258+
259+
Issue3670LastInput struct {
260+
g.Meta `name:"last"`
261+
Country string `name:"country" arg:"true"`
262+
Singer string `name:"singer" arg:"true"`
263+
}
264+
265+
Issue3670LastOutput struct {
266+
Content string
267+
}
268+
)
269+
270+
func (receiver Issue3670Last) LastRecv(ctx context.Context, in Issue3670LastInput) (out *Issue3670LastOutput, err error) {
271+
out = &Issue3670LastOutput{
272+
Content: gjson.MustEncodeString(in),
273+
}
274+
return
275+
}
276+
277+
func Test_Issue3670(t *testing.T) {
278+
gtest.C(t, func(t *gtest.T) {
279+
var (
280+
ctx = gctx.New()
281+
err error
282+
)
283+
284+
third, err := gcmd.NewFromObject(Issue3670Third{})
285+
t.AssertNil(err)
286+
287+
second, err := gcmd.NewFromObject(Issue3670Second{})
288+
t.AssertNil(err)
289+
err = second.AddCommand(third)
290+
t.AssertNil(err)
291+
292+
first, err := gcmd.NewFromObject(Issue3670First{})
293+
t.AssertNil(err)
294+
err = first.AddCommand(second)
295+
t.AssertNil(err)
296+
297+
command := &Issue3670FirstCommand{first}
298+
299+
value, err := command.RunWithSpecificArgs(
300+
ctx,
301+
[]string{"main", "second", "third", "last", "china", "邓丽君"},
302+
)
303+
t.AssertNil(err)
304+
305+
t.Assert(value.(*Issue3670LastOutput).Content, `{"Country":"china","Singer":"邓丽君"}`)
306+
})
307+
}

0 commit comments

Comments
 (0)