Documentation ¶
Overview ¶
Package getopt provides a minimal, getopt(3)-like argument parsing implementation with POSIX compatible semantics.
Index ¶
- type InvalidOptionError
- type MissingArgumentError
- type Option
- func (o *Option) Float32() (float32, error)
- func (o *Option) Float64() (float64, error)
- func (o *Option) HasArg() bool
- func (o *Option) Int() (int, error)
- func (o *Option) Int32() (int32, error)
- func (o *Option) Int64() (int64, error)
- func (o *Option) String() string
- func (o *Option) Uint() (uint, error)
- func (o *Option) Uint32() (uint32, error)
- func (o *Option) Uint64() (uint64, error)
- type Scanner
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type InvalidOptionError ¶
type InvalidOptionError byte
InvalidOptionError is returned when scanner encounters an option not listed in optstring.
func (InvalidOptionError) Error ¶
func (e InvalidOptionError) Error() string
type MissingArgumentError ¶
type MissingArgumentError byte
MissingArgumentError is returned when option is missing a required argument.
func (MissingArgumentError) Error ¶
func (e MissingArgumentError) Error() string
type Scanner ¶
type Scanner struct {
// contains filtered or unexported fields
}
Scanner contains option scanner data.
func New ¶
New returns a new options scanner using os.Args as the command line arguments source. The option string optstring may contain the following elements: individual characters, and characters followed by a colon to indicate an option argument is to follow. If optstring starts with ':' then all option argument are treated as optional.
func NewArgv ¶
New returns a new options scanner using passed argv as the command line argument source. The option string optstring may contain the following elements: individual characters, and characters followed by a colon to indicate an option argument is to follow. If optstring starts with ':' then all option argument are treated as optional.
Example ¶
scanner, err := NewArgv("a:bz::v", []string{"getopt", "-ba42", "-v", "-z", "--", "-w", "arg1", "arg2"}) if err != nil { panic("error creating scanner: " + err.Error()) } for scanner.Scan() { opt, err := scanner.Option() if err != nil { panic("error: " + err.Error()) } if opt.HasArg() { fmt.Printf("%s: got option %q with arg %q\n", scanner.ProgramName(), opt.Opt, opt) } else { fmt.Printf("%s: got option %q\n", scanner.ProgramName(), opt.Opt) } } fmt.Printf("%s: remaining arguments: %q\n", scanner.ProgramName(), scanner.Args())
Output: getopt: got option 'b' getopt: got option 'a' with arg "42" getopt: got option 'v' getopt: got option 'z' getopt: remaining arguments: ["-w" "arg1" "arg2"]
func (*Scanner) Option ¶
Option returns the next option or an error when it encounters an unknown option or an option that is missing a required argument. If optstring starts with ':' then all arguments are treated as optional and missing arguments do not cause errors.
func (*Scanner) ProgramName ¶
ProgramName returns basename of argv[0].