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 Choices[T comparable](choices []T) func(v any) (bool, string)
- func Max[T cmp.Ordered](maxValue T) func(v any) (bool, string)
- func MaxStringLen(length int) func(v any) (bool, string)
- func Min[T cmp.Ordered](minValue T) func(v any) (bool, string)
- func MinStringLen(length int) func(v any) (bool, string)
- 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 Range[T cmp.Ordered](minValue, maxValue T) func(v any) (bool, string)
- type Context
- func (ctx *Context) AddFlag(flagType FlagType, name, shortName string, valuePtr any, usage string, ...) *Flag
- func (ctx *Context) AddFlagPtr(flag *Flag) *Flag
- func (ctx *Context) AddSubCommand(name, description string, handler func()) *Subcommand
- func (ctx *Context) Parse(argv []string) (*Subcommand, error)
- func (ctx *Context) PrintUsage(w io.Writer)
- type Flag
- type FlagType
- type Subcommand
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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"
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(flagType FlagType, name, shortName string, valuePtr any, usage string, required bool, validator ...func(any) (bool, string)) *Flag
Add a flag to the context.
func (*Context) AddFlagPtr ¶ added in v0.1.6
Add a flag to a subcommand.
func (*Context) AddSubCommand ¶
func (ctx *Context) AddSubCommand(name, description string, handler func()) *Subcommand
Add a subcommand to the context.
func (*Context) Parse ¶
func (ctx *Context) Parse(argv []string) (*Subcommand, error)
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 Flag ¶ added in v0.1.0
type Flag struct { FlagType FlagType Name string ShortName string Value any // pointer to default value. Will be populated by Parse. Usage string Required bool Validator func(any) (bool, string) }
A Flag as parsed from the command line.
type Subcommand ¶ added in v0.1.2
type Subcommand struct { Handler func() // Subcommand callback handler. Will be invoked by user if it matches. // contains filtered or unexported fields }
A Subcommand. It can have its own flags.
func (*Subcommand) AddFlag ¶ added in v0.1.2
func (cmd *Subcommand) AddFlag(flagType FlagType, name, shortName string, valuePtr any, usage string, required bool, validator ...func(any) (bool, string)) *Subcommand
Add a flag to a subcommand.
func (*Subcommand) AddFlagPtr ¶ added in v0.1.6
func (cmd *Subcommand) AddFlagPtr(flag *Flag) *Subcommand
Add a flag to a subcommand.
func (*Subcommand) PrintUsage ¶ added in v0.1.2
func (cmd *Subcommand) PrintUsage(w io.Writer)