Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(os/gcmd): argument index calculating error in multilevel command #3807

Merged
merged 5 commits into from
Sep 28, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion os/gcmd/gcmd_command_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ 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)
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":"邓丽君"}`)
})
}
Loading