Help us shape the future of Go! Take the 2026 Go Developer Survey

flags

package
v1.21.41 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2026 License: Apache-2.0 Imports: 17 Imported by: 2

Documentation

Index

Constants

View Source
const ARGS = "__args__"

Variables

This section is empty.

Functions

func AssignFieldValue

func AssignFieldValue(structValue reflect.Value, fv *FlagValue, args []string, isStdinAvailable bool) error

AssignFieldValue assigns a flag value to a struct field using the field path

func GetFieldByPath

func GetFieldByPath(structValue reflect.Value, fieldPath []int) reflect.Value

GetFieldByPath navigates through embedded structs using field indices

func PopulateFromRequest

func PopulateFromRequest(optsValue reflect.Value, fields []FieldInfo, flagMap map[string]string, args []string) error

PopulateFromRequest fills optsValue by parsing flagMap and args directly, without touching any shared pflag pointer. This is the entry point used by the HTTP data-func dispatcher in cobra_command.go.

The CLI path keeps the existing pflag-backed pipeline (AssignFieldValue); only the HTTP path benefits from per-request isolation. Concurrent requests allocate their own optsValue, so no locking is required.

Precedence per field: explicit flagMap[FlagName] → args (for the IsArgs field) → DefaultValue → type zero value. Stdin is intentionally skipped on this path — HTTP requests have no terminal.

Types

type FieldInfo

type FieldInfo struct {
	FieldName    string
	FieldPath    []int // Indices to navigate from root struct to this field
	FieldType    reflect.Type
	FlagName     string
	Help         string
	DefaultValue string
	ShortFlag    string
	Required     bool
	Hidden       bool
	IsStdin      bool
	IsArgs       bool
}

FieldInfo contains metadata about a struct field for flag parsing

func ParseStructFields

func ParseStructFields(structType reflect.Type) ([]FieldInfo, error)

ParseStructFields recursively parses struct fields including embedded structs and returns a flat list of all fields with flag tags

type FlagValue

type FlagValue struct {
	FieldName      string
	FieldPath      []int // Field indices for navigating embedded structs
	FieldType      reflect.Type
	DefaultValue   string
	Required       bool
	IsStdin        bool
	IsArgs         bool
	StringPtr      *string
	IntPtr         *int
	BoolPtr        *bool
	StringSlicePtr *[]string
	IntSlicePtr    *[]int
	DurationPtr    *duration.Duration
	TimePtr        *time.Time
}

FlagValue holds information about a flag and its binding

func BindFlag

func BindFlag(cmd *cobra.Command, info FieldInfo) *FlagValue

BindFlag creates and binds a flag to a Cobra command based on field info

Jump to

Keyboard shortcuts

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