Involved Source Files Package flag implements command-line flag parsing.
# Usage
Define flags using [flag.String], [Bool], [Int], etc.
This declares an integer flag, -n, stored in the pointer nFlag, with type *int:
import "flag"
var nFlag = flag.Int("n", 1234, "help message for flag n")
If you like, you can bind the flag to a variable using the Var() functions.
var flagvar int
func init() {
flag.IntVar(&flagvar, "flagname", 1234, "help message for flagname")
}
Or you can create custom flags that satisfy the Value interface (with
pointer receivers) and couple them to flag parsing by
flag.Var(&flagVal, "name", "help message for flagname")
For such flags, the default value is just the initial value of the variable.
After all flags are defined, call
flag.Parse()
to parse the command line into the defined flags.
Flags may then be used directly. If you're using the flags themselves,
they are all pointers; if you bind to variables, they're values.
fmt.Println("ip has value ", *ip)
fmt.Println("flagvar has value ", flagvar)
After parsing, the arguments following the flags are available as the
slice [flag.Args] or individually as [flag.Arg](i).
The arguments are indexed from 0 through [flag.NArg]-1.
# Command line flag syntax
The following forms are permitted:
-flag
--flag // double dashes are also permitted
-flag=x
-flag x // non-boolean flags only
One or two dashes may be used; they are equivalent.
The last form is not permitted for boolean flags because the
meaning of the command
cmd -x *
where * is a Unix shell wildcard, will change if there is a file
called 0, false, etc. You must use the -flag=false form to turn
off a boolean flag.
Flag parsing stops just before the first non-flag argument
("-" is a non-flag argument) or after the terminator "--".
Integer flags accept 1234, 0664, 0x1234 and may be negative.
Boolean flags may be:
1, 0, t, f, T, F, true, false, TRUE, FALSE, True, False
Duration flags accept any input valid for time.ParseDuration.
The default set of command-line flags is controlled by
top-level functions. The [FlagSet] type allows one to define
independent sets of flags, such as to implement subcommands
in a command-line interface. The methods of [FlagSet] are
analogous to the top-level functions for the command-line
flag set.
Code Examples
// These examples demonstrate more intricate uses of the flag package.
package main
import (
"errors"
"flag"
"fmt"
"strings"
"time"
)
// Example 1: A single string flag called "species" with default value "gopher".
var species = flag.String("species", "gopher", "the species we are studying")
// Example 2: Two flags sharing a variable, so we can have a shorthand.
// The order of initialization is undefined, so make sure both use the
// same default value. They must be set up with an init function.
var gopherType string
func init() {
const (
defaultGopher = "pocket"
usage = "the variety of gopher"
)
flag.StringVar(&gopherType, "gopher_type", defaultGopher, usage)
flag.StringVar(&gopherType, "g", defaultGopher, usage+" (shorthand)")
}
// Example 3: A user-defined flag type, a slice of durations.
type interval []time.Duration
// String is the method to format the flag's value, part of the flag.Value interface.
// The String method's output will be used in diagnostics.
func (i *interval) String() string {
return fmt.Sprint(*i)
}
// Set is the method to set the flag value, part of the flag.Value interface.
// Set's argument is a string to be parsed to set the flag.
// It's a comma-separated list, so we split it.
func (i *interval) Set(value string) error {
// If we wanted to allow the flag to be set multiple times,
// accumulating values, we would delete this if statement.
// That would permit usages such as
// -deltaT 10s -deltaT 15s
// and other combinations.
if len(*i) > 0 {
return errors.New("interval flag already set")
}
for _, dt := range strings.Split(value, ",") {
duration, err := time.ParseDuration(dt)
if err != nil {
return err
}
*i = append(*i, duration)
}
return nil
}
// Define a flag to accumulate durations. Because it has a special type,
// we need to use the Var function and therefore create the flag during
// init.
var intervalFlag interval
func init() {
// Tie the command-line flag to the intervalFlag variable and
// set a usage message.
flag.Var(&intervalFlag, "deltaT", "comma-separated list of intervals to use between events")
}
func main() {
// All the interesting pieces are with the variables declared above, but
// to enable the flag package to see the flags defined there, one must
// execute, typically at the start of main (not init!):
// flag.Parse()
// We don't call it here because this code is a function called "Example"
// that is part of the testing suite for the package, which has already
// parsed the flags. When viewed at pkg.go.dev, however, the function is
// renamed to "main" and it could be run as a standalone example.
}
package main
import (
"flag"
"fmt"
"os"
)
func main() {
fs := flag.NewFlagSet("ExampleBoolFunc", flag.ContinueOnError)
fs.SetOutput(os.Stdout)
fs.BoolFunc("log", "logs a dummy message", func(s string) error {
fmt.Println("dummy message:", s)
return nil
})
fs.Parse([]string{"-log"})
fs.Parse([]string{"-log=0"})
}
package main
import (
"errors"
"flag"
"fmt"
"net"
"os"
)
func main() {
fs := flag.NewFlagSet("ExampleFunc", flag.ContinueOnError)
fs.SetOutput(os.Stdout)
var ip net.IP
fs.Func("ip", "`IP address` to parse", func(s string) error {
ip = net.ParseIP(s)
if ip == nil {
return errors.New("could not parse IP")
}
return nil
})
fs.Parse([]string{"-ip", "127.0.0.1"})
fmt.Printf("{ip: %v, loopback: %t}\n\n", ip, ip.IsLoopback())
// 256 is not a valid IPv4 component
fs.Parse([]string{"-ip", "256.0.0.1"})
fmt.Printf("{ip: %v, loopback: %t}\n\n", ip, ip.IsLoopback())
}
package main
import (
"flag"
"fmt"
"net"
"os"
)
func main() {
fs := flag.NewFlagSet("ExampleTextVar", flag.ContinueOnError)
fs.SetOutput(os.Stdout)
var ip net.IP
fs.TextVar(&ip, "ip", net.IPv4(192, 168, 0, 100), "`IP address` to parse")
fs.Parse([]string{"-ip", "127.0.0.1"})
fmt.Printf("{ip: %v}\n\n", ip)
// 256 is not a valid IPv4 component
ip = nil
fs.Parse([]string{"-ip", "256.0.0.1"})
fmt.Printf("{ip: %v}\n\n", ip)
}
package main
import (
"flag"
"fmt"
"net/url"
)
type URLValue struct {
URL *url.URL
}
func (v URLValue) String() string {
if v.URL != nil {
return v.URL.String()
}
return ""
}
func (v URLValue) Set(s string) error {
if u, err := url.Parse(s); err != nil {
return err
} else {
*v.URL = *u
}
return nil
}
var u = &url.URL{}
func main() {
fs := flag.NewFlagSet("ExampleValue", flag.ExitOnError)
fs.Var(&URLValue{u}, "url", "URL to parse")
fs.Parse([]string{"-url", "https://golang.org/pkg/flag/"})
fmt.Printf(`{scheme: %q, host: %q, path: %q}`, u.Scheme, u.Host, u.Path)
}
A Flag represents the state of a flag. // default value (as text); for usage message // name as it appears on command line // help message // value as set
func Lookup(name string) *Flag
func (*FlagSet).Lookup(name string) *Flag
func UnquoteUsage(flag *Flag) (name string, usage string)
A FlagSet represents a set of defined flags. The zero value of a FlagSet
has no name and has [ContinueOnError] error handling.
[Flag] names must be unique within a FlagSet. An attempt to define a flag whose
name is already in use will cause a panic. Usage is the function called when an error occurs while parsing flags.
The field is a function (not a method) that may be changed to point to
a custom error handler. What happens after Usage is called depends
on the ErrorHandling setting; for the command line, this defaults
to ExitOnError, which exits the program after calling Usage. Arg returns the i'th argument. Arg(0) is the first remaining argument
after flags have been processed. Arg returns an empty string if the
requested element does not exist. Args returns the non-flag arguments. Bool defines a bool flag with specified name, default value, and usage string.
The return value is the address of a bool variable that stores the value of the flag. BoolFunc defines a flag with the specified name and usage string without requiring values.
Each time the flag is seen, fn is called with the value of the flag.
If fn returns a non-nil error, it will be treated as a flag value parsing error. BoolVar defines a bool flag with specified name, default value, and usage string.
The argument p points to a bool variable in which to store the value of the flag. Duration defines a time.Duration flag with specified name, default value, and usage string.
The return value is the address of a time.Duration variable that stores the value of the flag.
The flag accepts a value acceptable to time.ParseDuration. DurationVar defines a time.Duration flag with specified name, default value, and usage string.
The argument p points to a time.Duration variable in which to store the value of the flag.
The flag accepts a value acceptable to time.ParseDuration. ErrorHandling returns the error handling behavior of the flag set. Float64 defines a float64 flag with specified name, default value, and usage string.
The return value is the address of a float64 variable that stores the value of the flag. Float64Var defines a float64 flag with specified name, default value, and usage string.
The argument p points to a float64 variable in which to store the value of the flag. Func defines a flag with the specified name and usage string.
Each time the flag is seen, fn is called with the value of the flag.
If fn returns a non-nil error, it will be treated as a flag value parsing error. Init sets the name and error handling property for a flag set.
By default, the zero [FlagSet] uses an empty name and the
[ContinueOnError] error handling policy. Int defines an int flag with specified name, default value, and usage string.
The return value is the address of an int variable that stores the value of the flag. Int64 defines an int64 flag with specified name, default value, and usage string.
The return value is the address of an int64 variable that stores the value of the flag. Int64Var defines an int64 flag with specified name, default value, and usage string.
The argument p points to an int64 variable in which to store the value of the flag. IntVar defines an int flag with specified name, default value, and usage string.
The argument p points to an int variable in which to store the value of the flag. Lookup returns the [Flag] structure of the named flag, returning nil if none exists. NArg is the number of arguments remaining after flags have been processed. NFlag returns the number of flags that have been set. Name returns the name of the flag set. Output returns the destination for usage and error messages. [os.Stderr] is returned if
output was not set or was set to nil. Parse parses flag definitions from the argument list, which should not
include the command name. Must be called after all flags in the [FlagSet]
are defined and before flags are accessed by the program.
The return value will be [ErrHelp] if -help or -h were set but not defined. Parsed reports whether f.Parse has been called. PrintDefaults prints, to standard error unless configured otherwise, the
default values of all defined command-line flags in the set. See the
documentation for the global function PrintDefaults for more information. Set sets the value of the named flag. SetOutput sets the destination for usage and error messages.
If output is nil, [os.Stderr] is used. String defines a string flag with specified name, default value, and usage string.
The return value is the address of a string variable that stores the value of the flag. StringVar defines a string flag with specified name, default value, and usage string.
The argument p points to a string variable in which to store the value of the flag. TextVar defines a flag with a specified name, default value, and usage string.
The argument p must be a pointer to a variable that will hold the value
of the flag, and p must implement encoding.TextUnmarshaler.
If the flag is used, the flag value will be passed to p's UnmarshalText method.
The type of the default value must be the same as the type of p. Uint defines a uint flag with specified name, default value, and usage string.
The return value is the address of a uint variable that stores the value of the flag. Uint64 defines a uint64 flag with specified name, default value, and usage string.
The return value is the address of a uint64 variable that stores the value of the flag. Uint64Var defines a uint64 flag with specified name, default value, and usage string.
The argument p points to a uint64 variable in which to store the value of the flag. UintVar defines a uint flag with specified name, default value, and usage string.
The argument p points to a uint variable in which to store the value of the flag. Var defines a flag with the specified name and usage string. The type and
value of the flag are represented by the first argument, of type [Value], which
typically holds a user-defined implementation of [Value]. For instance, the
caller could create a flag that turns a comma-separated string into a slice
of strings by giving the slice the methods of [Value]; in particular, [Set] would
decompose the comma-separated string into the slice. Visit visits the flags in lexicographical order, calling fn for each.
It visits only those flags that have been set. VisitAll visits the flags in lexicographical order, calling fn for each.
It visits all flags, even those not set.
func NewFlagSet(name string, errorHandling ErrorHandling) *FlagSet
var CommandLine *FlagSet
Getter is an interface that allows the contents of a [Value] to be retrieved.
It wraps the [Value] interface, rather than being part of it, because it
appeared after Go 1 and its compatibility rules. All [Value] types provided
by this package satisfy the [Getter] interface, except the type used by [Func].( Getter) Get() any( Getter) Set(string) error( Getter) String() string
Getter : Value
Getter : expvar.Var
Getter : fmt.Stringer
Value is the interface to the dynamic value stored in a flag.
(The default value is represented as a string.)
If a Value has an IsBoolFlag() bool method returning true,
the command-line parser makes -name equivalent to -name=true
rather than using the next command-line argument.
Set is called once, in command line order, for each flag present.
The flag package may call the [String] method with a zero-valued receiver,
such as a nil pointer.( Value) Set(string) error( Value) String() stringGetter(interface)
Value : expvar.Var
Value : fmt.Stringer
func Var(value Value, name string, usage string)
func (*FlagSet).Var(value Value, name string, usage string)
Package-Level Functions (total 33)
Arg returns the i'th command-line argument. Arg(0) is the first remaining argument
after flags have been processed. Arg returns an empty string if the
requested element does not exist.
Args returns the non-flag command-line arguments.
Bool defines a bool flag with specified name, default value, and usage string.
The return value is the address of a bool variable that stores the value of the flag.
BoolFunc defines a flag with the specified name and usage string without requiring values.
Each time the flag is seen, fn is called with the value of the flag.
If fn returns a non-nil error, it will be treated as a flag value parsing error.
BoolVar defines a bool flag with specified name, default value, and usage string.
The argument p points to a bool variable in which to store the value of the flag.
Duration defines a time.Duration flag with specified name, default value, and usage string.
The return value is the address of a time.Duration variable that stores the value of the flag.
The flag accepts a value acceptable to time.ParseDuration.
DurationVar defines a time.Duration flag with specified name, default value, and usage string.
The argument p points to a time.Duration variable in which to store the value of the flag.
The flag accepts a value acceptable to time.ParseDuration.
Float64 defines a float64 flag with specified name, default value, and usage string.
The return value is the address of a float64 variable that stores the value of the flag.
Float64Var defines a float64 flag with specified name, default value, and usage string.
The argument p points to a float64 variable in which to store the value of the flag.
Func defines a flag with the specified name and usage string.
Each time the flag is seen, fn is called with the value of the flag.
If fn returns a non-nil error, it will be treated as a flag value parsing error.
Int defines an int flag with specified name, default value, and usage string.
The return value is the address of an int variable that stores the value of the flag.
Int64 defines an int64 flag with specified name, default value, and usage string.
The return value is the address of an int64 variable that stores the value of the flag.
Int64Var defines an int64 flag with specified name, default value, and usage string.
The argument p points to an int64 variable in which to store the value of the flag.
IntVar defines an int flag with specified name, default value, and usage string.
The argument p points to an int variable in which to store the value of the flag.
Lookup returns the [Flag] structure of the named command-line flag,
returning nil if none exists.
NArg is the number of arguments remaining after flags have been processed.
NewFlagSet returns a new, empty flag set with the specified name and
error handling property. If the name is not empty, it will be printed
in the default usage message and in error messages.
NFlag returns the number of command-line flags that have been set.
Parse parses the command-line flags from [os.Args][1:]. Must be called
after all flags are defined and before flags are accessed by the program.
Parsed reports whether the command-line flags have been parsed.
PrintDefaults prints, to standard error unless configured otherwise,
a usage message showing the default settings of all defined
command-line flags.
For an integer valued flag x, the default output has the form
-x int
usage-message-for-x (default 7)
The usage message will appear on a separate line for anything but
a bool flag with a one-byte name. For bool flags, the type is
omitted and if the flag name is one byte the usage message appears
on the same line. The parenthetical default is omitted if the
default is the zero value for the type. The listed type, here int,
can be changed by placing a back-quoted name in the flag's usage
string; the first such item in the message is taken to be a parameter
name to show in the message and the back quotes are stripped from
the message when displayed. For instance, given
flag.String("I", "", "search `directory` for include files")
the output will be
-I directory
search directory for include files.
To change the destination for flag messages, call [CommandLine].SetOutput.
Set sets the value of the named command-line flag.
String defines a string flag with specified name, default value, and usage string.
The return value is the address of a string variable that stores the value of the flag.
StringVar defines a string flag with specified name, default value, and usage string.
The argument p points to a string variable in which to store the value of the flag.
TextVar defines a flag with a specified name, default value, and usage string.
The argument p must be a pointer to a variable that will hold the value
of the flag, and p must implement encoding.TextUnmarshaler.
If the flag is used, the flag value will be passed to p's UnmarshalText method.
The type of the default value must be the same as the type of p.
Uint defines a uint flag with specified name, default value, and usage string.
The return value is the address of a uint variable that stores the value of the flag.
Uint64 defines a uint64 flag with specified name, default value, and usage string.
The return value is the address of a uint64 variable that stores the value of the flag.
Uint64Var defines a uint64 flag with specified name, default value, and usage string.
The argument p points to a uint64 variable in which to store the value of the flag.
UintVar defines a uint flag with specified name, default value, and usage string.
The argument p points to a uint variable in which to store the value of the flag.
UnquoteUsage extracts a back-quoted name from the usage
string for a flag and returns it and the un-quoted usage.
Given "a `name` to show" it returns ("name", "a name to show").
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.
Var defines a flag with the specified name and usage string. The type and
value of the flag are represented by the first argument, of type [Value], which
typically holds a user-defined implementation of [Value]. For instance, the
caller could create a flag that turns a comma-separated string into a slice
of strings by giving the slice the methods of [Value]; in particular, [Set] would
decompose the comma-separated string into the slice.
Visit visits the command-line flags in lexicographical order, calling fn
for each. It visits only those flags that have been set.
VisitAll visits the command-line flags in lexicographical order, calling
fn for each. It visits all flags, even those not set.
Package-Level Variables (total 3)
CommandLine is the default set of command-line flags, parsed from [os.Args].
The top-level functions such as [BoolVar], [Arg], and so on are wrappers for the
methods of CommandLine.
ErrHelp is the error returned if the -help or -h flag is invoked
but no such flag is defined.
Usage prints a usage message documenting all defined command-line flags
to [CommandLine]'s output, which by default is [os.Stderr].
It is called when an error occurs while parsing flags.
The function is a variable that may be changed to point to a custom function.
By default it prints a simple header and calls [PrintDefaults]; for details about the
format of the output and how to control it, see the documentation for [PrintDefaults].
Custom usage functions may choose to exit the program; by default exiting
happens anyway as the command line's error handling strategy is set to
[ExitOnError].
Package-Level Constants (total 3)
These constants cause [FlagSet.Parse] to behave as described if the parse fails.
These constants cause [FlagSet.Parse] to behave as described if the parse fails.
These constants cause [FlagSet.Parse] to behave as described if the parse fails.
The pages are generated with Goldsv0.7.0-preview. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.