getopt: git.sr.ht/~sircmpwn/getopt Index | Files

package getopt

import "git.sr.ht/~sircmpwn/getopt"

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 flag[0]-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)
	}
}

[0]: https://golang.org/pkg/flag/

Index

Package Files

bool.go commandline.go duration.go flagset.go float.go getopt.go int.go string.go uint.go

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.

func Arg

func Arg(i int) string

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 Args

func Args() []string

Args returns the non-flag command-line arguments.

func Bool

func Bool(name string, value bool, usage string) *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

func BoolVar(p *bool, name string, value bool, usage string)

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

func Duration(name string, value time.Duration, usage string) *time.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

func DurationVar(p *time.Duration, name string, value time.Duration, usage string)

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

func Float64(name string, value float64, usage string) *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

func Float64Var(p *float64, name string, value float64, usage string)

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

func Int(name string, value int, usage string) *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

func Int64(name string, value int64, usage string) *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

func Int64Var(p *int64, name string, value int64, usage string)

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

func IntVar(p *int, name string, value int, usage string)

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 NFlag

func NFlag() int

NFlag returns the number of command-line flags that have been set.

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 Parsed

func Parsed() bool

Parsed reports whether the command-line flags have been parsed.

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 Set

func Set(name, value string) error

Set sets the value of the named flag.

func SetOutput

func SetOutput(output io.Writer)

SetOutput sets the destination for usage and error messages. If output is nil, os.Stderr is used.

func String

func String(name string, value string, usage string) *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

func StringVar(p *string, name string, value string, usage string)

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

func Uint(name string, value uint, usage string) *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

func Uint64(name string, value uint64, usage string) *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

func Uint64Var(p *uint64, name string, value uint64, usage string)

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

func UintVar(p *uint, name string, value uint, usage string)

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

func Var(value flag.Value, name string, usage string)

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 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.

func Lookup

func Lookup(name string) *Flag

Lookup returns the Flag structure of the named flag, returning nil if none exists.

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

func (set *FlagSet) Arg(i int) string

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) Args

func (set *FlagSet) Args() []string

Args returns the non-flag command-line arguments.

func (*FlagSet) Bool

func (set *FlagSet) Bool(name string, value bool, usage string) *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

func (set *FlagSet) BoolVar(p *bool, name string, value bool, usage string)

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

func (set *FlagSet) Duration(name string, value time.Duration, usage string) *time.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

func (set *FlagSet) DurationVar(p *time.Duration, name string, value time.Duration, usage string)

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

func (set *FlagSet) Float64(name string, value float64, usage string) *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

func (set *FlagSet) Float64Var(p *float64, name string, value float64, usage string)

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

func (set *FlagSet) Int(name string, value int, usage string) *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

func (set *FlagSet) Int64(name string, value int64, usage string) *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

func (set *FlagSet) Int64Var(p *int64, name string, value int64, usage string)

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

func (set *FlagSet) IntVar(p *int, name string, value int, usage string)

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

func (set *FlagSet) Lookup(name string) *Flag

Lookup returns the Flag structure of the named flag, returning nil if none exists.

func (*FlagSet) NArg

func (set *FlagSet) NArg() int

NArg is the number of arguments remaining after flags have been processed.

func (*FlagSet) NFlag

func (set *FlagSet) NFlag() int

NFlag returns the number of command-line flags that have been set.

func (*FlagSet) Output

func (set *FlagSet) Output() io.Writer

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

func (set *FlagSet) 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 (*FlagSet) ParseSlice

func (set *FlagSet) ParseSlice(args []string) (err error)

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) Parsed

func (set *FlagSet) Parsed() bool

Parsed reports whether the command-line flags have been parsed.

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) Set

func (set *FlagSet) Set(name, value string) error

Set sets the value of the named flag.

func (*FlagSet) SetOutput

func (set *FlagSet) SetOutput(output io.Writer)

SetOutput sets the destination for usage and error messages. If output is nil, os.Stderr is used.

func (*FlagSet) String

func (set *FlagSet) String(name string, value string, usage string) *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

func (set *FlagSet) StringVar(p *string, name string, value string, usage string)

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

func (set *FlagSet) Uint(name string, value uint, usage string) *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

func (set *FlagSet) Uint64(name string, value uint64, usage string) *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

func (set *FlagSet) Uint64Var(p *uint64, name string, value uint64, usage string)

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

func (set *FlagSet) UintVar(p *uint, name string, value uint, usage string)

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

func (set *FlagSet) Var(value flag.Value, name string, usage string)

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.

func (*FlagSet) Visit

func (set *FlagSet) Visit(fn func(*Flag))

Visit visits the command-line flags in lexicographical order, calling fn for each.

func (*FlagSet) VisitAll

func (set *FlagSet) VisitAll(fn func(*Flag))

VisitAll visits the command-line flags in lexicographical order, calling fn for each. It visits all flags, even those not set.

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

type Option struct {
    Option rune
    Value  string
}

In the case of "-o example", Option is 'o' and "example" is Value. For options which do not take an argument, Value is "".

func Getopts

func Getopts(argv []string, spec string) ([]Option, int, error)

Getopts implements a POSIX-compatible options interface.

Returns a slice of options and the index of the first non-option argument.

If an error is returned, you must print it to stderr to be POSIX complaint.

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

Package getopt imports 9 packages (graph).

Version v1.0.0 (latest) | Published Dec 18, 2020 | Platform: linux/amd64 | Updated 1 month ago

.
Tools for package owners.