Documentation ¶
Overview ¶
getopt is a POSIX-compatible implementation of getopt(3) for Go.
Example usage:
import ( "os" "git.sr.ht/~sircmpwn/getopt" ) func main() { opts, optind, err := getopt.Getopts(os.Args, "abc:d:") if err != nil { panic(err) } for _, opt := range opts { switch opt.Option { case 'a': println("Option -a specified") case 'b': println("Option -b specified") case 'c': println("Option -c specified: " + opt.Value) case 'd': println("Option -d specified: " + opt.Value) } } println("Remaining arguments:") for _, arg := range os.Args[optind:] { println(arg) } }
A flag0-like interface is also supported.
import ( "git.sr.ht/~sircmpwn/getopt" ) func main() { a := getopt.Bool("a", false, "turn on option a") b := getopt.Int("b", 1, "set b to a numerical value") var opt string getopt.StringVar(&opt, "c", "", "let c be specified string") err := getopt.Parse() if err != nil { panic(err) } print("Value of a: ") println(*a) print("Value of b: ") println(*b) println("Value of c: " + opt) println("Remaining arguments:") for _, arg := range getopt.Args() { println(arg) } }
Index ¶
- Variables
- func Arg(i int) string
- func Args() []string
- func Bool(name string, value bool, usage string) *bool
- 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 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 SetOutput(output io.Writer)
- func String(name string, value string, usage string) *string
- func StringVar(p *string, name string, value string, usage string)
- 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 Var(value flag.Value, name string, usage string)
- type Flag
- type FlagSet
- func (set *FlagSet) Arg(i int) string
- func (set *FlagSet) Args() []string
- func (set *FlagSet) Bool(name string, value bool, usage string) *bool
- func (set *FlagSet) BoolVar(p *bool, name string, value bool, usage string)
- func (set *FlagSet) Duration(name string, value time.Duration, usage string) *time.Duration
- func (set *FlagSet) DurationVar(p *time.Duration, name string, value time.Duration, usage string)
- func (set *FlagSet) ErrorHandling() flag.ErrorHandling
- func (set *FlagSet) Float64(name string, value float64, usage string) *float64
- func (set *FlagSet) Float64Var(p *float64, name string, value float64, usage string)
- func (set *FlagSet) Int(name string, value int, usage string) *int
- func (set *FlagSet) Int64(name string, value int64, usage string) *int64
- func (set *FlagSet) Int64Var(p *int64, name string, value int64, usage string)
- func (set *FlagSet) IntVar(p *int, name string, value int, usage string)
- func (set *FlagSet) Lookup(name string) *Flag
- func (set *FlagSet) NArg() int
- func (set *FlagSet) NFlag() int
- func (set *FlagSet) Output() io.Writer
- func (set *FlagSet) Parse() error
- func (set *FlagSet) ParseSlice(args []string) (err error)
- func (set *FlagSet) Parsed() bool
- func (set *FlagSet) PrintDefaults()
- func (set *FlagSet) Set(name, value string) error
- func (set *FlagSet) SetOutput(output io.Writer)
- func (set *FlagSet) String(name string, value string, usage string) *string
- func (set *FlagSet) StringVar(p *string, name string, value string, usage string)
- func (set *FlagSet) Uint(name string, value uint, usage string) *uint
- func (set *FlagSet) Uint64(name string, value uint64, usage string) *uint64
- func (set *FlagSet) Uint64Var(p *uint64, name string, value uint64, usage string)
- func (set *FlagSet) UintVar(p *uint, name string, value uint, usage string)
- func (set *FlagSet) Var(value flag.Value, name string, usage string)
- func (set *FlagSet) Visit(fn func(*Flag))
- func (set *FlagSet) VisitAll(fn func(*Flag))
- type MissingOptionError
- type Option
- type UnknownOptionError
Constants ¶
This section is empty.
Variables ¶
var CommandLine = NewFlagSet(os.Args[0], flag.ExitOnError)
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.
var Usage = CommandLine.Usage
Usage prints a usage message documenting all defined command-line flags to 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 ¶
func Arg ¶
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.
func Bool ¶
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.
func BoolVar ¶
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.
func Duration ¶
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.
func DurationVar ¶
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.
func Float64 ¶
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.
func Float64Var ¶
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 Int ¶
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.
func Int64 ¶
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.
func Int64Var ¶
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.
func IntVar ¶
IntVar defines a int flag with specified name, default value, and usage string. The argument p points to a int variable in which to store the value of the flag.
func NArg ¶
func NArg() int
NArg is the number of arguments remaining after flags have been processed.
func Parse ¶
func Parse() error
Parse parses the command-line flags from os.Args. Must be called after all flags are defined and before flags are accessed by the program.
func PrintDefaults ¶
func PrintDefaults()
PrintDefaults prints, to standard error unless configured otherwise, a usage message showing the default settings of all defined command-line flags.
func SetOutput ¶
SetOutput sets the destination for usage and error messages. If output is nil, os.Stderr is used.
func String ¶
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.
func StringVar ¶
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.
func Uint ¶
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.
func Uint64 ¶
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.
func Uint64Var ¶
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.
func UintVar ¶
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.
func Var ¶
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.
Types ¶
type Flag ¶
type Flag struct { Name string Rune rune Value flag.Value Usage string // contains filtered or unexported fields }
A Flag represents the state of a flag.
type FlagSet ¶
type FlagSet struct { Usage func() // contains filtered or unexported fields }
A FlagSet represents a set of defined flags. The zero value of a FlagSet has no name and has ContinueOnError error handling.
func NewFlagSet ¶
func NewFlagSet(name string, err flag.ErrorHandling) *FlagSet
NewFlagSet returns a new, empty flag set.
func (*FlagSet) Arg ¶
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.
func (*FlagSet) Bool ¶
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.
func (*FlagSet) BoolVar ¶
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.
func (*FlagSet) Duration ¶
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.
func (*FlagSet) DurationVar ¶
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.
func (*FlagSet) ErrorHandling ¶
func (set *FlagSet) ErrorHandling() flag.ErrorHandling
ErrorHandling returns the error handling behavior of the flag set.
func (*FlagSet) Float64 ¶
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.
func (*FlagSet) Float64Var ¶
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 (*FlagSet) Int ¶
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.
func (*FlagSet) Int64 ¶
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.
func (*FlagSet) Int64Var ¶
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.
func (*FlagSet) IntVar ¶
IntVar defines a int flag with specified name, default value, and usage string. The argument p points to a int variable in which to store the value of the flag.
func (*FlagSet) Lookup ¶
Lookup returns the Flag structure of the named flag, returning nil if none exists.
func (*FlagSet) Output ¶
Output returns the destination for usage and error messages. os.Stderr is returned if output was not set or was set to nil.
func (*FlagSet) Parse ¶
Parse parses the command-line flags from os.Args. Must be called after all flags are defined and before flags are accessed by the program.
func (*FlagSet) ParseSlice ¶
ParseSlice parses the command-line flags from args. Must be called after all flags are defined and before flags are accessed by the program.
func (*FlagSet) PrintDefaults ¶
func (set *FlagSet) PrintDefaults()
PrintDefaults prints, to standard error unless configured otherwise, a usage message showing the default settings of all defined command-line flags.
func (*FlagSet) SetOutput ¶
SetOutput sets the destination for usage and error messages. If output is nil, os.Stderr is used.
func (*FlagSet) String ¶
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.
func (*FlagSet) StringVar ¶
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.
func (*FlagSet) Uint ¶
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.
func (*FlagSet) Uint64 ¶
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.
func (*FlagSet) Uint64Var ¶
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.
func (*FlagSet) UintVar ¶
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.
func (*FlagSet) Var ¶
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.
type MissingOptionError ¶
type MissingOptionError rune
This is returned when an option with a mandatory argument is missing that argument.
func (MissingOptionError) Error ¶
func (e MissingOptionError) Error() string
type Option ¶
In the case of "-o example", Option is 'o' and "example" is Value. For options which do not take an argument, Value is "".
type UnknownOptionError ¶
type UnknownOptionError rune
This is returned when an unknown option is found in argv, but not in the option spec.
func (UnknownOptionError) Error ¶
func (e UnknownOptionError) Error() string