flag

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2025 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package flag adds flags to commands.

Index

Constants

This section is empty.

Variables

View Source
var ErrParser = errors.New("cannot parse parameter")

ErrParser can be used when implementations of Parser fail.

Functions

func AnyString

func AnyString[T ~string](s string) (T, error)

AnyString is a Parser which returns any string as type T. Prefer using NotEmptyTrimmed or NotEmpty to prevent users from accidentally passing empty or non-trimmed flag values.

func NotEmpty

func NotEmpty[T ~string](s string) (T, error)

NotEmpty is a commonly used Parser and requires that the given string is not empty without trimming whitespace.

func NotEmptyTrimmed

func NotEmptyTrimmed[T ~string](s string) (T, error)

NotEmptyTrimmed is a commonly used Parser and requires that the given string is not empty after whitespace was trimmed.

Types

type Binder

type Binder func(flag *pflag.Flag) error

Binder is called during command execution to actually bind the flag. Binding is deferred to ensure that the flag value has been parsed properly.

type Binding

type Binding interface {
	BindTo() Binder
}

Binding allows a command flag to be bound to other sources of configuration. This interface is detected while the flag is registered, see RegisterOptions.AfterRegistration. See for example github.com/neiser/go-nagini/flag/binding.Viper.

type Parser

type Parser[T any] func(s string) (T, error)

Parser is used in String to convert from string to the generic type T. Note that this signature matches standard conversion functions, such as strconv.Atoi. Implementations of Parser may use ErrParser when an error occurs.

type RegisterModifier

type RegisterModifier func(options *RegisterOptions)

RegisterModifier tweak RegisterOptions.

func Persistent

func Persistent() RegisterModifier

Persistent makes the parameter to be registered as a persistent flag. The flag is then inherited to sub commands.

func WithUsage

func WithUsage(usage string, args ...any) RegisterModifier

WithUsage sets the usage string as a RegisterModifier.

type RegisterOptions

type RegisterOptions struct {
	// Name is the flag name (double dash prefix). This should always be set!
	Name string
	// Shorthand is an optional short flag (single dash prefix).
	Shorthand string
	// Usage describes how to use that flag.
	Usage string
	// Deprecated is shown as an alternative for this deprecated flag.
	Deprecated string
	// Hidden hides the flag from the usage help output.
	Hidden bool
	// Required forces this flag to be present.
	Required bool
	// Persistent makes the flag to be registered as a persistent flag.
	// The flag is then inherited to sub commands.
	// See RegisterOptions.SelectFlags
	Persistent bool
}

RegisterOptions are used when registering a flag with a cobra.Command and the pflag.Flag. Some properties here are straightforwardly set, but some others need support in AfterRegistration below.

func (RegisterOptions) AfterRegistration

func (o RegisterOptions) AfterRegistration(cmd *cobra.Command, flag *pflag.Flag, value Value)

AfterRegistration is called after the parameter was registered. Note that 'value' parameter can be nil (happens when registering a simple FlagBool parameter). See command.Command.

func (RegisterOptions) Apply

func (o RegisterOptions) Apply(modifiers ...RegisterModifier) RegisterOptions

Apply applies the given RegisterModifier's to this instance of RegisterOptions.

func (RegisterOptions) SelectFlags

func (o RegisterOptions) SelectFlags(cmd *cobra.Command) *pflag.FlagSet

SelectFlags is used when registering parameters. See command package.

type SliceParser

type SliceParser[T any] func(ss []string) ([]T, error)

SliceParser is a Parser for SliceValue, see Slice and ParseSliceOf.

func ParseSliceOf

func ParseSliceOf[T any](p Parser[T]) SliceParser[T]

ParseSliceOf turns a Parser into a SliceParser, calling the given single element parser for each slice element. It propagates parsing failures of the single element parser and telling at which element the error happened.

type SliceTargetParser

type SliceTargetParser interface {
	ParseAndReplace(ss []string) error
	ParseAndAppend(ss ...string) error
}

SliceTargetParser can be implemented by given target pointers Implementations of SliceTargetParser may use ErrParser when an error occurs.

type SliceValue

type SliceValue interface {
	Value
	pflag.SliceValue
}

SliceValue is constructed from Slice. This interface explicitly declares that this is a pflag.SliceValue, but it must also implement Value to make it registrable as a command flag. See also String and Bool.

func Slice

func Slice[T any, E ~[]T](target *E, parser SliceParser[T]) SliceValue

Slice construct a new Slice flag having multiple values, parsed by given SliceParser. See also ParseSliceOf. If SliceParser is nil, falls back to an implementation of SliceTargetParser on type *E, panics if nothing is found.

type TargetParser

type TargetParser interface {
	Parse(s string) error
}

TargetParser can be implemented by given target pointers Implementations of TargetParser may use ErrParser when an error occurs.

type Value

type Value interface {
	pflag.Value
	// Target returns the target pointer, used as key for looking up flags registered to a command.
	// See [github.com/neiser/go-nagini/command.Command.Flag].
	Target() any
	// IsBoolFlag returns true if the target points to a type with kind boolean.
	// See also Bool.
	IsBoolFlag() bool
}

Value extends pflag.Value with a method to obtain the target pointer of the registered flag and support for boolean-like behavior.

func Bool

func Bool(target *bool) Value

Bool provides a boolean flag. It is a simple wrapper around String, and relies on the support by [Value.IsBoolFlag].

func String

func String[T any](target *T, parser Parser[T]) Value

String constructs a new flag with a generic type as a target. The Parser converts from the given string value to the target type. If the given Parser is nil, falls back to implementation of TargetParser on type *T, panics if nothing is found. See also Bool and Slice.

Directories

Path Synopsis
Package binding binds flags to external configuration systems, such as Viper.
Package binding binds flags to external configuration systems, such as Viper.

Jump to

Keyboard shortcuts

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