gflag

package
v0.5.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 28, 2018 License: BSD-3-Clause Imports: 7 Imported by: 0

Documentation

Overview

Package gflag implements GNU-style command line flag parsing. It supports the transformation of programs using the Go standard library flag package. However it doesn't target full compatibility with the Go standard library flag package. The Flag structure doesn't support all fields of the flag package and the Var method and function does have a different signature.

The typical use case looks like this:

  b := Bool("flag-b", "b", false, "boolean flag")
  h := Bool("help", "h", false, "prints this message")

  Parse()

  if *h {
	  gflag.Usage()
  }

Index

Constants

This section is empty.

Variables

View Source
var CommandLine = NewFlagSet(os.Args[0], ExitOnError)

CommandLine is the default set of command-line flags parsed from os.Args. The top-level functions such as BoolVar, Arg, etc. are wrappers for the methods of command line.

View Source
var Usage = func() {
	fmt.Fprintf(CommandLine.out(), "Usage of %s:\n", os.Args[0])
	PrintDefaults()
}

Usage prints the default usage message.

Functions

func Arg

func Arg(i int) string

Arg provides the argument number i after parsing of the command line flags.

func Args

func Args() []string

Args returns all arguments after the command line flags have been parsed.

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 BoolP

func BoolP(name, shorthands string, value bool, usage string) *bool

BoolP defines a bool flag with specified name, shorthands, 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 BoolVarP

func BoolVarP(p *bool, name, shorthands string, value bool, usage string)

BoolVarP defines a bool flag with specified name, shorthands, default value and usage string. The argument p points to a bool variable in which to store the value of the flag.

func Counter

func Counter(name string, value int, usage string) *int

Counter defines a counter flag with specified name, default value and usage string. The return value is the address of an integer variable that stores the value of the flag.

func CounterP

func CounterP(name, shorthands string, value int, usage string) *int

CounterP defines a counter flag with specified name, shorthands, default value and usage string. The return value is the address of an integer variable that stores the value of the flag.

func CounterVar

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

CounterVar defines a counter flag with specified name, default value and usage string. The argument p points to an integer variable in which to store the value of the flag.

func CounterVarP

func CounterVarP(p *int, name, shorthands string, value int, usage string)

CounterVarP defines a counter flag with specified name, shorthands, default value and usage string. The argument p points to an integer variable in which to store the value of the flag.

func Int

func Int(name string, value int, usage string) *int

Int defines an integer flag with specified name, default value and usage string. The return value is the address of an integer variable that stores the value of the flag.

func IntP

func IntP(name, shorthands string, value int, usage string) *int

IntP defines an integer flag with specified name, shorthands, default value and usage string. The return value is the address of an integer variable that stores the value of the flag.

func IntVar

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

IntVar defines an integer flag with specified name, default value and usage string. The argument p points to an integer variable in which to store the value of the flag.

func IntVarP

func IntVarP(p *int, name, shorthands string, value int, usage string)

IntVarP defines an integer flag with specified name, shorthands, default value and usage string. The argument p points to an integer variable in which to store the value of the flag.

func NArg

func NArg() int

NArg returns the number of remaining arguments after command line parsing.

func Parse

func Parse()

Parse parses the command line.

func Parsed

func Parsed() bool

Parsed returns whether the command line has already been parsed.

func Preset

func Preset(start, end, value int, usage string) *int

Preset defines a range of preset flags starting at start and ending at end. The return value is the address of a preset variable in which to store the value of the flag.

If start is 1 and end is 9 the flags -1 to -9 will be supported.

func PresetVar

func PresetVar(p *int, start, end, value int, usage string)

PresetVar defines a range of preset flags starting at start and ending at end. The argument p points to a preset variable in which to store the value of the flag.

If start is 1 and end is 9 the flags -1 to -9 will be supported.

func PrintDefaults

func PrintDefaults()

PrintDefaults prints the information about all command line flags.

func String

