2023-11-10 13:17:01 -05:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-11-10 15:11:50 -05:00
|
|
|
|
2023-11-12 17:19:34 -05:00
|
|
|
"go.fifitido.net/cmd/opt"
|
2023-11-10 13:17:01 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func (c *Command) ShowHelp() {
|
|
|
|
cmdPath := c.CommandPath()
|
|
|
|
|
2023-11-11 19:16:05 -05:00
|
|
|
fmt.Println(c.longDescription)
|
2023-11-10 13:17:01 -05:00
|
|
|
fmt.Println()
|
|
|
|
fmt.Println("Usage: ")
|
|
|
|
fmt.Printf(" %s ", cmdPath)
|
|
|
|
|
|
|
|
if len(c.subcommands) > 0 {
|
|
|
|
if c.CanRun() {
|
|
|
|
fmt.Print("[command] ")
|
|
|
|
} else {
|
|
|
|
fmt.Print("<command> ")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-11 19:28:58 -05:00
|
|
|
fmt.Println("[options]")
|
2023-11-10 13:17:01 -05:00
|
|
|
|
|
|
|
if len(c.subcommands) > 0 {
|
|
|
|
fmt.Println()
|
|
|
|
|
|
|
|
if c.isRoot {
|
|
|
|
fmt.Println("Available commands:")
|
|
|
|
} else {
|
|
|
|
fmt.Println("Available subcommands:")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range c.subcommands {
|
2023-11-11 19:16:05 -05:00
|
|
|
fmt.Println(" " + s.name + " " + s.shortDescription)
|
2023-11-10 13:17:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println()
|
2023-11-11 19:28:58 -05:00
|
|
|
fmt.Println("Available options:")
|
2023-11-10 13:17:01 -05:00
|
|
|
|
2023-11-11 19:28:58 -05:00
|
|
|
paddedWidth := c.opts.MaxWidth()
|
|
|
|
for _, f := range c.opts {
|
2023-11-12 17:19:34 -05:00
|
|
|
fmt.Println(" " + opt.HelpLine(f, paddedWidth))
|
2023-11-10 13:17:01 -05:00
|
|
|
}
|
|
|
|
|
2023-11-12 17:19:34 -05:00
|
|
|
globalOpts := opt.Globals()
|
2023-11-11 19:28:58 -05:00
|
|
|
if len(globalOpts) > 0 {
|
|
|
|
paddedWidth = globalOpts.MaxWidth()
|
2023-11-11 19:16:05 -05:00
|
|
|
fmt.Println()
|
2023-11-11 19:28:58 -05:00
|
|
|
fmt.Println("Global options:")
|
|
|
|
for _, f := range globalOpts {
|
2023-11-12 17:19:34 -05:00
|
|
|
fmt.Println(" " + opt.HelpLine(f, paddedWidth))
|
2023-11-11 19:16:05 -05:00
|
|
|
}
|
|
|
|
}
|
2023-11-10 13:17:01 -05:00
|
|
|
|
|
|
|
if len(c.subcommands) > 0 {
|
|
|
|
fmt.Println()
|
2023-11-11 19:17:46 -05:00
|
|
|
fmt.Printf("Run '%s <command> --help' for more information about a command.\n", c.Root().CommandPath())
|
2023-11-10 13:17:01 -05:00
|
|
|
}
|
|
|
|
}
|