diff --git a/os/gcmd/gcmd_command.go b/os/gcmd/gcmd_command.go index 347b8e66d..557a7531c 100644 --- a/os/gcmd/gcmd_command.go +++ b/os/gcmd/gcmd_command.go @@ -103,9 +103,7 @@ func (c *Command) doAddCommand(command *Command) error { // AddObject adds one or more sub-commands to current command using struct object. func (c *Command) AddObject(objects ...interface{}) error { - var ( - commands []*Command - ) + var commands []*Command for _, object := range objects { rootCommand, err := NewFromObject(object) if err != nil { diff --git a/os/gcmd/gcmd_command_object.go b/os/gcmd/gcmd_command_object.go index 45e6501e9..e8bd99177 100644 --- a/os/gcmd/gcmd_command_object.go +++ b/os/gcmd/gcmd_command_object.go @@ -35,6 +35,13 @@ var ( // NewFromObject creates and returns a root command object using given object. func NewFromObject(object interface{}) (rootCmd *Command, err error) { + switch c := object.(type) { + case Command: + return &c, nil + case *Command: + return c, nil + } + originValueAndKind := reflection.OriginValueAndKind(object) if originValueAndKind.OriginKind != reflect.Struct { err = gerror.Newf( diff --git a/os/gcmd/gcmd_z_unit_test.go b/os/gcmd/gcmd_z_unit_test.go index d9967e794..605b0b3fa 100644 --- a/os/gcmd/gcmd_z_unit_test.go +++ b/os/gcmd/gcmd_z_unit_test.go @@ -133,6 +133,11 @@ gf get golang.org/x/sys } err = commandRoot.AddCommand( commandEnv, + ) + if err != nil { + g.Log().Fatal(ctx, err) + } + err = commandRoot.AddObject( commandTest, ) if err != nil {