diff --git a/os/gcmd/gcmd_command_object.go b/os/gcmd/gcmd_command_object.go index 9b819065b..afa97ab86 100644 --- a/os/gcmd/gcmd_command_object.go +++ b/os/gcmd/gcmd_command_object.go @@ -202,21 +202,27 @@ func newCommandFromMethod(object interface{}, method reflect.Value) (command Com if data == nil { data = map[string]interface{}{} } + // Handle orphan options. + for _, option := range command.Options { + if option.Orphan && parser.ContainsOpt(option.Name) { + data[option.Name] = "true" + } + } + // Default values from struct tag. err = mergeDefaultStructValue(data, inputObject.Interface()) if err != nil { return nil, err } // Construct input parameters. if len(data) > 0 { - - } - if inputObject.Kind() == reflect.Ptr { - err = gconv.Scan(data, inputObject.Interface()) - } else { - err = gconv.Struct(data, inputObject.Addr().Interface()) - } - if err != nil { - return + if inputObject.Kind() == reflect.Ptr { + err = gconv.Scan(data, inputObject.Interface()) + } else { + err = gconv.Struct(data, inputObject.Addr().Interface()) + } + if err != nil { + return + } } // Parameters validation. diff --git a/os/gcmd/gcmd_command_run.go b/os/gcmd/gcmd_command_run.go index d0efb1b59..49fd508b3 100644 --- a/os/gcmd/gcmd_command_run.go +++ b/os/gcmd/gcmd_command_run.go @@ -23,6 +23,9 @@ func (c *Command) Run(ctx context.Context) error { // RunWithValue calls custom function that bound to this command with value output. func (c *Command) RunWithValue(ctx context.Context) (value interface{}, err error) { + // Add built-in help option, just for info only. + c.Options = append(c.Options, defaultHelpOption) + // Parse command arguments and options using default algorithm. parser, err := Parse(nil) if err != nil { @@ -57,8 +60,6 @@ func (c *Command) RunWithValue(ctx context.Context) (value interface{}, err erro } func (c *Command) doRun(ctx context.Context, parser *Parser) (value interface{}, err error) { - // Add built-in help option, just for info only. - c.Options = append(c.Options, defaultHelpOption) // Check built-in help command. if parser.ContainsOpt(helpOptionName) || parser.ContainsOpt(helpOptionNameShort) { if c.HelpFunc != nil { diff --git a/util/gtag/gtag_z_example_test.go b/util/gtag/gtag_z_example_test.go new file mode 100644 index 000000000..124a8db45 --- /dev/null +++ b/util/gtag/gtag_z_example_test.go @@ -0,0 +1,28 @@ +// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + +package gtag_test + +import ( + "fmt" + + "github.com/gogf/gf/v2/frame/g" + "github.com/gogf/gf/v2/util/gmeta" + "github.com/gogf/gf/v2/util/gtag" +) + +func ExampleSet() { + type User struct { + g.Meta `name:"User Struct" description:"{UserDescription}"` + } + gtag.Sets(g.MapStrStr{ + `UserDescription`: `This is a demo struct named "User Struct"`, + }) + fmt.Println(gmeta.Get(User{}, `description`)) + + // Output: + // This is a demo struct named "User Struct" +}