parser

package
v0.11.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 1, 2025 License: BSD-3-Clause Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
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

func CamelToFlag(s, flagDivider string) string

CamelToFlag transforms s from CamelCase to flag-case.

func EnsureAddr

func EnsureAddr(val reflect.Value) reflect.Value

EnsureAddr we get the address of a given value.

func FlagToEnv

func FlagToEnv(s, flagDivider, envDivider string) string

FlagToEnv transforms s from flag-case to CAMEL_CASE.

func IsStringFalsy

func IsStringFalsy(s string) bool

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.

func Scan

func Scan(data any, handler Handler) error

Scan actually scans the type, recursively if needed.

Types

type FieldContext

type FieldContext struct {
	Value reflect.Value
	Field reflect.StructField
	Tag   *Tag
	Opts  *Opts
}

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

type FlagFunc func(flag string, tag *Tag, val reflect.Value) error

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 Handler

type Handler func(val reflect.Value, field *reflect.StructField) (bool, error)

Handler is a function that can be applied to a struct field.

type OptFunc

type OptFunc func(opt *Opts)

OptFunc sets values in Opts structure.

func CopyOpts

func CopyOpts(opts *Opts) OptFunc

CopyOpts returns a copy of the given options.

func DescTag

func DescTag(val string) OptFunc

DescTag sets custom description tag. It is "desc" by default.

func EnvDivider

func EnvDivider(val string) OptFunc

EnvDivider sets custom divider for environment variables.

func EnvPrefix

func EnvPrefix(val string) OptFunc

EnvPrefix sets prefix that will be applied for all environment variables (if they are not marked as ~).

func FlagDivider

func FlagDivider(val string) OptFunc

FlagDivider sets custom divider for flags. It is dash by default. e.g. "flag-name".

func FlagHandler

func FlagHandler(val FlagFunc) OptFunc

FlagHandler sets the handler function for flags.

func FlagTag

func FlagTag(val string) OptFunc

FlagTag sets custom flag tag. It is "flag" be default.

func Flatten

func Flatten(val bool) OptFunc

Flatten set flatten option.

func ParseAll

func ParseAll() OptFunc

ParseAll orders the parser to generate a flag for all struct fields.

func Prefix

func Prefix(val string) OptFunc

Prefix sets prefix that will be applied for all flags (if they are not marked as ~).

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.

func WithVars

func WithVars(vars map[string]string) OptFunc

WithVars adds a map of variables for expansion.

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.

func DefOpts

func DefOpts() *Opts

DefOpts returns the default parsing options.

func (*Opts) Apply

func (o *Opts) Apply(optFuncs ...OptFunc) *Opts

Apply applies the given options to the current options.

func (*Opts) Copy

func (o *Opts) Copy() *Opts

Copy returns a copy of the 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

func ParsePositionalStruct(val reflect.Value, stag *Tag, opts *Opts) ([]*Positional, error)

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

type Tag map[string][]string

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.

func (*Tag) Get

func (t *Tag) Get(key string) (string, bool)

Get returns the value of a tag.

func (*Tag) GetMany

func (t *Tag) GetMany(key string) []string

GetMany returns the values of a tag.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL