cmd/help.go

64 lines
1.2 KiB
Go
Raw Normal View History

2023-11-10 13:17:01 -05:00
package cmd
import (
"fmt"
2023-11-10 15:11:50 -05:00
2023-11-11 19:28:58 -05:00
"go.fifitido.net/cmd/opts"
2023-11-10 13:17:01 -05:00
)
func (c *Command) ShowHelp() {
cmdPath := c.CommandPath()
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 {
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 {
fmt.Println(" " + opts.HelpLine(f, paddedWidth))
2023-11-10 13:17:01 -05:00
}
2023-11-11 19:28:58 -05:00
globalOpts := opts.Globals()
if len(globalOpts) > 0 {
paddedWidth = globalOpts.MaxWidth()
fmt.Println()
2023-11-11 19:28:58 -05:00
fmt.Println("Global options:")
for _, f := range globalOpts {
fmt.Println(" " + opts.HelpLine(f, paddedWidth))
}
}
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
}
}