Documentation
¶
Index ¶
- func AddOptionally[T any](set ExtendableOptionSet, opts ...T)
- func Filter[T any](set OptionSetProvider) []T
- func Finalize(ctx context.Context, set OptionSetProvider, val FinalizationSet) error
- func GetFrom[T any](set OptionSetProvider) T
- func Implements[T any](o Options) bool
- func RetrieveFrom(set OptionSetProvider, proto interface{}) bool
- func SelectByInterface[T any](set OptionSet, sel ...OptionSelector) []T
- 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 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 Filter ¶
func Filter[T any](set OptionSetProvider) []T
Filter extracts elements of type T from the provided OptionSetProvider and returns them as a slice of T.
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 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 Implements ¶
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 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.
func Select ¶
func Select(set OptionSet, sel OptionSelector) DefaultOptionSet
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
}
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.
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 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
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 givey 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 Validation method in some Options object requiring to forward Validation to a nested OptionSet. Note: If an object implements a Validation method, it is also responsible to handle nested options.