Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AssignValueToCliFields ¶ added in v1.0.2
func AssignValueToCliFields(v interface{}, prefix string, c ContextReader)
AssignValueToCliFields recursively assigns values from CLI flags to struct fields. It uses reflection to iterate over the struct fields and set their values based on CLI flags. Parameters:
- v: a pointer to the struct to populate
- prefix: prefix for the CLI flags (used for nested structs)
- c: the CLI context containing the flag values
func Parse ¶
func Parse[A any](c ContextReader) A
Parse converts CLI context into a typed configuration struct. It uses reflection to map CLI flags to struct fields based on struct tags. Usage:
type Config struct {
Host string `cli:"host"`
Port int `cli:"port"`
Timeout time.Duration `cli:"timeout"`
Database struct {
Name string `cli:"name"`
User string `cli:"user"`
} `cli-prefix:"db-"`
}
cfg := clix.Parse[Config](ctx)
func ParseCommand ¶ added in v1.1.1
func ParseCommand[A any](cmd CommandReaderV3) A
ParseCommand converts a v3 CommandReaderV3 to a v2 ContextReader and uses the default Parse command, it is just a shorthand for `clix.Parse[Config](clix.V3(cmd))` example
func(ctx context.Context, cmd *cli.Command) error {
config := clix.ParseCommand[Config](cmd)
func ParseContext ¶ added in v1.1.1
func ParseContext[A any](ctx ContextReader) A
ParseContext is an alias for `clix.Parse[Config](ctx) to align with the v3 function name`
Types ¶
type CommandReaderV3 ¶ added in v1.1.0
type CommandReaderV3 interface {
String(name string) string
Int(name string) int
Uint(name string) uint
Bool(name string) bool
Float(name string) float64
Timestamp(name string) time.Time
Duration(name string) time.Duration
StringSlice(name string) []string
IntSlice(name string) []int
UintSlice(name string) []uint
FloatSlice(name string) []float64
}
CommandReaderV3 is the interface that a v3 cli.Command must support in order to be convertible to a v2 ContextReader and the use of clix.Parse[]()
type ContextReader ¶ added in v1.0.2
type ContextReader interface {
String(name string) string
Int(name string) int
Int64(name string) int64
Uint(name string) uint
Uint64(name string) uint64
Bool(name string) bool
Float64(name string) float64
Timestamp(name string) *time.Time
Duration(name string) time.Duration
StringSlice(name string) []string
IntSlice(name string) []int
Int64Slice(name string) []int64
UintSlice(name string) []uint
Uint64Slice(name string) []uint64
Float64Slice(name string) []float64
}
ContextReader defines the interface for reading values from a CLI context. This abstraction allows for easier testing by allowing mock implementations.
func V3 ¶ added in v1.1.0
func V3(cmd CommandReaderV3) ContextReader
V3 converts a v3 CommandReaderV3 to a v2 ContextReader example
func(ctx context.Context, cmd *cli.Command) error {
config := clix.Parse[Config](clix.V3(cmd))