From 211de24485046e43399737bf89188c4116bded3c Mon Sep 17 00:00:00 2001 From: Ryan Cavicchioni Date: Sun, 24 Jan 2021 20:48:38 -0600 Subject: [PATCH] Add helper to print subcommand help --- cmd/help.go | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/cmd/help.go b/cmd/help.go index 8f68693..b41a31b 100644 --- a/cmd/help.go +++ b/cmd/help.go @@ -23,19 +23,7 @@ func HelpCmd(args CmdArgs) (int, error) { if len(argv) == 0 { printHelp(commandRegistry) } else if len(argv) >= 1 { - subCmd, ok := commandRegistry[argv[0]] - if !ok { - fmt.Printf("unknown commnnd: %s\n", argv[0]) - return ExitError, nil - } - - if subCmd.Use != "" { - fmt.Printf("usage:\n lume %s %s\n", subCmd.Name, subCmd.Use) - fmt.Println() - } - - fmt.Print("flags:\n") - subCmd.Flags.PrintDefaults() + printCmdHelp(argv[0]) } return ExitSuccess, nil @@ -58,3 +46,20 @@ func printHelp(commands map[string]Command) { fmt.Printf(" %-*s %s\n", maxLen, c.Name, c.Short) } } + +func printCmdHelp(name string) error { + subCmd, ok := commandRegistry[name] + if !ok { + return fmt.Errorf("unknown commnnd: %s\n", name) + } + + if subCmd.Use != "" { + fmt.Printf("usage:\n lume %s %s\n", subCmd.Name, subCmd.Use) + fmt.Println() + } + + fmt.Print("flags:\n") + subCmd.Flags.PrintDefaults() + + return nil +}