From 3a20883a81483b3fd5377309e583a20c546fd21e Mon Sep 17 00:00:00 2001 From: John Guo Date: Sat, 11 Dec 2021 00:12:58 +0800 Subject: [PATCH] improve package gcmd --- os/gcmd/gcmd_command_object.go | 55 ++++++++++++++++++++++++---------- 1 file changed, 40 insertions(+), 15 deletions(-) diff --git a/os/gcmd/gcmd_command_object.go b/os/gcmd/gcmd_command_object.go index efd14cb4b..d611b1d2f 100644 --- a/os/gcmd/gcmd_command_object.go +++ b/os/gcmd/gcmd_command_object.go @@ -62,32 +62,24 @@ func NewFromObject(object interface{}) (rootCmd *Command, err error) { } for i := 0; i < originValueAndKind.InputValue.NumMethod(); i++ { var ( - method = originValueAndKind.InputValue.Method(i) - methodCommand *Command + method = originValueAndKind.InputValue.Method(i) + methodCmd *Command ) - methodCommand, err = newCommandFromMethod(object, method) + methodCmd, err = newCommandFromMethod(object, method) if err != nil { return } - if nameSet.Contains(methodCommand.Name) { + if nameSet.Contains(methodCmd.Name) { err = gerror.Newf( `command name should be unique, found duplicated command name in method "%s"`, method.Type().String(), ) return } - if rootCommandName == methodCommand.Name { - if rootCmd.Func == nil { - rootCmd.Func = methodCommand.Func - } - if rootCmd.FuncWithValue == nil { - rootCmd.FuncWithValue = methodCommand.FuncWithValue - } - if len(rootCmd.Arguments) == 0 { - rootCmd.Arguments = methodCommand.Arguments - } + if rootCommandName == methodCmd.Name { + methodToRootCmdWhenNameEqual(rootCmd, methodCmd) } else { - subCommands = append(subCommands, methodCommand) + subCommands = append(subCommands, methodCmd) } } if len(subCommands) > 0 { @@ -96,6 +88,39 @@ func NewFromObject(object interface{}) (rootCmd *Command, err error) { return } +func methodToRootCmdWhenNameEqual(rootCmd *Command, methodCmd *Command) { + if rootCmd.Usage == "" { + rootCmd.Usage = methodCmd.Usage + } + if rootCmd.Brief == "" { + rootCmd.Brief = methodCmd.Brief + } + if rootCmd.Description == "" { + rootCmd.Description = methodCmd.Description + } + if rootCmd.Examples == "" { + rootCmd.Examples = methodCmd.Examples + } + if rootCmd.Func == nil { + rootCmd.Func = methodCmd.Func + } + if rootCmd.FuncWithValue == nil { + rootCmd.FuncWithValue = methodCmd.FuncWithValue + } + if rootCmd.HelpFunc == nil { + rootCmd.HelpFunc = methodCmd.HelpFunc + } + if len(rootCmd.Arguments) == 0 { + rootCmd.Arguments = methodCmd.Arguments + } + if !rootCmd.Strict { + rootCmd.Strict = methodCmd.Strict + } + if rootCmd.Config == "" { + rootCmd.Config = methodCmd.Config + } +} + func newCommandFromObjectMeta(object interface{}) (command *Command, err error) { var ( metaData = gmeta.Data(object)