Add hook support.
This commit is contained in:
committed by
Gerald Kaszuba
parent
f60fe01f08
commit
cf89213e1e
@@ -1,7 +1,6 @@
|
||||
package kong
|
||||
|
||||
import (
|
||||
"io"
|
||||
"text/template"
|
||||
)
|
||||
|
||||
@@ -18,20 +17,20 @@ usage: {{.Name}}
|
||||
|
||||
var defaultHelpTemplate = template.Must(template.New("help").Parse(defaultHelp))
|
||||
|
||||
// WriteHelp to w.
|
||||
//
|
||||
// If w is nil, the default stdout writer will be used.
|
||||
//
|
||||
// If args are provided, help will be written in the context o
|
||||
func (k *Kong) WriteHelp(w io.Writer, args ...interface{}) error {
|
||||
if w == nil {
|
||||
w = k.stdout
|
||||
// Help returns a Hook that will display help and exit.
|
||||
func Help(tmpl *template.Template, tmplctx map[string]interface{}) Hook {
|
||||
return func(app *Kong, ctx *Context, trace *Trace) error {
|
||||
merged := map[string]interface{}{
|
||||
"Application": app.Model,
|
||||
}
|
||||
for k, v := range tmplctx {
|
||||
merged[k] = v
|
||||
}
|
||||
err := tmpl.Execute(app.Stdout, merged)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
app.Exit(0)
|
||||
return nil
|
||||
}
|
||||
ctx := map[string]interface{}{
|
||||
"Application": k.Model,
|
||||
}
|
||||
for k, v := range k.helpContext {
|
||||
ctx[k] = v
|
||||
}
|
||||
return k.help.Execute(w, ctx)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user