Documentation
¶
Overview ¶
Package flag provides a command line flag definition and parsing library.
Flag is intentionally internal so the only interaction is via the Flag option on a command.
Index ¶
- func AddToSet[T flag.Flaggable](set *Set, f *Flag[T]) error
- type Config
- type Flag
- func (f *Flag[T]) Default() string
- func (f *Flag[T]) EnvVar() string
- func (f *Flag[T]) IsSlice() bool
- func (f *Flag[T]) Name() string
- func (f *Flag[T]) NoArgValue() string
- func (f *Flag[T]) Set(str string) error
- func (f *Flag[T]) Short() rune
- func (f *Flag[T]) String() string
- func (f *Flag[T]) Type() string
- func (f *Flag[T]) Usage() string
- type Set
- func (s *Set) All() iter.Seq2[string, Value]
- func (s *Set) Args() []string
- func (s *Set) ExtraArgs() []string
- func (s *Set) Get(name string) (Value, bool)
- func (s *Set) GetShort(short rune) (Value, bool)
- func (s *Set) Help() (value, ok bool)
- func (s *Set) Parse(args []string) error
- func (s *Set) Sorted() iter.Seq2[string, Value]
- func (s *Set) Version() (value, ok bool)
- type Value
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Config ¶ added in v0.18.0
type Config[T flag.Flaggable] struct { // DefaultValue holds the intended default value of the flag. DefaultValue T // EnvVar is the name of an environment variable that may set this flag's value // if the flag is not explicitly provided on the command line. EnvVar string }
Config represents the internal configuration of a Flag.
type Flag ¶
Flag represents a single command line flag.
func New ¶
func New[T flag.Flaggable](p *T, name string, short rune, usage string, config Config[T]) (*Flag[T], error)
New constructs and returns a new Flag.
The name should be as it appears on the command line, e.g. "force" for a --force flag. An optional shorthand can be created by setting short to a single letter value, e.g. "f" to also create a -f version of "force".
func (*Flag[T]) Default ¶ added in v0.18.1
Default returns the default value for the flag, as a string.
If the flag's default is unset (i.e. the zero value for its type), an empty string is returned.
func (*Flag[T]) EnvVar ¶ added in v0.20.0
EnvVar returns the name of the environment variable associated with this flag, or an empty string if none was configured.
func (*Flag[T]) IsSlice ¶ added in v0.20.0
IsSlice reports whether the flag holds a slice value that accumulates repeated calls to Set. Returns false for []byte and net.IP, which are parsed atomically.
func (*Flag[T]) NoArgValue ¶
NoArgValue returns a string representation of value the flag should hold when it is given no arguments on the command line. For example a boolean flag --delete, when passed without arguments implies --delete true.
func (*Flag[T]) Set ¶
Set sets a Flag value based on string input, i.e. parsing from the command line.
func (*Flag[T]) Short ¶
Short returns the shorthand registered for the flag (e.g. -d for --delete), or NoShortHand if the flag should be long only.
func (*Flag[T]) String ¶
String implements fmt.Stringer for a Flag, and also implements the String part of Value, allowing a flag to print itself.
type Set ¶
type Set struct {
// contains filtered or unexported fields
}
Set is a set of command line flags.
func (*Set) All ¶ added in v0.18.1
All returns an iterator through the flags in the flagset in alphabetical order by name.
func (*Set) Args ¶
Args returns a slice of all the non-flag arguments, including any following a "--" terminator.
func (*Set) ExtraArgs ¶
ExtraArgs returns any arguments after a "--" was encountered, or nil if there were none.
func (*Set) Get ¶
Get gets a flag from the Set by name and a boolean to indicate whether it was present.
func (*Set) GetShort ¶
GetShort gets a flag from the Set by it's shorthand and a boolean to indicate whether it was present.
func (*Set) Help ¶
Help returns whether the Set has a boolean flag named "help" and what the value of that flag is currently set to, it simplifies checking for --help.
type Value ¶
type Value interface {
// Name returns the name of the flag.
Name() string
// Short returns the shorthand of the flag (or NoShortHand).
Short() rune
// Usage returns the usage line for the flag.
Usage() string
// String returns the stored value of a flag as a string.
String() string
// Default return the default value of a flag as a string.
//
// If the flag's default is the zero value for it's type,
// an empty string is returned.
Default() string
// EnvVar returns the name of the environment variable associated with this flag,
// or an empty string if none was configured.
EnvVar() string
// NoArgValue returns astring representation of the value of the flag when no
// args are passed (e.g --bool implies --bool true).
NoArgValue() string
// Type returns the string representation of the flag type e.g. "bool".
Type() string
// IsSlice reports whether the flag holds a slice value that accumulates
// repeated calls to Set (e.g. []string, []int). Note that []byte and net.IP
// are NOT slice flags in this sense — they are parsed atomically.
IsSlice() bool
// Set sets the stored value of a flag by parsing the string "str".
Set(str string) error
}
Value is an interface representing a Flag value that can be set from the command line.