Don't apply Options twice. This is less confusing.
This commit is contained in:
@@ -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
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user