cmd/help.go

78 lines
1.5 KiB
Go

package cmd
import (
"fmt"
"strings"
"go.fifitido.net/cmd/opt"
)
func (c *Command) ShowHelp() {
cmdPath := c.CommandPath()
binaryName := c.Root().Name
fmt.Println(c.LongDescription)
fmt.Println()
fmt.Println("Usage: ")
fmt.Printf(" %s %s ", binaryName, cmdPath)
if len(c.Subcommands) > 0 {
if c.CanRun() {
fmt.Print("[command] ")
} else {
fmt.Print("<command> ")
}
}
fmt.Print("[options]")
if len(c.Args) > 0 {
for _, a := range c.Args {
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:")
}
paddedWidth := c.Subcommands.MaxWidth()
for _, s := range c.Subcommands {
spaceSize := 2 + paddedWidth - len(s.Name)
fmt.Println(" " + s.Name + strings.Repeat(" ", spaceSize) + 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", binaryName)
}
}