diff --git a/os/gcmd/gcmd_command_run.go b/os/gcmd/gcmd_command_run.go index 988e8a3bc..55c1005a7 100644 --- a/os/gcmd/gcmd_command_run.go +++ b/os/gcmd/gcmd_command_run.go @@ -12,6 +12,8 @@ import ( "fmt" "os" + "github.com/gogf/gf/v2/errors/gcode" + "github.com/gogf/gf/v2/errors/gerror" "github.com/gogf/gf/v2/os/gcfg" "github.com/gogf/gf/v2/text/gstr" "github.com/gogf/gf/v2/util/gconv" @@ -45,14 +47,13 @@ func (c *Command) RunWithValue(ctx context.Context) (value interface{}, err erro } // Print error and help command if no command found. - fmt.Printf( - "ERROR: command \"%s\" not found for arguments \"%s\"\n", + err = gerror.NewCodef( + gcode.CodeNotFound, + `command "%s" not found for arguments "%s"`, gstr.Join(args, " "), gstr.Join(os.Args, " "), ) - c.Print() - - return nil, nil + return } func (c *Command) doRun(ctx context.Context, parser *Parser) (value interface{}, err error) { diff --git a/os/gcmd/gcmd_z_unit_test.go b/os/gcmd/gcmd_z_unit_test.go index 7117bdef0..2a6ae2d7b 100644 --- a/os/gcmd/gcmd_z_unit_test.go +++ b/os/gcmd/gcmd_z_unit_test.go @@ -13,6 +13,8 @@ import ( "fmt" "testing" + "github.com/gogf/gf/v2/errors/gcode" + "github.com/gogf/gf/v2/errors/gerror" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/os/gcmd" "github.com/gogf/gf/v2/os/gctx" @@ -127,7 +129,9 @@ gf get golang.org/x/sys } if err = commandRoot.Run(ctx); err != nil { - g.Log().Fatal(ctx, err) + if gerror.Code(err) == gcode.CodeNotFound { + commandRoot.Print() + } } }) }