Skip to content

Commit

Permalink
👔 up: update app and cmd help render logic
Browse files Browse the repository at this point in the history
- fix cmd.binFile and binName error on Windows
- add new flag value type KVString, ConfString
  • Loading branch information
inhere committed Feb 26, 2023
1 parent eba208b commit dd8e114
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 8 deletions.
22 changes: 15 additions & 7 deletions base.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/gookit/color"
"github.com/gookit/goutil/maputil"
"github.com/gookit/goutil/structs"
"github.com/gookit/goutil/sysutil"
)

/*************************************************************
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions gcli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 6 additions & 0 deletions gflag/gflag.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 8 additions & 0 deletions help.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package gcli

import (
"fmt"
"strings"
"text/template"

"github.com/gookit/color"
"github.com/gookit/gcli/v3/helper"
"github.com/gookit/goutil/maputil"
"github.com/gookit/goutil/strutil"
"github.com/gookit/goutil/sysutil"
)

/*************************************************************
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

0 comments on commit dd8e114

Please sign in to comment.