Skip to content

Commit

Permalink
up: update the flag options help render some logic
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jul 7, 2022
1 parent b883810 commit e7b66d2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
8 changes: 4 additions & 4 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,8 @@ func (c *Command) Next() {
* standalone running
*************************************************************/

var errCallRunOnApp = errors.New("c.Run() method can only be called in standalone mode")
var errCallRunOnSub = errors.New("c.Run() cannot allow call at subcommand")
// var errCallRunOnApp = errors.New("c.Run() method can only be called in standalone mode")
// var errCallRunOnSub = errors.New("c.Run() cannot allow call at subcommand")

// MustRun Alone the current command, will panic on error
//
Expand Down Expand Up @@ -671,13 +671,13 @@ func (c *Command) ShowHelp() {
// - on standalone, will not init c.core.gFlags
"GOpts": nil,
// parse options to string
"Options": c.Flags.String(),
"Options": c.Flags.BuildHelp(),
// always upper first char
"Desc": c.HelpDesc(),
}

if c.NotStandalone() {
vars["GOpts"] = c.GFlags().String()
vars["GOpts"] = c.GFlags().BuildHelp()
}

// render help message
Expand Down
22 changes: 16 additions & 6 deletions gflag.go
Original file line number Diff line number Diff line change
Expand Up @@ -689,8 +689,8 @@ func (fs *Flags) PrintHelpPanel() {
color.Fprint(fs.out, fs.String())
}

// String for all flag options
func (fs *Flags) String() string {
// BuildHelp string for all flag options
func (fs *Flags) BuildHelp() string {
if fs.buf == nil {
fs.buf = new(bytes.Buffer)
}
Expand All @@ -703,6 +703,11 @@ func (fs *Flags) String() string {
return fs.buf.String()
}

// String for all flag options
func (fs *Flags) String() string {
return fs.BuildHelp()
}

func (fs *Flags) formatOneFlag(f *flag.Flag) {
// Skip render:
// - meta is not exists(Has ensured that it is not a short name)
Expand All @@ -717,19 +722,26 @@ func (fs *Flags) formatOneFlag(f *flag.Flag) {
name := f.Name
// eg: "-V, --version" length is: 13
nameLen := fs.names[name]
// display description on new line
descNl := fs.cfg.DescNewline

var nlIndent string
if descNl {
nlIndent = "\n "
} else {
nlIndent = "\n " + strings.Repeat(" ", fs.flagMaxLen)
}

// add prefix '-' to option
fullName = cflag.AddPrefixes(name, meta.Shorts)

s = fmt.Sprintf(" <info>%s</>", fullName)

// - build flag type info
typeName, desc := flag.UnquoteUsage(f)
// typeName: option value data type: int, string, ..., bool value will return ""
if !fs.cfg.WithoutType && len(typeName) > 0 {
typeLen := len(typeName) + 1
if nameLen+typeLen > fs.flagMaxLen {
if !descNl && nameLen+typeLen > fs.flagMaxLen {
descNl = true
} else {
nameLen += typeLen
Expand All @@ -738,8 +750,6 @@ func (fs *Flags) formatOneFlag(f *flag.Flag) {
s += fmt.Sprintf(" <magenta>%s</>", typeName)
}

// display description on new line
nlIndent := "\n " + strings.Repeat(" ", fs.flagMaxLen)
if descNl {
s += nlIndent
} else {
Expand Down

0 comments on commit e7b66d2

Please sign in to comment.