Skip to content

Commit

Permalink
helptext: add a separate --help-all flags to display parent options
Browse files Browse the repository at this point in the history
Introduce the --help-all flag as specified in ipfs/kubo#6640. This
flag allows to call more verbose help only when it is necessary.
  • Loading branch information
eugene-babichenko committed Oct 16, 2019
1 parent 467c713 commit 47a8f95
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
41 changes: 35 additions & 6 deletions cli/helptext.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,30 +142,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 @@ -178,8 +180,6 @@ func LongHelp(rootName string, root *cmds.Command, path []string, out io.Writer)
MoreHelp: (cmd != root),
}

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

if len(cmd.Helptext.LongDescription) > 0 {
fields.Description = cmd.Helptext.LongDescription
}
Expand All @@ -203,6 +203,17 @@ 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
}

// display options of parent commands
parentOptionsList := make([]string, len(path)-1)
for i := len(path) - 1; i > 0; i-- {
Expand All @@ -227,6 +238,24 @@ func LongHelp(rootName string, root *cmds.Command, path []string, out io.Writer)
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()

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

return longHelpTemplate.Execute(out, fields)
}

// ShortHelp writes a formatted CLI helptext string to a Writer for the given command
func ShortHelp(rootName string, root *cmds.Command, path []string, out io.Writer) error {
cmd, err := root.Get(path)
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

0 comments on commit 47a8f95

Please sign in to comment.