Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

helptext: display options for parent commands #179

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 57 additions & 7 deletions cli/helptext.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ const longHelpFormat = `USAGE

{{.Indent}}For more information about each command, use:
{{.Indent}}'{{.Path}} <subcmd> --help'
{{end}}{{if .MoreHelp}}
{{.Indent}}To include information about arguments of parent commands, use:
{{.Indent}}'{{.Path}} --help-all'
{{end}}
`
const shortHelpFormat = `USAGE
Expand All @@ -113,6 +116,8 @@ SUBCOMMANDS
{{end}}{{if .MoreHelp}}
{{.Indent}}For more information about each command, use:
{{.Indent}}'{{.Path}} <subcmd> --help'
{{.Indent}}To include information about arguments of parent commands, use:
{{.Indent}}'{{.Path}} --help-all'
{{end}}
`

Expand Down Expand Up @@ -142,30 +147,32 @@ var ErrNoHelpRequested = errors.New("no help requested")
func HandleHelp(appName string, req *cmds.Request, out io.Writer) error {
long, _ := req.Options[cmds.OptLongHelp].(bool)
short, _ := req.Options[cmds.OptShortHelp].(bool)
all, _ := req.Options[cmds.OptHelpAll].(bool)

switch {
case long:
return LongHelp(appName, req.Root, req.Path, out)
case short:
return ShortHelp(appName, req.Root, req.Path, out)
case all:
return HelpAll(appName, req.Root, req.Path, out)
default:
return ErrNoHelpRequested
}
}

// LongHelp writes a formatted CLI helptext string to a Writer for the given command
func LongHelp(rootName string, root *cmds.Command, path []string, out io.Writer) error {
func makeLongHelpFields(rootName string, path []string, root *cmds.Command, width int) (*helpFields, error) {
cmd, err := root.Get(path)
if err != nil {
return err
return nil, err
}

pathStr := rootName
if len(path) > 0 {
pathStr += " " + strings.Join(path, " ")
}

fields := helpFields{
fields := &helpFields{
Indent: indentStr,
Path: pathStr,
Tagline: cmd.Helptext.Tagline,
Expand All @@ -175,11 +182,8 @@ func LongHelp(rootName string, root *cmds.Command, path []string, out io.Writer)
Subcommands: cmd.Helptext.Subcommands,
Description: cmd.Helptext.ShortDescription,
Usage: cmd.Helptext.Usage,
MoreHelp: (cmd != root),
}

width := getTerminalWidth(out) - len(indentStr)

if len(cmd.Helptext.LongDescription) > 0 {
fields.Description = cmd.Helptext.LongDescription
}
Expand All @@ -203,6 +207,52 @@ func LongHelp(rootName string, root *cmds.Command, path []string, out io.Writer)
fields.Synopsis = generateSynopsis(width, cmd, pathStr)
}

return fields, nil
}

func HelpAll(rootName string, root *cmds.Command, path []string, out io.Writer) error {
width := getTerminalWidth(out) - len(indentStr)

fields, err := makeLongHelpFields(rootName, path, root, width)
if err != nil {
return err
}

fields.MoreHelp = false

// display options of parent commands
parentOptionsList := make([]string, len(path)-1)
for i := len(path) - 1; i > 0; i-- {
parentPath := path[:i]
parentCmd, err := root.Get(parentPath)
if err != nil {
return err
}
parentPathString := strings.Join(parentPath, " ")
parentOptionsString := strings.Join(optionText(width, parentCmd), "\n")
parentOptionsList = append(parentOptionsList, fmt.Sprintf("%s\n%s", parentPathString, parentOptionsString))
}
parentOptions := strings.Join(parentOptionsList, "\n\n")
fields.Options = fmt.Sprintf("%s\n\n%s", fields.Options, parentOptions)

// trim the extra newlines (see TrimNewlines doc)
fields.TrimNewlines()

// indent all fields that have been set
fields.IndentAll()

return longHelpTemplate.Execute(out, fields)
}

// LongHelp writes a formatted CLI helptext string to a Writer for the given command
func LongHelp(rootName string, root *cmds.Command, path []string, out io.Writer) error {
width := getTerminalWidth(out) - len(indentStr)

fields, err := makeLongHelpFields(rootName, path, root, width)
if err != nil {
return err
}

// trim the extra newlines (see TrimNewlines doc)
fields.TrimNewlines()

Expand Down
1 change: 1 addition & 0 deletions opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const (
TimeoutOpt = "timeout"
OptShortHelp = "h"
OptLongHelp = "help"
OptHelpAll = "help-all"
DerefLong = "dereference-args"
StdinName = "stdin-name"
Hidden = "hidden"
Expand Down