mflag

package
v1.11.0 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2016 License: Apache-2.0, BSD-3-Clause Imports: 11 Imported by: 0

README

Package mflag (aka multiple-flag) implements command-line flag parsing.
It's an hacky fork of the official golang package

It adds:

  • both short and long flag version
    ./example -s red ./example --string blue

  • multiple names for the same option

$>./example -h
Usage of example:
  -s, --string="": a simple string

It is very flexible on purpose, so you can do things like:

$>./example -h
Usage of example:
  -s, -string, --string="": a simple string

Or:

$>./example -h
Usage of example:
  -oldflag, --newflag="": a simple string

You can also hide some flags from the usage, so if we want only --newflag:

$>./example -h
Usage of example:
  --newflag="": a simple string
$>./example -oldflag str
str

See example.go for more details.

Documentation

Index

Constants

View Source
const (
	Exact nArgRequirementType = iota
	Max
	Min
)

Indicator used to pass to BadArgs function

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, and on are wrappers for the methods of CommandLine.

View Source
var ErrHelp = errors.New("flag: help requested")

ErrHelp is the error returned if the flag -help is invoked but no such flag is defined.

View Source
var ErrRetry = errors.New("flag: retry")

ErrRetry is the error returned if you need to try letter by letter

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

ShortUsage prints to standard error a usage message documenting the standard command layout The function is a variable that may be changed to point to a custom function.

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

Usage prints to standard error a usage message documenting all defined command-line flags. The function is a variable that may be changed to point to a custom function.

Functions

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.

func Args

func Args() []string

Args returns the non-flag command-line arguments.

func Bool

