Skip to content

Commit

Permalink
👔 up(gflag): update the command arguments help render logic
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Feb 9, 2023
1 parent 3657720 commit fa84b6f
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 10 deletions.
2 changes: 1 addition & 1 deletion base.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ type base struct {
// helpVars custom add vars for render help template.
helpVars map[string]any

// Ctx for command
// Ctx data for command, allow add custom context data.
Ctx *Context
// Logo ASCII logo setting
Logo *Logo
Expand Down
4 changes: 2 additions & 2 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type Command struct {
// Name is the command name.
Name string
// Desc is the command description message.
// Can use string-var in contents, eg: {$cmd}
Desc string

// Aliases is the command name's alias names
Expand Down Expand Up @@ -100,6 +101,7 @@ type Command struct {
// Func is the command handler func. Func Runner
Func RunnerFunc
// Help is the long help message text
// Can use string-var in contents, eg: {$cmd}
Help string
// HelpRender custom render cmd help message
HelpRender func(c *Command)
Expand Down Expand Up @@ -574,7 +576,6 @@ func (c *Command) Root() *Command {
if c.parent != nil {
return c.parent.Root()
}

return c
}

Expand All @@ -598,7 +599,6 @@ func (c *Command) ParentName() string {
if c.parent != nil {
return c.parent.Name
}

return ""
}

Expand Down
16 changes: 14 additions & 2 deletions gflag/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,14 +238,26 @@ func (ags *CliArgs) BuildArgsHelp() string {
return ""
}

indent := strutil.Repeat(" ", ags.argWidth+3)

var sb strings.Builder
for _, arg := range ags.args {
sb.WriteString(fmt.Sprintf(
"<info>%s</> %s%s\n",
" <info>%s</> %s",
strutil.PadRight(arg.HelpName(), " ", ags.argWidth),
getRequiredMark(arg.Required),
strutil.UpperFirst(arg.Desc),
))

// multi lines
if strings.ContainsRune(arg.Desc, '\n') {
first, other := strutil.QuietCut(arg.Desc, "\n")
sb.WriteString(strutil.UpperFirst(first))
sb.WriteByte('\n')
sb.WriteString(strutil.Indent(other, indent))
} else {
sb.WriteString(strutil.UpperFirst(arg.Desc))
}
sb.WriteByte('\n')
}

return sb.String()
Expand Down
7 changes: 6 additions & 1 deletion gflag/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ const (
shortSepChar = ","
)

// DefaultOptWidth for render help
var DefaultOptWidth = 20

// CliOpts cli options management
type CliOpts struct {
// name inherited from gcli.Command
Expand Down Expand Up @@ -55,6 +58,7 @@ func (ops *CliOpts) InitFlagSet(name string) {
ops.fSet.SetOutput(io.Discard)
// nothing to do ... render usage on after parsed
ops.fSet.Usage = func() {}
ops.optMaxLen = DefaultOptWidth
}

// SetName for CliArgs
Expand Down Expand Up @@ -342,7 +346,8 @@ func (ops *CliOpts) checkFlagInfo(opt *CliOpt) string {
helpLen := opt.helpNameLen()
// fix: must exclude Hidden option
if !opt.Hidden {
ops.optMaxLen = mathutil.MaxInt(ops.optMaxLen, helpLen)
// +7: type placeholder width
ops.optMaxLen = mathutil.MaxInt(ops.optMaxLen, helpLen+6)
}

// check short names
Expand Down
9 changes: 5 additions & 4 deletions help.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,9 @@ var CmdHelpTemplate = `{{.Desc}}
<comment>Global Options:</>
{{.GOpts}}{{end}}{{if .Options}}
<comment>Options:</>
{{.Options}}{{end}}{{if .Cmd.HasArgs}}
<comment>Arguments:</>{{range $a := .Cmd.Args}}
<info>{{$a.HelpName | printf "%-12s"}}</> {{if $a.Required}}<red>*</>{{end}}{{$a.Desc | ucFirst}}{{end}}
{{end}}{{ if .Subs }}
{{.Options}}{{end}}{{if .ArgsHelp}}
<comment>Arguments:</>
{{.ArgsHelp}}{{end}}{{ if .Subs }}
<comment>Subcommands:</>{{range $n,$c := .Subs}}
<info>{{$c.Name | paddingName }}</> {{$c.HelpDesc}}{{if $c.Aliases}} (alias: <green>{{ join $c.Aliases ","}}</>){{end}}{{end}}
{{end}}{{if .Cmd.Examples}}
Expand Down Expand Up @@ -223,6 +222,8 @@ func (c *Command) ShowHelp() (err error) {
"GOpts": nil,
// parse options to string
"Options": c.Flags.BuildOptsHelp(),
// parse options to string
"ArgsHelp": c.Flags.BuildArgsHelp(),
// always upper first char
"Desc": c.HelpDesc(),
// user custom help vars
Expand Down

0 comments on commit fa84b6f

Please sign in to comment.