Documentation
¶
Overview ¶
Package flag adds flags to commands.
Index ¶
- Variables
- func AnyString[T ~string](s string) (T, error)
- func NotEmpty[T ~string](s string) (T, error)
- func NotEmptyTrimmed[T ~string](s string) (T, error)
- type Binder
- type Binding
- type Parser
- type RegisterModifier
- type RegisterOptions
- type SliceParser
- type SliceTargetParser
- type SliceValue
- type TargetParser
- type Value
Constants ¶
This section is empty.
Variables ¶
var ErrParser = errors.New("cannot parse parameter")
ErrParser can be used when implementations of Parser fail.
Functions ¶
func AnyString ¶
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 ¶
NotEmpty is a commonly used Parser and requires that the given string is not empty without trimming whitespace.
func NotEmptyTrimmed ¶
NotEmptyTrimmed is a commonly used Parser and requires that the given string is not empty after whitespace was trimmed.
Types ¶
type Binder ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
Bool provides a boolean flag. It is a simple wrapper around String, and relies on the support by [Value.IsBoolFlag].