cmd/help.go

78 lines
1.5 KiB
Go
Raw Permalink Normal View History

2023-11-10 13:17:01 -05:00
package cmd
import (
"fmt"
2023-11-16 22:58:49 -05:00
"strings"
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()
binaryName := c.Root().Name
2023-11-10 13:17:01 -05:00
fmt.Println(c.LongDescription)
2023-11-10 13:17:01 -05:00
fmt.Println()
fmt.Println("Usage: ")
fmt.Printf(" %s %s ", binaryName, cmdPath)
2023-11-10 13:17:01 -05:00
if len(c.Subcommands) > 0 {
2023-11-10 13:17:01 -05:00
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()
2023-11-10 13:17:01 -05:00
if len(c.Subcommands) > 0 {
2023-11-10 13:17:01 -05:00
fmt.Println()
if c.isRoot {
fmt.Println("Available commands:")
} else {
fmt.Println("Available subcommands:")
}
2023-11-16 22:58:49 -05:00
paddedWidth := c.Subcommands.MaxWidth()
for _, s := range c.Subcommands {
2023-11-16 22:58:49 -05:00
spaceSize := 2 + paddedWidth - len(s.Name)
fmt.Println(" " + s.Name + strings.Repeat(" ", spaceSize) + s.ShortDescription)
2023-11-10 13:17:01 -05:00
}
}
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))
}
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 {
fmt.Println()
2023-11-11 19:28:58 -05:00
fmt.Println("Global options:")
paddedWidth := globalOpts.MaxWidth()
2023-11-11 19:28:58 -05:00
for _, f := range globalOpts {
2023-11-12 17:19:34 -05:00
fmt.Println(" " + opt.HelpLine(f, paddedWidth))
}
}
2023-11-10 13:17:01 -05:00
if len(c.Subcommands) > 0 {
2023-11-10 13:17:01 -05:00
fmt.Println()
fmt.Printf("Run '%s <command> --help' for more information about a command.\n", binaryName)
2023-11-10 13:17:01 -05:00
}
}