func String(name, value, 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 StringP

func StringP(name, shorthands, value, usage string) *string

StringP defines a string flag with specified name, shorthands, 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, value, 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 StringVarP

func StringVarP(p *string, name, shorthands, value, usage string)

StringVarP defines an string flag with specified name, shorthands, default value and usage string. The argument p points to a string variable in which to store the value of the flag.

func Var

func Var(value Value, name string, hasArg HasArg)

Var creates a flag for the given option name for the command line.

func VarP

func VarP(value Value, name, shorthands string, hasArg HasArg)

VarP creates a flag for the given value for the command line.

Types

type ErrorHandling

type ErrorHandling int

ErrorHandling defines how flag parsing errors are handled.

const (
	ContinueOnError ErrorHandling = iota
	ExitOnError
	PanicOnError
)

The constants define how errors should be handled.

type Flag

type Flag struct {
	Name       string
	Shorthands string
	HasArg     HasArg
	Value      Value
}

Flag represents a single flag.

type FlagSet

type FlagSet struct {
	// Provides a custom usage function if set.
	Usage func()
	// contains filtered or unexported fields
}

FlagSet represents a set of option flags.

func NewFlagSet

func NewFlagSet(name string, errorHandling ErrorHandling) *FlagSet

NewFlagSet creates a new flag set.

func (*FlagSet) Arg

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

Arg returns the argument number i after parsing has been successful.

func (*FlagSet) Args

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

Args returns all arguments after parsing.

func (*FlagSet) Bool

func (f *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) BoolP

func (f *FlagSet) BoolP(name, shorthands string, value bool, usage string) *bool

BoolP defines a bool flag with specified name, shorthands, 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 (f *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) BoolVarP

func (f *FlagSet) BoolVarP(p *bool, name, shorthands string, value bool, usage string)

BoolVarP defines a bool flag with specified name, shorthands, default value and usage string. The argument p points to a bool variable in which to store the value of the flag.

func (*FlagSet) Counter

func (f *FlagSet) Counter(name string, value int, usage string) *int

Counter defines a counter flag with specified name, default value and usage string. The return value is the address of an integer variable that stores the value of the flag.

func (*FlagSet) CounterP

func (f *FlagSet) CounterP(name, shorthands string, value int, usage string) *int

CounterP defines a counter flag with specified name, shorthands, default value and usage string. The return value is the address of an integer variable that stores the value of the flag.

func (*FlagSet) CounterVar

func (f *FlagSet) CounterVar(p *int, name string, value int, usage string)

CounterVar defines a counter flag with specified name, default value and usage string. The argument p points to an integer variable in which to store the value of the flag.

func (*FlagSet) CounterVarP

func (f *FlagSet) CounterVarP(p *int, name, shorthands string, value int, usage string)

CounterVarP defines a counter flag with specified name, shorthands, default value and usage string. The argument p points to an integer variable in which to store the value of the flag.

func (*FlagSet) Init

func (f *FlagSet) Init(name string, errorHandling ErrorHandling)

Init initializes a flag set variable.

func (*FlagSet) Int

func (f *FlagSet) Int(name string, value int, usage string) *int

Int defines an integer flag with specified name, default value and usage string. The return value is the address of an integer variable that stores the value of the flag.

func (*FlagSet) IntP

func (f *FlagSet) IntP(name, shorthands string, value int, usage string) *int

IntP defines an integer flag with specified name, shorthands, default value and usage string. The return value is the address of an integer variable that stores the value of the flag.

func (*FlagSet) IntVar

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

IntVar defines an integer flag with specified name, default value and usage string. The argument p points to an integer variable in which to store the value of the flag.

func (*FlagSet) IntVarP

func (f *FlagSet) IntVarP(p *int, name, shorthands string, value int, usage string)

IntVarP defines an integer flag with specified name, shorthands, default value and usage string. The argument p points to an integer variable in which to store the value of the flag.

func (*FlagSet) NArg

func (f *FlagSet) NArg() int

NArg returns the number of remaining arguments after parsing.

func (*FlagSet) Parse

func (f *FlagSet) Parse(arguments []string) error

Parse parses the arguments. If an error happens the error is printed as well as the usage information.

func (*FlagSet) Parsed

func (f *FlagSet) Parsed() bool

Parsed returns whether the flag set has already been parsed.

func (*FlagSet) Preset

func (f *FlagSet) Preset(start, end, value int, usage string) *int

Preset defines a range of preset flags starting at start and ending at end. The return value is the address of a preset variable in which to store the value of the flag.

If start is 1 and end is 9 the flags -1 to -9 will be supported.

func (*FlagSet) PresetVar

func (f *FlagSet) PresetVar(p *int, start, end, value int, usage string)

PresetVar defines a range of preset flags starting at start and ending at end. The argument p points to a preset variable in which to store the value of the flag.

If start is 1 and end is 9 the flags -1 to -9 will be supported.

func (*FlagSet) PrintDefaults

func (f *FlagSet) PrintDefaults()

PrintDefaults prints information about all flags.

func (*FlagSet) SetOutput

func (f *FlagSet) SetOutput(w io.Writer)

SetOutput sets the default output writer for the flag set.

func (*FlagSet) String

func (f *FlagSet) String(name, value, 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) StringP

func (f *FlagSet) StringP(name, shorthands, value, usage string) *string

StringP defines a string flag with specified name, shorthands, 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 (f *FlagSet) StringVar(p *string, name, value, 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) StringVarP

func (f *FlagSet) StringVarP(p *string, name, shorthands, value, usage string)

StringVarP defines an string flag with specified name, shorthands, default value and usage string. The argument p points to a string variable in which to store the value of the flag.

func (*FlagSet) Var

func (f *FlagSet) Var(value Value, name string, hasArg HasArg)

Var creates a flag for the given option name.

func (*FlagSet) VarP

func (f *FlagSet) VarP(value Value, name, shorthands string, hasArg HasArg)

VarP creates a flag with a long and shorthand options.

type HasArg

type HasArg int

HasArg defines whether a flag argument is required, optional or not supported.

const (
	RequiredArg HasArg = iota
	NoArg
	OptionalArg
)

The constants define whether a flag argument is required, not supported or optional.

type Value

type Value interface {
	Set(string) error
	Update()
	Get() interface{}
	String() string
}

Value is the interface to the value of a specific flag.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL