From 771917d2fc7a75636a389501124166dd9c1280d9 Mon Sep 17 00:00:00 2001 From: Roberto Hidalgo Date: Sun, 19 Mar 2023 19:18:10 -0600 Subject: [PATCH] tweak help/--help --- internal/commands/help.go | 57 +++++++++++++++++++---------------- internal/registry/registry.go | 4 +-- 2 files changed, 33 insertions(+), 28 deletions(-) diff --git a/internal/commands/help.go b/internal/commands/help.go index 9e4d908..e5692bc 100644 --- a/internal/commands/help.go +++ b/internal/commands/help.go @@ -15,6 +15,14 @@ import ( "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{ Use: _c.HelpCommandName + " [command]", Short: "Display usage information for any command", @@ -39,34 +47,31 @@ var Help = &cobra.Command{ return completions, cobra.ShellCompDirectiveNoFileComp }, Run: func(c *cobra.Command, args []string) { - cmd, _, e := c.Root().Find(args) - if cmd == nil || e != nil || (len(args) > 0 && cmd != nil && cmd.Name() != args[len(args)-1]) { - if cmd == nil { - err := c.Root().Help() - if err != nil { - logrus.Error(err) - os.Exit(statuscode.ProgrammerError) - } - 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 len(args) > 0 && c != nil && c.Name() != args[len(args)-1] { + c, topicArgs, err := c.Root().Find(args) + if err == nil && c != nil && len(topicArgs) == 0 { + // exact command help + cobra.CheckErr(c.Help()) + os.Exit(statuscode.RenderHelp) + return } + + 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 { - cmd.InitDefaultHelpFlag() // make possible 'help' flag to be shown - cobra.CheckErr(cmd.Help()) + c.InitDefaultHelpFlag() // make possible 'help' flag to be shown + cobra.CheckErr(c.Help()) } os.Exit(statuscode.RenderHelp) diff --git a/internal/registry/registry.go b/internal/registry/registry.go index 9ff63f0..a81c85f 100644 --- a/internal/registry/registry.go +++ b/internal/registry/registry.go @@ -40,7 +40,7 @@ type CommandRegistry struct { } 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 } @@ -91,7 +91,7 @@ func Execute(version string) error { if idx == len(cmd.Path)-1 { leaf := ToCobra(cmd, cmdRoot.Options) container.AddCommand(leaf) - log.Debugf("cobra: %s => %s", leaf.Name(), container.CommandPath()) + log.Tracef("cobra: %s => %s", leaf.Name(), container.CommandPath()) break }