Documentation ¶
Index ¶
- func ChecksumFlagSet(flagSet *flag.FlagSet, flagFilter func(flag *flag.Flag) bool) []byte
- func CommaStringToSlice(input string) []string
- func IsFlagDynamic(f *flag.Flag) bool
- func Parse[T DynValueTypes](input string) (val T, err error)
- func ReadFileFlags(flagSet *flag.FlagSet) error
- func ValidateDynFloat64Range(fromInclusive float64, toInclusive float64) func(float64) error
- func ValidateDynInt64Range(fromInclusive int64, toInclusive int64) func(int64) error
- func ValidateDynSetMinElements[T comparable](count int) func(Set[T]) error
- func ValidateDynSliceMinElements[T any](count int) func([]T) error
- func ValidateDynStringMatchesRegex(matcher *regexp.Regexp) func(string) error
- func ValidateDynStringSetMinElements(count int) func(Set[string]) error
- func ValidateDynStringSliceMinElements(count int) func([]string) error
- func ValidateRange[T constraints.Ordered](fromInclusive T, toInclusive T) func(T) error
- type DynBoolValue
- type DynDurationValue
- type DynFloat64Value
- type DynInt64Value
- type DynJSONValue
- type DynStringSetValue
- type DynStringSliceValue
- type DynStringValue
- type DynValue
- func (d *DynValue[T]) Get() T
- func (d *DynValue[T]) PostSet(val T) error
- func (d *DynValue[T]) Set(rawInput string) error
- func (d *DynValue[T]) String() string
- func (d *DynValue[T]) Type() string
- func (d *DynValue[T]) WithFileFlag(defaultPath string) (*DynValue[T], *FileReadValue)
- func (d *DynValue[T]) WithInputMutator(mutator func(inp string) string) *DynValue[T]
- func (d *DynValue[T]) WithNotifier(notifier func(oldValue T, newValue T)) *DynValue[T]
- func (d *DynValue[T]) WithSyncNotifier(notifier func(oldValue T, newValue T)) *DynValue[T]
- func (d *DynValue[T]) WithValidator(validator func(T) error) *DynValue[T]
- func (d *DynValue[T]) WithValueMutator(mutator func(inp T) T) *DynValue[T]
- type DynValueTypes
- type DynamicBoolValueTag
- type DynamicFlagValue
- type DynamicFlagValueTag
- type DynamicJSONFlagValue
- type FileReadValue
- type Set
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ChecksumFlagSet ¶
ChecksumFlagSet will generate a FNV of the *set* values in a FlagSet.
func CommaStringToSlice ¶ added in v1.34.0
CommaStringToSlice converts a coma separated string to a slice.
func IsFlagDynamic ¶
IsFlagDynamic returns whether the given Flag has been created in a Dynamic mode.
func Parse ¶ added in v1.33.0
func Parse[T DynValueTypes](input string) (val T, err error)
Parse converts from string to our supported types (it's the beginning of the missing generics strconv.Parse[T]).
func ReadFileFlags ¶
ReadFileFlags parses the flagset to discover all "fileread" flags and evaluates them.
By reading and evaluating it means: attempts to read the file and set the value.
func ValidateDynFloat64Range ¶
ValidateDynFloat64Range returns a validator that checks if the float value is in range.
func ValidateDynInt64Range ¶
ValidateDynInt64Range returns a validator function that checks if the integer value is in range.
func ValidateDynSetMinElements ¶ added in v1.33.0
func ValidateDynSetMinElements[T comparable](count int) func(Set[T]) error
ValidateDynSetMinElements validates that the given Set has at least x elements.
func ValidateDynSliceMinElements ¶ added in v1.33.0
ValidateDynSliceMinElements validates that the given Set has at least x elements.
func ValidateDynStringMatchesRegex ¶
ValidateDynStringMatchesRegex returns a validator function that checks all flag's values against regex.
func ValidateDynStringSliceMinElements ¶
ValidateDynStringSliceMinElements validates that the given string slice has at least x elements.
func ValidateRange ¶ added in v1.33.0
func ValidateRange[T constraints.Ordered](fromInclusive T, toInclusive T) func(T) error
ValidateRange returns a validator that checks if the value is in the given range.
Types ¶
type DynBoolValue ¶ added in v1.6.2
type DynBoolValue struct { DynamicBoolValueTag DynValue[bool] }
DynStringSetValue implements a dynamic set of strings.
type DynDurationValue ¶
func DynDuration ¶
func DynDuration(flagSet *flag.FlagSet, name string, value time.Duration, usage string) *DynDurationValue
DynDuration creates a `Flag` that represents `time.Duration` which is safe to change dynamically at runtime.
type DynFloat64Value ¶
func DynFloat64 ¶
DynFloat64 creates a `Flag` that represents `float64` which is safe to change dynamically at runtime.
type DynInt64Value ¶
type DynJSONValue ¶
type DynJSONValue struct { DynValue[interface{}] // contains filtered or unexported fields }
DynJSONValue is a flag-related JSON struct value wrapper.
func DynJSON ¶
func DynJSON(flagSet *flag.FlagSet, name string, value interface{}, usage string) *DynJSONValue
DynJSON creates a `Flag` that is backed by an arbitrary JSON which is safe to change dynamically at runtime. The `value` must be a pointer to a struct that is JSON (un)marshallable. New values based on the default constructor of `value` type will be created on each update.
func (*DynJSONValue) IsJSON ¶
func (d *DynJSONValue) IsJSON() bool
IsJSON always return true (method is present for the DynamicJSONFlagValue interface tagging).
func (*DynJSONValue) Set ¶
func (d *DynJSONValue) Set(rawInput string) error
Set updates the value from a string representation in a thread-safe manner. This operation may return an error if the provided `input` doesn't parse, or the resulting value doesn't pass an optional validator. If a notifier is set on the value, it will be invoked in a separate go-routine.
func (*DynJSONValue) String ¶
func (d *DynJSONValue) String() string
String returns the canonical string representation of the type.
type DynStringSetValue ¶
DynStringSetValue implements a dynamic set of strings.
func DynStringSet ¶
func DynStringSet(flagSet *flag.FlagSet, name string, value []string, usage string) *DynStringSetValue
DynStringSet creates a `Flag` that represents `map[string]struct{}` which is safe to change dynamically at runtime. Unlike `pflag.StringSlice`, consecutive sets don't append to the slice, but override it.
func (*DynStringSetValue) Contains ¶
func (d *DynStringSetValue) Contains(val string) bool
Contains returns whether the specified string is in the flag.
func (*DynStringSetValue) String ¶
func (d *DynStringSetValue) String() string
String represents the canonical representation of the type.
type DynStringSliceValue ¶
func DynStringSlice ¶
func DynStringSlice(flagSet *flag.FlagSet, name string, value []string, usage string) *DynStringSliceValue
DynStringSlice creates a `Flag` that represents `[]string` which is safe to change dynamically at runtime. Unlike `pflag.StringSlice`, consecutive sets don't append to the slice, but override it.
type DynStringValue ¶
type DynValue ¶ added in v1.33.0
type DynValue[T any] struct { DynamicFlagValueTag // contains filtered or unexported fields }
func (*DynValue[T]) Get ¶ added in v1.33.0
func (d *DynValue[T]) Get() T
Get retrieves the value in a thread-safe manner.
func (*DynValue[T]) Set ¶ added in v1.33.0
Set updates the value from a string representation in a thread-safe manner. This operation may return an error if the provided `input` doesn't parse, or the resulting value doesn't pass an optional validator. If a notifier is set on the value, it will be invoked in a separate go-routine.
func (*DynValue[T]) String ¶ added in v1.33.0
String returns the canonical string representation of the type.
func (*DynValue[T]) WithFileFlag ¶ added in v1.33.0
func (d *DynValue[T]) WithFileFlag(defaultPath string) (*DynValue[T], *FileReadValue)
WithFileFlag adds an companion <name>_path flag that allows this value to be read from a file with dflag.ReadFileFlags.
This is useful for reading large JSON files as flags. If the companion flag's value (whether default or overwritten) is set to empty string, nothing is read.
Flag value reads are subject to notifiers and validators.
func (*DynValue[T]) WithInputMutator ¶ added in v1.33.0
WithInputMutator changes the default input string processing (TrimSpace).
func (*DynValue[T]) WithNotifier ¶ added in v1.33.0
WithNotifier adds a function is called every time a new value is successfully set. Each notifier is executed in a new go-routine.
func (*DynValue[T]) WithSyncNotifier ¶ added in v1.33.0
WithSyncNotifier adds a function is called synchronously every time a new value is successfully set.
func (*DynValue[T]) WithValidator ¶ added in v1.33.0
WithValidator adds a function that checks values before they're set. Any error returned by the validator will lead to the value being rejected. Validators are executed on the same go-routine as the call to `Set`.
func (*DynValue[T]) WithValueMutator ¶ added in v1.33.0
WithValueMutator adds a function that changes the value of a flag as needed.
type DynValueTypes ¶ added in v1.33.0
type DynValueTypes interface { bool | time.Duration | float64 | int64 | string | []string | Set[string] }
DynValueTypes are the types currently supported by Parse[T] and thus by Dyn[T]. DynJSON is special.
type DynamicBoolValueTag ¶ added in v1.33.0
type DynamicBoolValueTag struct{}
func (*DynamicBoolValueTag) IsBoolFlag ¶ added in v1.33.0
func (*DynamicBoolValueTag) IsBoolFlag() bool
type DynamicFlagValue ¶
type DynamicFlagValue interface {
IsDynamicFlag() bool
}
DynamicFlagValue interface is a tag to know if a type is dynamic or not.
type DynamicFlagValueTag ¶
type DynamicFlagValueTag struct{}
DynamicFlagValueTag is a struct all dynamic flag inherit for marking they are dynamic.
func (*DynamicFlagValueTag) IsDynamicFlag ¶
func (*DynamicFlagValueTag) IsDynamicFlag() bool
IsDynamicFlag always returns true.
type DynamicJSONFlagValue ¶
type DynamicJSONFlagValue interface {
IsJSON() bool
}
DynamicJSONFlagValue is a tag interface for JSON dynamic flags.
type FileReadValue ¶
type FileReadValue struct { DynamicFlagValueTag // contains filtered or unexported fields }
FileReadValue is a flag that wraps another flag and makes it readable from a local file in the filesystem.
func FileReadFlag ¶
func FileReadFlag(flagSet *flag.FlagSet, parentFlagName string, defaultFilePath string) *FileReadValue
FileReadFlag creates a `Flag` that allows you to pass a flag.
If defaultFilePath is non empty, the dflag.ReadFileFlags will expect the file to be there.
func (*FileReadValue) Set ¶
func (f *FileReadValue) Set(path string) error
Set updates the value from a string representation of the file path.
func (*FileReadValue) String ¶
func (f *FileReadValue) String() string
type Set ¶ added in v1.33.0
type Set[T comparable] map[T]struct{}
func SetFromSlice ¶ added in v1.33.0
func SetFromSlice[T comparable](items []T) Set[T]
SetFromSlice constructs a Set from a slice.