Documentation
¶
Index ¶
- Constants
- Variables
- func DecodeBase64Bytes(raw string) ([]byte, error)
- func DecodeHexBytes(raw string) ([]byte, error)
- func InferDecodeHooks(c *cobra.Command, name string, typ reflect.Type) bool
- func InferDefineHooks(c *cobra.Command, name, short, descr string, structField reflect.StructField, ...) bool
- func RegisterDecodeHook(typ reflect.Type, annotationName string, hook mapstructure.DecodeHookFunc)
- func RegisterUserDecodeHook(typ reflect.Type, decode DecodeHookFunc)
- func RestoreDecodeRegistries(snap DecodeRegistrySnapshot)
- func StoreCompletionHookFuncDirect(c *cobra.Command, flagName string, complete CompleteHookFunc)
- func StoreDecodeHookFuncDirect(c *cobra.Command, flagname string, decode DecodeHookFunc, target reflect.Type)
- func StringToBoolSliceHookFunc() mapstructure.DecodeHookFunc
- func StringToCSVStringSliceHookFunc() mapstructure.DecodeHookFunc
- func StringToDurationSliceHookFunc() mapstructure.DecodeHookFunc
- func StringToEnumHookFunc[E ~string](values map[E][]string) mapstructure.DecodeHookFunc
- func StringToIPMaskHookFunc() mapstructure.DecodeHookFunc
- func StringToIPSliceHookFunc() mapstructure.DecodeHookFunc
- func StringToInt64MapHookFunc() mapstructure.DecodeHookFunc
- func StringToIntEnumHookFunc[E ~int | ~int8 | ~int16 | ~int32 | ~int64](values map[E][]string) mapstructure.DecodeHookFunc
- func StringToIntMapHookFunc() mapstructure.DecodeHookFunc
- func StringToIntSliceHookFunc(sep string) mapstructure.DecodeHookFunc
- func StringToNamedBytesHookFunc(targetType reflect.Type, decode func(string) ([]byte, error)) mapstructure.DecodeHookFunc
- func StringToRawBytesHookFunc() mapstructure.DecodeHookFunc
- func StringToSlogLevelHookFunc() mapstructure.DecodeHookFunc
- func StringToStringMapHookFunc() mapstructure.DecodeHookFunc
- func StringToUintSliceHookFunc() mapstructure.DecodeHookFunc
- func WrapWithEnumValues(v pflag.Value, values []string) pflag.Value
- type CompleteHookFunc
- type DecodeHookFunc
- type DecodeRegistrySnapshot
- type DefineHookFunc
- func DefineBase64BytesHookFunc() DefineHookFunc
- func DefineBoolSliceHookFunc() DefineHookFunc
- func DefineDurationSliceHookFunc() DefineHookFunc
- func DefineHexBytesHookFunc() DefineHookFunc
- func DefineIPHookFunc() DefineHookFunc
- func DefineIPMaskHookFunc() DefineHookFunc
- func DefineIPNetHookFunc() DefineHookFunc
- func DefineIPSliceHookFunc() DefineHookFunc
- func DefineInt64MapHookFunc() DefineHookFunc
- func DefineIntEnumHookFunc[E ~int | ~int8 | ~int16 | ~int32 | ~int64](values map[E][]string) DefineHookFunc
- func DefineIntMapHookFunc() DefineHookFunc
- func DefineRawBytesHookFunc() DefineHookFunc
- func DefineSlogLevelHookFunc() DefineHookFunc
- func DefineStringEnumHookFunc[E ~string](values map[E][]string) DefineHookFunc
- func DefineStringMapHookFunc() DefineHookFunc
- func DefineTimeDurationHookFunc() DefineHookFunc
- func DefineUintSliceHookFunc() DefineHookFunc
Constants ¶
const (
FlagDecodeHookAnnotation = "leodido/structcli/flag-decode-hooks"
)
Variables ¶
var AnnotationToDecodeHookRegistry map[string]mapstructure.DecodeHookFunc
AnnotationToDecodeHookRegistry maps annotation names to decode hook functions
var DecodeHookRegistry = map[reflect.Type]decodingAnnotation{ reflect.TypeFor[time.Duration](): { "StringToTimeDurationHookFunc", mapstructure.StringToTimeDurationHookFunc(), }, reflect.TypeFor[[]time.Duration](): { "StringToDurationSliceHookFunc", StringToDurationSliceHookFunc(), }, reflect.TypeFor[[]bool](): { "StringToBoolSliceHookFunc", StringToBoolSliceHookFunc(), }, reflect.TypeFor[[]uint](): { "StringToUintSliceHookFunc", StringToUintSliceHookFunc(), }, reflect.TypeFor[map[string]string](): { "StringToStringMapHookFunc", StringToStringMapHookFunc(), }, reflect.TypeFor[map[string]int](): { "StringToIntMapHookFunc", StringToIntMapHookFunc(), }, reflect.TypeFor[map[string]int64](): { "StringToInt64MapHookFunc", StringToInt64MapHookFunc(), }, reflect.TypeFor[net.IP](): { "StringToIPHookFunc", mapstructure.StringToIPHookFunc(), }, reflect.TypeFor[net.IPMask](): { "StringToIPMaskHookFunc", StringToIPMaskHookFunc(), }, reflect.TypeFor[net.IPNet](): { "StringToIPNetHookFunc", mapstructure.StringToIPNetHookFunc(), }, reflect.TypeFor[[]net.IP](): { "StringToIPSliceHookFunc", StringToIPSliceHookFunc(), }, reflect.TypeFor[slog.Level](): { "StringToSlogLevelHookFunc", StringToSlogLevelHookFunc(), }, reflect.TypeFor[[]string](): { "StringToCSVStringSliceHookFunc", StringToCSVStringSliceHookFunc(), }, reflect.TypeFor[[]int](): { "StringToIntSliceHookFunc", StringToIntSliceHookFunc(","), }, reflect.TypeFor[[]uint8](): { "StringToRawBytesHookFunc", StringToRawBytesHookFunc(), }, }
DecodeHookRegistry maps types to their decode hook metadata.
var DefineHookRegistry = map[reflect.Type]DefineHookFunc{ reflect.TypeFor[time.Duration](): DefineTimeDurationHookFunc(), reflect.TypeFor[[]time.Duration](): DefineDurationSliceHookFunc(), reflect.TypeFor[[]bool](): DefineBoolSliceHookFunc(), reflect.TypeFor[[]uint](): DefineUintSliceHookFunc(), reflect.TypeFor[map[string]string](): DefineStringMapHookFunc(), reflect.TypeFor[map[string]int](): DefineIntMapHookFunc(), reflect.TypeFor[map[string]int64](): DefineInt64MapHookFunc(), reflect.TypeFor[net.IP](): DefineIPHookFunc(), reflect.TypeFor[net.IPMask](): DefineIPMaskHookFunc(), reflect.TypeFor[net.IPNet](): DefineIPNetHookFunc(), reflect.TypeFor[[]net.IP](): DefineIPSliceHookFunc(), reflect.TypeFor[slog.Level](): DefineSlogLevelHookFunc(), reflect.TypeFor[[]uint8](): DefineRawBytesHookFunc(), }
DefineHookRegistry maps types to their flag definition functions.
Functions ¶
func DecodeBase64Bytes ¶ added in v0.18.0
DecodeBase64Bytes decodes a base64-encoded string into bytes.
func DecodeHexBytes ¶ added in v0.18.0
DecodeHexBytes decodes a hex-encoded string into bytes.
func InferDefineHooks ¶
func InferDefineHooks(c *cobra.Command, name, short, descr string, structField reflect.StructField, fieldValue reflect.Value) bool
InferDefineHooks looks up a define hook for the field's type and registers the flag if found. Falls back to the string-keyed registry for types whose reflect.Type is unavailable in this package.
func RegisterDecodeHook ¶ added in v0.16.0
func RegisterDecodeHook(typ reflect.Type, annotationName string, hook mapstructure.DecodeHookFunc)
RegisterDecodeHook registers a decode hook for a custom type. It updates both DecodeHookRegistry and AnnotationToDecodeHookRegistry. Panics on duplicate type or annotation name.
func RegisterUserDecodeHook ¶ added in v0.18.0
func RegisterUserDecodeHook(typ reflect.Type, decode DecodeHookFunc)
RegisterUserDecodeHook wraps a user-provided DecodeHookFunc into a mapstructure.DecodeHookFunc and registers it for the given type. The wrapper filters by target type and source kind (string).
func RestoreDecodeRegistries ¶ added in v0.16.0
func RestoreDecodeRegistries(snap DecodeRegistrySnapshot)
RestoreDecodeRegistries replaces both decode registries from a snapshot.
func StoreCompletionHookFuncDirect ¶ added in v0.18.0
func StoreCompletionHookFuncDirect(c *cobra.Command, flagName string, complete CompleteHookFunc)
StoreCompletionHookFuncDirect registers a typed completion hook for a flag.
func StoreDecodeHookFuncDirect ¶ added in v0.18.0
func StoreDecodeHookFuncDirect(c *cobra.Command, flagname string, decode DecodeHookFunc, target reflect.Type)
StoreDecodeHookFuncDirect registers a typed decode hook for a flag.
func StringToBoolSliceHookFunc ¶ added in v0.12.0
func StringToBoolSliceHookFunc() mapstructure.DecodeHookFunc
func StringToCSVStringSliceHookFunc ¶ added in v0.12.0
func StringToCSVStringSliceHookFunc() mapstructure.DecodeHookFunc
StringToCSVStringSliceHookFunc converts textual input into []string using CSV semantics.
func StringToDurationSliceHookFunc ¶ added in v0.12.0
func StringToDurationSliceHookFunc() mapstructure.DecodeHookFunc
func StringToEnumHookFunc ¶ added in v0.16.0
func StringToEnumHookFunc[E ~string](values map[E][]string) mapstructure.DecodeHookFunc
StringToEnumHookFunc creates a decode hook that converts string values to a ~string enum type during configuration unmarshaling. It supports case-insensitive matching and aliases.
func StringToIPMaskHookFunc ¶ added in v0.12.0
func StringToIPMaskHookFunc() mapstructure.DecodeHookFunc
StringToIPMaskHookFunc converts textual input into net.IPMask.
func StringToIPSliceHookFunc ¶ added in v0.12.0
func StringToIPSliceHookFunc() mapstructure.DecodeHookFunc
StringToIPSliceHookFunc converts textual and list input into []net.IP.
func StringToInt64MapHookFunc ¶ added in v0.12.0
func StringToInt64MapHookFunc() mapstructure.DecodeHookFunc
func StringToIntEnumHookFunc ¶ added in v0.16.0
func StringToIntEnumHookFunc[E ~int | ~int8 | ~int16 | ~int32 | ~int64](values map[E][]string) mapstructure.DecodeHookFunc
StringToIntEnumHookFunc creates a decode hook that converts string values to an integer-based enum type during configuration unmarshaling. It supports case-insensitive matching and aliases.
func StringToIntMapHookFunc ¶ added in v0.12.0
func StringToIntMapHookFunc() mapstructure.DecodeHookFunc
func StringToIntSliceHookFunc ¶
func StringToIntSliceHookFunc(sep string) mapstructure.DecodeHookFunc
StringToIntSliceHookFunc creates a decode hook that converts comma-separated string values to []int slices during configuration unmarshaling.
func StringToNamedBytesHookFunc ¶ added in v0.12.0
func StringToNamedBytesHookFunc(targetType reflect.Type, decode func(string) ([]byte, error)) mapstructure.DecodeHookFunc
StringToNamedBytesHookFunc converts encoded textual input into a named []byte type.
func StringToRawBytesHookFunc ¶ added in v0.12.0
func StringToRawBytesHookFunc() mapstructure.DecodeHookFunc
StringToRawBytesHookFunc converts plain textual input into raw []byte.
func StringToSlogLevelHookFunc ¶ added in v0.9.1
func StringToSlogLevelHookFunc() mapstructure.DecodeHookFunc
StringToSlogLevelHookFunc creates a decode hook that converts string values to slog.Level types during configuration unmarshaling.
func StringToStringMapHookFunc ¶ added in v0.12.0
func StringToStringMapHookFunc() mapstructure.DecodeHookFunc
func StringToUintSliceHookFunc ¶ added in v0.12.0
func StringToUintSliceHookFunc() mapstructure.DecodeHookFunc
Types ¶
type CompleteHookFunc ¶ added in v0.11.0
type CompleteHookFunc func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective)
CompleteHookFunc defines the completion hook for a struct field flag.
type DecodeHookFunc ¶
type DecodeRegistrySnapshot ¶ added in v0.16.0
type DecodeRegistrySnapshot struct {
// contains filtered or unexported fields
}
DecodeRegistrySnapshot holds opaque copies of both decode registries for test isolation.
func SnapshotDecodeRegistries ¶ added in v0.16.0
func SnapshotDecodeRegistries() DecodeRegistrySnapshot
SnapshotDecodeRegistries returns a deep copy of both decode registries.
type DefineHookFunc ¶
type DefineHookFunc func(name, descr string, structField reflect.StructField, fieldValue reflect.Value) (pflag.Value, string)
DefineHookFunc defines how to create a flag for a custom type.
It receives flag metadata and struct field information and must return a pflag.Value that knows how to set the underlying field's value, along with an optional enhanced description for the flag's usage message.
The short flag name is not passed here. The caller registers the returned pflag.Value with the appropriate short name via VarP.
func DefineBase64BytesHookFunc ¶ added in v0.12.0
func DefineBase64BytesHookFunc() DefineHookFunc
func DefineBoolSliceHookFunc ¶ added in v0.12.0
func DefineBoolSliceHookFunc() DefineHookFunc
func DefineDurationSliceHookFunc ¶ added in v0.12.0
func DefineDurationSliceHookFunc() DefineHookFunc
func DefineHexBytesHookFunc ¶ added in v0.12.0
func DefineHexBytesHookFunc() DefineHookFunc
func DefineIPHookFunc ¶ added in v0.12.0
func DefineIPHookFunc() DefineHookFunc
func DefineIPMaskHookFunc ¶ added in v0.12.0
func DefineIPMaskHookFunc() DefineHookFunc
func DefineIPNetHookFunc ¶ added in v0.12.0
func DefineIPNetHookFunc() DefineHookFunc
func DefineIPSliceHookFunc ¶ added in v0.12.0
func DefineIPSliceHookFunc() DefineHookFunc
func DefineInt64MapHookFunc ¶ added in v0.12.0
func DefineInt64MapHookFunc() DefineHookFunc
func DefineIntEnumHookFunc ¶ added in v0.16.0
func DefineIntEnumHookFunc[E ~int | ~int8 | ~int16 | ~int32 | ~int64](values map[E][]string) DefineHookFunc
DefineIntEnumHookFunc creates a DefineHookFunc for a registered integer-based enum. It wraps enumflag/v2 and attaches EnumValuer metadata via WrapWithEnumValues.
func DefineIntMapHookFunc ¶ added in v0.12.0
func DefineIntMapHookFunc() DefineHookFunc
func DefineRawBytesHookFunc ¶ added in v0.12.0
func DefineRawBytesHookFunc() DefineHookFunc
func DefineSlogLevelHookFunc ¶ added in v0.9.1
func DefineSlogLevelHookFunc() DefineHookFunc
DefineSlogLevelHookFunc creates a flag definition function for slog.Level.
It returns an enum flag that implements pflag.Value.
func DefineStringEnumHookFunc ¶ added in v0.16.0
func DefineStringEnumHookFunc[E ~string](values map[E][]string) DefineHookFunc
DefineStringEnumHookFunc creates a DefineHookFunc for a registered ~string enum. The returned hook creates an enumStringValue that validates on Set() and appends "{val1,val2,...}" to the flag description.
func DefineStringMapHookFunc ¶ added in v0.12.0
func DefineStringMapHookFunc() DefineHookFunc
func DefineTimeDurationHookFunc ¶
func DefineTimeDurationHookFunc() DefineHookFunc
func DefineUintSliceHookFunc ¶ added in v0.12.0
func DefineUintSliceHookFunc() DefineHookFunc