mirror of
https://github.com/gogf/gf.git
synced 2025-04-05 03:05:05 +08:00
fix(os/gcmd): argument index calculating error in multilevel command (#3807)
This commit is contained in:
parent
66ee52c96a
commit
ea09457d84
@ -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 (
|
||||
|
@ -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{}{}
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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…
x
Reference in New Issue
Block a user