flag

package module
v0.0.19 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2022 License: MIT Imports: 15 Imported by: 50

Documentation

Index

Constants

This section is empty.

Variables

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

ErrHelp is returned if the help flag is specified.

Functions

func Add

func Add(flags ...Flag)

Add adds the given flags to the standard flag set.

func AddSet

func AddSet(s Set)

AddSet adds a given flagset.

func AddSets

func AddSets(S ...Set)

AddSets adds the given flagsets.

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

func NArg

func NArg() int

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

func Parse

func Parse()

Parse parses flags and command-line arguments. It then calls Validate for each registered flag set in turn. On parse or validation error, Parse prints the error and a usage message to os.Stderr and exits (via os.Exit) with a non-zero error code. If the help flag (-h or -help) is provided then Parse prints a usage message to os.Stderr and exits (via os.Exit) with error code zero. For more control of behaviour on error, see ParseWithErr. Exactly one of Parse and ParseWithErr should be called, after all flags and sets have been registered.

func ParseWithErr

func ParseWithErr() error

ParseWithErr parses flags and command-line arguments. It then calls Validate for each registered flag set in turn. It returns ErrHelp if the help flag (-h or -help) was present. Exactly one of Parse and ParseWithErr should be called, after all flags and sets have been registered.

func SetGlobalFooter

func SetGlobalFooter(s string)

SetGlobalFooter sets the global footer for the usage message.

func SetGlobalHeader

func SetGlobalHeader(s string)

SetGlobalHeader sets the global header for the usage message.

func SetName

func SetName(name string)

SetName sets the name of the standard flag set.

func SetUsageFooter

func SetUsageFooter(s string)

SetUsageFooter sets the footer in the usage message for the standard flag set.

func SetUsageHeader

func SetUsageHeader(s string)

SetUsageHeader sets the header in the usage message for the standard flag set.

func SetValidate

func SetValidate(f func() error)

SetValidate sets the validation function for the standard flag set

func Usage

func Usage()

Usage prints a usage message to os.Stderr.

Types

type BasicSet

type BasicSet struct {
	// contains filtered or unexported fields
}

BasicSet is a basic flag set.

func NewBasicSet

func NewBasicSet(name string, flags ...Flag) *BasicSet

NewBasicSet defines a FlagSet with the given name and flags, and trivial Validate method.

func (*BasicSet) Add

func (S *BasicSet) Add(f ...Flag)

Add adds the specified flags to s.

func (*BasicSet) Flags

func (S *BasicSet) Flags() []Flag

Flags returns the flags in the set, in lexicographic order. If there are no flags in the set, it returns a non-nil empty slice.

func (*BasicSet) Name

func (S *BasicSet) Name() string

Name returns the name of the collection of flags s.

func (*BasicSet) SetUsageFooter

func (S *BasicSet) SetUsageFooter(s string)

SetUsageFooter sets the footer text for the usage message for this flag set.

func (*BasicSet) SetUsageHeader

func (S *BasicSet) SetUsageHeader(s string)

SetUsageHeader sets the header text for the usage message for this flag set.

func (*BasicSet) SetValidate

func (S *BasicSet) SetValidate(f func() error)

SetValidate sets the validation function.

func (*BasicSet) UsageFooter

func (S *BasicSet) UsageFooter() string

UsageFooter returns the footer text in the usage message (if any).

func (*BasicSet) UsageHeader

func (S *BasicSet) UsageHeader() string

UsageHeader returns the header text in the usage message (if any).

func (*BasicSet) Validate

func (S *BasicSet) Validate() error

Validate validates the flag set.

type Flag

type Flag interface {
	Name() string        // Name returns the flag name (without leading hyphen).
	Description() string // Description returns a one-line description of this variable.
	Usage() string       // Usage returns long-form usage text (or the empty string if not required).
	Parse(string) error  // Parse parses the string.
}

Flag is the interface a flag satisfies.

A Flag can optionally satisfy the interface

type isBooleaner struct {
	// IsBoolean returns true if and only if this flag is a boolean
	flag in the sense of the flag package.
	IsBoolean() bool
}

to indicate whether or not it is a boolean flag in the sense of the flag package.

A Flag can optionally satisfy the interface

type alternativeNamer interface {
	// AlternativeName returns an alternative flag name (without leading
	// hyphen).
	AlternativeName() string
}

to provide an additional flag name (without leading hyphen) to Name.

func Bool

func Bool(name string, b *bool, def bool, description string, usage string) Flag

Bool returns a Flag that represents a boolean flag with the given name, description, usage string, backing variable, and default value.

func Duration

func Duration(name string, d *time.Duration, def time.Duration, description string, usage string) Flag

Duration returns a Flag that represents a time.Duration-valued flag with the given name, description, and usage string. It has backing variable d, and default value def.

func Func added in v0.0.19

func Func(name string, description string, usage string, fn func(string) error) Flag

Func returns a Flag with the given name, description, and usage string. Each time the flag is seen, fn is called with the value of the flag. If fn returns a non-nil error, it will be treated as a flag value parsing error.

func Int

func Int(name string, n *int, def int, description string, usage string) Flag

Int returns a Flag that represents an integer flag with the given name, description, usage string, backing variable, and default value.

func Path

func Path(name string, s *string, def string, description string, usage string) Flag

Path returns a Flag that represents a file path flag with the given name, description, and usage string. It has backing variable s, and default value def.

func String

func String(name string, s *string, def string, description string, usage string) Flag

String returns a Flag that represents a string-valued flag with the given name, description, and usage string. It has backing variable s, and default value def.

func StringNoDefault added in v0.0.16

func StringNoDefault(name string, s *string, description string, usage string) Flag

StringNoDefault returns a Flag that represents a string-valued flag with the given name, description, and usage string. It has backing variable s.

func TextVar added in v0.0.19

func TextVar(name string, p encoding.TextUnmarshaler, value encoding.TextMarshaler, description string, usage string) Flag

TextVar returns a Flag with the given name, description, default value, and usage string. The argument p must be a pointer to a variable that will hold the value of the flag, and p must implement encoding.TextUnmarshaler. If the flag is used, the flag value will be passed to p's UnmarshalText method. The type of the default value must be the same as the type of p.

type Set

type Set interface {
	Flags() []Flag       // Flags returns the members of the set, in lexicographic order.
	Name() string        // Name returns the name of this collection of flags (or the empty string if not required).
	UsageFooter() string // UsageFooter returns the footer for the usage message for this flag set.
	UsageHeader() string // UsageHeader returns the header for the usage message for this flag set.
	Validate() error     // Validate validates this flag set.
}

Set is the interface describing a set of flags

func Merge

func Merge(name string, S ...Set) Set

Merge amalgamates the flag sets in S, returning them as a single flag set with the given name. The usage header and footer messages are concatenated. The Validate method of the returned flag set is obtained by calling s.Validate() for each member s of S in turn and returning the first non-nil result (if any).

Jump to

Keyboard shortcuts

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