Namespace: go.std.flag
v1.0Contents
Summary
Provides a low-level interface to the flag package.
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=x
-flag x // non-boolean flags only
One or two minus signs 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.
Index
- *ErrorHandling
- *Flag
- *FlagSet
- Arg
- Args
- Bool
- CommandLine
- ContinueOnError
- Duration
- DurationVar
- ErrHelp
- ErrorHandling
- ExitOnError
- Flag
- FlagSet
- Float64
- Getter
- Int
- Int64
- Lookup
- NArg
- NFlag
- NewFlagSet
- PanicOnError
- Parse
- Parsed
- PrintDefaults
- Set
- String
- Uint
- Uint64
- UnquoteUsage
- Usage
- Value
- Var
- arrayOfErrorHandling
- arrayOfFlag
- arrayOfFlagSet
- arrayOfGetter
- arrayOfValue
Legend
-
Constant
Variable
Function
Macro
Special form
Type
GoVar
Receiver/Method
Constants
Constants are variables with :const true in their metadata. Joker currently does not recognize them as special; as such, it allows redefining them or their values.-
(None.)
Variables
-
CommandLine
Var v1.0CommandLine 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.
-
ContinueOnError
GoObject v1.0Return a descriptive error.
-
ErrHelp
Var v1.0ErrHelp is the error returned if the -help or -h flag is invoked
but no such flag is defined.
-
ExitOnError
GoObject v1.0Call os.Exit(2) or for -h/-help Exit(0).
-
PanicOnError
GoObject v1.0Call panic with a descriptive error.
-
Usage
Var v1.0Usage 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.
Functions, Macros, and Special Forms
-
Arg
Function v1.0(Arg i)
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.
Go input arguments: (i int)
Go returns: string
Joker input arguments: [^Int i]
Joker returns: ^String -
Args
Function v1.0(Args)
Args returns the non-flag command-line arguments.
Go returns: []string
Joker input arguments: []
Joker returns: ^arrayOfString -
Bool
Function v1.0(Bool name value usage)
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.
Go input arguments: (name string, value bool, usage string)
Go returns: *bool
Joker input arguments: [^String name, ^Boolean value, ^String usage]
Joker returns: ^*Boolean -
Duration
Function v1.0(Duration name value usage)
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.
Go input arguments: (name string, value time.Duration, usage string)
Go returns: *time.Duration
Joker input arguments: [^String name, ^go.std.time/Duration value, ^String usage]
Joker returns: ^go.std.time/*Duration -
DurationVar
Function v1.0(DurationVar p name value usage)
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.
Go input arguments: (p *time.Duration, name string, value time.Duration, usage string)
Joker input arguments: [^go.std.time/*Duration p, ^String name, ^go.std.time/Duration value, ^String usage] -
Float64
Function v1.0(Float64 name value usage)
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.
Go input arguments: (name string, value float64, usage string)
Go returns: *float64
Joker input arguments: [^String name, ^Double value, ^String usage]
Joker returns: ^*Double -
Int
Function v1.0(Int name value usage)
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.
Go input arguments: (name string, value int, usage string)
Go returns: *int
Joker input arguments: [^String name, ^Int value, ^String usage]
Joker returns: ^*Int -
Int64
Function v1.0(Int64 name value usage)
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.
Go input arguments: (name string, value int64, usage string)
Go returns: *int64
Joker input arguments: [^String name, ^BigInt value, ^String usage]
Joker returns: ^*BigInt -
Lookup
Function v1.0(Lookup name)
Lookup returns the Flag structure of the named command-line flag,
returning nil if none exists.
Go input arguments: (name string)
Go returns: *Flag
Joker input arguments: [^String name]
Joker returns: ^*Flag -
NArg
Function v1.0(NArg)
NArg is the number of arguments remaining after flags have been processed.
Go returns: int
Joker input arguments: []
Joker returns: ^Int -
NFlag
Function v1.0(NFlag)
NFlag returns the number of command-line flags that have been set.
Go returns: int
Joker input arguments: []
Joker returns: ^Int -
NewFlagSet
Function v1.0(NewFlagSet name errorHandling)
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.
Go input arguments: (name string, errorHandling ErrorHandling)
Go returns: *FlagSet
Joker input arguments: [^String name, ^ErrorHandling errorHandling]
Joker returns: ^*FlagSet -
Parse
Function v1.0(Parse)
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.
Joker input arguments: [] -
Parsed
Function v1.0(Parsed)
Parsed reports whether the command-line flags have been parsed.
Go returns: bool
Joker input arguments: []
Joker returns: ^Boolean -
PrintDefaults
Function v1.0(PrintDefaults)
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.
Joker input arguments: [] -
Set
Function v1.0(Set name value)
Set sets the value of the named command-line flag.
Go input arguments: (name string, value string)
Go returns: error
Joker input arguments: [^String name, ^String value]
Joker returns: ^Error -
String
Function v1.0(String name value usage)
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.
Go input arguments: (name string, value string, usage string)
Go returns: *string
Joker input arguments: [^String name, ^String value, ^String usage]
Joker returns: ^*String -
Uint
Function v1.0(Uint name value usage)
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.
Go input arguments: (name string, value uint, usage string)
Go returns: *uint
Joker input arguments: [^String name, ^Number value, ^String usage]
Joker returns: ^*Number -
Uint64
Function v1.0(Uint64 name value usage)
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.
Go input arguments: (name string, value uint64, usage string)
Go returns: *uint64
Joker input arguments: [^String name, ^Number value, ^String usage]
Joker returns: ^*Number -
UnquoteUsage
Function v1.0(UnquoteUsage 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.
Go input arguments: (flag *Flag)
Go returns: (name string, usage string)
Joker input arguments: [^*Flag flag]
Joker returns: [^String name, ^String usage] -
Var
Function v1.0(Var value name usage)
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.
Go input arguments: (value Value, name string, usage string)
Joker input arguments: [^Value value, ^String name, ^String usage]
Types
-
*ErrorHandling
Concrete Type v1.0ErrorHandling defines how FlagSet.Parse behaves if the parse fails.
-
*Flag
Concrete Type v1.0A Flag represents the state of a flag.
-
*FlagSet
Concrete Type v1.0A 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.
-
Arg
Receiver for *FlagSet v1.0([i])
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
Receiver for *FlagSet v1.0([])
Args returns the non-flag arguments.
-
Bool
Receiver for *FlagSet v1.0([name value usage])
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.
-
Duration
Receiver for *FlagSet v1.0([name value usage])
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
Receiver for *FlagSet v1.0([p name value usage])
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
Receiver for *FlagSet v1.0([])
ErrorHandling returns the error handling behavior of the flag set.
-
Float64
Receiver for *FlagSet v1.0([name value usage])
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.
-
Init
Receiver for *FlagSet v1.0([name errorHandling])
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
Receiver for *FlagSet v1.0([name value usage])
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
Receiver for *FlagSet v1.0([name value usage])
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.
-
Lookup
Receiver for *FlagSet v1.0([name])
Lookup returns the Flag structure of the named flag, returning nil if none exists.
-
NArg
Receiver for *FlagSet v1.0([])
NArg is the number of arguments remaining after flags have been processed.
-
NFlag
Receiver for *FlagSet v1.0([])
NFlag returns the number of flags that have been set.
-
Name
Receiver for *FlagSet v1.0([])
Name returns the name of the flag set.
-
Output
Receiver for *FlagSet v1.0([])
Output returns the destination for usage and error messages. os.Stderr is returned if
output was not set or was set to nil.
-
Parse
Receiver for *FlagSet v1.0([arguments])
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
Receiver for *FlagSet v1.0([])
Parsed reports whether f.Parse has been called.
-
PrintDefaults
Receiver for *FlagSet v1.0([])
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
Receiver for *FlagSet v1.0([name value])
Set sets the value of the named flag.
-
SetOutput
Receiver for *FlagSet v1.0([output])
SetOutput sets the destination for usage and error messages.
If output is nil, os.Stderr is used.
-
String
Receiver for *FlagSet v1.0([name value usage])
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.
-
Uint
Receiver for *FlagSet v1.0([name value usage])
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
Receiver for *FlagSet v1.0([name value usage])
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.
-
Var
Receiver for *FlagSet v1.0([value name usage])
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.
-
ErrorHandling
Concrete Type v1.0ErrorHandling defines how FlagSet.Parse behaves if the parse fails.
-
Flag
Concrete Type v1.0A Flag represents the state of a flag.
-
FlagSet
Concrete Type v1.0A 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.
-
Getter
Abstract Type v1.0Getter 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.
-
Get
Method for Getter v1.0([])
-
Set
Method for Getter v1.0([arg1])
-
String
Method for Getter v1.0([])
-
Value
Abstract Type v1.0Value 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.
-
Set
Method for Value v1.0([arg1])
-
String
Method for Value v1.0([])
-
arrayOfErrorHandling
Concrete Type v1.0ErrorHandling defines how FlagSet.Parse behaves if the parse fails.
-
arrayOfFlag
Concrete Type v1.0A Flag represents the state of a flag.
-
arrayOfFlagSet
Concrete Type v1.0A 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.
-
arrayOfGetter
Concrete Type v1.0Getter 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.
-
arrayOfValue
Concrete Type v1.0Value 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.