Added simple man page support

This commit is contained in:
Alexander Rødseth
2013-11-15 12:40:18 +01:00
parent f93652a890
commit 7050f048d1
2 changed files with 59 additions and 5 deletions

36
help.go
View File

@@ -44,6 +44,32 @@ OPTIONS:
{{end}}
`
var ManPageTemplate = `.\" -*-Nroff-*-
.\"
.TH "{{.Name}}" 1 "{{.Compiled.Day}} {{.Compiled.Month}} {{.Compiled.Year}}" "" ""
.SH NAME
{{.Name}} \- {{.Usage}}
.SH SYNOPSIS
.B {{.Name}}
.nf
command {{.Name}} [command options] [arguments...]
.fi
.SH COMMANDS
.nf
{{range .Commands}}{{.Name}}{{with .ShortName}}, {{.}}{{end}}{{ "\t" }}{{.Usage}}
{{end}}
.fi
.SH OPTIONS
.nf
{{range .Flags}}{{.}}
{{end}}
.fi
.SH VERSION
.B {{.Version}}
.SH AUTHOR
.B {{.Name}} was written by {{.Author}} <{{.Email}}>
`
var helpCommand = Command{
Name: "help",
ShortName: "h",
@@ -63,6 +89,11 @@ func ShowAppHelp(c *Context) {
printHelp(AppHelpTemplate, c.App)
}
// Generates (prints) man page for the App
func GenerateManPage(c *Context) {
printHelp(ManPageTemplate, c.App)
}
// Prints help for the given command
func ShowCommandHelp(c *Context, command string) {
for _, c := range c.App.Commands {
@@ -83,7 +114,10 @@ func ShowVersion(c *Context) {
func printHelp(templ string, data interface{}) {
w := tabwriter.NewWriter(os.Stdout, 0, 8, 1, '\t', 0)
t := template.Must(template.New("help").Parse(templ))
t.Execute(w, data)
err := t.Execute(w, data)
if err != nil {
panic(err)
}
w.Flush()
}