flags

package
v1.21.60 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2026 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const ARGS = "__args__"

Variables

This section is empty.

Functions

func AllowedValues added in v1.21.57

func AllowedValues(flag *pflag.Flag) []string

AllowedValues returns the schema values attached by SetAllowedValues.

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, options ...RequestOption) 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.

func SetAllowedValues added in v1.21.57

func SetAllowedValues(flag *pflag.Flag, values ...string)

SetAllowedValues attaches the values accepted by a flag for schema consumers.

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
	// CLIFileRead and RPCFileRead opt the field into `@file`/`@url` expansion,
	// from `clicky:"cli-file-read"` and `clicky:"rpc-file-read"`. They are
	// separate because the two paths read from different machines: the CLI
	// reads the operator's disk, the RPC handler reads the server's. See
	// fileaccess.go.
	CLIFileRead bool
	RPCFileRead bool
	// Enum is the field's closed set of accepted values, from an
	// `enum:"a,b,c"` tag. It travels into the OpenAPI parameter and the
	// published action schema, so a front end renders the real choices instead
	// of hardcoding a list that drifts from the server's.
	Enum []string
}

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 FileReadPolicy added in v1.21.60

type FileReadPolicy struct {
	// Enabled is the field's opt-in for this path. False passes the value
	// through untouched.
	Enabled bool
	// Remote marks a value that arrived over the network. It tightens the URL
	// rules, because a server fetching a caller-supplied URL can reach hosts
	// the caller cannot.
	Remote bool
	// contains filtered or unexported fields
}

FileReadPolicy decides whether an `@` value may be expanded, and how strictly.

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
	CLIFileRead    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

type RequestOption added in v1.21.60

type RequestOption func(*populateRequestOptions)

RequestOption customizes request-scoped argument decoding.

func WithRequestContext added in v1.21.60

func WithRequestContext(ctx context.Context) RequestOption

WithRequestContext propagates request cancellation into file and URL expansion.

Jump to

Keyboard shortcuts

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