func Bool(names []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, names []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(names []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.

func DurationVar

func DurationVar(p *time.Duration, names []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.

func Float64

func Float64(names []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, names []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(names []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(names []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, names []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, names []string, value int, usage string)

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

func IsSet added in v1.4.0

func IsSet(name string) bool

IsSet indicates whether the specified flag was specified at all on the cmd line.

func Merge added in v1.8.0

func Merge(dest *FlagSet, flagsets ...*FlagSet) error

Merge is an helper function that merges n FlagSets into a single dest FlagSet In case of name collision between the flagsets it will apply the destination FlagSet's errorHandling behavior.

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

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.

func Parsed

func Parsed() bool

Parsed returns true if the command-line flags have been parsed.

func PrintDefaults

func PrintDefaults()

PrintDefaults prints to standard error the default values of all defined command-line flags.

func Set

func Set(name, value string) error

Set sets the value of the named command-line flag.

func String

func String(names []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, names []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(names []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 Uint16 added in v1.9.0

func Uint16(names []string, value uint16, usage string) *uint16

Uint16 defines a uint16 flag with specified name, default value, and usage string. The return value is the address of a uint16 variable that stores the value of the flag.

func Uint16Var added in v1.9.0

func Uint16Var(p *uint16, names []string, value uint16, usage string)

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

func Uint64

func Uint64(names []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, names []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, names []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 Value, names []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 Visit

func Visit(fn func(*Flag))

Visit visits the command-line flags in lexicographical order, calling fn for each. It visits only those flags that have been set.

func VisitAll

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

Types

type ErrorHandling

type ErrorHandling int

ErrorHandling defines how to handle flag parsing errors.

const (
	ContinueOnError ErrorHandling = iota
	ExitOnError
	PanicOnError
)

ErrorHandling strategies available when a flag parsing error occurs

type Flag

type Flag struct {
	Names    []string // name as it appears on command line
	Usage    string   // help message
	Value    Value    // value as set
	DefValue string   // default value (as text); for usage message
}

A Flag represents the state of a flag.

func Lookup

func Lookup(name string) *Flag

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

type FlagSet

type FlagSet struct {
	// Usage is the function called when an error occurs while parsing flags.
	// The field is a function (not a method) that may be changed to point to
	// a custom error handler.
	Usage      func()
	ShortUsage 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, errorHandling ErrorHandling) *FlagSet

NewFlagSet returns a new, empty flag set with the specified name and error handling property.

func (*FlagSet) Arg

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

Arg returns the i'th argument. Arg(0) is the first remaining argument after flags have been processed.

func (*FlagSet) Args

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

Args returns the non-flag arguments.

func (*FlagSet) Bool

func (fs *FlagSet) Bool(names []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 (fs *FlagSet) BoolVar(p *bool, names []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) CheckArgs added in v1.5.0

func (fs *FlagSet) CheckArgs() (message string)

CheckArgs uses the requirements set by FlagSet.Require() to validate the number of arguments. If the requirements are not met, an error message string is returned.

func (*FlagSet) Duration

func (fs *FlagSet) Duration(names []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.

func (*FlagSet) DurationVar

func (fs *FlagSet) DurationVar(p *time.Duration, names []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.

func (*FlagSet) FlagCount added in v1.3.0

func (fs *FlagSet) FlagCount() int

FlagCount returns the number of flags that have been defined.

func (*FlagSet) FlagCountUndeprecated added in v1.3.0

func (fs *FlagSet) FlagCountUndeprecated() int

FlagCountUndeprecated returns the number of undeprecated flags that have been defined.

func (*FlagSet) Float64

func (fs *FlagSet) Float64(names []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 (fs *FlagSet) Float64Var(p *float64, names []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) Init

func (fs *FlagSet) Init(name string, errorHandling 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.

func (*FlagSet) Int

func (fs *FlagSet) Int(names []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 (fs *FlagSet) Int64(names []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 (fs *FlagSet) Int64Var(p *int64, names []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 (fs *FlagSet) IntVar(p *int, names []string, value int, usage string)

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

func (*FlagSet) IsEmpty added in v1.8.0

func (fs *FlagSet) IsEmpty() bool

IsEmpty reports if the FlagSet is actually empty.

func (*FlagSet) IsSet added in v1.4.0

func (fs *FlagSet) IsSet(name string) bool

IsSet indicates whether the specified flag is set in the given FlagSet

func (*FlagSet) Lookup

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

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

func (*FlagSet) NArg

func (fs *FlagSet) NArg() int

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

func (*FlagSet) NFlag

func (fs *FlagSet) NFlag() int

NFlag returns the number of flags that have been set.

func (*FlagSet) Name added in v1.5.0

func (fs *FlagSet) Name() string

Name returns the name of the FlagSet.

func (*FlagSet) Out added in v1.5.0

func (fs *FlagSet) Out() io.Writer

Out returns the destination for usage and error messages.

func (*FlagSet) Parse

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

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 was set but not defined.

func (*FlagSet) ParseFlags added in v1.7.0

func (fs *FlagSet) ParseFlags(args []string, withHelp bool) error

ParseFlags is a utility function that adds a help flag if withHelp is true, calls fs.Parse(args) and prints a relevant error message if there are incorrect number of arguments. It returns error only if error handling is set to ContinueOnError and parsing fails. If error handling is set to ExitOnError, it's safe to ignore the return value.

func (*FlagSet) Parsed

func (fs *FlagSet) Parsed() bool

Parsed reports whether fs.Parse has been called.

func (*FlagSet) PrintDefaults

func (fs *FlagSet) PrintDefaults()

PrintDefaults prints, to standard error unless configured otherwise, the default values of all defined flags in the set.

func (*FlagSet) ReportError added in v1.7.0

func (fs *FlagSet) ReportError(str string, withHelp bool)

ReportError is a utility method that prints a user-friendly message containing the error that occurred during parsing and a suggestion to get help

func (*FlagSet) Require added in v1.5.0

func (fs *FlagSet) Require(nArgRequirementType nArgRequirementType, nArg int)

Require adds a requirement about the number of arguments for the FlagSet. The first parameter can be Exact, Max, or Min to respectively specify the exact, the maximum, or the minimal number of arguments required. The actual check is done in FlagSet.CheckArgs().

func (*FlagSet) Set

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

Set sets the value of the named flag.

func (*FlagSet) SetOutput

func (fs *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 (fs *FlagSet) String(names []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 (fs *FlagSet) StringVar(p *string, names []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 (fs *FlagSet) Uint(names []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) Uint16 added in v1.9.0

func (fs *FlagSet) Uint16(names []string, value uint16, usage string) *uint16

Uint16 defines a uint16 flag with specified name, default value, and usage string. The return value is the address of a uint16 variable that stores the value of the flag.

func (*FlagSet) Uint16Var added in v1.9.0

func (fs *FlagSet) Uint16Var(p *uint16, names []string, value uint16, usage string)

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

func (*FlagSet) Uint64

func (fs *FlagSet) Uint64(names []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 (fs *FlagSet) Uint64Var(p *uint64, names []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 (fs *FlagSet) UintVar(p *uint, names []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 (fs *FlagSet) Var(value Value, names []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 (fs *FlagSet) Visit(fn func(*Flag))

Visit visits the flags in lexicographical order, calling fn for each. It visits only those flags that have been set.

func (*FlagSet) VisitAll

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

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

type Getter

type Getter interface {
	Value
	Get() interface{}
}

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

type Value

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

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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