Documentation
¶
Overview ¶
Package flag is an alternative to the standard library package of the same name. This implementation adheres to POSIX/GNU standards for option and argument parsing. This package aims to be a simple drop-in replacement for the standard implementation and will continue to track the standard library flag package. No new features deviating from the standard implementation will be accepted.
Unless otherwise mentioned, the documentation for the standard library implementation applies here too. Refer to the documentation there for more information:
https://pkg.go.dev/flag
Usage ¶
Start by initializing a new FlagSet.
fs := flag.NewFlagSet("hello", ContinueOnError)
Add flags to your FlagSet with FlagSet.StringVar, FlagSet.BoolVar, FlagSet.IntVar, etc.
var ( output string all bool count int ) fs.StringVar(&output, "output", "-", "output file location") fs.BoolVar(&all, "a", false, "show all") fs.IntVar(&count, "c", 0, "limit results to count") fs.IntVar(&count, "count", 0, "limit results to count")
The example above declares a long string flag '--output', a short '-a' bool flag, and aliased flags '-c' / '--count'.
After all flags are defined, call FlagSet.Parse to parse the flags.
err := fs.Parse(os.Args[1:])
One parsed, any remaining (unparsed) arguments can be accessed with FlagSet.Arg or FlagSet.Args.
Syntax ¶
FlagSet distinguishes between long and short flags. A long flag is any flag whose name contains more than a single character, while a short flag has a name with a single character.
-a // short boolean flag --all // long boolean flag --all=false // disabled long boolean flag -c 12 // short integer flag -c12 // short integer flag with immediate value --count 12 // long integer value --count=12 // long integer value with immediate value
Short boolean flags may be combined into a single argument, and short flags accepting arguments may be "stuck" to the value:
-ac12 // equivalent to '-a -c 12'
Flag parsing stops just before the first non-flag argument ("-" is a non-flag argument) or after the terminator "--".
Flags which accept a number (FlagSet.Int, FlagSet.Uint, FlagSet.Float64, etc) will parse their arguments with strconv. For integers, binary/octal/decimal/hexadecimal numbers are accepted (see strconv.ParseInt and strconv.ParseUint). For floats, anything parseable by strconv.ParseFloat is accepted.
--count 12 --count 0xC --count 0o14 --count 0b1100 --count 1.2E1
Boolean flags with an immediate value may be anything parseable by strconv.ParseBool.
--all=false --all=FALSE --all=f --all=0
Duration flags accept any input valid for time.ParseDuration.
--since=3m2s
Index ¶
- Variables
- func Arg(i int) string
- func Args() []string
- func Bool(name string, value bool, usage string) *bool
- func BoolFunc(name, usage string, fn func(string) error)
- func BoolVar(p *bool, name string, value bool, usage string)
- func Duration(name string, value time.Duration, usage string) *time.Duration
- func DurationVar(p *time.Duration, name string, value time.Duration, usage string)
- func Float64(name string, value float64, usage string) *float64
- func Float64Var(p *float64, name string, value float64, usage string)
- func Func(name, usage string, fn func(string) error)
- func Int(name string, value int, usage string) *int
- func Int64(name string, value int64, usage string) *int64
- func Int64Var(p *int64, name string, value int64, usage string)
- func IntVar(p *int, name string, value int, usage string)
- func NArg() int
- func NFlag() int
- func Parse() error
- func Parsed() bool
- func PrintDefaults()
- func Set(name, value string) error
- func String(name string, value string, usage string) *string
- func StringVar(p *string, name string, value string, usage string)
- func TextVar(p encoding.TextUnmarshaler, name string, value encoding.TextMarshaler, ...)
- func Uint(name string, value uint, usage string) *uint
- func Uint64(name string, value uint64, usage string) *uint64
- func Uint64Var(p *uint64, name string, value uint64, usage string)
- func UintVar(p *uint, name string, value uint, usage string)
- func UnquoteUsage(flg *Flag) (string, string)
- func Var(value Value, name string, usage string)
- func Visit(fn func(*Flag))
- func VisitAll(fn func(*Flag))
- type ErrorHandling
- type Flag
- type FlagSet
- func (f *FlagSet) Arg(i int) string
- func (f *FlagSet) Args() []string
- func (f *FlagSet) Bool(name string, value bool, usage string) *bool
- func (f *FlagSet) BoolFunc(name, usage string, fn func(string) error)
- func (f *FlagSet) BoolVar(p *bool, name string, value bool, usage string)
- func (f *FlagSet) Duration(name string, value time.Duration, usage string) *time.Duration
- func (f *FlagSet) DurationVar(p *time.Duration, name string, value time.Duration, usage string)
- func (f *FlagSet) ErrorHandling() ErrorHandling
- func (f *FlagSet) Float64(name string, value float64, usage string) *float64
- func (f *FlagSet) Float64Var(p *float64, name string, value float64, usage string)
- func (f *FlagSet) Func(name, usage string, fn func(string) error)
- func (f *FlagSet) Init(name string, errorHandling ErrorHandling)
- func (f *FlagSet) Int(name string, value int, usage string) *int
- func (f *FlagSet) Int64(name string, value int64, usage string) *int64
- func (f *FlagSet) Int64Var(p *int64, name string, value int64, usage string)
- func (f *FlagSet) IntVar(p *int, name string, value int, usage string)
- func (f *FlagSet) Lookup(name string) *Flag
- func (f *FlagSet) NArg() int
- func (f *FlagSet) NFlag() int
- func (f *FlagSet) Name() string
- func (f *FlagSet) Output() io.Writer
- func (f *FlagSet) Parse(arguments []string) error
- func (f *FlagSet) Parsed() bool
- func (f *FlagSet) PrintDefaults()
- func (f *FlagSet) Set(name, value string) error
- func (f *FlagSet) SetOutput(output io.Writer)
- func (f *FlagSet) String(name string, value string, usage string) *string
- func (f *FlagSet) StringVar(p *string, name string, value string, usage string)
- func (f *FlagSet) TextVar(p encoding.TextUnmarshaler, name string, value encoding.TextMarshaler, ...)
- func (f *FlagSet) Uint(name string, value uint, usage string) *uint
- func (f *FlagSet) Uint64(name string, value uint64, usage string) *uint64
- func (f *FlagSet) Uint64Var(p *uint64, name string, value uint64, usage string)
- func (f *FlagSet) UintVar(p *uint, name string, value uint, usage string)
- func (f *FlagSet) Var(value Value, name string, usage string)
- func (f *FlagSet) Visit(fn func(*Flag))
- func (f *FlagSet) VisitAll(fn func(*Flag))
- type Getter
- type Value
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var (
ErrHelp = errors.New("flag: help requested")
)
var Usage = func() { _, err := fmt.Fprintf(CommandLine.Output(), "Usage of %s:\n", CommandLine.Name()) if err != nil { panic(err) } CommandLine.PrintDefaults() }
Usage is a simple usage function which prints usage information for the global CommandLine.
Functions ¶
func DurationVar ¶
func Parse ¶
func Parse() error
Set invokes FlagSet.Parse for the global CommandLine with arguments from os.Args.
func PrintDefaults ¶
func PrintDefaults()
PrintDefaults calls FlagSet.PrintDefaults for the global CommandLine.
func TextVar ¶
func TextVar(p encoding.TextUnmarshaler, name string, value encoding.TextMarshaler, usage string)
func UnquoteUsage ¶
UnquoteUsage extracts a back-quoted name from the usage string for a Flag and returns it and the un-quoted usage.
Given
var output string
fs := flag.NewFlagSet("echo", ContinueOnError)
fs.StringVar(&output, "output", "-", "output `file` location")
UnquoteUsage would return
("file", "output file location").
If there are no back quotes, the name is an educated guess of the type of the flag's value, or the empty string if the flag is boolean.
func Var ¶
Set calls FlagSet.Var for the global CommandLine.
func VisitAll ¶
func VisitAll(fn func(*Flag))
VisitAll calls FlagSet.VisitAll for the global CommandLine.
Types ¶
type ErrorHandling ¶
type ErrorHandling int
ErrorHandling configures the error handling policy on a flag set, configuring the behaviour of FlagSet.Parse.
const ( // Return a descriptive error. ContinueOnError ErrorHandling = iota // Call os.Exit(2) or for -h/--help Exit(0). ExitOnError // Call panic with a descriptive error. PanicOnError )
type Flag ¶
type Flag struct {
// Name is the name of the flag, as it appears at the command line.
Name string
// Usage is a descriptive help message for the flag.
Usage string
// Value represents the value of the flag as parsed.
Value Value
// DefValue is the (stringified) default value for the flag.
DefValue string
}
Flag represents a single short or long flag. It is identified by its name (e.g. 'h' for '-h' and 'help' for '--help'), and the value of the flag is represented by the Value interface.
type FlagSet ¶
type FlagSet struct {
// Usage is a function called when an error occurs while parsing flags. It is invoked directly after an error is
// encountered, but immediately before [FlagSet.Parse] returns the error or exits/panics (see [ErrorHandling]).
//
// If nil, defaults to [PrintDefaults].
Usage func()
// contains filtered or unexported fields
}
FlagSet is a set of flags. The zero value of a FlagSet has no name and has ContinueOnError error handling policy.
Unlike FlagSet in the standard library package, this FlagSet parses flags with POSIX/GNU semantics.
Flag names must be unique within a FlagSet. An attempt to define a flag whose name is already in use will cause a panic. Flag names must also be comprised of only alphanumeric characters, hyphens '-' (except at beginning or end of name), and periods '.'
Example ¶
Two flags with the same Value can be used to create shorthand aliases for longer flags. For example, you may want to provide a shorthand `-u` for the longer `--until` flag. Simply use FlagSet.Lookup to fetch the flag and FlagSet.Var to create the shorthand.
package main
import (
"fmt"
"time"
"github.com/brandon1024/cmder/flag"
)
// Two flags with the same [Value] can be used to create shorthand aliases for longer flags. For example, you may want
// to provide a shorthand `-u` for the longer `--until` flag. Simply use [FlagSet.Lookup] to fetch the flag and
// [FlagSet.Var] to create the shorthand.
func main() {
var since, until time.Duration
fs := flag.NewFlagSet("alises", flag.ContinueOnError)
fs.DurationVar(&since, "since", -time.Minute, "show items since")
fs.Var(alias(fs.Lookup("since"), "s"))
fs.DurationVar(&until, "until", time.Duration(0), "show items until")
fs.Var(alias(fs.Lookup("until"), "u"))
args := []string{
"--since=-12m", "-u", "1m",
}
if err := fs.Parse(args); err != nil {
panic(err)
}
fmt.Printf("since: %s\n", since.String())
fmt.Printf("until: %s\n", until.String())
}
func alias(flg *flag.Flag, name string) (flag.Value, string, string) {
return flg.Value, name, flg.Usage
}
Output: since: -12m0s until: 1m0s
var (
CommandLine *FlagSet
)
CommandLine is the default set of command-line flags parsed from os.Args.
func NewFlagSet ¶
func NewFlagSet(name string, errorHandling ErrorHandling) *FlagSet
NewFlagSet returns a new flag set with the given name and error handling policy.
func (*FlagSet) Arg ¶
Arg returns the i'th remaining argument after calling FlagSet.Parse. Returns an empty string if the argument does not exist, or FlagSet.Parse was not called.
func (*FlagSet) Args ¶
Args returns a slice of non-flag arguments remaining after calling FlagSet.Parse.
func (*FlagSet) DurationVar ¶
func (*FlagSet) ErrorHandling ¶
func (f *FlagSet) ErrorHandling() ErrorHandling
ErrorHandling returns the error handling policy for this flag set.
func (*FlagSet) Float64Var ¶
func (*FlagSet) Init ¶
func (f *FlagSet) Init(name string, errorHandling ErrorHandling)
Init sets the name and error handling policy for this flag set.
func (*FlagSet) NArg ¶
NArg returns the number of non-flag arguments remaining after calling FlagSet.Parse.
func (*FlagSet) Name ¶
Name returns the name of this flag set as given to NewFlagSet or FlagSet.Init.
func (*FlagSet) Output ¶
Output returns the io.Writer to which usage information is written, according to the ErrorHandling policy. The writer returned is the same given to NewFlagSet or FlagSet.SetOutput.
func (*FlagSet) Parse ¶
Parse processes the given arguments and updates the flags of this flag set. The arguments given should not include the command name. Parse should only be called after all flags have been registered and before flags are accessed by the application.
The return value will be ErrHelp if -help or -h were set but not defined.
func (*FlagSet) Parsed ¶
Parsed returns whether or not FlagSet.Parse has been invoked on this flag set.
func (*FlagSet) PrintDefaults ¶
func (f *FlagSet) PrintDefaults()
PrintDefaults prints usage information and default values for all flags of this flag set to the output location configured with NewFlagSet or FlagSet.SetOutput.
func (*FlagSet) Set ¶
Set updates the value of a flag with the given string. Returns an error if the flag doesn't exist or the value is invalid.
func (*FlagSet) SetOutput ¶
SetOutput sets the io.Writer to use when writing usage information, according to the ErrorHandling policy.
func (*FlagSet) TextVar ¶
func (f *FlagSet) TextVar(p encoding.TextUnmarshaler, name string, value encoding.TextMarshaler, usage string)
type Getter ¶
Getter extends Value but additionally supports retrieval of the typed flag value. All flag types implement this interface, with the exception of FlagSet.BoolFunc and FlagSet.Func.
type Value ¶
type Value interface {
// String returns the current value of the flag, represented as a string.
String() string
// Set updates the value of this flag.
Set(string) error
}
Value describes the actual value of a Flag.
Example ¶
You can define custom types implementing flag.Value to handle different types of flags, like timestamps, IP addresses, string maps or slices.
package main
import (
"fmt"
"time"
"github.com/brandon1024/cmder/flag"
)
// TimeVar is a [flag.Value] for flags that accept timestamps in [time.RFC3339] format. TimeVar also implements
// [Flag.Getter].
type TimeVar time.Time
// String returns the [time.RFC3339] representation of the timestamp flag.
func (t *TimeVar) String() string {
return time.Time(*t).Format(time.RFC3339)
}
// Set fulfills the [flag.Value] interface. The given value must be a correctly formatted [time.RFC3339] timestamp.
func (t *TimeVar) Set(value string) error {
tm, err := time.Parse(time.RFC3339, value)
if err == nil {
*t = TimeVar(tm)
}
return err
}
// Get fulfills the [flag.Getter] interface, allowing typed access to the flag value. In this case, returns a
// [time.Time].
func (t *TimeVar) Get() any {
return time.Time(*t)
}
// You can define custom types implementing [flag.Value] to handle different types of flags, like timestamps, IP
// addresses, string maps or slices.
func main() {
var since TimeVar
fs := flag.NewFlagSet("custom", flag.ContinueOnError)
fs.Var(&since, "since", "show items since")
args := []string{
"--since", "2025-01-01T00:00:00Z",
}
if err := fs.Parse(args); err != nil {
panic(err)
}
fmt.Printf("since: %s\n", since.String())
}
Output: since: 2025-01-01T00:00:00Z