Skip to content

Commit

Permalink
fix(os/gcmd): argument index calculating error in multilevel command (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
oldme-git authored Sep 28, 2024
1 parent 66ee52c commit ea09457
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 6 deletions.
5 changes: 4 additions & 1 deletion frame/g/g.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
4 changes: 0 additions & 4 deletions os/gcmd/gcmd_command_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}{}
Expand Down
3 changes: 2 additions & 1 deletion os/gcmd/gcmd_command_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
73 changes: 73 additions & 0 deletions os/gcmd/gcmd_z_unit_issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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":"邓丽君"}`)
})
}

0 comments on commit ea09457

Please sign in to comment.