Documentation ¶
Index ¶
- Constants
- func Parse()
- func PrettyPrintUsage(w io.Writer, f *Flags) func()
- func Register(name string, defaultValue any, description string, handler func(v Value) error)
- func RegisterCommand(cmd *Command)
- func RegisterPtr(ptr, defaultValue any, name string, description string, ...)
- type Allowed
- type Command
- type ErrorHandling
- type ErrorMap
- type Flags
- func (f *Flags) Errors() ErrorMap
- func (f *Flags) HasError() bool
- func (f *Flags) Ran() bool
- func (f *Flags) Register(name string, defaultValue any, description string, handler func(v Value) error)
- func (f *Flags) RegisterCommand(cmd *Command)
- func (f *Flags) RegisterPtr(ptr, defaultValue any, name string, description string, ...)
- func (f *Flags) Run() (wasRan bool)
- type Value
Constants ¶
const ( // See std.FlagSet. ContinueOnError is the default. ContinueOnError = ErrorHandling(flag.ContinueOnError) ExitOnError = ErrorHandling(flag.ExitOnError) PanicOnError = ErrorHandling(flag.PanicOnError) )
Variables ¶
This section is empty.
Functions ¶
func PrettyPrintUsage ¶
PrettyPrintUsage returns a function that prints the usage of the flag set to the given writer.
func RegisterCommand ¶ added in v1.6.2
func RegisterCommand(cmd *Command)
Register a new command to the default flag set.
Types ¶
type Allowed ¶
Allowed is the allowed types for a command.
This is for type assertion of the flag, but remains unused.
type Command ¶
type Command struct { // Name of the command Name string // Description to show for the command Description string // Default value for the command Default any // Handler func to be called when the command is called Handler func(v Value) error // contains filtered or unexported fields }
A command to add to a flag set.
This command will be parsed and handled by the flag set.
The default value needs to be set!
This is for type assertion of the flag.
Available types are:
- string
- bool
- int64
- int
- uint64
- uint
- float64
func PtrCommand ¶
func PtrCommand[T Allowed](name string, description string, ptr *T, def T, handler func(v Value) error) *Command
PtrCommand creates a command that stores a pointer to a value. The pointer must be non-nil.
type ErrorHandling ¶
type ErrorHandling flag.ErrorHandling
ErrorHandling is the error handling for the flag set.
type Flags ¶
type Flags struct { Commands []*Command FlagSet *flag.FlagSet Info string // contains filtered or unexported fields }
A wrapper for the std.FlagSet.
func NewFlags ¶
func NewFlags(name string, handling ...ErrorHandling) *Flags
Initialize a new flag set.
func (*Flags) Register ¶
func (f *Flags) Register(name string, defaultValue any, description string, handler func(v Value) error)
Register a new command to the flag set.
The defaultvalue cannot be nil!
This is for type assertion purposes.
func (*Flags) RegisterCommand ¶
Register a new command to the flag set.
type Value ¶
type Value interface { // String returns the string representation of the value. // Beware! This will convert the value to a string if it is not a string. String() string // Int returns the value as an integer. Int() int // Uint returns the value as an unsigned integer. Uint() uint // Float returns the value as a float. Float() float64 // Bool returns the value as a boolean. Bool() bool // IsZero returns true if the value is zero. IsZero() bool // Any returns the value as an interface{}. Any() interface{} // Set sets the value. Set(s string) error }
Default value for a flag.