cmd/help.go

74 lines
1.4 KiB
Go

package cmd
import (
"fmt"
"go.fifitido.net/cmd/opt"
)
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.Print("[options]")
if len(c.arguments) > 0 {
for _, a := range c.arguments {
if a.Required {
fmt.Print(" <" + a.Name + ">")
} else {
fmt.Print(" [" + a.Name + "]")
}
}
}
fmt.Println()
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)
}
}
if len(c.opts) > 0 {
fmt.Println()
fmt.Println("Available options:")
paddedWidth := c.opts.MaxWidth()
for _, f := range c.opts {
fmt.Println(" " + opt.HelpLine(f, paddedWidth))
}
}
globalOpts := opt.Globals()
if len(globalOpts) > 0 {
fmt.Println()
fmt.Println("Global options:")
paddedWidth := globalOpts.MaxWidth()
for _, f := range globalOpts {
fmt.Println(" " + opt.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())
}
}