Documentation
¶
Index ¶
- Constants
- func CamelToFlag(s, flagDivider string) string
- func EnsureAddr(val reflect.Value) reflect.Value
- func FlagToEnv(s, flagDivider, envDivider string) string
- func IsStringFalsy(s string) bool
- func ParseField(val reflect.Value, fld reflect.StructField, opts *Opts) ([]*Flag, *Positional, bool, error)
- func ParseGroup(ctx *FieldContext) ([]*Flag, []*Positional, error)
- func Scan(data any, handler Handler) error
- type FieldContext
- type Flag
- type FlagFunc
- type Handler
- type OptFunc
- func CopyOpts(opts *Opts) OptFunc
- func DescTag(val string) OptFunc
- func EnvDivider(val string) OptFunc
- func EnvPrefix(val string) OptFunc
- func FlagDivider(val string) OptFunc
- func FlagHandler(val FlagFunc) OptFunc
- func FlagTag(val string) OptFunc
- func Flatten(val bool) OptFunc
- func ParseAll() OptFunc
- func Prefix(val string) OptFunc
- func Validator(val validation.ValidateFunc) OptFunc
- func WithCompleter(name string, completer carapace.CompletionCallback) OptFunc
- func WithVars(vars map[string]string) OptFunc
- type Opts
- type Positional
- type Tag
Constants ¶
const ( // DefaultTagName is the default struct tag name. DefaultTagName = "long" // DefaultShortTagName is the default short struct tag name. DefaultShortTagName = "short" // DefaultEnvTag is the default env struct tag name. DefaultEnvTag = "env" )
Variables ¶
This section is empty.
Functions ¶
func CamelToFlag ¶
CamelToFlag transforms s from CamelCase to flag-case.
func EnsureAddr ¶
EnsureAddr we get the address of a given value.
func IsStringFalsy ¶
IsStringFalsy returns true if a string is considered "falsy" (empty, "false", "no", or "0").
func ParseField ¶
func ParseField(val reflect.Value, fld reflect.StructField, opts *Opts) ([]*Flag, *Positional, bool, error)
ParseField is the updated version of ParseField that returns the new parser.Positional type.
func ParseGroup ¶
func ParseGroup(ctx *FieldContext) ([]*Flag, []*Positional, error)
ParseGroup is the updated version of ParseGroup that returns the new parser.Positional type.
Types ¶
type FieldContext ¶
FieldContext holds all the relevant information about a struct field being parsed.
func NewFieldContext ¶
func NewFieldContext(val reflect.Value, fld reflect.StructField, opts *Opts) (*FieldContext, error)
NewFieldContext creates and populates a new context for a given field. It handles the parsing of the struct tag.
type Flag ¶
type Flag struct {
Name string // name as it appears on command line
Short string // optional short name
EnvNames []string // OS Environment-based names
Usage string // help message
Placeholder string // placeholder for the flag's value
Value values.Value // value as set
RValue reflect.Value // Type value to use for completions
DefValue []string // default value (as text); for usage message
Hidden bool // Flag hidden from descriptions/completions
Deprecated bool // Not in use anymore
Required bool // If true, the option _must_ be specified on the command line.
Persistent bool // If true, the flag is persistent on its command.
Choices []string // If non empty, only a certain set of values is allowed for an option.
OptionalValue []string // The optional value of the option.
Negatable *string // If not nil, a negation flag is generated with the given prefix.
Separator *string // Custom separator for slice values.
MapSeparator *string // Custom separator for map values.
XORGroup []string // Mutually exclusive flag groups.
ANDGroup []string // "AND" flag groups.
Tag *Tag // Field tag
}
Flag structure might be used by cli/flag libraries for their flag generation.
type FlagFunc ¶
FlagFunc is a generic function that can be applied to each value that will end up being a flags *Flag, so that users can perform more arbitrary operations on each.
type OptFunc ¶
type OptFunc func(opt *Opts)
OptFunc sets values in Opts structure.
func EnvDivider ¶
EnvDivider sets custom divider for environment variables.
func EnvPrefix ¶
EnvPrefix sets prefix that will be applied for all environment variables (if they are not marked as ~).
func FlagDivider ¶
FlagDivider sets custom divider for flags. It is dash by default. e.g. "flag-name".
func FlagHandler ¶
FlagHandler sets the handler function for flags.
func ParseAll ¶
func ParseAll() OptFunc
ParseAll orders the parser to generate a flag for all struct fields.
func Validator ¶
func Validator(val validation.ValidateFunc) OptFunc
Validator sets validator function for flags.
func WithCompleter ¶
func WithCompleter(name string, completer carapace.CompletionCallback) OptFunc
WithCompleter adds a custom completer function to the parser options.
type Opts ¶
type Opts struct {
// DescTag is the struct tag name for description.
DescTag string
// FlagTag is the struct tag name for flag.
FlagTag string
// Delimiter for flags.
FlagDivider string
// Delimiter for environment variables.
EnvDivider string
// Prefix for all flags.
Prefix string
// Prefix for all environment variables.
EnvPrefix string
// Flatten anonymous structures.
Flatten bool
// ParseAllFields specifies either to parse all fields or only tagged ones.
ParseAll bool
// Validator is the validation function for flags.
Validator validation.ValidateFunc
// FlagFunc is a generic function that can be applied to each flag.
FlagFunc FlagFunc
// Vars is a map of variables that can be used for expansion.
Vars map[string]string
// GlobalVars is a map of variables that are applied globally.
GlobalVars map[string]string
// Completers is a map of custom completer functions.
Completers map[string]carapace.CompletionCallback
}
Opts specifies different parsing options.
type Positional ¶
type Positional struct {
Name string
Usage string
Value reflect.Value
PValue values.Value
Min int
Max int
Index int // The position in the struct (n'th struct field used as a slot)
StartMin int
StartMax int
Passthrough bool
Tag *Tag
Validator func(val string) error
}
Positional represents a positional argument defined in a struct field.
func ParsePositionalStruct ¶
ParsePositionalStruct scans a struct value that is tagged as a legacy `positional-args:"true"` container and returns a slice of parsed Positional arguments.
type Tag ¶
Tag is a map of struct tags.
func GetFieldTag ¶
func GetFieldTag(field reflect.StructField) (*Tag, bool, error)
GetFieldTag returns the struct tags for a given field.