cmd/help.go

64 lines
1.2 KiB
Go

package cmd
import (
"fmt"
"go.fifitido.net/cmd/opts"
)
func (c *Command) ShowHelp() {
cmdPath := c.CommandPath()
fmt.Println(c.longDescription)
fmt.Println()
fmt.Println("Usage: ")
fmt.Printf(" %s ", cmdPath)
if len(c.subcommands) > 0 {
if c.CanRun() {
fmt.Print("[command] ")
} else {
fmt.Print("<command> ")
}
}
fmt.Println("[options]")
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)
}
}
fmt.Println()
fmt.Println("Available options:")
paddedWidth := c.opts.MaxWidth()
for _, f := range c.opts {
fmt.Println(" " + opts.HelpLine(f, paddedWidth))
}
globalOpts := opts.Globals()
if len(globalOpts) > 0 {
paddedWidth = globalOpts.MaxWidth()
fmt.Println()
fmt.Println("Global options:")
for _, f := range globalOpts {
fmt.Println(" " + opts.HelpLine(f, paddedWidth))
}
}
if len(c.subcommands) > 0 {
fmt.Println()
fmt.Printf("Run '%s <command> --help' for more information about a command.\n", c.Root().CommandPath())
}
}