flag

package
v1.6.2 Latest Latest
Warning

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

Go to latest
Published: May 10, 2023 License: BSD-1-Clause Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
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 Parse added in v1.6.2

func Parse()

Parse the default flag set.

func PrettyPrintUsage

func PrettyPrintUsage(w io.Writer, f *Flags) func()

PrettyPrintUsage returns a function that prints the usage of the flag set to the given writer.

func Register added in v1.6.2

func Register(name string, defaultValue any, description string, handler func(v Value) error)

Register a new command to the default flag set.

func RegisterCommand added in v1.6.2

func RegisterCommand(cmd *Command)

Register a new command to the default flag set.

func RegisterPtr added in v1.6.2

func RegisterPtr(ptr, defaultValue any, name string, description string, handler func(v Value) error)

Register a command for a pointer to a value to the default flag set.

Types

type Allowed

type Allowed interface {
	~string | ~bool | ~int64 | ~int | ~uint64 | ~uint | ~float64
}

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.

func (*Command) Execute

func (c *Command) Execute() error

Execute the command, this will call the handler function with the value of the parsed command.

func (*Command) Init

func (c *Command) Init(f *flag.FlagSet)

Initialize the command, this will add the command to the std.FlagSet.

type ErrorHandling

type ErrorHandling flag.ErrorHandling

ErrorHandling is the error handling for the flag set.

type ErrorMap

type ErrorMap map[string]error

ErrorMap is a map of errors. Errors for commands can be stored in this map.

func (ErrorMap) Error

func (e ErrorMap) Error() string

Error returns the error string.

func (ErrorMap) Has

func (e ErrorMap) Has(s string) bool

Has checks if the error map has an error for the given key.

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) Errors

func (f *Flags) Errors() ErrorMap

Return the underlying error map of the flag set.

func (*Flags) HasError

func (f *Flags) HasError() bool

Check if the FlagSet contains errors after parsing.

func (*Flags) Ran

func (f *Flags) Ran() bool

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

func (f *Flags) RegisterCommand(cmd *Command)

Register a new command to the flag set.

func (*Flags) RegisterPtr

func (f *Flags) RegisterPtr(ptr, defaultValue any, name string, description string, handler func(v Value) error)

Register a command for a pointer to a value.

The defaultvalue cannot be nil!

This is for type assertion purposes.

func (*Flags) Run

func (f *Flags) Run() (wasRan bool)

Execute 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.

Jump to

Keyboard shortcuts

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