Don't apply Options twice. This is less confusing.

This commit is contained in:
Alec Thomas
2018-06-08 13:41:23 +10:00
parent 149a83692d
commit c535900861
2 changed files with 14 additions and 13 deletions
+12 -4
View File
@@ -40,6 +40,10 @@ type Kong struct {
registry *Registry
noDefaultHelp bool
help func(*Context) error
// Set temporarily by Options. These are moved into the Model after build().
name string
description string
}
// New creates a new Kong parser on grammar.
@@ -63,12 +67,16 @@ func New(grammar interface{}, options ...Option) (*Kong, error) {
if err != nil {
return k, err
}
model.Name = filepath.Base(os.Args[0])
if k.name == "" {
model.Name = filepath.Base(os.Args[0])
} else {
model.Name = k.name
}
if k.description != "" {
model.Help = k.description
}
k.Model = model
for _, option := range options {
option(k)
}
return k, nil
}
+2 -9
View File
@@ -6,9 +6,6 @@ import (
)
// Options apply optional changes to the Kong application.
//
// Note that Options are applied twice: once just prior to the grammar is constructed and once after. In the
// former case, Kong.Application will be nil.
type Option func(k *Kong)
// ExitFunction overrides the function used to terminate. This is useful for testing or interactive use.
@@ -26,9 +23,7 @@ func NoDefaultHelp() Option {
// Name overrides the application name.
func Name(name string) Option {
return func(k *Kong) {
if k.Model != nil {
k.Model.Name = name
}
k.name = name
}
}
@@ -55,9 +50,7 @@ func NamedMapper(name string, mapper Mapper) Option {
// Description sets the application description.
func Description(description string) Option {
return func(k *Kong) {
if k.Model != nil {
k.Model.Help = description
}
k.description = description
}
}