Add helper to print subcommand help

This commit is contained in:
Ryan Cavicchioni 2021-01-24 20:48:38 -06:00
parent 743d0eed7c
commit 211de24485
Signed by: ryanc
GPG Key ID: 877EEDAF9245103D

View File

@ -23,19 +23,7 @@ func HelpCmd(args CmdArgs) (int, error) {
if len(argv) == 0 { if len(argv) == 0 {
printHelp(commandRegistry) printHelp(commandRegistry)
} else if len(argv) >= 1 { } else if len(argv) >= 1 {
subCmd, ok := commandRegistry[argv[0]] printCmdHelp(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()
} }
return ExitSuccess, nil return ExitSuccess, nil
@ -58,3 +46,20 @@ func printHelp(commands map[string]Command) {
fmt.Printf(" %-*s %s\n", maxLen, c.Name, c.Short) 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
}