From 467c71384f56876268e3ce6feb45512511d37fd8 Mon Sep 17 00:00:00 2001 From: Yevhenii Babichenko Date: Tue, 15 Oct 2019 17:15:14 +0300 Subject: [PATCH] helptext: display options for parent commands This helps to understand which options are available for the given subcommand without calling help for parent commands by providing more informative output. Resolves ipfs/go-ipfs#6640 --- cli/helptext.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/cli/helptext.go b/cli/helptext.go index 1c5480a3..fb211a11 100644 --- a/cli/helptext.go +++ b/cli/helptext.go @@ -203,6 +203,21 @@ func LongHelp(rootName string, root *cmds.Command, path []string, out io.Writer) fields.Synopsis = generateSynopsis(width, cmd, pathStr) } + // 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()