tweak help/--help

This commit is contained in:
Roberto Hidalgo 2023-03-19 19:18:10 -06:00
parent 5b405d6747
commit 771917d2fc
2 changed files with 33 additions and 28 deletions

View File

@ -15,6 +15,14 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
func cobraCommandFullName(c *cobra.Command) []string {
name := []string{c.Name()}
if c.HasParent() {
name = append(cobraCommandFullName(c.Parent()), name...)
}
return name
}
var Help = &cobra.Command{ var Help = &cobra.Command{
Use: _c.HelpCommandName + " [command]", Use: _c.HelpCommandName + " [command]",
Short: "Display usage information for any command", Short: "Display usage information for any command",
@ -39,34 +47,31 @@ var Help = &cobra.Command{
return completions, cobra.ShellCompDirectiveNoFileComp return completions, cobra.ShellCompDirectiveNoFileComp
}, },
Run: func(c *cobra.Command, args []string) { Run: func(c *cobra.Command, args []string) {
cmd, _, e := c.Root().Find(args) if len(args) > 0 && c != nil && c.Name() != args[len(args)-1] {
if cmd == nil || e != nil || (len(args) > 0 && cmd != nil && cmd.Name() != args[len(args)-1]) { c, topicArgs, err := c.Root().Find(args)
if cmd == nil { if err == nil && c != nil && len(topicArgs) == 0 {
err := c.Root().Help() // exact command help
if err != nil { cobra.CheckErr(c.Help())
logrus.Error(err) os.Exit(statuscode.RenderHelp)
os.Exit(statuscode.ProgrammerError) return
}
logrus.Errorf("Unknown help topic %s", args)
os.Exit(statuscode.NotFound)
} else {
err := cmd.Help()
if err != nil {
logrus.Error(err)
os.Exit(statuscode.ProgrammerError)
}
if len(args) > 1 {
logrus.Errorf("Unknown help topic %s for %s", args[1], args[0])
} else {
logrus.Errorf("Unknown help topic %s for %s", runtime.Executable, args[0])
}
os.Exit(statuscode.NotFound)
} }
if err != nil {
logrus.Error(err)
os.Exit(statuscode.ProgrammerError)
}
fullName := strings.Join(cobraCommandFullName(c), " ")
cobra.CheckErr(c.Help())
if len(topicArgs) > 0 {
logrus.Errorf("Unknown help topic \"%s\" for %s", topicArgs[0], fullName)
} else {
logrus.Errorf("Unknown help topic \"%s\"", args[0])
}
os.Exit(statuscode.NotFound)
} else { } else {
cmd.InitDefaultHelpFlag() // make possible 'help' flag to be shown c.InitDefaultHelpFlag() // make possible 'help' flag to be shown
cobra.CheckErr(cmd.Help()) cobra.CheckErr(c.Help())
} }
os.Exit(statuscode.RenderHelp) os.Exit(statuscode.RenderHelp)

View File

@ -40,7 +40,7 @@ type CommandRegistry struct {
} }
func Register(cmd *command.Command) { func Register(cmd *command.Command) {
log.Debugf("adding to registry: %s", cmd.FullName()) log.Tracef("adding to registry: %s", cmd.FullName())
registry.kv[cmd.FullName()] = cmd registry.kv[cmd.FullName()] = cmd
} }
@ -91,7 +91,7 @@ func Execute(version string) error {
if idx == len(cmd.Path)-1 { if idx == len(cmd.Path)-1 {
leaf := ToCobra(cmd, cmdRoot.Options) leaf := ToCobra(cmd, cmdRoot.Options)
container.AddCommand(leaf) container.AddCommand(leaf)
log.Debugf("cobra: %s => %s", leaf.Name(), container.CommandPath()) log.Tracef("cobra: %s => %s", leaf.Name(), container.CommandPath())
break break
} }