2013-07-14 23:30:58 +00:00
|
|
|
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
|
|
|
|
Version string
|
2013-07-15 02:16:30 +00:00
|
|
|
Commands []Command
|
|
|
|
Flags []Flag
|
2013-07-15 00:37:43 +00:00
|
|
|
}
|
2013-07-14 23:30:58 +00:00
|
|
|
|
|
|
|
var HelpCommand = Command{
|
|
|
|
Name: "help",
|
|
|
|
ShortName: "h",
|
2013-07-15 01:17:15 +00:00
|
|
|
Usage: "Shows a list of commands or help for one command",
|
2013-07-14 23:30:58 +00:00
|
|
|
}
|
|
|
|
|
2013-07-15 01:17:15 +00:00
|
|
|
func init() {
|
2013-07-15 01:19:53 +00:00
|
|
|
HelpCommand.Action = ShowHelp
|
2013-07-15 01:17:15 +00:00
|
|
|
}
|
|
|
|
|
2013-07-18 00:14:25 +00:00
|
|
|
func ShowHelp(c *Context) {
|
2013-07-15 00:53:34 +00:00
|
|
|
helpTemplate := `NAME:
|
|
|
|
{{.Name}} - {{.Usage}}
|
2013-07-15 00:37:43 +00:00
|
|
|
|
|
|
|
USAGE:
|
2013-07-15 00:53:34 +00:00
|
|
|
{{.Name}} [global options] command [command options] [arguments...]
|
2013-07-15 00:37:43 +00:00
|
|
|
|
|
|
|
VERSION:
|
2013-07-15 00:53:34 +00:00
|
|
|
{{.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}}
|
2013-07-15 00:53:34 +00:00
|
|
|
{{end}}
|
2013-07-15 02:16:30 +00:00
|
|
|
GLOBAL OPTIONS
|
|
|
|
{{range .Flags}}{{.}}
|
|
|
|
{{end}}
|
2013-07-15 00:37:43 +00:00
|
|
|
`
|
|
|
|
data := HelpData{
|
|
|
|
Name,
|
|
|
|
Usage,
|
|
|
|
Version,
|
2013-07-15 02:16:30 +00:00
|
|
|
append(Commands, HelpCommand),
|
2013-07-16 15:11:05 +00:00
|
|
|
Flags,
|
2013-07-15 00:37:43 +00:00
|
|
|
}
|
|
|
|
|
2013-07-14 23:30:58 +00:00
|
|
|
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))
|
2013-07-15 00:53:34 +00:00
|
|
|
t.Execute(w, data)
|
2013-07-15 00:37:43 +00:00
|
|
|
w.Flush()
|
2013-07-14 23:30:58 +00:00
|
|
|
}
|