From dd8e114098da7caa92443b843ed712a5b4b10f9a Mon Sep 17 00:00:00 2001 From: Inhere Date: Sun, 26 Feb 2023 11:44:21 +0800 Subject: [PATCH] :necktie: up: update app and cmd help render logic - fix cmd.binFile and binName error on Windows - add new flag value type KVString, ConfString --- base.go | 22 +++++++++++++++------- cmd.go | 2 +- gcli.go | 3 +++ gflag/gflag.go | 6 ++++++ help.go | 8 ++++++++ 5 files changed, 33 insertions(+), 8 deletions(-) diff --git a/base.go b/base.go index de2fd19..4709004 100644 --- a/base.go +++ b/base.go @@ -12,6 +12,7 @@ import ( "github.com/gookit/color" "github.com/gookit/goutil/maputil" "github.com/gookit/goutil/structs" + "github.com/gookit/goutil/sysutil" ) /************************************************************* @@ -56,18 +57,25 @@ func (ctx *Context) InitCtx() *Context { binFile := os.Args[0] workDir, _ := os.Getwd() - // binName will contain work dir path on Windows - // if envutil.IsWin() { - // binFile = strings.Replace(CLI.binName, workDir+"\\", "", 1) - // } - ctx.pid = os.Getpid() // more info ctx.osName = runtime.GOOS ctx.workDir = workDir - ctx.binDir = filepath.Dir(binFile) ctx.binFile = binFile - ctx.binName = filepath.Base(binFile) + + // with path + if strings.ContainsRune(binFile, os.PathSeparator) { + ctx.binDir = filepath.Dir(binFile) + ctx.binName = filepath.Base(binFile) + } else { + ctx.binName = binFile + + if fpath, err := sysutil.FindExecutable(binFile); err == nil { + ctx.binFile = fpath + ctx.binDir = filepath.Dir(fpath) + } + } + ctx.argLine = strings.Join(os.Args[1:], " ") return ctx } diff --git a/cmd.go b/cmd.go index 197b9ac..98cf931 100644 --- a/cmd.go +++ b/cmd.go @@ -308,7 +308,7 @@ func (c *Command) initCommandBase(cName string) { // binName with command path "binWithPath": binWithPath, // binFile with command - "fullCmd": c.Ctx.binFile + " " + cName, + "fullCmd": binWithPath, }) c.base.cmdNames = make(map[string]int) diff --git a/gcli.go b/gcli.go index 786e688..725e34a 100644 --- a/gcli.go +++ b/gcli.go @@ -255,6 +255,9 @@ type Booleans = cflag.Booleans // EnumString The string flag list, implemented flag.Value interface type EnumString = cflag.EnumString +// KVString The key-value string flag, repeatable. +type KVString = cflag.KVString + // ConfString The config-string flag, INI format, like nginx-config type ConfString = cflag.ConfString diff --git a/gflag/gflag.go b/gflag/gflag.go index 7a6d55d..0ed39b3 100644 --- a/gflag/gflag.go +++ b/gflag/gflag.go @@ -63,3 +63,9 @@ type Booleans = cflag.Booleans // EnumString The string flag list, implemented flag.Value interface type EnumString = cflag.EnumString + +// KVString The key-value string flag, repeatable. +type KVString = cflag.KVString + +// ConfString The config-string flag, INI format, like nginx-config. +type ConfString = cflag.ConfString diff --git a/help.go b/help.go index 246be25..badbc3c 100644 --- a/help.go +++ b/help.go @@ -1,6 +1,7 @@ package gcli import ( + "fmt" "strings" "text/template" @@ -8,6 +9,7 @@ import ( "github.com/gookit/gcli/v3/helper" "github.com/gookit/goutil/maputil" "github.com/gookit/goutil/strutil" + "github.com/gookit/goutil/sysutil" ) /************************************************************* @@ -81,6 +83,9 @@ func (app *App) showApplicationHelp() bool { // parse help vars and render color tags color.Print(app.ReplacePairs(s)) + if sysutil.IsLinux() { + fmt.Println() + } return false } @@ -244,5 +249,8 @@ func (c *Command) ShowHelp() (err error) { // parse gcli help vars then print help // fmt.Printf("%#v\n", s) color.Print(c.ReplacePairs(str)) + if sysutil.IsLinux() { + fmt.Println() + } return }