Don't apply Options twice. This is less confusing.
This commit is contained in:
@@ -40,6 +40,10 @@ type Kong struct {
|
|||||||
registry *Registry
|
registry *Registry
|
||||||
noDefaultHelp bool
|
noDefaultHelp bool
|
||||||
help func(*Context) error
|
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.
|
// New creates a new Kong parser on grammar.
|
||||||
@@ -63,12 +67,16 @@ func New(grammar interface{}, options ...Option) (*Kong, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return k, err
|
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
|
k.Model = model
|
||||||
|
|
||||||
for _, option := range options {
|
|
||||||
option(k)
|
|
||||||
}
|
|
||||||
return k, nil
|
return k, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+2
-9
@@ -6,9 +6,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// Options apply optional changes to the Kong application.
|
// 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)
|
type Option func(k *Kong)
|
||||||
|
|
||||||
// ExitFunction overrides the function used to terminate. This is useful for testing or interactive use.
|
// 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.
|
// Name overrides the application name.
|
||||||
func Name(name string) Option {
|
func Name(name string) Option {
|
||||||
return func(k *Kong) {
|
return func(k *Kong) {
|
||||||
if k.Model != nil {
|
k.name = name
|
||||||
k.Model.Name = name
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,9 +50,7 @@ func NamedMapper(name string, mapper Mapper) Option {
|
|||||||
// Description sets the application description.
|
// Description sets the application description.
|
||||||
func Description(description string) Option {
|
func Description(description string) Option {
|
||||||
return func(k *Kong) {
|
return func(k *Kong) {
|
||||||
if k.Model != nil {
|
k.description = description
|
||||||
k.Model.Help = description
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user