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 CLI
- func (c *CLI) Bool(name, shortName string, valuePtr *bool, usage string) *Flag
- func (c *CLI) DirPath(name, shortName string, valuePtr *string, usage string) *Flag
- func (c *CLI) Duration(name, shortName string, valuePtr *time.Duration, usage string) *Flag
- func (c *CLI) Email(name, shortName string, valuePtr *string, usage string) *Flag
- func (c *CLI) FilePath(name, shortName string, valuePtr *string, usage string) *Flag
- func (c *CLI) Float32(name, shortName string, valuePtr *float32, usage string) *Flag
- func (c *CLI) Float64(name, shortName string, valuePtr *float64, usage string) *Flag
- func (c *CLI) GenBashCompletion(w io.Writer)
- func (c *CLI) GenZshCompletion(w io.Writer)
- func (c *CLI) HostPortPair(name, shortName string, valuePtr *string, usage string) *Flag
- func (c *CLI) IP(name, shortName string, valuePtr *net.IP, usage string) *Flag
- func (c *CLI) InstallCompletion(shell string) error
- func (c *CLI) Int(name, shortName string, valuePtr *int, usage string) *Flag
- func (c *CLI) Int64(name, shortName string, valuePtr *int64, usage string) *Flag
- func (c *CLI) IntSlice(name, shortName string, valuePtr *[]int, usage string) *Flag
- func (c *CLI) MAC(name, shortName string, valuePtr *net.HardwareAddr, usage string) *Flag
- func (c *CLI) Parse(argv []string) (*subcommand, error)
- func (c *CLI) PrintUsage(w io.Writer)
- func (c *CLI) Rune(name, shortName string, valuePtr *rune, usage string) *Flag
- func (c *CLI) String(name, shortName string, valuePtr *string, usage string) *Flag
- func (c *CLI) StringSlice(name, shortName string, valuePtr *[]string, usage string) *Flag
- func (c *CLI) SubCommand(name, description string, handler func()) *subcommand
- func (c *CLI) Time(name, shortName string, valuePtr *time.Time, usage string) *Flag
- func (c *CLI) URL(name, shortName string, valuePtr *url.URL, usage string) *Flag
- func (c *CLI) UUID(name, shortName string, valuePtr *uuid.UUID, usage string) *Flag
- func (c *CLI) UninstallCompletion(shell string) error
- type Flag
- type FlagValidator
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 CLI ¶ added in v0.2.0
type CLI struct {
// contains filtered or unexported fields
}
Global flag context. Stores global flags and subcommands.
func (*CLI) Bool ¶ added in v0.2.0
Bool adds a boolean flag to the CLI. Boolean flags don't require a value; their presence sets them to true. Parameters:
- name: The long name of the flag
- shortName: The short name of the flag, can be empty
- valuePtr: Pointer to a bool variable where the parsed value will be stored
- usage: Description of the flag shown in help text
Returns the created Flag for further configuration.
func (*CLI) DirPath ¶ added in v0.2.0
DirPath adds a directory path flag to the CLI. Can optionally validate that the directory exists. Parameters:
- name: The long name of the flag
- shortName: The short name of the flag, can be empty
- valuePtr: Pointer to a string variable where the parsed value will be stored
- usage: Description of the flag shown in help text
Returns the created Flag for further configuration.
func (*CLI) Duration ¶ added in v0.2.0
Duration adds a time.Duration flag to the CLI. Accepts duration strings like "5s", "2m", "1h30m", etc. Parameters:
- name: The long name of the flag
- shortName: The short name of the flag, can be empty
- valuePtr: Pointer to a time.Duration variable where the parsed value will be stored
- usage: Description of the flag shown in help text
Returns the created Flag for further configuration.
func (*CLI) Email ¶ added in v0.2.0
Email adds an email address flag to the CLI. Validates basic email format. Parameters:
- name: The long name of the flag
- shortName: The short name of the flag, can be empty
- valuePtr: Pointer to a string variable where the parsed value will be stored
- usage: Description of the flag shown in help text
Returns the created Flag for further configuration.
func (*CLI) FilePath ¶ added in v0.2.0
FilePath adds a file path flag to the CLI. Can optionally validate that the file exists. Parameters:
- name: The long name of the flag
- shortName: The short name of the flag, can be empty
- valuePtr: Pointer to a string variable where the parsed value will be stored
- usage: Description of the flag shown in help text
Returns the created Flag for further configuration.
func (*CLI) Float32 ¶ added in v0.2.0
Float32 adds a 32-bit floating point flag to the CLI. Parameters:
- name: The long name of the flag
- shortName: The short name of the flag, can be empty
- valuePtr: Pointer to a float32 variable where the parsed value will be stored
- usage: Description of the flag shown in help text
Returns the created Flag for further configuration.
func (*CLI) Float64 ¶ added in v0.2.0
Float64 adds a 64-bit floating point flag to the CLI. Parameters:
- name: The long name of the flag
- shortName: The short name of the flag, can be empty
- valuePtr: Pointer to a float64 variable where the parsed value will be stored
- usage: Description of the flag shown in help text
Returns the created Flag for further configuration.
func (*CLI) GenBashCompletion ¶ added in v0.2.0
GenBashCompletion generates a bash completion script and writes it to w. The completion script provides intelligent tab completion for commands, subcommands, and flags. Only long-form flags (--flag) are shown in completions to reduce clutter; short flags can still be used but won't appear in completion suggestions.
func (*CLI) GenZshCompletion ¶ added in v0.2.0
GenZshCompletion generates a zsh completion script and writes it to w. The completion script provides intelligent tab completion for commands, subcommands, and flags. Only long-form flags (--flag) are shown in completions to reduce clutter; short flags can still be used but won't appear in completion suggestions.
The completion will show both subcommands and global flags when pressing tab at the command prompt, making it easy to discover both options. GenZshCompletion generates a zsh completion script and writes it to w.
func (*CLI) HostPortPair ¶ added in v0.2.0
HostPortPair adds a host:port pair flag to the CLI. Accepts values like "localhost:8080" or "192.168.1.1:443". Parameters:
- name: The long name of the flag
- shortName: The short name of the flag, can be empty
- valuePtr: Pointer to a string variable where the parsed value will be stored
- usage: Description of the flag shown in help text
Returns the created Flag for further configuration.
func (*CLI) IP ¶ added in v0.2.0
IP adds an IP address flag to the CLI. Accepts both IPv4 and IPv6 addresses. Parameters:
- name: The long name of the flag
- shortName: The short name of the flag, can be empty
- valuePtr: Pointer to a net.IP variable where the parsed value will be stored
- usage: Description of the flag shown in help text
Returns the created Flag for further configuration.
func (*CLI) InstallCompletion ¶ added in v0.2.0
InstallCompletion installs shell completion scripts for the CLI application. This function automatically detects the user's shell (bash or zsh) and installs the appropriate completion script to the user's home directory.
For bash, the completion script is installed to ~/.bash_completion.d/<binName> and a source line is added to ~/.bashrc if not already present.
For zsh, the completion script is installed to ~/.zsh/completion/_<binName> and the necessary fpath and compinit lines are added to ~/.zshrc if not already present.
Parameters:
- shell: The shell to install completions for ("bash" or "zsh")
Returns an error if installation fails.
Example:
cli := goflag.NewCLI()
if err := cli.InstallCompletion("bash"); err != nil {
fmt.Fprintf(os.Stderr, "Failed to install completion: %v\n", err)
}
func (*CLI) Int ¶ added in v0.2.0
Int adds an integer flag to the CLI. Parameters:
- name: The long name of the flag
- shortName: The short name of the flag, can be empty
- valuePtr: Pointer to an int variable where the parsed value will be stored
- usage: Description of the flag shown in help text
Returns the created Flag for further configuration.
func (*CLI) Int64 ¶ added in v0.2.0
Int64 adds a 64-bit integer flag to the CLI. Parameters:
- name: The long name of the flag
- shortName: The short name of the flag, can be empty
- valuePtr: Pointer to an int64 variable where the parsed value will be stored
- usage: Description of the flag shown in help text
Returns the created Flag for further configuration.
func (*CLI) IntSlice ¶ added in v0.2.0
IntSlice adds an integer slice flag to the CLI. Can be specified multiple times to build a list of integer values. Parameters:
- name: The long name of the flag
- shortName: The short name of the flag, can be empty
- valuePtr: Pointer to a []int variable where the parsed values will be stored
- usage: Description of the flag shown in help text
Returns the created Flag for further configuration.
func (*CLI) MAC ¶ added in v0.2.0
MAC adds a MAC address flag to the CLI. Accepts MAC addresses in standard formats (e.g., "01:23:45:67:89:ab"). Parameters:
- name: The long name of the flag
- shortName: The short name of the flag, can be empty
- valuePtr: Pointer to a net.HardwareAddr variable where the parsed value will be stored
- usage: Description of the flag shown in help text
Returns the created Flag for further configuration.
func (*CLI) Parse ¶ added in v0.2.0
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 (*CLI) PrintUsage ¶ added in v0.2.0
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
func (*CLI) Rune ¶ added in v0.2.0
Rune adds a single Unicode character flag to the CLI. Parameters:
- name: The long name of the flag
- shortName: The short name of the flag, can be empty
- valuePtr: Pointer to a rune variable where the parsed value will be stored
- usage: Description of the flag shown in help text
Returns the created Flag for further configuration.
func (*CLI) String ¶ added in v0.2.0
String adds a string flag to the CLI. Parameters:
- name: The long name of the flag (e.g., "output")
- shortName: The short name of the flag (e.g., "o"), can be empty
- valuePtr: Pointer to a string variable where the parsed value will be stored
- usage: Description of the flag shown in help text
Returns the created Flag for further configuration (e.g., setting required status).
func (*CLI) StringSlice ¶ added in v0.2.0
StringSlice adds a string slice flag to the CLI. Can be specified multiple times to build a list of values. Parameters:
- name: The long name of the flag
- shortName: The short name of the flag, can be empty
- valuePtr: Pointer to a []string variable where the parsed values will be stored
- usage: Description of the flag shown in help text
Returns the created Flag for further configuration.
func (*CLI) SubCommand ¶ added in v0.2.0
Add a subcommand to the command-line context.
func (*CLI) Time ¶ added in v0.2.0
Time adds a time.Time flag to the CLI. Accepts various time formats for parsing. Parameters:
- name: The long name of the flag
- shortName: The short name of the flag, can be empty
- valuePtr: Pointer to a time.Time variable where the parsed value will be stored
- usage: Description of the flag shown in help text
Returns the created Flag for further configuration.
func (*CLI) URL ¶ added in v0.2.0
URL adds a URL flag to the CLI. Validates and parses URLs according to RFC 3986. Parameters:
- name: The long name of the flag
- shortName: The short name of the flag, can be empty
- valuePtr: Pointer to a *url.URL variable where the parsed value will be stored
- usage: Description of the flag shown in help text
Returns the created Flag for further configuration.
func (*CLI) UUID ¶ added in v0.2.0
UUID adds a UUID flag to the CLI. Accepts UUIDs in standard format (e.g., "550e8400-e29b-41d4-a716-446655440000"). Parameters:
- name: The long name of the flag
- shortName: The short name of the flag, can be empty
- valuePtr: Pointer to a string or UUID type where the parsed value will be stored
- usage: Description of the flag shown in help text
Returns the created Flag for further configuration.
func (*CLI) UninstallCompletion ¶ added in v0.2.0
UninstallCompletion removes shell completion scripts for the CLI application. This function removes the completion script from the user's home directory and cleans up any references to it in shell configuration files.
For bash, the completion script is removed from ~/.bash_completion.d/<binName> and the source line is removed from ~/.bashrc.
For zsh, the completion script is removed from ~/.zsh/completion/_<binName>. Note that fpath and compinit lines remain in ~/.zshrc as they may be used by other completions.
Parameters:
- shell: The shell to uninstall completions for ("bash" or "zsh")
Returns an error if uninstallation fails.
Example:
cli := goflag.NewCLI()
if err := cli.UninstallCompletion("bash"); err != nil {
fmt.Fprintf(os.Stderr, "Failed to uninstall completion: %v\n", err)
}
type Flag ¶ added in v0.1.0
type Flag struct {
// contains filtered or unexported fields
}
A Flag as parsed from the command line.
func (*Flag) Validate ¶ added in v0.1.0
func (flag *Flag) Validate(validators ...FlagValidator) *Flag
Add validator to last flag in the subcommand chain. If no flag exists, it panics.