Generalise post-build options.
This commit is contained in:
@@ -41,9 +41,8 @@ type Kong struct {
|
||||
noDefaultHelp bool
|
||||
help func(*Context) error
|
||||
|
||||
// Set temporarily by Options. These are moved into the Model after build().
|
||||
name string
|
||||
description string
|
||||
// Set temporarily by Options. These are applied after build().
|
||||
postBuildOptions []Option
|
||||
}
|
||||
|
||||
// New creates a new Kong parser on grammar.
|
||||
@@ -67,16 +66,13 @@ func New(grammar interface{}, options ...Option) (*Kong, error) {
|
||||
if err != nil {
|
||||
return k, err
|
||||
}
|
||||
if k.name == "" {
|
||||
model.Name = filepath.Base(os.Args[0])
|
||||
} else {
|
||||
model.Name = k.name
|
||||
}
|
||||
if k.description != "" {
|
||||
model.Help = k.description
|
||||
}
|
||||
model.Name = filepath.Base(os.Args[0])
|
||||
k.Model = model
|
||||
|
||||
for _, option := range k.postBuildOptions {
|
||||
option(k)
|
||||
}
|
||||
|
||||
return k, nil
|
||||
}
|
||||
|
||||
|
||||
+12
-8
@@ -23,7 +23,18 @@ func NoDefaultHelp() Option {
|
||||
// Name overrides the application name.
|
||||
func Name(name string) Option {
|
||||
return func(k *Kong) {
|
||||
k.name = name
|
||||
k.postBuildOptions = append(k.postBuildOptions, func(k *Kong) {
|
||||
k.Model.Name = name
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Description sets the application description.
|
||||
func Description(description string) Option {
|
||||
return func(k *Kong) {
|
||||
k.postBuildOptions = append(k.postBuildOptions, func(k *Kong) {
|
||||
k.Model.Help = description
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,13 +58,6 @@ func NamedMapper(name string, mapper Mapper) Option {
|
||||
return func(k *Kong) { k.registry.RegisterName(name, mapper) }
|
||||
}
|
||||
|
||||
// Description sets the application description.
|
||||
func Description(description string) Option {
|
||||
return func(k *Kong) {
|
||||
k.description = description
|
||||
}
|
||||
}
|
||||
|
||||
// Writers overrides the default writers. Useful for testing or interactive use.
|
||||
func Writers(stdout, stderr io.Writer) Option {
|
||||
return func(k *Kong) {
|
||||
|
||||
Reference in New Issue
Block a user