flagutil

package
v0.8.18-rc-2 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2026 License: MPL-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package flagutil provides utilities for parsing and preprocessing complex CLI flags, including byte sizes and durations.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidSyntax         = errors.New("invalid syntax")
	ErrRequiredArgHasDefault = errors.New("required ARG cannot have a default value")
	ErrGlobalArgNotInBase    = errors.New("global ARG can only be set in the base target")
)

These are errors that can be returned from ParseArgArgs.

Functions

func BuildArgMatrix

func BuildArgMatrix(args []string) ([][]string, error)

BuildArgMatrix builds a 2-dimensional slice of arguments that contains all combinations.

func GetArgsCopy

func GetArgsCopy(cmd earthfile.Command) []string

GetArgsCopy returns a deep copy of parsed args.

func IsInParamsForm

func IsInParamsForm(str string) bool

IsInParamsForm determines if the args slice uses params form representation.

func ParseArgArgs

func ParseArgArgs(
	cmd earthfile.Command, isBaseTarget, explicitGlobalFeature bool,
) (cmdopts.Arg, string, *string, error)

ParseArgArgs parses the ARG command's arguments and returns the argOpts, key, value (or nil if missing), or error.

func ParseArgs

func ParseArgs(command string, data any, args []string) ([]string, error)

ParseArgs parses flags and args from a command string.

func ParseArgsCleaned

func ParseArgsCleaned(cmdName string, opts any, args []string) ([]string, error)

ParseArgsCleaned parses arguments properly handling quoting rules.

func ParseArgsWithValueModifier

func ParseArgsWithValueModifier(
	command string, data any, args []string, argumentModFunc ArgumentModFunc,
) ([]string, error)

ParseArgsWithValueModifier parses flags and args from a command string; it accepts an optional argumentModFunc which is called before each flag value is parsed, and allows one to change the value. if the flag value.

func ParseArgsWithValueModifierAndOptions

func ParseArgsWithValueModifierAndOptions(
	command string, data any, args []string, argumentModFunc ArgumentModFunc, parserOptions flags.Options,
) ([]string, error)

ParseArgsWithValueModifierAndOptions is similar to ParseArgsWithValueModifier, but allows changing the parser options.

func ParseArgsWithValueModifierCleaned

func ParseArgsWithValueModifierCleaned(
	cmdName string, opts any, args []string, argumentModFunc ArgumentModFunc,
) ([]string, error)

ParseArgsWithValueModifierCleaned parses args similarly, extracting a value modifier if applicable.

func ParseImageNameAndAttrs

func ParseImageNameAndAttrs(s string) (string, map[string]string, error)

ParseImageNameAndAttrs parses the image name and returns its registry attributes.

func ParseLoad

func ParseLoad(loadStr string) (image string, target string, extraArgs []string, err error)

ParseLoad splits a --load value into the image, target, & extra args. Example: --load my-image=(+target --arg1 foo --arg2=bar).

func ParseParams

func ParseParams(str string) (string, []string, error)

ParseParams turns "(+target --flag=something)" into "+target" and []string{"--flag=something"}, or "\"(+target --flag=something)\"" into "+target" and []string{"--flag=something"}.

func SplitFlagString

func SplitFlagString(values []string) []string

SplitFlagString would return an array of values from the StringSlice, whether it's passed using multiple occuranced of the flag or with the values passed with a command. For example:

--platform linux/amd64 --platform linux/arm64 and --platform "linux/amd64,linux/arm64"

Types

type ArgumentModFunc

type ArgumentModFunc func(flagName string, opt *flags.Option, flagVal *string) (*string, error)

ArgumentModFunc accepts a flagName which corresponds to the long flag name, and a pointer to a flag value. The pointer is nil if no flag was given. the function returns a new pointer set to nil if one wants to pretend as if no value was given, or a pointer to a new value which will be parsed. Note: this was created to allow passing --no-cache=$SOME_VALUE; where we must expand $SOME_VALUE into a true/false value before it is parsed. If this feature is used extensively, then it might be time to completely fork go-flags with a version where we can include control over expansion struct tags.

type ByteSizeValue

type ByteSizeValue uint64

ByteSizeValue represents a byte size that can be parsed from a flag.

func (*ByteSizeValue) Get added in v0.8.18

func (b *ByteSizeValue) Get() any

Get implements the [cli.GenericFlag].Get method.

func (*ByteSizeValue) Set

func (b *ByteSizeValue) Set(s string) error

Set implements the [cli.GenericFlag].Set method.

func (*ByteSizeValue) String

func (b *ByteSizeValue) String() string

String implements fmt.Stringer.

type Duration

type Duration time.Duration

Duration implements cli.GenericFlag methods to support time.Duration with days, e.g. 1d.

func (*Duration) Get added in v0.8.18

func (d *Duration) Get() any

Get implements the [cli.GenericFlag].Get method.

func (*Duration) Set

func (d *Duration) Set(value string) error

Set implements the [cli.GenericFlag].Set method.

func (*Duration) String

func (d *Duration) String() string

String implements the fmt.Stringer.

Jump to

Keyboard shortcuts

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