mirror of
https://gitee.com/johng/gf
synced 2026-06-06 02:25:47 +08:00
improve package gcmd
This commit is contained in:
@ -30,7 +30,7 @@ type Command struct {
|
||||
Strict bool // Strict parsing options, which means it returns error if invalid option given.
|
||||
Config string // Config node name, which also retrieves the values from config component along with command line.
|
||||
parent *Command // Parent command for internal usage.
|
||||
commands []Command // Sub commands of this command.
|
||||
commands []*Command // Sub commands of this command.
|
||||
}
|
||||
|
||||
// Function is a custom command callback function that is bound to a certain argument.
|
||||
@ -69,7 +69,7 @@ func CommandFromCtx(ctx context.Context) *Command {
|
||||
}
|
||||
|
||||
// AddCommand adds one or more sub-commands to current command.
|
||||
func (c *Command) AddCommand(commands ...Command) error {
|
||||
func (c *Command) AddCommand(commands ...*Command) error {
|
||||
for _, cmd := range commands {
|
||||
cmd.Name = gstr.Trim(cmd.Name)
|
||||
if cmd.Name == "" {
|
||||
@ -84,7 +84,7 @@ func (c *Command) AddCommand(commands ...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
|
||||
commands []*Command
|
||||
)
|
||||
for _, object := range objects {
|
||||
rootCommand, err := NewFromObject(object)
|
||||
|
||||
@ -37,7 +37,7 @@ var (
|
||||
)
|
||||
|
||||
// NewFromObject creates and returns a root command object using given object.
|
||||
func NewFromObject(object interface{}) (rootCmd Command, err error) {
|
||||
func NewFromObject(object interface{}) (rootCmd *Command, err error) {
|
||||
originValueAndKind := utils.OriginValueAndKind(object)
|
||||
if originValueAndKind.OriginKind != reflect.Struct {
|
||||
err = gerror.Newf(
|
||||
@ -55,12 +55,12 @@ func NewFromObject(object interface{}) (rootCmd Command, err error) {
|
||||
var (
|
||||
nameSet = gset.NewStrSet()
|
||||
rootCommandName = gmeta.Get(object, tagNameRoot).String()
|
||||
subCommands []Command
|
||||
subCommands []*Command
|
||||
)
|
||||
for i := 0; i < originValueAndKind.InputValue.NumMethod(); i++ {
|
||||
var (
|
||||
method = originValueAndKind.InputValue.Method(i)
|
||||
methodCommand Command
|
||||
methodCommand *Command
|
||||
)
|
||||
methodCommand, err = newCommandFromMethod(object, method)
|
||||
if err != nil {
|
||||
@ -93,7 +93,7 @@ func NewFromObject(object interface{}) (rootCmd Command, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func newCommandFromObjectMeta(object interface{}) (command Command, err error) {
|
||||
func newCommandFromObjectMeta(object interface{}) (command *Command, err error) {
|
||||
var (
|
||||
metaData = gmeta.Data(object)
|
||||
)
|
||||
@ -130,7 +130,7 @@ func newCommandFromObjectMeta(object interface{}) (command Command, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func newCommandFromMethod(object interface{}, method reflect.Value) (command Command, err error) {
|
||||
func newCommandFromMethod(object interface{}, method reflect.Value) (command *Command, err error) {
|
||||
var (
|
||||
reflectType = method.Type()
|
||||
)
|
||||
@ -208,7 +208,7 @@ func newCommandFromMethod(object interface{}, method reflect.Value) (command Com
|
||||
// Create function that has value return.
|
||||
command.FuncWithValue = func(ctx context.Context, parser *Parser) (out interface{}, err error) {
|
||||
ctx = context.WithValue(ctx, CtxKeyParser, parser)
|
||||
ctx = context.WithValue(ctx, CtxKeyCommand, &command)
|
||||
ctx = context.WithValue(ctx, CtxKeyCommand, command)
|
||||
|
||||
defer func() {
|
||||
if exception := recover(); exception != nil {
|
||||
|
||||
@ -134,13 +134,13 @@ func (c *Command) searchCommand(args []string) *Command {
|
||||
// If this command needs argument,
|
||||
// it then gives all its left arguments to it.
|
||||
if cmd.NeedArgs {
|
||||
return &cmd
|
||||
return cmd
|
||||
}
|
||||
// Recursively searching the command.
|
||||
if cmd.Name == args[0] {
|
||||
leftArgs := args[1:]
|
||||
if len(leftArgs) == 0 {
|
||||
return &cmd
|
||||
return cmd
|
||||
}
|
||||
return cmd.searchCommand(leftArgs)
|
||||
}
|
||||
|
||||
@ -81,7 +81,7 @@ func Test_Command(t *testing.T) {
|
||||
Name: "gf",
|
||||
}
|
||||
// env
|
||||
commandEnv := gcmd.Command{
|
||||
commandEnv := &gcmd.Command{
|
||||
Name: "env",
|
||||
Func: func(ctx context.Context, parser *gcmd.Parser) error {
|
||||
fmt.Println("env")
|
||||
@ -89,7 +89,7 @@ func Test_Command(t *testing.T) {
|
||||
},
|
||||
}
|
||||
// test
|
||||
commandTest := gcmd.Command{
|
||||
commandTest := &gcmd.Command{
|
||||
Name: "test",
|
||||
Brief: "test brief",
|
||||
Description: "test description current Golang environment variables",
|
||||
@ -145,7 +145,7 @@ func Test_Command_Print(t *testing.T) {
|
||||
Use 'gf help COMMAND' or 'gf COMMAND -h' for detail about a command, which has '...' in the tail of their comments.`,
|
||||
}
|
||||
// env
|
||||
commandEnv := gcmd.Command{
|
||||
commandEnv := &gcmd.Command{
|
||||
Name: "env",
|
||||
Brief: "show current Golang environment variables, long brief.long brief.long brief.long brief.long brief.long brief.long brief.long brief.",
|
||||
Description: "show current Golang environment variables",
|
||||
@ -157,7 +157,7 @@ Use 'gf help COMMAND' or 'gf COMMAND -h' for detail about a command, which has '
|
||||
g.Log().Fatal(ctx, err)
|
||||
}
|
||||
// get
|
||||
commandGet := gcmd.Command{
|
||||
commandGet := &gcmd.Command{
|
||||
Name: "get",
|
||||
Brief: "install or update GF to system in default...",
|
||||
Description: "show current Golang environment variables",
|
||||
@ -212,7 +212,7 @@ gf build main.go -n my-app -v 1.0 -a amd64,386 -s linux,windows,darwin -p ./dock
|
||||
return nil
|
||||
},
|
||||
}
|
||||
if err = c.AddCommand(commandBuild); err != nil {
|
||||
if err = c.AddCommand(&commandBuild); err != nil {
|
||||
g.Log().Fatal(ctx, err)
|
||||
}
|
||||
c.Run(ctx)
|
||||
|
||||
Reference in New Issue
Block a user