Formatted help text via templates
This commit is contained in:
parent
6016cafda3
commit
5b788be8a6
2
cli.go
2
cli.go
@ -6,7 +6,7 @@ import "os"
|
||||
var Name = os.Args[0]
|
||||
|
||||
// Description of the program.
|
||||
var Usage = ""
|
||||
var Usage = "<No Description>"
|
||||
|
||||
// Version of the program
|
||||
var Version = "0.0.0"
|
||||
|
52
help.go
52
help.go
@ -1,8 +1,17 @@
|
||||
package cli
|
||||
|
||||
import "fmt"
|
||||
import "os"
|
||||
import "log"
|
||||
|
||||
import "text/tabwriter"
|
||||
import "text/template"
|
||||
|
||||
type HelpData struct {
|
||||
Name string
|
||||
Usage string
|
||||
Commands []Command
|
||||
Version string
|
||||
}
|
||||
|
||||
var HelpCommand = Command{
|
||||
Name: "help",
|
||||
@ -11,14 +20,43 @@ var HelpCommand = Command{
|
||||
Action: ShowHelp,
|
||||
}
|
||||
|
||||
var helpTemplate = `NAME:
|
||||
{{.Name}} - {{.Usage}}
|
||||
|
||||
USAGE:
|
||||
{{.Name}} [global-options] COMMAND [command-options]
|
||||
|
||||
VERSION:
|
||||
{{.Version}}
|
||||
|
||||
COMMANDS:
|
||||
{{range .Commands}}{{.Name}}{{ "\t" }}{{.Usage}}
|
||||
{{end}}
|
||||
|
||||
`
|
||||
|
||||
var ShowHelp = func(name string) {
|
||||
|
||||
data := HelpData{
|
||||
Name,
|
||||
Usage,
|
||||
Commands,
|
||||
Version,
|
||||
}
|
||||
|
||||
w := tabwriter.NewWriter(os.Stdout, 0, 8, 1, '\t', 0)
|
||||
fmt.Printf("Usage: %v [global-options] COMMAND [command-options]\n\n", Name)
|
||||
if Commands != nil {
|
||||
fmt.Printf("The most commonly used %v commands are:\n", Name)
|
||||
for _, c := range Commands {
|
||||
fmt.Fprintln(w, " "+c.Name+"\t"+c.Usage)
|
||||
}
|
||||
t := template.Must(template.New("help").Parse(helpTemplate))
|
||||
err := t.Execute(w, data)
|
||||
w.Flush()
|
||||
if err != nil {
|
||||
log.Println("executing template:", err)
|
||||
}
|
||||
// fmt.Printf("Usage: %v [global-options] COMMAND [command-options]\n\n", Name)
|
||||
// if Commands != nil {
|
||||
// fmt.Printf("The most commonly used %v commands are:\n", Name)
|
||||
// for _, c := range Commands {
|
||||
// fmt.Fprintln(w, " "+c.Name+"\t"+c.Usage)
|
||||
// }
|
||||
// w.Flush()
|
||||
// }
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user