1
0
mirror of https://github.com/gogf/gf.git synced 2025-04-05 11:18:50 +08:00

improve AddObject to support Command/*Command parameter for package gcmd (#2587)

This commit is contained in:
John Guo 2023-04-18 10:09:07 +08:00 committed by GitHub
parent 3991eb053c
commit da8297d770
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 3 deletions

View File

@ -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 {

View File

@ -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(

View File

@ -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 {