improve command not found error for package gcmd

This commit is contained in:
John Guo
2022-01-01 19:57:43 +08:00
parent 0a7e4be4ef
commit c17530a6db
2 changed files with 11 additions and 6 deletions

View File

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

View File

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