Documentation
¶
Index ¶
- func AddOptionally[T any](set ExtendableOptionSet, opts ...T)
- func Assure[T Options](opts OptionSet, f func() T, check ...matcher.Matcher[T]) error
- func ExecuteLifecycle(ctx context.Context, name string, options OptionSetProvider, run Runner, ...) error
- func Filter[T any](set OptionSetProvider, check ...matcher.Matcher[T]) []T
- func Finalize(ctx context.Context, set OptionSetProvider, val FinalizationSet) error
- func FinalizedOptions[O any](ctx context.Context, opts OptionSet, s FinalizationSet) (O, error)
- func GetFilteredFrom[T any](set OptionSetProvider, filter matcher.Matcher[T]) T
- func GetFilteredFrom2[T any](set OptionSetProvider, filter matcher.Matcher[T]) (T, bool)
- func GetFrom[T any](set OptionSetProvider) T
- func GetFrom2[T any](set OptionSetProvider) (T, bool)
- func Implements[T any](o Options) bool
- func Prepare(ctx context.Context, set OptionSetProvider, val PreparationSet) error
- func PreparedFilteredOptions[O any](ctx context.Context, opts OptionSet, s PreparationSet) ([]O, error)
- func PreparedOptions[O any](ctx context.Context, opts OptionSet, s PreparationSet) (O, error)
- func RetrieveFrom(set OptionSetProvider, proto interface{}) bool
- func SelectByInterface[T any](set OptionSet, sel ...OptionSelector) []T
- func SetAssured[T Options](tgt *T, opts OptionSet, f func() T, check ...matcher.Matcher[T]) error
- func Validate(ctx context.Context, set OptionSetProvider, val ValidationSet) error
- func ValidatedFilteredOptions[O any](ctx context.Context, opts OptionSet, s ValidationSet) ([]O, error)
- func ValidatedOptions[O any](ctx context.Context, opts OptionSet, s ValidationSet) (O, error)
- type DefaultOptionSet
- type ExtendableOptionSet
- type Finalizable
- type FinalizationSet
- type NoOptions
- type OptionSelector
- type OptionSet
- type OptionSetProvider
- type Options
- type OptionsRef
- type Preparable
- type PreparationSet
- type Runner
- type SetBasedOptions
- type SimpleOption
- type Usage
- type Validatable
- type ValidationSet
- type VarPFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddOptionally ¶
func AddOptionally[T any](set ExtendableOptionSet, opts ...T)
func Assure ¶
Assure expects an ExtendableOptionSet and adds an options object provided by a factory method, if it is not yet present. Optional matchers can be used to apply additional filters, for example an instance filter for types applicable for different option names.
func ExecuteLifecycle ¶
func ExecuteLifecycle(ctx context.Context, name string, options OptionSetProvider, run Runner, args ...string) error
ExecuteLifecycle is a default lifecycle executor based on a Runner used to run the application in the run phase.
func Filter ¶
func Filter[T any](set OptionSetProvider, check ...matcher.Matcher[T]) []T
Filter extracts elements of type T from the provided OptionSetProvider and returns them as a slice of T. An optional matcher can be used to additionally filter the result.
func Finalize ¶
func Finalize(ctx context.Context, set OptionSetProvider, val FinalizationSet) error
Finalize checks whether the provided OptionSetProvider or its nested options implement the Finalizable interface and finalizes them. It returns an error if any finalization fails or nil if all finalizations succeed.
func FinalizedOptions ¶
FinalizedOptions provides a finalized Options object of the given type. The type is typically a pointer type to the Options struct.
func GetFilteredFrom ¶
func GetFilteredFrom[T any](set OptionSetProvider, filter matcher.Matcher[T]) T
GetFilteredFrom is like GetFrom, but uses an instance filter to select an instance for a type supporting different instance specific flag names.
func GetFilteredFrom2 ¶
func GetFilteredFrom2[T any](set OptionSetProvider, filter matcher.Matcher[T]) (T, bool)
func GetFrom ¶
func GetFrom[T any](set OptionSetProvider) T
GetFrom retrieves an option of type T from the provided OptionSetProvider and returns it. T is typically a pointer to an option struct of type Options. If an interface type is used the first found implementation is returned. To get all options implementing an interface use Filter.
func GetFrom2 ¶
func GetFrom2[T any](set OptionSetProvider) (T, bool)
func Implements ¶
func Prepare ¶
func Prepare(ctx context.Context, set OptionSetProvider, val PreparationSet) error
Prepare checks whether the provided OptionSetProvider or its nested options implement the Preparable interface and prepares them. It returns an error if any preparation fails or nil if all preparations succeed. Preparation should be called after the setup of an OptionSet and before it is added to a pflag.FlagSet. It is intended to link various options, check whether thay are compatible and prepare dependent default values and/or values helps.
func PreparedFilteredOptions ¶
func PreparedFilteredOptions[O any](ctx context.Context, opts OptionSet, s PreparationSet) ([]O, error)
PreparedFilteredOptions filters an OptionSet for elements of type O and prepares each element against a PreparationSet. Returns the filtered and prepared list of elements or an error if preparation fails. It is typically used with an interface type to get all Options objects implementing this interface.
func PreparedOptions ¶
PreparedOptions provides a validated Options object of the given type. The type is typically a pointer type to the Options struct.
func RetrieveFrom ¶
func RetrieveFrom(set OptionSetProvider, proto interface{}) bool
RetrieveFrom extracts the option for a given target. This might be a
- pointer to a struct implementing the Options interface which will fill the struct with a copy of the options OR
- a pointer to such a pointer which will be filled with the pointer to the actual member of the OptionSet.
func SelectByInterface ¶
func SelectByInterface[T any](set OptionSet, sel ...OptionSelector) []T
func SetAssured ¶
func Validate ¶
func Validate(ctx context.Context, set OptionSetProvider, val ValidationSet) error
Validate checks whether the provided OptionSetProvider or its nested options implement the Validatable interface and validates them. It returns an error if any validation fails or nil if all validations succeed.
func ValidatedFilteredOptions ¶
func ValidatedFilteredOptions[O any](ctx context.Context, opts OptionSet, s ValidationSet) ([]O, error)
ValidatedFilteredOptions filters an OptionSet for elements of type O and validates each element against a ValidationSet. Returns the filtered and validated list of elements or an error if validation fails. It is typically used with an interface type to get all Options objects implementing this interface.
func ValidatedOptions ¶
ValidatedOptions provides a validated Options object of the given type. The type is typically a pointer type to the Options struct.
Types ¶
type DefaultOptionSet ¶
type DefaultOptionSet []Options
DefaultOptionSet defines a slice of Options, representing a basic implementation of an OptionSet. Use it as pointer or use NewOptionSet.
func (*DefaultOptionSet) Add ¶
func (s *DefaultOptionSet) Add(o ...Options) ExtendableOptionSet
func (*DefaultOptionSet) AddFlags ¶
func (s *DefaultOptionSet) AddFlags(fs *pflag.FlagSet)
func (*DefaultOptionSet) AsOptionSet ¶
func (s *DefaultOptionSet) AsOptionSet() OptionSet
func (*DefaultOptionSet) Options ¶
func (s *DefaultOptionSet) Options(yield func(Options) bool)
func (*DefaultOptionSet) Usage ¶
func (s *DefaultOptionSet) Usage() string
type ExtendableOptionSet ¶
type ExtendableOptionSet interface {
OptionSet
Add(o ...Options) ExtendableOptionSet
}
func NewOptionSet ¶
func NewOptionSet(opts ...Options) ExtendableOptionSet
NewOptionSet creates a new ExtendableOptionSet.
func Select ¶
func Select(set OptionSet, sel OptionSelector) ExtendableOptionSet
type Finalizable ¶
type Finalizable interface {
Finalize(ctx context.Context, opts OptionSet, v FinalizationSet) error
}
Finalizable represents a type that can perform a finalization operation with a context and a set of options. Options keeping external state should implement this interface.
type FinalizationSet ¶
type FinalizationSet set.Set[Finalizable]
FinalizationSet is a set of finalization elements that ensures each element is finalized only once within a context. It keeps a set of already finalized objects. If there are cyclic finalizations, only the first call finalizes the object. The order therefore depends on the order of the executed initial finalizations, No error is provided for such cyclic scenarios.
func (FinalizationSet) FinalizeSet ¶
func (s FinalizationSet) FinalizeSet(ctx context.Context, opts OptionSet, set OptionSetProvider) error
FinalizeSet finalizes the OptionSet given by the OptionSetProvider against a more general OptionSet using the provided FinalizationSet. It iterates over the options in the set and applies validation using the provided context and the general OptionSet. If validation fails for any option, the function returns the respective error. This function is intended to be used by Validation method in some Options object requiring to forward Validation to a nested OptionSet. Note: If an object implements a Finalize method, it is also responsible to handle nested options.
type OptionSelector ¶
func Always ¶
func Always() OptionSelector
func And ¶
func And(s ...OptionSelector) OptionSelector
func Never ¶
func Never() OptionSelector
func Not ¶
func Not(s OptionSelector) OptionSelector
func Or ¶
func Or(s ...OptionSelector) OptionSelector
type OptionSet ¶
type OptionSet interface {
Options
OptionSetProvider
Options(yield func(Options) bool)
}
OptionSet is an interface representing a set of Options. It acts as Options and OptionSetProvider and provides a method to iterate over nested Options. There is an intended lifecycle for an OptionSet:
- Composition of the set
- Preparation using Prepare and a PreparationSet to complete the option definition incorporation other options of the final OptionSet.
- Apply to pflag.FlagSet
- Evaluate on current command line options.
- Validation using Validate and a ValidationSet to validate the settings and prepare some state usable by the intended application.
- (Run the application using the options (potentially with the From calls from various options to retrieve them from the OptionSet.
- Finalization using Finalize and a FinalizationSet to cleanup temporary state build during Validation.
type OptionSetProvider ¶
type OptionSetProvider interface {
AsOptionSet() OptionSet
}
OptionSetProvider defines an interface for types capable of providing an OptionSet by implementing the AsOptionSet method.
type Options ¶
Options provides an interface for adding flags to a given pflag.FlagSet using the AddFlags method.
type OptionsRef ¶
type OptionsRef[T Options] struct { Options T // contains filtered or unexported fields }
func NewDefaultOptionsRef ¶
func NewDefaultOptionsRef[T Options](check ...matcher.Matcher[T]) *OptionsRef[T]
NewDefaultOptionsRef like NewOptionsRef, but uses standard object creation.
func NewOptionsRef ¶
func NewOptionsRef[T Options](f func() T, check ...matcher.Matcher[T]) *OptionsRef[T]
NewOptionsRef creates a new dynamic reference to Options added once to an OptionSet on the fly during the preparation phase. This can be used to handled Options shared among other aggregating Options structures.
func (*OptionsRef[T]) AddFlags ¶
func (o *OptionsRef[T]) AddFlags(fs *pflag.FlagSet)
func (*OptionsRef[T]) Prepare ¶
func (o *OptionsRef[T]) Prepare(ctx context.Context, opts OptionSet, v PreparationSet) error
func (*OptionsRef[T]) Unwrap ¶
func (o *OptionsRef[T]) Unwrap() Options
type Preparable ¶
type Preparable interface {
Prepare(ctx context.Context, opts OptionSet, v PreparationSet) error
}
Preparable defines an interface for objects that can be prepared based on an OptionSet within a given context. Optionally, the given context as well as the other options in the OptionSet can also be used to complete the option state. If nested elements are used, they must be prepared using the given PreparationSet to assert they are already prepared before used.
type PreparationSet ¶
type PreparationSet set.Set[Preparable]
PreparationSet is a set of Preparable elements that ensures each element is prepared only once within a context. It keeps a set of already prepared objects. If there are cyclic evaluations, only the first call evaluates the object. The order therefore depends on the order of the executed initial preparations, No error is provided for such cyclic scenarios.
func (PreparationSet) PrepareSet ¶
func (s PreparationSet) PrepareSet(ctx context.Context, opts OptionSet, set OptionSetProvider) error
PrepareSet validates the OptionSet given by an OptionSetProvider against a more general OptionSet using the provided PreparationSet. It iterates over the options in the set and applies preparation using the provided context and the general OptionSet. If preparation fails for any option, the function returns the respective error. This function is intended to be used by Prepare methods in some Options object requiring to forward preparation to a nested OptionSet. Note: If an object implements a Prepare method, it is also responsible to handle nested options.
type Runner ¶
Runner is the interface used to run an aplication based on an option lifecycle management.
type SetBasedOptions ¶
type SetBasedOptions struct {
// contains filtered or unexported fields
}
func (*SetBasedOptions) Add ¶
func (s *SetBasedOptions) Add(o ...Options) OptionSet
func (*SetBasedOptions) AddFlags ¶
func (s *SetBasedOptions) AddFlags(fs *pflag.FlagSet)
func (*SetBasedOptions) AsOptionSet ¶
func (s *SetBasedOptions) AsOptionSet() OptionSet
func (*SetBasedOptions) Options ¶
func (s *SetBasedOptions) Options(yield func(Options) bool)
type SimpleOption ¶
func NewSimpleOption ¶
func NewSimpleOption[V any, T Options](self T, def V, long, short, desc string) SimpleOption[V, T]
func NewSimpleOptionWithSetter ¶
func NewSimpleOptionWithSetter[V any, T Options](self T, setter VarPFunc[V], def V, long, short, desc string) SimpleOption[V, T]
func (*SimpleOption[V, T]) AddFlags ¶
func (o *SimpleOption[V, T]) AddFlags(fs *pflag.FlagSet)
func (*SimpleOption[V, T]) Set ¶
func (o *SimpleOption[V, T]) Set(v V) T
func (*SimpleOption[V, T]) Value ¶
func (o *SimpleOption[V, T]) Value() V
func (*SimpleOption[V, T]) WithDescription ¶
func (o *SimpleOption[V, T]) WithDescription(s string) T
func (*SimpleOption[V, T]) WithNames ¶
func (o *SimpleOption[V, T]) WithNames(l, s string) T
type Usage ¶
type Usage interface {
Usage() string
}
Usage is an interface representing an entity capable of producing a usage string via the Usage method. This info is a length description of the purpose of the option, which can be used in the command description.
type Validatable ¶
type Validatable interface {
Validate(ctx context.Context, opts OptionSet, v ValidationSet) error
}
Validatable defines an interface for objects that can be validated based on an OptionSet within a given context. Optionally, the given context as well as the other options in the OptionSet can also be used to complete the option state. If nested elements are used, they must be validated using the given ValidationSet to assert they are already validated before used.
type ValidationSet ¶
type ValidationSet set.Set[Validatable]
ValidationSet is a set of Validatable elements that ensures each element is validated only once within a context. It keeps a set of already validated objects. If there are cyclic evaluations, only the first call evaluates the object. The order therefore depends on the order of the executed initial validations, No error is provided for such cyclic scenarios.
func (ValidationSet) ValidateSet ¶
func (s ValidationSet) ValidateSet(ctx context.Context, opts OptionSet, set OptionSetProvider) error
ValidateSet validates the OptionSet given by an OptionSetProvider against a more general OptionSet using the provided ValidationSet. It iterates over the options in the set and applies validation using the provided context and the general OptionSet. If validation fails for any option, the function returns the respective error. This function is intended to be used by Validate method in some Options object requiring to forward validation to a nested OptionSet. Note: If an object implements a Validate method, it is also responsible to handle nested options.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
files/app
command
|
|
|
graph/app
command
|
|
|
scheme
Package scheme uses the flagsets.OptionSet handling from package flagsets to handle a command line option based configuration of instances of dynamically registered Type declarations.
|
Package scheme uses the flagsets.OptionSet handling from package flagsets to handle a command line option based configuration of instances of dynamically registered Type declarations. |
|
Package pflags provides additional flag types according to the github.com/spf13/pflag package.
|
Package pflags provides additional flag types according to the github.com/spf13/pflag package. |
|
utils
|
|