mirror of
https://gitee.com/johng/gf
synced 2026-06-06 02:25:47 +08:00
fix issue orphan value parsing for sructured arguments of command for package gcmd
This commit is contained in:
@ -221,9 +221,7 @@ func newCommandFromMethod(object interface{}, method reflect.Value) (command *Co
|
||||
return
|
||||
}
|
||||
|
||||
var (
|
||||
inputObject reflect.Value
|
||||
)
|
||||
var inputObject reflect.Value
|
||||
if method.Type().In(1).Kind() == reflect.Ptr {
|
||||
inputObject = reflect.New(method.Type().In(1).Elem()).Elem()
|
||||
} else {
|
||||
@ -264,8 +262,19 @@ func newCommandFromMethod(object interface{}, method reflect.Value) (command *Co
|
||||
}
|
||||
} else {
|
||||
// Read argument from command line option name.
|
||||
if arg.Orphan && parser.GetOpt(arg.Name) != nil {
|
||||
data[arg.Name] = "true"
|
||||
if arg.Orphan {
|
||||
if orphanValue := parser.GetOpt(arg.Name); orphanValue != nil {
|
||||
if orphanValue.String() == "" {
|
||||
// Eg: gf -f
|
||||
data[arg.Name] = "true"
|
||||
} else {
|
||||
// Adapter with common user habits.
|
||||
// Eg:
|
||||
// `gf -f=0`: which parameter `f` is parsed as false
|
||||
// `gf -f=1`: which parameter `f` is parsed as true
|
||||
data[arg.Name] = orphanValue.Bool()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -243,7 +243,7 @@ func Test_Command_Pointer(t *testing.T) {
|
||||
t.AssertNil(err)
|
||||
t.Assert(value, `{"Content":"john"}`)
|
||||
})
|
||||
return
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var (
|
||||
ctx = gctx.New()
|
||||
@ -257,3 +257,42 @@ func Test_Command_Pointer(t *testing.T) {
|
||||
t.Assert(value, `{"Content":"john"}`)
|
||||
})
|
||||
}
|
||||
|
||||
type TestCommandOrphan struct {
|
||||
g.Meta `name:"root" root:"root"`
|
||||
}
|
||||
|
||||
type TestCommandOrphanIndexInput struct {
|
||||
g.Meta `name:"index"`
|
||||
Orphan1 bool `short:"n1" orphan:"true"`
|
||||
Orphan2 bool `short:"n2" orphan:"true"`
|
||||
Orphan3 bool `short:"n3" orphan:"true"`
|
||||
}
|
||||
type TestCommandOrphanIndexOutput struct {
|
||||
Orphan1 bool
|
||||
Orphan2 bool
|
||||
Orphan3 bool
|
||||
}
|
||||
|
||||
func (c *TestCommandOrphan) Index(ctx context.Context, in TestCommandOrphanIndexInput) (out *TestCommandOrphanIndexOutput, err error) {
|
||||
out = &TestCommandOrphanIndexOutput{
|
||||
Orphan1: in.Orphan1,
|
||||
Orphan2: in.Orphan2,
|
||||
Orphan3: in.Orphan3,
|
||||
}
|
||||
return
|
||||
}
|
||||
func Test_Command_Orphan_Parameter(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var ctx = gctx.New()
|
||||
cmd, err := gcmd.NewFromObject(TestCommandOrphan{})
|
||||
t.AssertNil(err)
|
||||
|
||||
os.Args = []string{"root", "index", "-n1", "-n2=0", "-n3=1"}
|
||||
value, err := cmd.RunWithValueError(ctx)
|
||||
t.AssertNil(err)
|
||||
t.Assert(value.(*TestCommandOrphanIndexOutput).Orphan1, true)
|
||||
t.Assert(value.(*TestCommandOrphanIndexOutput).Orphan2, false)
|
||||
t.Assert(value.(*TestCommandOrphanIndexOutput).Orphan3, true)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user