Convert Hook to an Option.

This commit is contained in:
Alec Thomas
2018-05-27 13:37:17 -04:00
committed by Gerald Kaszuba
parent cf89213e1e
commit afbb431641
4 changed files with 56 additions and 28 deletions
+26 -12
View File
@@ -9,7 +9,7 @@ import (
"text/template"
)
type Hook func(app *Kong, ctx *Context, trace *Trace) error
type HookFunction func(app *Kong, ctx *Context, trace *Trace) error
// Error reported by Kong.
type Error struct{ msg string }
@@ -37,10 +37,11 @@ type Kong struct {
Stdout io.Writer
Stderr io.Writer
help *template.Template
helpContext map[string]interface{}
helpFuncs template.FuncMap
hooks map[reflect.Value]Hook
help *template.Template
helpContext map[string]interface{}
helpFuncs template.FuncMap
hooks map[reflect.Value]HookFunction
noDefaultHelp bool
}
// New creates a new Kong parser into ast.
@@ -52,7 +53,7 @@ func New(ast interface{}, options ...Option) (*Kong, error) {
help: defaultHelpTemplate,
helpContext: map[string]interface{}{},
helpFuncs: template.FuncMap{},
hooks: map[reflect.Value]Hook{},
hooks: map[reflect.Value]HookFunction{},
}
model, err := build(ast)
@@ -66,9 +67,28 @@ func New(ast interface{}, options ...Option) (*Kong, error) {
option(k)
}
if !k.noDefaultHelp {
k.integrateHelp()
}
return k, nil
}
func (k *Kong) integrateHelp() {
helpValue := false
help := &Flag{
Value: Value{
Name: "help",
Help: "Show context-sensitive help.",
Flag: true,
Value: reflect.ValueOf(&helpValue).Elem(),
Decoder: kindDecoders[reflect.Bool],
},
}
k.Model.Flags = append([]*Flag{help}, k.Model.Flags...)
Hook(&helpValue, Help(defaultHelpTemplate, nil))(k)
}
// Trace parses the command-line, validating and collecting matching grammar nodes.
func (k *Kong) Trace(args []string) (*Context, error) {
p := &Context{
@@ -89,12 +109,6 @@ func (k *Kong) Trace(args []string) (*Context, error) {
return p, nil
}
// Hook to execute when a command is encountered.
func (k *Kong) Hook(ptr interface{}, hook Hook) *Kong {
k.hooks[reflect.ValueOf(ptr)] = hook
return k
}
// Parse arguments into target.
//
// The returned "command" is a space separated path to the final selected command, if any. Commands appear as