hub/commands/help.go

65 строки
1.2 KiB
Go
Исходник Обычный вид История

2013-05-29 22:58:46 +04:00
package commands
2013-04-09 08:53:13 +04:00
import (
"fmt"
"log"
"os"
"text/template"
)
var cmdHelp = &Command{
Usage: "help [command]",
2013-05-24 18:25:50 +04:00
Short: "Show help",
2013-05-29 22:58:46 +04:00
Long: `Shows usage for a command.`,
2013-04-09 08:53:13 +04:00
}
func init() {
cmdHelp.Run = runHelp // break init loop
}
func runHelp(cmd *Command, args []string) {
if len(args) == 0 {
2013-05-29 22:58:46 +04:00
PrintUsage()
2013-04-09 08:53:13 +04:00
return // not os.Exit(2); success
}
if len(args) != 1 {
log.Fatal("too many arguments")
}
2013-06-18 01:15:59 +04:00
for _, cmd := range All() {
2013-04-09 08:53:13 +04:00
if cmd.Name() == args[0] {
2013-05-29 22:58:46 +04:00
cmd.PrintUsage()
2013-04-09 08:53:13 +04:00
return
}
}
2013-04-11 19:33:43 +04:00
fmt.Fprintf(os.Stderr, "Unknown help topic: %q. Run 'gh help'.\n", args[0])
2013-04-09 08:53:13 +04:00
os.Exit(2)
}
var usageTemplate = template.Must(template.New("usage").Parse(`Usage: gh [command] [options] [arguments]
2013-06-18 01:15:59 +04:00
Remote Commands:{{range .RemoteCommands}}{{if .Runnable}}{{if .List}}
2013-06-18 01:09:47 +04:00
{{.Name | printf "%-16s"}} {{.Short}}{{end}}{{end}}{{end}}
2013-06-18 01:15:59 +04:00
GitHub Commands:{{range .GitHubCommands}}{{if .Runnable}}{{if .List}}
2013-04-11 19:33:43 +04:00
{{.Name | printf "%-16s"}} {{.Short}}{{end}}{{end}}{{end}}
2013-04-09 08:53:13 +04:00
See 'gh help [command]' for more information about a command.
`))
2013-05-29 22:58:46 +04:00
func PrintUsage() {
2013-04-09 08:53:13 +04:00
usageTemplate.Execute(os.Stdout, struct {
2013-06-18 01:09:47 +04:00
RemoteCommands []*Command
GitHubCommands []*Command
2013-04-09 08:53:13 +04:00
}{
2013-06-18 01:09:47 +04:00
Remote,
GitHub,
2013-04-09 08:53:13 +04:00
})
}
2013-05-29 22:58:46 +04:00
func Usage() {
PrintUsage()
2013-04-09 08:53:13 +04:00
os.Exit(2)
}