Documentation ¶
Overview ¶
Package config allows to build up trees of option sources using the OptionSet interface. The tree is built up by calling the AddSource methods. This function just composes the tree, it does not propagate option declarations. Each node may therefore contain explicitly declared options. The option declarations are propgated up the tree if the it is explicitly completed or added to a flagset (AddToFlagSet) or another OptionSet (AddOptionsToSet). Once propagated the nodes cannot be modified anymore. This propagation may use the local option declarations to decide how those options should be visible in the next upper OptionSet. This package offers support for three kinds of propagations:
- Flat: the optiuons is just propagated as it is
- Prefixed: the option is propagated with a prefix (it changes its name)
- Shared: The option is propagated as it is but shared with other parallel OptionSources
- PrefixedShared: The option is propagated with a prefix. Additionally a shared Option with the flat name is propagated.
Once the the options are finally set by parsing a command line using a pflag.FlagSet the top level OptionSet must be evaluated calling the Evaluate method. This method may do semnatic checks but the main task is to propagate the settings down the tree. For Flat and Prefixed propagations there is always a dedicated single optio target directly filled by the FlagSet. But for shared options there might be multiple targets. This propagation is the task of the Evalue methods.
Index ¶
- Variables
- func IdenityStringMapper(s string) string
- func MapToArguments(prefix string, flags *pflag.FlagSet, data map[string]interface{}) ([]string, error)
- func MergeConfig(yaml []byte, flags *pflag.FlagSet, override bool) error
- func MergeConfigFile(fileName string, flags *pflag.FlagSet, override bool) error
- func MergeFlags(flags *pflag.FlagSet, args []string, override bool) error
- func ParseConfig(bytes []byte, flags *pflag.FlagSet) ([]string, error)
- func Print(log Writer, gap string, opts Options)
- func PrintfWriter(msgfmt string, args ...interface{})
- func ReadConfigFile(name string, flags *pflag.FlagSet) ([]string, error)
- type ArbitraryOption
- func (this *ArbitraryOption) AddToCommand(cmd *cobra.Command)
- func (this *ArbitraryOption) BoolValue() bool
- func (this *ArbitraryOption) Changed() bool
- func (this *ArbitraryOption) DurationValue() time.Duration
- func (this *ArbitraryOption) Flag() *pflag.Flag
- func (this *ArbitraryOption) IntValue() int
- func (this *ArbitraryOption) IsArray() bool
- func (this *ArbitraryOption) IsSlice() bool
- func (this *ArbitraryOption) StringArray() []string
- func (this *ArbitraryOption) StringValue() string
- func (this *ArbitraryOption) UintValue() uint
- func (this *ArbitraryOption) Value() interface{}
- type DefaultOptionSet
- func (this *DefaultOptionSet) AddOption(otype OptionType, target interface{}, name, short string, def interface{}, ...) interface{}
- func (this *DefaultOptionSet) AddOptionsToSet(set OptionSet)
- func (this *DefaultOptionSet) AddRenamedOption(opt *ArbitraryOption, name, short string, desc string) interface{}
- func (this *DefaultOptionSet) AddSource(key string, src OptionSource)
- func (this *DefaultOptionSet) AddToCommand(cmd *cobra.Command)
- func (this *DefaultOptionSet) AddToFlags(flags *pflag.FlagSet)
- func (this *DefaultOptionSet) Complete()
- func (this *DefaultOptionSet) Evaluate() error
- func (this *DefaultOptionSet) GetOption(name string) *ArbitraryOption
- func (this *DefaultOptionSet) GetSource(key string) OptionSource
- func (this *DefaultOptionSet) Name() string
- func (this *DefaultOptionSet) Prefix() string
- func (this *DefaultOptionSet) SetValidator(v OptionValidator)
- func (this *DefaultOptionSet) VisitOptions(f OptionVisitor)
- func (this *DefaultOptionSet) VisitSources(f OptionSourceVisitor)
- type GenericOptionSource
- func (this *GenericOptionSource) AddBoolOption(kind OptionKind, target *bool, name, short string, def bool, desc string) *bool
- func (this *GenericOptionSource) AddDurationOption(kind OptionKind, target *time.Duration, name, short string, def time.Duration, ...) *time.Duration
- func (this *GenericOptionSource) AddIntOption(kind OptionKind, target *int, name, short string, def int, desc string) *int
- func (this *GenericOptionSource) AddOption(kind OptionKind, otype OptionType, target interface{}, name, short string, ...) interface{}
- func (this *GenericOptionSource) AddOptionsToSet(set OptionSet)
- func (this *GenericOptionSource) AddStringArrayOption(kind OptionKind, target *[]string, name, short string, def []string, ...) *[]string
- func (this *GenericOptionSource) AddStringOption(kind OptionKind, target *string, name, short string, def string, desc string) *string
- func (this *GenericOptionSource) AddUintOption(kind OptionKind, target *uint, name, short string, def uint, desc string) *uint
- func (this *GenericOptionSource) Complete()
- func (this *GenericOptionSource) Evaluate() error
- func (this *GenericOptionSource) Flat() OptionSet
- func (this *GenericOptionSource) GetOption(name string) *ArbitraryOption
- func (this *GenericOptionSource) GetSource(name string) OptionSource
- func (this *GenericOptionSource) Name() string
- func (this *GenericOptionSource) Prefix() string
- func (this *GenericOptionSource) Prefixed() OptionSet
- func (this *GenericOptionSource) PrefixedShared() OptionSet
- func (this *GenericOptionSource) Shared() OptionSet
- func (this *GenericOptionSource) VisitOptions(f OptionVisitor)
- func (this *GenericOptionSource) VisitSources(f OptionSourceVisitor)
- type OptionCompleter
- type OptionEvaluator
- type OptionGroup
- type OptionKind
- type OptionSet
- type OptionSetProxy
- func (p OptionSetProxy) AddBoolOption(target *bool, name, short string, def bool, desc string) *bool
- func (p OptionSetProxy) AddDurationOption(target *time.Duration, name, short string, def time.Duration, desc string) *time.Duration
- func (p OptionSetProxy) AddIntOption(target *int, name, short string, def int, desc string) *int
- func (p OptionSetProxy) AddOption(otype OptionType, target interface{}, name, short string, def interface{}, ...) interface{}
- func (p OptionSetProxy) AddStringArrayOption(target *[]string, name, short string, def []string, desc string) *[]string
- func (p OptionSetProxy) AddStringOption(target *string, name, short string, def string, desc string) *string
- func (p OptionSetProxy) AddUintOption(target *uint, name, short string, def uint, desc string) *uint
- type OptionSource
- type OptionSourceGroup
- type OptionSourceSource
- type OptionSourceVisitor
- type OptionType
- type OptionValidator
- type OptionVisitor
- type Options
- type SharedOptionSet
- type StringMapper
- type Validatable
- type Writer
Constants ¶
This section is empty.
Variables ¶
var ( StringOption = optionTypeImpl(tStringOption) StringArrayOption = optionTypeImpl(tStringArrayOption) IntOption = optionTypeImpl(tIntOption) UintOption = optionTypeImpl(tUintOption) BoolOption = optionTypeImpl(tBoolOption) DurationOption = optionTypeImpl(tDurationOption) )
Functions ¶
func IdenityStringMapper ¶
func MapToArguments ¶
func MergeConfig ¶
MergeConfig merges a flagset with yaml or json config data
func MergeConfigFile ¶
MergeConfigFile reads a yaml or json config file and merges it into a given flagset
func MergeFlags ¶
MergeFlags adds arguments to a flag set. Arguments that are already defined in the flag set are ignored if override is set to false If the flag is a slice override appends and does NOT replace the value. The pflag API does not allow to replace a value.
func ParseConfig ¶
ParseConfig parses yaml or json data to a list of equivalent command line arguments
func PrintfWriter ¶
func PrintfWriter(msgfmt string, args ...interface{})
Types ¶
type ArbitraryOption ¶
type ArbitraryOption struct { Name string Description string Default interface{} Target interface{} Type OptionType FlagSet *pflag.FlagSet }
func (*ArbitraryOption) AddToCommand ¶
func (this *ArbitraryOption) AddToCommand(cmd *cobra.Command)
func (*ArbitraryOption) BoolValue ¶
func (this *ArbitraryOption) BoolValue() bool
func (*ArbitraryOption) Changed ¶
func (this *ArbitraryOption) Changed() bool
func (*ArbitraryOption) DurationValue ¶
func (this *ArbitraryOption) DurationValue() time.Duration
func (*ArbitraryOption) Flag ¶
func (this *ArbitraryOption) Flag() *pflag.Flag
func (*ArbitraryOption) IntValue ¶
func (this *ArbitraryOption) IntValue() int
func (*ArbitraryOption) IsArray ¶
func (this *ArbitraryOption) IsArray() bool
func (*ArbitraryOption) IsSlice ¶
func (this *ArbitraryOption) IsSlice() bool
func (*ArbitraryOption) StringArray ¶
func (this *ArbitraryOption) StringArray() []string
func (*ArbitraryOption) StringValue ¶
func (this *ArbitraryOption) StringValue() string
func (*ArbitraryOption) UintValue ¶
func (this *ArbitraryOption) UintValue() uint
func (*ArbitraryOption) Value ¶
func (this *ArbitraryOption) Value() interface{}
type DefaultOptionSet ¶
type DefaultOptionSet struct { OptionSetProxy // contains filtered or unexported fields }
func NewDefaultOptionSet ¶
func NewDefaultOptionSet(name, prefix string) *DefaultOptionSet
func (*DefaultOptionSet) AddOption ¶
func (this *DefaultOptionSet) AddOption(otype OptionType, target interface{}, name, short string, def interface{}, desc string) interface{}
func (*DefaultOptionSet) AddOptionsToSet ¶
func (this *DefaultOptionSet) AddOptionsToSet(set OptionSet)
func (*DefaultOptionSet) AddRenamedOption ¶
func (this *DefaultOptionSet) AddRenamedOption(opt *ArbitraryOption, name, short string, desc string) interface{}
func (*DefaultOptionSet) AddSource ¶
func (this *DefaultOptionSet) AddSource(key string, src OptionSource)
func (*DefaultOptionSet) AddToCommand ¶
func (this *DefaultOptionSet) AddToCommand(cmd *cobra.Command)
func (*DefaultOptionSet) AddToFlags ¶
func (this *DefaultOptionSet) AddToFlags(flags *pflag.FlagSet)
func (*DefaultOptionSet) Complete ¶
func (this *DefaultOptionSet) Complete()
func (*DefaultOptionSet) Evaluate ¶
func (this *DefaultOptionSet) Evaluate() error
func (*DefaultOptionSet) GetOption ¶
func (this *DefaultOptionSet) GetOption(name string) *ArbitraryOption
func (*DefaultOptionSet) GetSource ¶
func (this *DefaultOptionSet) GetSource(key string) OptionSource
func (*DefaultOptionSet) Name ¶
func (this *DefaultOptionSet) Name() string
func (*DefaultOptionSet) Prefix ¶
func (this *DefaultOptionSet) Prefix() string
func (*DefaultOptionSet) SetValidator ¶
func (this *DefaultOptionSet) SetValidator(v OptionValidator)
func (*DefaultOptionSet) VisitOptions ¶
func (this *DefaultOptionSet) VisitOptions(f OptionVisitor)
func (*DefaultOptionSet) VisitSources ¶
func (this *DefaultOptionSet) VisitSources(f OptionSourceVisitor)
type GenericOptionSource ¶
type GenericOptionSource struct {
// contains filtered or unexported fields
}
func NewGenericOptionSource ¶
func NewGenericOptionSource(name, prefix string, descmapper StringMapper) *GenericOptionSource
func (*GenericOptionSource) AddBoolOption ¶
func (this *GenericOptionSource) AddBoolOption(kind OptionKind, target *bool, name, short string, def bool, desc string) *bool
func (*GenericOptionSource) AddDurationOption ¶
func (this *GenericOptionSource) AddDurationOption(kind OptionKind, target *time.Duration, name, short string, def time.Duration, desc string) *time.Duration
func (*GenericOptionSource) AddIntOption ¶
func (this *GenericOptionSource) AddIntOption(kind OptionKind, target *int, name, short string, def int, desc string) *int
func (*GenericOptionSource) AddOption ¶
func (this *GenericOptionSource) AddOption(kind OptionKind, otype OptionType, target interface{}, name, short string, def interface{}, desc string) interface{}
func (*GenericOptionSource) AddOptionsToSet ¶
func (this *GenericOptionSource) AddOptionsToSet(set OptionSet)
func (*GenericOptionSource) AddStringArrayOption ¶
func (this *GenericOptionSource) AddStringArrayOption(kind OptionKind, target *[]string, name, short string, def []string, desc string) *[]string
func (*GenericOptionSource) AddStringOption ¶
func (this *GenericOptionSource) AddStringOption(kind OptionKind, target *string, name, short string, def string, desc string) *string
func (*GenericOptionSource) AddUintOption ¶
func (this *GenericOptionSource) AddUintOption(kind OptionKind, target *uint, name, short string, def uint, desc string) *uint
func (*GenericOptionSource) Complete ¶
func (this *GenericOptionSource) Complete()
func (*GenericOptionSource) Evaluate ¶
func (this *GenericOptionSource) Evaluate() error
func (*GenericOptionSource) Flat ¶
func (this *GenericOptionSource) Flat() OptionSet
func (*GenericOptionSource) GetOption ¶
func (this *GenericOptionSource) GetOption(name string) *ArbitraryOption
func (*GenericOptionSource) GetSource ¶
func (this *GenericOptionSource) GetSource(name string) OptionSource
func (*GenericOptionSource) Name ¶
func (this *GenericOptionSource) Name() string
func (*GenericOptionSource) Prefix ¶
func (this *GenericOptionSource) Prefix() string
func (*GenericOptionSource) Prefixed ¶
func (this *GenericOptionSource) Prefixed() OptionSet
func (*GenericOptionSource) PrefixedShared ¶
func (this *GenericOptionSource) PrefixedShared() OptionSet
func (*GenericOptionSource) Shared ¶
func (this *GenericOptionSource) Shared() OptionSet
func (*GenericOptionSource) VisitOptions ¶
func (this *GenericOptionSource) VisitOptions(f OptionVisitor)
func (*GenericOptionSource) VisitSources ¶
func (this *GenericOptionSource) VisitSources(f OptionSourceVisitor)
type OptionCompleter ¶
type OptionCompleter interface {
Complete()
}
OptionCompleter must be impmented by OptionSource to execute code to complete the option set. It should lock the object for further changes of described options. Basically it must be called by the AddOptionsToSet prior to handling the actual option declarations if nested targets are involved to call AddOptionsToSet on nested targets to complete the actal set.
type OptionEvaluator ¶
type OptionEvaluator interface {
Evaluate() error
}
OptionEvaluator must be implemented by OptionSource to execute code after option parsing is completed check and evaluate the actual settings
type OptionGroup ¶
type OptionGroup interface { Options OptionSourceSource }
OptionGroup is a group set of options and option sources offering access to options
type OptionKind ¶
type OptionKind func(t *GenericOptionSource) OptionSet
type OptionSet ¶
type OptionSet interface { Options OptionSource OptionCompleter OptionEvaluator OptionSourceGroup AddStringOption(target *string, name, short, def string, desc string) *string AddStringArrayOption(target *[]string, name, short string, def []string, desc string) *[]string AddIntOption(target *int, name, short string, def int, desc string) *int AddUintOption(target *uint, name, short string, def uint, desc string) *uint AddBoolOption(target *bool, name, short string, def bool, desc string) *bool AddDurationOption(target *time.Duration, name, short string, def time.Duration, desc string) *time.Duration AddOption(otype OptionType, target interface{}, name, short string, def interface{}, desc string) interface{} AddRenamedOption(opt *ArbitraryOption, name, short string, desc string) interface{} // AddToFlags must complete the actual source if it contains nested // option sources (implements OptionSourceSource) to figure out all relevant // options to add. Afterwards the set should contain all options finally // added to the flag set (queryable by GetOption). AddToFlags(flags *pflag.FlagSet) }
OptionSet is an OptionSource that can be used to add arbitrary options, either directly or by adding further nested OptionSource s.
func Flat ¶
func Flat(t *GenericOptionSource) OptionSet
Flat options are just added as they are to an outer option sink
func Prefixed ¶
func Prefixed(t *GenericOptionSource) OptionSet
Prefixed options are just added with a name prefix to an outer option sink
func PrefixedShared ¶
func PrefixedShared(t *GenericOptionSource) OptionSet
PrefixedShared options are prefixed options that additionally declares a shared flat option on the sink. If the flat option is changed the value is propagated to option sources declaring this shared option. If the perfixed option is set, only this source get the value.
func Shared ¶
func Shared(t *GenericOptionSource) OptionSet
Shared options are flat options that can be shared with other OptionSources (if they are also shared). The value of this option is distributed to all shared option targets.
type OptionSetProxy ¶
type OptionSetProxy func(otype OptionType, target interface{}, name, short string, def interface{}, desc string) interface{}
func (OptionSetProxy) AddBoolOption ¶
func (OptionSetProxy) AddDurationOption ¶
func (OptionSetProxy) AddIntOption ¶
func (OptionSetProxy) AddOption ¶
func (p OptionSetProxy) AddOption(otype OptionType, target interface{}, name, short string, def interface{}, desc string) interface{}
func (OptionSetProxy) AddStringArrayOption ¶
func (OptionSetProxy) AddStringOption ¶
func (OptionSetProxy) AddUintOption ¶
type OptionSource ¶
type OptionSource interface { // AddOptionsToSet must complete the actual source if it contains nested // option sources (implements OptionSourceSource) to figure out all relevant // options to add. This can be done explicitly or implicitly by forwording // the call to nested sources. AddOptionsToSet(set OptionSet) }
OptionSource is the interface used to add arbitrary options to an OptionSet.
type OptionSourceGroup ¶
type OptionSourceGroup interface { OptionSourceSource AddSource(key string, src OptionSource) }
OptionSourceGroup is a modifyable group of OptionSource
type OptionSourceSource ¶
type OptionSourceSource interface { Name() string VisitSources(OptionSourceVisitor) GetSource(key string) OptionSource }
OptionSourceSource is a group of OptionSource
type OptionSourceVisitor ¶
type OptionSourceVisitor func(string, OptionSource) bool
OptionSourceVisitor is used to visit sources in an OptionGroup
type OptionType ¶
type OptionValidator ¶
type OptionVisitor ¶
type OptionVisitor func(*ArbitraryOption) bool
type Options ¶
type Options interface { // GetOption gives access to configured options. Nested OptionSources are only // visible after the OptionSet has been completed. GetOption(name string) *ArbitraryOption VisitOptions(OptionVisitor) }
Options is an element offering named options
type SharedOptionSet ¶
type SharedOptionSet struct { // contains filtered or unexported fields }
func NewSharedOptionSet ¶
func NewSharedOptionSet(name, prefix string, descMapper StringMapper) *SharedOptionSet
func (*SharedOptionSet) AddOptionsToSet ¶
func (this *SharedOptionSet) AddOptionsToSet(set OptionSet)
func (*SharedOptionSet) Evaluate ¶
func (this *SharedOptionSet) Evaluate() error
func (*SharedOptionSet) Unshare ¶
func (this *SharedOptionSet) Unshare(name string)
type StringMapper ¶
type Validatable ¶
type Validatable interface {
SetValidator(validator OptionValidator)
}