urfave-cli/help.go

50 lines
882 B
Go
Raw Normal View History

package cli
import "os"
import "text/tabwriter"
2013-07-15 00:37:43 +00:00
import "text/template"
type HelpData struct {
Name string
Usage string
Commands []Command
Version string
}
var HelpCommand = Command{
Name: "help",
ShortName: "h",
Usage: "Shows a list of commands or help for one command",
}
func init() {
HelpCommand.Action = ShowHelp
}
func ShowHelp(name string) {
helpTemplate := `NAME:
{{.Name}} - {{.Usage}}
2013-07-15 00:37:43 +00:00
USAGE:
{{.Name}} [global options] command [command options] [arguments...]
2013-07-15 00:37:43 +00:00
VERSION:
{{.Version}}
2013-07-15 00:37:43 +00:00
COMMANDS:
2013-07-15 01:00:03 +00:00
{{range .Commands}}{{.Name}}{{with .ShortName}}, {{.}}{{end}}{{ "\t" }}{{.Usage}}
{{end}}
2013-07-15 00:37:43 +00:00
`
data := HelpData{
Name,
Usage,
append(Commands, HelpCommand),
2013-07-15 00:37:43 +00:00
Version,
}
w := tabwriter.NewWriter(os.Stdout, 0, 8, 1, '\t', 0)
2013-07-15 00:37:43 +00:00
t := template.Must(template.New("help").Parse(helpTemplate))
t.Execute(w, data)
2013-07-15 00:37:43 +00:00
w.Flush()
}