Documentation ¶
Overview ¶
A simple flag package for go. Support for --flag value and -flag values. Built in subcommand support and flag validation. Author: Dr. Abiira Nathan. Date: Sept 25. 2023 License: MIT License
Index ¶
- func Bool(name, shortName string, value bool, usage string, required ...bool) *gflag
- func DirPath(name, shortName string, value string, usage string, required ...bool) *gflag
- func Duration(name, shortName string, value time.Duration, usage string, required ...bool) *gflag
- func Email(name, shortName string, value string, usage string, required ...bool) *gflag
- func FilePath(name, shortName string, value string, usage string, required ...bool) *gflag
- func Float32(name, shortName string, value float32, usage string, required ...bool) *gflag
- func Float64(name, shortName string, value float64, usage string, required ...bool) *gflag
- func GenerateCompletions(ctx Getter, cmd Getter)
- func HostPortPair(name, shortName string, value string, usage string, required ...bool) *gflag
- func IP(name, shortName string, value net.IP, usage string, required ...bool) *gflag
- func Int(name, shortName string, value int, usage string, required ...bool) *gflag
- func Int64(name, shortName string, value int64, usage string, required ...bool) *gflag
- func IntSlice(name, shortName string, value []int, usage string, required ...bool) *gflag
- func MAC(name, shortName string, value net.HardwareAddr, usage string, required ...bool) *gflag
- func ParseBool(value string) (bool, error)
- func ParseDirPath(value string) (string, error)
- func ParseDuration(value string) (time.Duration, error)
- func ParseEmail(value string) (string, error)
- func ParseFilePath(value string) (string, error)
- func ParseFloat32(value string) (float32, error)
- func ParseFloat64(value string) (float64, error)
- func ParseHostPort(value string) (string, error)
- func ParseIP(value string) (net.IP, error)
- func ParseInt(value string) (int, error)
- func ParseInt64(value string) (int64, error)
- func ParseIntSlice(value string) ([]int, error)
- func ParseMAC(value string) (net.HardwareAddr, error)
- func ParseRune(value string) (rune, error)
- func ParseStringSlice(value string) ([]string, error)
- func ParseTime(value string) (time.Time, error)
- func ParseUUID(value string) (uuid.UUID, error)
- func ParseUrl(value string) (*url.URL, error)
- func Rune(name, shortName string, value rune, usage string, required ...bool) *gflag
- func String(name, shortName string, value string, usage string, required ...bool) *gflag
- func StringSlice(name, shortName string, value []string, usage string, required ...bool) *gflag
- func SubCommand(name, description string, handler Handler) *subcommand
- func Time(name, shortName string, value time.Time, usage string, required ...bool) *gflag
- func URL(name, shortName string, value *url.URL, usage string, required ...bool) *gflag
- func UUID(name, shortName string, value uuid.UUID, usage string, required ...bool) *gflag
- type Context
- func (ctx *Context) AddFlag(flag *gflag) *gflag
- func (ctx *Context) AddSubCommand(cmd *subcommand) *subcommand
- func (ctx *Context) GenerateBashCompletions(w io.Writer)
- func (ctx *Context) GenerateZshCompletions(w io.Writer)
- func (ctx *Context) Get(name string) any
- func (ctx *Context) GetBool(name string) bool
- func (ctx *Context) GetDirPath(name string) string
- func (ctx *Context) GetDuration(name string) time.Duration
- func (ctx *Context) GetEmail(name string) string
- func (ctx *Context) GetFilePath(name string) string
- func (ctx *Context) GetFloat32(name string) float32
- func (ctx *Context) GetFloat64(name string) float64
- func (ctx *Context) GetHostPortPair(name string) string
- func (ctx *Context) GetIP(name string) net.IP
- func (ctx *Context) GetInt(name string) int
- func (ctx *Context) GetInt64(name string) int64
- func (ctx *Context) GetIntSlice(name string) []int
- func (ctx *Context) GetMAC(name string) net.HardwareAddr
- func (ctx *Context) GetRune(name string) rune
- func (ctx *Context) GetString(name string) string
- func (ctx *Context) GetStringSlice(name string) []string
- func (ctx *Context) GetTime(name string) time.Time
- func (ctx *Context) GetURL(name string) *url.URL
- func (ctx *Context) GetUUID(name string) uuid.UUID
- func (ctx *Context) Parse(argv []string) (*subcommand, error)
- func (ctx *Context) PrintUsage(w io.Writer)
- type Getter
- type Handler
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GenerateCompletions ¶
func HostPortPair ¶
func MAC ¶
func MAC(name, shortName string, value net.HardwareAddr, usage string, required ...bool) *gflag
func ParseDirPath ¶
Resolve dirname from value and check that it exists.
func ParseDuration ¶
Parse a string to a duration. Uses time.ParseDuration. Supported units are "ns", "us" (or "µs"), "ms", "s", "m", "h". e.g 1h30m, 1h, 1m30s, 1m, 1m30s, 1ms, 1us, 1ns
func ParseEmail ¶
parse email from string with mail.Parse
func ParseFilePath ¶
Resolve absolute file path and check that it exists.
func ParseHostPort ¶
parse host:port pair from value An empty string is considered a valid host. :) e.g ":8000" is a valid host-port pair.
func ParseIntSlice ¶
Parse a comma-seperated string into a slice of ints.
func ParseStringSlice ¶
Parse a comma-seperated string into a slice of strings.
func ParseTime ¶
Parse a string to a time.Time. Uses time.Parse. Supported formats are: "2006-01-02T15:04 MST"
func StringSlice ¶
func SubCommand ¶
Create a new subcommand.
Types ¶
type Context ¶
type Context struct {
// contains filtered or unexported fields
}
Global flag context. Stores global flags and subcommands.
func (*Context) AddFlag ¶
func (ctx *Context) AddFlag(flag *gflag) *gflag
Add a flag to the context.
func (*Context) AddSubCommand ¶
func (ctx *Context) AddSubCommand(cmd *subcommand) *subcommand
Add a subcommand to the context.
func (*Context) GenerateBashCompletions ¶
GenerateBashCompletions generates a Bash completion script for goflag.
func (*Context) GenerateZshCompletions ¶
GenerateZshCompletion generates a Zsh completion script for goflag.
func (*Context) GetBool ¶
Get the value of a flag as a bool. Panics if the flag is not found or the value is not a bool.
func (*Context) GetDirPath ¶
Get the value of a flag as a string. Panics if the flag is not found or the value is not a string.
func (*Context) GetDuration ¶
Get the value of a flag as a time.Duration. Panics if the flag is not found or the value is not a time.Duration.
func (*Context) GetEmail ¶
Get the value of a flag as a string. Panics if the flag is not found or the value is not a string.
func (*Context) GetFilePath ¶
Get the value of a flag as a string. Panics if the flag is not found or the value is not a string.
func (*Context) GetFloat32 ¶
Get the value of a flag as a float32. Panics if the flag is not found or the value is not a float32.
func (*Context) GetFloat64 ¶
Get the value of a flag as a float64. Panics if the flag is not found or the value is not a float64.
func (*Context) GetHostPortPair ¶
Get the value of a flag as a string. Panics if the flag is not found or the value is not a string.
func (*Context) GetIP ¶
Get the value of a flag as a net.IP. Panics if the flag is not found or the value is not a net.IP.
func (*Context) GetInt ¶
Get the value of a flag as a int. Panics if the flag is not found or the value is not a int.
func (*Context) GetInt64 ¶
Get the value of a flag as a int64. Panics if the flag is not found or the value is not a int64.
func (*Context) GetIntSlice ¶
Get the value of a flag as a []int. Panics if the flag is not found or the value is not a []int.
func (*Context) GetMAC ¶
func (ctx *Context) GetMAC(name string) net.HardwareAddr
Get the value of a flag as a net.HardwareAddr. Panics if the flag is not found or the value is not a net.HardwareAddr.
func (*Context) GetRune ¶
Get the value of a flag as a rune. Panics if the flag is not found or the value is not a rune.
func (*Context) GetString ¶
Get the value of a flag as a string. Panics if the flag is not found or the value is not a string.
func (*Context) GetStringSlice ¶
Get the value of a flag as a []string. Panics if the flag is not found or the value is not a []string.
func (*Context) GetTime ¶
Get the value of a flag as a time.Time. Panics if the flag is not found or the value is not a time.Time.
func (*Context) GetURL ¶
Get the value of a flag as a *url.URL. Panics if the flag is not found or the value is not a *url.URL.
func (*Context) GetUUID ¶
Get the value of a flag as a uuid.UUID. Panics if the flag is not found or the value is not a uuid.UUID.
func (*Context) Parse ¶
Parse the flags and subcommands. args should be os.Args. The first argument is ignored as it is the program name.
Populates the values of the flags and also finds the matching subcommand. Returns the matching subcommand.
func (*Context) PrintUsage ¶
Print the usage to the writer. Called by Parse if the help flag is present. help flag is automatically added to the context. May be called as help, --help, -h, --h
Help for a given subcommand can be printed by passing the subcommand name as the glag --subcommand or -c. e.g. --help -c greet
type Getter ¶
type Getter interface { Get(name string) any GetString(name string) string GetInt(name string) int GetInt64(name string) int64 GetFloat32(name string) float32 GetFloat64(name string) float64 GetBool(name string) bool GetStringSlice(name string) []string GetRune(name string) rune GetTime(name string) time.Time GetDuration(name string) time.Duration GetIntSlice(name string) []int }