diff --git a/frame/g/g.go b/frame/g/g.go index 20bf3720572..5c196c5cbf5 100644 --- a/frame/g/g.go +++ b/frame/g/g.go @@ -4,7 +4,10 @@ // If a copy of the MIT was not distributed with this file, // You can obtain one at https://github.com/gogf/gf. -// Package g provides commonly used type/function defines and coupled calling for creating commonly used objects. +// Package g provides commonly used type/function defines and coupled calling for creating commonly-used objects. +// +// Note that, using package g might make the compiled binary a little bit bigger, as it imports a few frequently-used +// packages whatever you use them or not. package g import ( diff --git a/os/gcmd/gcmd_command_object.go b/os/gcmd/gcmd_command_object.go index 43ecc4f9103..c7e49b54714 100644 --- a/os/gcmd/gcmd_command_object.go +++ b/os/gcmd/gcmd_command_object.go @@ -267,10 +267,6 @@ func newCommandFromMethod( ) if value := ctx.Value(CtxKeyArgumentsIndex); value != nil { argIndex = value.(int) - // Use the left args to assign to input struct object. - if argIndex < len(arguments) { - arguments = arguments[argIndex:] - } } if data == nil { data = map[string]interface{}{} diff --git a/os/gcmd/gcmd_command_run.go b/os/gcmd/gcmd_command_run.go index 80e29842ded..13cde00505a 100644 --- a/os/gcmd/gcmd_command_run.go +++ b/os/gcmd/gcmd_command_run.go @@ -93,7 +93,8 @@ func (c *Command) RunWithSpecificArgs(ctx context.Context, args []string) (value parsedArgs = parsedArgs[1:] // Find the matched command and run it. - lastCmd, foundCmd, newCtx := c.searchCommand(ctx, parsedArgs, 0) + // It here `fromArgIndex` set to 1 to calculate the argument index in to `newCtx`. + lastCmd, foundCmd, newCtx := c.searchCommand(ctx, parsedArgs, 1) if foundCmd != nil { return foundCmd.doRun(newCtx, args, parser) } diff --git a/os/gcmd/gcmd_z_unit_issue_test.go b/os/gcmd/gcmd_z_unit_issue_test.go index 19bb4a1c1da..fd4b5c790a0 100644 --- a/os/gcmd/gcmd_z_unit_issue_test.go +++ b/os/gcmd/gcmd_z_unit_issue_test.go @@ -232,3 +232,76 @@ func Test_Issue3417(t *testing.T) { ) }) } + +// https://github.com/gogf/gf/issues/3670 +type ( + Issue3670FirstCommand struct { + *gcmd.Command + } + + Issue3670First struct { + g.Meta `name:"first"` + } + + Issue3670Second struct { + g.Meta `name:"second"` + } + + Issue3670Third struct { + g.Meta `name:"third"` + Issue3670Last + } + + Issue3670Last struct { + g.Meta `name:"last"` + } + + Issue3670LastInput struct { + g.Meta `name:"last"` + Country string `name:"country" arg:"true"` + Singer string `name:"singer" arg:"true"` + } + + Issue3670LastOutput struct { + Content string + } +) + +func (receiver Issue3670Last) LastRecv(ctx context.Context, in Issue3670LastInput) (out *Issue3670LastOutput, err error) { + out = &Issue3670LastOutput{ + Content: gjson.MustEncodeString(in), + } + return +} + +func Test_Issue3670(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + var ( + ctx = gctx.New() + err error + ) + + third, err := gcmd.NewFromObject(Issue3670Third{}) + t.AssertNil(err) + + second, err := gcmd.NewFromObject(Issue3670Second{}) + t.AssertNil(err) + err = second.AddCommand(third) + t.AssertNil(err) + + first, err := gcmd.NewFromObject(Issue3670First{}) + t.AssertNil(err) + err = first.AddCommand(second) + t.AssertNil(err) + + command := &Issue3670FirstCommand{first} + + value, err := command.RunWithSpecificArgs( + ctx, + []string{"main", "second", "third", "last", "china", "邓丽君"}, + ) + t.AssertNil(err) + + t.Assert(value.(*Issue3670LastOutput).Content, `{"Country":"china","Singer":"邓丽君"}`) + }) +}