2020-08-08 03:11:33 +00:00
|
|
|
package lumecmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2021-02-03 00:34:37 +00:00
|
|
|
"sort"
|
2020-08-08 03:11:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2021-02-01 01:40:27 +00:00
|
|
|
RegisterCommand("help", Command{
|
|
|
|
Func: HelpCmd,
|
|
|
|
Flags: func() *flag.FlagSet {
|
|
|
|
fs := flag.NewFlagSet("help", flag.ExitOnError)
|
2020-08-08 03:11:33 +00:00
|
|
|
|
2021-02-01 01:40:27 +00:00
|
|
|
return fs
|
|
|
|
}(),
|
2020-08-08 03:11:33 +00:00
|
|
|
Use: "<command>",
|
|
|
|
Short: "Show help for a command",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func HelpCmd(args CmdArgs) (int, error) {
|
|
|
|
argv := args.Flags.Args()
|
|
|
|
|
|
|
|
if len(argv) == 0 {
|
|
|
|
printHelp(commandRegistry)
|
|
|
|
} else if len(argv) >= 1 {
|
2021-01-25 02:48:38 +00:00
|
|
|
printCmdHelp(argv[0])
|
2020-08-08 03:11:33 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 03:08:13 +00:00
|
|
|
return ExitSuccess, nil
|
2020-08-08 03:11:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func printHelp(commands map[string]Command) {
|
|
|
|
var maxLen, cmdLen int
|
2021-02-03 00:34:37 +00:00
|
|
|
var keys []string
|
|
|
|
|
2020-08-08 03:11:33 +00:00
|
|
|
for _, c := range commands {
|
2021-02-03 00:34:37 +00:00
|
|
|
keys = append(keys, c.Name)
|
2020-08-08 03:11:33 +00:00
|
|
|
cmdLen = len(c.Name)
|
|
|
|
if cmdLen > maxLen {
|
|
|
|
maxLen = cmdLen
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("usage:\n lume <command> [<args...>]")
|
|
|
|
fmt.Println()
|
|
|
|
fmt.Println("\ncommands:")
|
2021-02-03 00:34:37 +00:00
|
|
|
|
|
|
|
sort.Strings(keys)
|
|
|
|
for _, k := range keys {
|
|
|
|
c := commands[k]
|
2020-08-08 03:11:33 +00:00
|
|
|
fmt.Printf(" %-*s %s\n", maxLen, c.Name, c.Short)
|
|
|
|
}
|
|
|
|
}
|
2021-01-25 02:48:38 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|