pflag

package
Version: v1.1.6 Latest Latest
Warning

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

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

README

Build Status

Description

pflag is a drop-in replacement for Go's flag package, implementing POSIX/GNU-style --flags.

pflag is compatible with the GNU extensions to the POSIX recommendations for command-line options. For a more precise description, see the "Command-line flag syntax" section below.

pflag is available under the same style of BSD license as the Go language, which can be found in the LICENSE file.

Installation

pflag is available using the standard go get command.

Install by running:

go get github.com/spf13/pflag

Run tests by running:

go test github.com/spf13/pflag

Usage

pflag is a drop-in replacement of Go's native flag package. If you import pflag under the name "flag" then all code should continue to function with no changes.

import flag "github.com/spf13/pflag"

There is one exception to this: if you directly instantiate the Flag struct there is one more field "Shorthand" that you will need to set. Most code never instantiates this struct directly, and instead uses functions such as String(), BoolVar(), and Var(), and is therefore unaffected.

Define flags using flag.String(), Bool(), Int(), etc.

This declares an integer flag, -flagname, stored in the pointer ip, with type *int.

var ip *int = flag.Int("flagname", 1234, "help message for flagname")

If you like, you can bind the flag to a variable using the Var() functions.

var flagvar int
func init() {
    flag.IntVar(&flagvar, "flagname", 1234, "help message for flagname")
}

Or you can create custom flags that satisfy the Value interface (with pointer receivers) and couple them to flag parsing by

flag.Var(&flagVal, "name", "help message for flagname")

For such flags, the default value is just the initial value of the variable.

After all flags are defined, call

flag.Parse()

to parse the command line into the defined flags.

Flags may then be used directly. If you're using the flags themselves, they are all pointers; if you bind to variables, they're values.

fmt.Println("ip has value ", *ip)
fmt.Println("flagvar has value ", flagvar)

There are helpers function to get values later if you have the FlagSet but it was difficult to keep up with all of the the flag pointers in your code. If you have a pflag.FlagSet with a flag called 'flagname' of type int you can use GetInt() to get the int value. But notice that 'flagname' must exist and it must be an int. GetString("flagname") will fail.

i, err := flagset.GetInt("flagname")

After parsing, the arguments after the flag are available as the slice flag.Args() or individually as flag.Arg(i). The arguments are indexed from 0 through flag.NArg()-1.

The pflag package also defines some new functions that are not in flag, that give one-letter shorthands for flags. You can use these by appending 'P' to the name of any function that defines a flag.

var ip = flag.IntP("flagname", "f", 1234, "help message")
var flagvar bool
func init() {
    flag.BoolVarP("boolname", "b", true, "help message")
}
flag.VarP(&flagVar, "varname", "v", 1234, "help message")

Shorthand letters can be used with single dashes on the command line. Boolean shorthand flags can be combined with other shorthand flags.

The default set of command-line flags is controlled by top-level functions. The FlagSet type allows one to define independent sets of flags, such as to implement subcommands in a command-line interface. The methods of FlagSet are analogous to the top-level functions for the command-line flag set.

Setting no option default values for flags

After you create a flag it is possible to set the pflag.NoOptDefVal for the given flag. Doing this changes the meaning of the flag slightly. If a flag has a NoOptDefVal and the flag is set on the command line without an option the flag will be set to the NoOptDefVal. For example given:

var ip = flag.IntP("flagname", "f", 1234, "help message")
flag.Lookup("flagname").NoOptDefVal = "4321"

Would result in something like

Parsed Arguments Resulting Value
--flagname=1357 ip=1357
--flagname ip=4321
[nothing] ip=1234

Command line flag syntax

--flag    // boolean flags, or flags with no option default values
--flag x  // only on flags without a default value
--flag=x

Unlike the flag package, a single dash before an option means something different than a double dash. Single dashes signify a series of shorthand letters for flags. All but the last shorthand letter must be boolean flags or a flag with a default value

// boolean or flags where the 'no option default value' is set
-f
-f=true
-abc
but
-b true is INVALID

// non-boolean and flags without a 'no option default value'
-n 1234
-n=1234
-n1234

// mixed
-abcs "hello"
-absd="hello"
-abcs1234

Flag parsing stops after the terminator "--". Unlike the flag package, flags can be interspersed with arguments anywhere on the command line before this terminator.

Integer flags accept 1234, 0664, 0x1234 and may be negative. Boolean flags (in their long form) accept 1, 0, t, f, true, false, TRUE, FALSE, True, False. Duration flags accept any input valid for time.ParseDuration.

Mutating or "Normalizing" Flag names

It is possible to set a custom flag name 'normalization function.' It allows flag names to be mutated both when created in the code and when used on the command line to some 'normalized' form. The 'normalized' form is used for comparison. Two examples of using the custom normalization func follow.

Example #1: You want -, _, and . in flags to compare the same. aka --my-flag == --my_flag == --my.flag

func wordSepNormalizeFunc(f *pflag.FlagSet, name string) pflag.NormalizedName {
	from := []string{"-", "_"}
	to := "."
	for _, sep := range from {
		name = strings.Replace(name, sep, to, -1)
	}
	return pflag.NormalizedName(name)
}

myFlagSet.SetNormalizeFunc(wordSepNormalizeFunc)

Example #2: You want to alias two flags. aka --old-flag-name == --new-flag-name

func aliasNormalizeFunc(f *pflag.FlagSet, name string) pflag.NormalizedName {
	switch name {
	case "old-flag-name":
		name = "new-flag-name"
		break
	}
	return pflag.NormalizedName(name)
}

myFlagSet.SetNormalizeFunc(aliasNormalizeFunc)

Deprecating a flag or its shorthand

It is possible to deprecate a flag, or just its shorthand. Deprecating a flag/shorthand hides it from help text and prints a usage message when the deprecated flag/shorthand is used.

Example #1: You want to deprecate a flag named "badflag" as well as inform the users what flag they should use instead.

// deprecate a flag by specifying its name and a usage message
flags.MarkDeprecated("badflag", "please use --good-flag instead")

This hides "badflag" from help text, and prints Flag --badflag has been deprecated, please use --good-flag instead when "badflag" is used.

Example #2: You want to keep a flag name "noshorthandflag" but deprecate its shortname "n".

// deprecate a flag shorthand by specifying its flag name and a usage message
flags.MarkShorthandDeprecated("noshorthandflag", "please use --noshorthandflag only")

This hides the shortname "n" from help text, and prints Flag shorthand -n has been deprecated, please use --noshorthandflag only when the shorthand "n" is used.

Note that usage message is essential here, and it should not be empty.

Hidden flags

It is possible to mark a flag as hidden, meaning it will still function as normal, however will not show up in usage/help text.

Example: You have a flag named "secretFlag" that you need for internal use only and don't want it showing up in help text, or for its usage text to be available.

// hide a flag by specifying its name
flags.MarkHidden("secretFlag")

More info

You can see the full reference documentation of the pflag package at godoc.org, or through go's standard documentation system by running godoc -http=:6060 and browsing to http://localhost:6060/pkg/github.com/ogier/pflag after installation.

Documentation

Overview

Package pflag is a drop-in replacement for Go's flag package, implementing POSIX/GNU-style --flags.

pflag is compatible with the GNU extensions to the POSIX recommendations for command-line options. See http://www.gnu.org/software/libc/manual/html_node/Argument-Syntax.html

Usage:

pflag is a drop-in replacement of Go's native flag package. If you import pflag under the name "flag" then all code should continue to function with no changes.

import flag "github.com/ogier/pflag"

There is one exception to this: if you directly instantiate the Flag struct

there is one more field "Shorthand" that you will need to set. Most code never instantiates this struct directly, and instead uses functions such as String(), BoolVar(), and Var(), and is therefore unaffected.

Define flags using flag.String(), Bool(), Int(), etc.

This declares an integer flag, -flagname, stored in the pointer ip, with type *int.

var ip = flag.Int("flagname", 1234, "help message for flagname")

If you like, you can bind the flag to a variable using the Var() functions.

var flagvar int
func init() {
	flag.IntVar(&flagvar, "flagname", 1234, "help message for flagname")
}

Or you can create custom flags that satisfy the Value interface (with pointer receivers) and couple them to flag parsing by

flag.Var(&flagVal, "name", "help message for flagname")

For such flags, the default value is just the initial value of the variable.

After all flags are defined, call

flag.Parse()

to parse the command line into the defined flags.

Flags may then be used directly. If you're using the flags themselves, they are all pointers; if you bind to variables, they're values.

fmt.Println("ip has value ", *ip)
fmt.Println("flagvar has value ", flagvar)

After parsing, the arguments after the flag are available as the slice flag.Args() or individually as flag.Arg(i). The arguments are indexed from 0 through flag.NArg()-1.

The pflag package also defines some new functions that are not in flag, that give one-letter shorthands for flags. You can use these by appending 'P' to the name of any function that defines a flag.

var ip = flag.IntP("flagname", "f", 1234, "help message")
var flagvar bool
func init() {
	flag.BoolVarP("boolname", "b", true, "help message")
}
flag.VarP(&flagVar, "varname", "v", 1234, "help message")

Shorthand letters can be used with single dashes on the command line. Boolean shorthand flags can be combined with other shorthand flags.

Command line flag syntax:

--flag    // boolean flags only
--flag=x

Unlike the flag package, a single dash before an option means something different than a double dash. Single dashes signify a series of shorthand letters for flags. All but the last shorthand letter must be boolean flags.

// boolean flags
-f
-abc
// non-boolean flags
-n 1234
-Ifile
// mixed
-abcs "hello"
-abcn1234

Flag parsing stops after the terminator "--". Unlike the flag package, flags can be interspersed with arguments anywhere on the command line before this terminator.

Integer flags accept 1234, 0664, 0x1234 and may be negative. Boolean flags (in their long form) accept 1, 0, t, f, true, false, TRUE, FALSE, True, False. Duration flags accept any input valid for time.ParseDuration.

The default set of command-line flags is controlled by top-level functions. The FlagSet type allows one to define independent sets of flags, such as to implement subcommands in a command-line interface. The methods of FlagSet are analogous to the top-level functions for the command-line flag set.

Example
// These examples demonstrate more intricate uses of the flag package.
package main

import (
	"errors"
	"fmt"
	"strings"
	"time"

	flag "github.com/spf13/pflag"
)

// Example 1: A single string flag called "species" with default value "gopher".
var species = flag.String("species", "gopher", "the species we are studying")

// Example 2: A flag with a shorthand letter.
var gopherType = flag.StringP("gopher_type", "g", "pocket", "the variety of gopher")

// Example 3: A user-defined flag type, a slice of durations.
type interval []time.Duration

// String is the method to format the flag's value, part of the flag.Value interface.
// The String method's output will be used in diagnostics.
func (i *interval) String() string {
	return fmt.Sprint(*i)
}

func (i *interval) Type() string {
	return "interval"
}

// Set is the method to set the flag value, part of the flag.Value interface.
// Set's argument is a string to be parsed to set the flag.
// It's a comma-separated list, so we split it.
func (i *interval) Set(value string) error {
	// If we wanted to allow the flag to be set multiple times,
	// accumulating values, we would delete this if statement.
	// That would permit usages such as
	//	-deltaT 10s -deltaT 15s
	// and other combinations.
	if len(*i) > 0 {
		return errors.New("interval flag already set")
	}
	for _, dt := range strings.Split(value, ",") {
		duration, err := time.ParseDuration(dt)
		if err != nil {
			return err
		}
		*i = append(*i, duration)
	}
	return nil
}

// Define a flag to accumulate durations. Because it has a special type,
// we need to use the Var function and therefore create the flag during
// init.

var intervalFlag interval

func init() {
	// Tie the command-line flag to the intervalFlag variable and
	// set a usage message.
	flag.Var(&intervalFlag, "deltaT", "comma-separated list of intervals to use between events")
}

func main() {
	// All the interesting pieces are with the variables declared above, but
	// to enable the flag package to see the flags defined there, one must
	// execute, typically at the start of main (not init!):
	//	flag.Parse()
	// We don't run it here because this is not a main function and
	// the testing suite has already parsed the flags.
}
Output:

Index

Examples

Constants

This section is empty.

Variables

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

The default set of command-line flags, parsed from os.Args.

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

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

View Source
var Usage = func() {
	fmt.Fprintf(os.Stderr, "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(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, shorthand string, value bool, usage string) *bool

BoolP is like Bool, but accepts a shorthand letter that can be used after a single dash.

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, shorthand string, value bool, usage string)

BoolVarP is like BoolVar, but accepts a shorthand letter that can be used after a single dash.

func Count added in v1.0.5

func Count(name string, usage string) *int

Count like Count only the flag is placed on the CommandLine isntead of a given flag set

func CountP added in v1.0.5

func CountP(name, shorthand string, usage string) *int

CountP is like Count only takes a shorthand for the flag name.

func CountVar added in v1.0.5

func CountVar(p *int, name string, usage string)

CountVar like CountVar only the flag is placed on the CommandLine instead of a given flag set

func CountVarP added in v1.0.5

func CountVarP(p *int, name, shorthand string, usage string)

CountVarP is like CountVar only take a shorthand for the flag name.

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.

func DurationP

func DurationP(name, shorthand string, value time.Duration, usage string) *time.Duration

DurationP is like Duration, but accepts a shorthand letter that can be used after a single dash.

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.

func DurationVarP

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

DurationVarP is like DurationVar, but accepts a shorthand letter that can be used after a single dash.

func Float32

func Float32(name string, value float32, usage string) *float32

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

func Float32P

func Float32P(name, shorthand string, value float32, usage string) *float32

Float32P is like Float32, but accepts a shorthand letter that can be used after a single dash.

func Float32Var

func Float32Var(p *float32, name string, value float32, usage string)

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

func Float32VarP

func Float32VarP(p *float32, name, shorthand string, value float32, usage string)

Float32VarP is like Float32Var, but accepts a shorthand letter that can be used after a single dash.

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 Float64P

func Float64P(name, shorthand string, value float64, usage string) *float64

Float64P is like Float64, but accepts a shorthand letter that can be used after a single dash.

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 Float64VarP

func Float64VarP(p *float64, name, shorthand string, value float64, usage string)

Float64VarP is like Float64Var, but accepts a shorthand letter that can be used after a single dash.

func IP

func IP(name string, value net.IP, usage string) *net.IP

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

func IPMask

func IPMask(name string, value net.IPMask, usage string) *net.IPMask

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

func IPMaskP

func IPMaskP(name, shorthand string, value net.IPMask, usage string) *net.IPMask

IPMaskP is like IP, but accepts a shorthand letter that can be used after a single dash.

func IPMaskVar

func IPMaskVar(p *net.IPMask, name string, value net.IPMask, usage string)

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

func IPMaskVarP

func IPMaskVarP(p *net.IPMask, name, shorthand string, value net.IPMask, usage string)

IPMaskVarP is like IPMaskVar, but accepts a shorthand letter that can be used after a single dash.

func IPNet added in v1.0.5

func IPNet(name string, value net.IPNet, usage string) *net.IPNet

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

func IPNetP added in v1.0.5

func IPNetP(name, shorthand string, value net.IPNet, usage string) *net.IPNet

IPNetP is like IPNet, but accepts a shorthand letter that can be used after a single dash.

func IPNetVar added in v1.0.5

func IPNetVar(p *net.IPNet, name string, value net.IPNet, usage string)

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

func IPNetVarP added in v1.0.5

func IPNetVarP(p *net.IPNet, name, shorthand string, value net.IPNet, usage string)

IPNetVarP is like IPNetVar, but accepts a shorthand letter that can be used after a single dash.

func IPP

func IPP(name, shorthand string, value net.IP, usage string) *net.IP

IPP is like IP, but accepts a shorthand letter that can be used after a single dash.

func IPVar

func IPVar(p *net.IP, name string, value net.IP, usage string)

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

func IPVarP

func IPVarP(p *net.IP, name, shorthand string, value net.IP, usage string)

IPVarP is like IPVar, but accepts a shorthand letter that can be used after a single dash.

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 Int32

func Int32(name string, value int32, usage string) *int32

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

func Int32P

func Int32P(name, shorthand string, value int32, usage string) *int32

Int32P is like Int32, but accepts a shorthand letter that can be used after a single dash.

func Int32Var

func Int32Var(p *int32, name string, value int32, usage string)

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

func Int32VarP

func Int32VarP(p *int32, name, shorthand string, value int32, usage string)

Int32VarP is like Int32Var, but accepts a shorthand letter that can be used after a single dash.

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 Int64P

func Int64P(name, shorthand string, value int64, usage string) *int64

Int64P is like Int64, but accepts a shorthand letter that can be used after a single dash.

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 Int64VarP

func Int64VarP(p *int64, name, shorthand string, value int64, usage string)

Int64VarP is like Int64Var, but accepts a shorthand letter that can be used after a single dash.

func Int8

func Int8(name string, value int8, usage string) *int8

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

func Int8P

func Int8P(name, shorthand string, value int8, usage string) *int8

Int8P is like Int8, but accepts a shorthand letter that can be used after a single dash.

func Int8Var

func Int8Var(p *int8, name string, value int8, usage string)

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

func Int8VarP

func Int8VarP(p *int8, name, shorthand string, value int8, usage string)

Int8VarP is like Int8Var, but accepts a shorthand letter that can be used after a single dash.

func IntP

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

IntP is like Int, but accepts a shorthand letter that can be used after a single dash.

func IntSlice added in v1.0.5

func IntSlice(name string, value []int, usage string) *[]int

IntSlice defines a []int flag with specified name, default value, and usage string. The return value is the address of a []int variable that stores the value of the flag.

func IntSliceP added in v1.0.5

func IntSliceP(name, shorthand string, value []int, usage string) *[]int

IntSliceP is like IntSlice, but accepts a shorthand letter that can be used after a single dash.

func IntSliceVar added in v1.0.5

func IntSliceVar(p *[]int, name string, value []int, usage string)

IntSliceVar 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 IntSliceVarP added in v1.0.5

func IntSliceVarP(p *[]int, name, shorthand string, value []int, usage string)

IntSliceVarP is like IntSliceVar, but accepts a shorthand letter that can be used after a single dash.

func IntVar

func IntVar(p *int, name 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 IntVarP

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

IntVarP is like IntVar, but accepts a shorthand letter that can be used after a single dash.

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 ParseIPv4Mask

func ParseIPv4Mask(s string) net.IPMask

ParseIPv4Mask written in IP form (e.g. 255.255.255.0). This function should really belong to the net package.

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 SetInterspersed

func SetInterspersed(interspersed bool)

SetInterspersed sets whether to support interspersed option/non-option arguments.

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 StringP

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

StringP is like String, but accepts a shorthand letter that can be used after a single dash.

func StringSlice added in v1.0.5

func StringSlice(name string, value []string, usage string) *[]string

StringSlice 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 StringSliceP added in v1.0.5

func StringSliceP(name, shorthand string, value []string, usage string) *[]string

StringSliceP is like StringSlice, but accepts a shorthand letter that can be used after a single dash.

func StringSliceVar added in v1.0.5

func StringSliceVar(p *[]string, name string, value []string, usage string)

StringSliceVar 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 StringSliceVarP added in v1.0.5

func StringSliceVarP(p *[]string, name, shorthand string, value []string, usage string)

StringSliceVarP is like StringSliceVar, but accepts a shorthand letter that can be used after a single dash.

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 StringVarP

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

StringVarP is like StringVar, but accepts a shorthand letter that can be used after a single dash.

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 Uint16

func Uint16(name string, value uint16, usage string) *uint16

Uint16 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 Uint16P

func Uint16P(name, shorthand string, value uint16, usage string) *uint16

Uint16P is like Uint16, but accepts a shorthand letter that can be used after a single dash.

func Uint16Var

func Uint16Var(p *uint16, name string, value uint16, usage string)

Uint16Var 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 Uint16VarP

func Uint16VarP(p *uint16, name, shorthand string, value uint16, usage string)

Uint16VarP is like Uint16Var, but accepts a shorthand letter that can be used after a single dash.

func Uint32

func Uint32(name string, value uint32, usage string) *uint32

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

func Uint32P

func Uint32P(name, shorthand string, value uint32, usage string) *uint32

Uint32P is like Uint32, but accepts a shorthand letter that can be used after a single dash.

func Uint32Var

func Uint32Var(p *uint32, name string, value uint32, usage string)

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

func Uint32VarP

func Uint32VarP(p *uint32, name, shorthand string, value uint32, usage string)

Uint32VarP is like Uint32Var, but accepts a shorthand letter that can be used after a single dash.

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 Uint64P

func Uint64P(name, shorthand string, value uint64, usage string) *uint64

Uint64P is like Uint64, but accepts a shorthand letter that can be used after a single dash.

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 Uint64VarP

func Uint64VarP(p *uint64, name, shorthand string, value uint64, usage string)

Uint64VarP is like Uint64Var, but accepts a shorthand letter that can be used after a single dash.

func Uint8

func Uint8(name string, value uint8, usage string) *uint8

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

func Uint8P

func Uint8P(name, shorthand string, value uint8, usage string) *uint8

Uint8P is like Uint8, but accepts a shorthand letter that can be used after a single dash.

func Uint8Var

func Uint8Var(p *uint8, name string, value uint8, usage string)

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

func Uint8VarP

func Uint8VarP(p *uint8, name, shorthand string, value uint8, usage string)

Uint8VarP is like Uint8Var, but accepts a shorthand letter that can be used after a single dash.

func UintP

func UintP(name, shorthand string, value uint, usage string) *uint

UintP is like Uint, but accepts a shorthand letter that can be used after a single dash.

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 UintVarP

func UintVarP(p *uint, name, shorthand string, value uint, usage string)

UintVarP is like UintVar, but accepts a shorthand letter that can be used after a single dash.

func Var

func Var(value 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 VarP

func VarP(value Value, name, shorthand, usage string)

VarP is like Var, but accepts a shorthand letter that can be used after a single dash.

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 will return an err from Parse() if an error is found
	ContinueOnError ErrorHandling = iota
	// ExitOnError will call os.Exit(2) if an error is found when parsing
	ExitOnError
	// PanicOnError will panic() if an error is found when parsing flags
	PanicOnError
)

type Flag

type Flag struct {
	Name                string              // name as it appears on command line
	Shorthand           string              // one-letter abbreviated flag
	Usage               string              // help message
	Value               Value               // value as set
	DefValue            string              // default value (as text); for usage message
	Changed             bool                // If the user set the value (or if left to default)
	NoOptDefVal         string              //default value (as text); if the flag is on the command line without any options
	Deprecated          string              // If this flag is deprecated, this string is the new or now thing to use
	Hidden              bool                // used by cobra.Command to allow flags to be hidden from help/usage text
	ShorthandDeprecated string              // If the shorthand of this flag is deprecated, this string is the new or now thing to use
	Annotations         map[string][]string // used by cobra.Command bash autocomple code
}

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.

func PFlagFromGoFlag added in v1.0.7

func PFlagFromGoFlag(goflag *goflag.Flag) *Flag

PFlagFromGoFlag will return a *pflag.Flag given a *flag.Flag

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()
	// contains filtered or unexported fields
}

A FlagSet represents a set of defined flags.

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

func (f *FlagSet) AddFlag(flag *Flag)

AddFlag will add the flag to the FlagSet

func (*FlagSet) AddFlagSet added in v1.0.7

func (f *FlagSet) AddFlagSet(newSet *FlagSet)

AddFlagSet adds one FlagSet to another. If a flag is already present in f the flag from newSet will be ignored

func (*FlagSet) AddGoFlag added in v1.0.7

func (f *FlagSet) AddGoFlag(goflag *goflag.Flag)

AddGoFlag will add the given *flag.Flag to the pflag.FlagSet

func (*FlagSet) AddGoFlagSet added in v1.0.7

func (f *FlagSet) AddGoFlagSet(newSet *goflag.FlagSet)

AddGoFlagSet will add the given *flag.FlagSet to the pflag.FlagSet

func (*FlagSet) Arg

func (f *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 (f *FlagSet) Args() []string

Args returns the non-flag arguments.

func (*FlagSet) ArgsLenAtDash added in v1.0.7

func (f *FlagSet) ArgsLenAtDash() int

ArgsLenAtDash will return the length of f.Args at the moment when a -- was found during arg parsing. This allows your program to know which args were before the -- and which came after.

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, shorthand string, value bool, usage string) *bool

BoolP is like Bool, but accepts a shorthand letter that can be used after a single dash.

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, shorthand string, value bool, usage string)

BoolVarP is like BoolVar, but accepts a shorthand letter that can be used after a single dash.

func (*FlagSet) Changed added in v1.0.7

func (f *FlagSet) Changed(name string) bool

Changed returns true if the flag was explicitly set during Parse() and false otherwise

func (*FlagSet) Count added in v1.0.5

func (f *FlagSet) Count(name string, usage string) *int

Count defines a count 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. A count flag will add 1 to its value evey time it is found on the command line

func (*FlagSet) CountP added in v1.0.5

func (f *FlagSet) CountP(name, shorthand string, usage string) *int

CountP is like Count only takes a shorthand for the flag name.

func (*FlagSet) CountVar added in v1.0.5

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

CountVar defines a count 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. A count flag will add 1 to its value evey time it is found on the command line

func (*FlagSet) CountVarP added in v1.0.5

func (f *FlagSet) CountVarP(p *int, name, shorthand string, usage string)

CountVarP is like CountVar only take a shorthand for the flag name.

func (*FlagSet) Duration

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

func (*FlagSet) DurationP

func (f *FlagSet) DurationP(name, shorthand string, value time.Duration, usage string) *time.Duration

DurationP is like Duration, but accepts a shorthand letter that can be used after a single dash.

func (*FlagSet) DurationVar

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

func (*FlagSet) DurationVarP

func (f *FlagSet) DurationVarP(p *time.Duration, name, shorthand string, value time.Duration, usage string)

DurationVarP is like DurationVar, but accepts a shorthand letter that can be used after a single dash.

func (*FlagSet) FlagUsages

func (f *FlagSet) FlagUsages() string

FlagUsages Returns a string containing the usage information for all flags in the FlagSet

func (*FlagSet) Float32

func (f *FlagSet) Float32(name string, value float32, usage string) *float32

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

func (*FlagSet) Float32P

func (f *FlagSet) Float32P(name, shorthand string, value float32, usage string) *float32

Float32P is like Float32, but accepts a shorthand letter that can be used after a single dash.

func (*FlagSet) Float32Var

func (f *FlagSet) Float32Var(p *float32, name string, value float32, usage string)

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

func (*FlagSet) Float32VarP

func (f *FlagSet) Float32VarP(p *float32, name, shorthand string, value float32, usage string)

Float32VarP is like Float32Var, but accepts a shorthand letter that can be used after a single dash.

func (*FlagSet) Float64

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

func (f *FlagSet) Float64P(name, shorthand string, value float64, usage string) *float64

Float64P is like Float64, but accepts a shorthand letter that can be used after a single dash.

func (*FlagSet) Float64Var

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

func (f *FlagSet) Float64VarP(p *float64, name, shorthand string, value float64, usage string)

Float64VarP is like Float64Var, but accepts a shorthand letter that can be used after a single dash.

func (*FlagSet) GetBool added in v1.0.5

func (f *FlagSet) GetBool(name string) (bool, error)

GetBool return the bool value of a flag with the given name

func (*FlagSet) GetCount added in v1.0.5

func (f *FlagSet) GetCount(name string) (int, error)

GetCount return the int value of a flag with the given name

func (*FlagSet) GetDuration added in v1.0.5

func (f *FlagSet) GetDuration(name string) (time.Duration, error)

GetDuration return the duration value of a flag with the given name

func (*FlagSet) GetFloat32 added in v1.0.5

func (f *FlagSet) GetFloat32(name string) (float32, error)

GetFloat32 return the float32 value of a flag with the given name

func (*FlagSet) GetFloat64 added in v1.0.5

func (f *FlagSet) GetFloat64(name string) (float64, error)

GetFloat64 return the float64 value of a flag with the given name

func (*FlagSet) GetIP added in v1.0.5

func (f *FlagSet) GetIP(name string) (net.IP, error)

GetIP return the net.IP value of a flag with the given name

func (*FlagSet) GetIPNet added in v1.0.5

func (f *FlagSet) GetIPNet(name string) (net.IPNet, error)

GetIPNet return the net.IPNet value of a flag with the given name

func (*FlagSet) GetIPv4Mask added in v1.0.5

func (f *FlagSet) GetIPv4Mask(name string) (net.IPMask, error)

GetIPv4Mask return the net.IPv4Mask value of a flag with the given name

func (*FlagSet) GetInt added in v1.0.5

func (f *FlagSet) GetInt(name string) (int, error)

GetInt return the int value of a flag with the given name

func (*FlagSet) GetInt32 added in v1.0.5

func (f *FlagSet) GetInt32(name string) (int32, error)

GetInt32 return the int32 value of a flag with the given name

func (*FlagSet) GetInt64 added in v1.0.5

func (f *FlagSet) GetInt64(name string) (int64, error)

GetInt64 return the int64 value of a flag with the given name

func (*FlagSet) GetInt8 added in v1.0.5

func (f *FlagSet) GetInt8(name string) (int8, error)

GetInt8 return the int8 value of a flag with the given name

func (*FlagSet) GetIntSlice added in v1.0.5

func (f *FlagSet) GetIntSlice(name string) ([]int, error)

GetIntSlice return the []int value of a flag with the given name

func (*FlagSet) GetNormalizeFunc added in v0.5.1

func (f *FlagSet) GetNormalizeFunc() func(f *FlagSet, name string) NormalizedName

GetNormalizeFunc returns the previously set NormalizeFunc of a function which does no translation, if not set previously.

func (*FlagSet) GetString added in v1.0.5

func (f *FlagSet) GetString(name string) (string, error)

GetString return the string value of a flag with the given name

func (*FlagSet) GetStringSlice added in v1.0.5

func (f *FlagSet) GetStringSlice(name string) ([]string, error)

GetStringSlice return the []string value of a flag with the given name

func (*FlagSet) GetUint added in v1.0.5

func (f *FlagSet) GetUint(name string) (uint, error)

GetUint return the uint value of a flag with the given name

func (*FlagSet) GetUint16 added in v1.0.5

func (f *FlagSet) GetUint16(name string) (uint16, error)

GetUint16 return the uint16 value of a flag with the given name

func (*FlagSet) GetUint32 added in v1.0.5

func (f *FlagSet) GetUint32(name string) (uint32, error)

GetUint32 return the uint32 value of a flag with the given name

func (*FlagSet) GetUint64 added in v1.0.5

func (f *FlagSet) GetUint64(name string) (uint64, error)

GetUint64 return the uint64 value of a flag with the given name

func (*FlagSet) GetUint8 added in v1.0.5

func (f *FlagSet) GetUint8(name string) (uint8, error)

GetUint8 return the uint8 value of a flag with the given name

func (*FlagSet) HasFlags

func (f *FlagSet) HasFlags() bool

HasFlags returns a bool to indicate if the FlagSet has any flags definied.

func (*FlagSet) IP

func (f *FlagSet) IP(name string, value net.IP, usage string) *net.IP

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

func (*FlagSet) IPMask

func (f *FlagSet) IPMask(name string, value net.IPMask, usage string) *net.IPMask

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

func (*FlagSet) IPMaskP

func (f *FlagSet) IPMaskP(name, shorthand string, value net.IPMask, usage string) *net.IPMask

IPMaskP is like IPMask, but accepts a shorthand letter that can be used after a single dash.

func (*FlagSet) IPMaskVar

func (f *FlagSet) IPMaskVar(p *net.IPMask, name string, value net.IPMask, usage string)

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

func (*FlagSet) IPMaskVarP

func (f *FlagSet) IPMaskVarP(p *net.IPMask, name, shorthand string, value net.IPMask, usage string)

IPMaskVarP is like IPMaskVar, but accepts a shorthand letter that can be used after a single dash.

func (*FlagSet) IPNet added in v1.0.5

func (f *FlagSet) IPNet(name string, value net.IPNet, usage string) *net.IPNet

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

func (*FlagSet) IPNetP added in v1.0.5

func (f *FlagSet) IPNetP(name, shorthand string, value net.IPNet, usage string) *net.IPNet

IPNetP is like IPNet, but accepts a shorthand letter that can be used after a single dash.

func (*FlagSet) IPNetVar added in v1.0.5

func (f *FlagSet) IPNetVar(p *net.IPNet, name string, value net.IPNet, usage string)

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

func (*FlagSet) IPNetVarP added in v1.0.5

func (f *FlagSet) IPNetVarP(p *net.IPNet, name, shorthand string, value net.IPNet, usage string)

IPNetVarP is like IPNetVar, but accepts a shorthand letter that can be used after a single dash.

func (*FlagSet) IPP

func (f *FlagSet) IPP(name, shorthand string, value net.IP, usage string) *net.IP

IPP is like IP, but accepts a shorthand letter that can be used after a single dash.

func (*FlagSet) IPVar

func (f *FlagSet) IPVar(p *net.IP, name string, value net.IP, usage string)

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

func (*FlagSet) IPVarP

func (f *FlagSet) IPVarP(p *net.IP, name, shorthand string, value net.IP, usage string)

IPVarP is like IPVar, but accepts a shorthand letter that can be used after a single dash.

func (*FlagSet) Init

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

func (f *FlagSet) Int32(name string, value int32, usage string) *int32

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

func (*FlagSet) Int32P

func (f *FlagSet) Int32P(name, shorthand string, value int32, usage string) *int32

Int32P is like Int32, but accepts a shorthand letter that can be used after a single dash.

func (*FlagSet) Int32Var

func (f *FlagSet) Int32Var(p *int32, name string, value int32, usage string)

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

func (*FlagSet) Int32VarP

func (f *FlagSet) Int32VarP(p *int32, name, shorthand string, value int32, usage string)

Int32VarP is like Int32Var, but accepts a shorthand letter that can be used after a single dash.

func (*FlagSet) Int64

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

func (f *FlagSet) Int64P(name, shorthand string, value int64, usage string) *int64

Int64P is like Int64, but accepts a shorthand letter that can be used after a single dash.

func (*FlagSet) Int64Var

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

func (f *FlagSet) Int64VarP(p *int64, name, shorthand string, value int64, usage string)

Int64VarP is like Int64Var, but accepts a shorthand letter that can be used after a single dash.

func (*FlagSet) Int8

func (f *FlagSet) Int8(name string, value int8, usage string) *int8

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

func (*FlagSet) Int8P

func (f *FlagSet) Int8P(name, shorthand string, value int8, usage string) *int8

Int8P is like Int8, but accepts a shorthand letter that can be used after a single dash.

func (*FlagSet) Int8Var

func (f *FlagSet) Int8Var(p *int8, name string, value int8, usage string)

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

func (*FlagSet) Int8VarP

func (f *FlagSet) Int8VarP(p *int8, name, shorthand string, value int8, usage string)

Int8VarP is like Int8Var, but accepts a shorthand letter that can be used after a single dash.

func (*FlagSet) IntP

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

IntP is like Int, but accepts a shorthand letter that can be used after a single dash.

func (*FlagSet) IntSlice added in v1.0.5

func (f *FlagSet) IntSlice(name string, value []int, usage string) *[]int

IntSlice defines a []int flag with specified name, default value, and usage string. The return value is the address of a []int variable that stores the value of the flag.

func (*FlagSet) IntSliceP added in v1.0.5

func (f *FlagSet) IntSliceP(name, shorthand string, value []int, usage string) *[]int

IntSliceP is like IntSlice, but accepts a shorthand letter that can be used after a single dash.

func (*FlagSet) IntSliceVar added in v1.0.5

func (f *FlagSet) IntSliceVar(p *[]int, name string, value []int, usage string)

IntSliceVar defines a intSlice 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) IntSliceVarP added in v1.0.5

func (f *FlagSet) IntSliceVarP(p *[]int, name, shorthand string, value []int, usage string)

IntSliceVarP is like IntSliceVar, but accepts a shorthand letter that can be used after a single dash.

func (*FlagSet) IntVar

func (f *FlagSet) IntVar(p *int, name 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) IntVarP

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

IntVarP is like IntVar, but accepts a shorthand letter that can be used after a single dash.

func (*FlagSet) Lookup

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

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

func (*FlagSet) MarkDeprecated added in v0.5.1

func (f *FlagSet) MarkDeprecated(name string, usageMessage string) error

MarkDeprecated indicated that a flag is deprecated in your program. It will continue to function but will not show up in help or usage messages. Using this flag will also print the given usageMessage.

func (*FlagSet) MarkHidden added in v1.0.7

func (f *FlagSet) MarkHidden(name string) error

MarkHidden sets a flag to 'hidden' in your program. It will continue to function but will not show up in help or usage messages.

func (*FlagSet) MarkShorthandDeprecated added in v1.0.7

func (f *FlagSet) MarkShorthandDeprecated(name string, usageMessage string) error

MarkShorthandDeprecated will mark the shorthand of a flag deprecated in your program. It will continue to function but will not show up in help or usage messages. Using this flag will also print the given usageMessage.

func (*FlagSet) NArg

func (f *FlagSet) NArg() int

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

func (*FlagSet) NFlag

func (f *FlagSet) NFlag() int

NFlag returns the number of flags that have been set.

func (*FlagSet) Parse

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

func (f *FlagSet) Parsed() bool

Parsed reports whether f.Parse has been called.

func (*FlagSet) PrintDefaults

func (f *FlagSet) PrintDefaults()

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

func (*FlagSet) Set

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

Set sets the value of the named flag.

func (*FlagSet) SetAnnotation added in v1.0.1

func (f *FlagSet) SetAnnotation(name, key string, values []string) error

SetAnnotation allows one to set arbitrary annotations on a flag in the FlagSet. This is sometimes used by spf13/cobra programs which want to generate additional bash completion information.

func (*FlagSet) SetInterspersed

func (f *FlagSet) SetInterspersed(interspersed bool)

SetInterspersed sets whether to support interspersed option/non-option arguments.

func (*FlagSet) SetNormalizeFunc added in v0.5.1

func (f *FlagSet) SetNormalizeFunc(n func(f *FlagSet, name string) NormalizedName)

SetNormalizeFunc allows you to add a function which can translate flag names. Flags added to the FlagSet will be translated and then when anything tries to look up the flag that will also be translated. So it would be possible to create a flag named "getURL" and have it translated to "geturl". A user could then pass "--getUrl" which may also be translated to "geturl" and everything will work.

func (*FlagSet) SetOutput

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

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

StringP is like String, but accepts a shorthand letter that can be used after a single dash.

func (*FlagSet) StringSlice added in v1.0.5

func (f *FlagSet) StringSlice(name string, value []string, usage string) *[]string

StringSlice 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) StringSliceP added in v1.0.5

func (f *FlagSet) StringSliceP(name, shorthand string, value []string, usage string) *[]string

StringSliceP is like StringSlice, but accepts a shorthand letter that can be used after a single dash.

func (*FlagSet) StringSliceVar added in v1.0.5

func (f *FlagSet) StringSliceVar(p *[]string, name string, value []string, usage string)

StringSliceVar 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) StringSliceVarP added in v1.0.5

func (f *FlagSet) StringSliceVarP(p *[]string, name, shorthand string, value []string, usage string)

StringSliceVarP is like StringSliceVar, but accepts a shorthand letter that can be used after a single dash.

func (*FlagSet) StringVar

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

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

StringVarP is like StringVar, but accepts a shorthand letter that can be used after a single dash.

func (*FlagSet) Uint

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

func (f *FlagSet) Uint16(name string, value uint16, usage string) *uint16

Uint16 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) Uint16P

func (f *FlagSet) Uint16P(name, shorthand string, value uint16, usage string) *uint16

Uint16P is like Uint16, but accepts a shorthand letter that can be used after a single dash.

func (*FlagSet) Uint16Var

func (f *FlagSet) Uint16Var(p *uint16, name string, value uint16, usage string)

Uint16Var 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) Uint16VarP

func (f *FlagSet) Uint16VarP(p *uint16, name, shorthand string, value uint16, usage string)

Uint16VarP is like Uint16Var, but accepts a shorthand letter that can be used after a single dash.

func (*FlagSet) Uint32

func (f *FlagSet) Uint32(name string, value uint32, usage string) *uint32

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

func (*FlagSet) Uint32P

func (f *FlagSet) Uint32P(name, shorthand string, value uint32, usage string) *uint32

Uint32P is like Uint32, but accepts a shorthand letter that can be used after a single dash.

func (*FlagSet) Uint32Var

func (f *FlagSet) Uint32Var(p *uint32, name string, value uint32, usage string)

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

func (*FlagSet) Uint32VarP

func (f *FlagSet) Uint32VarP(p *uint32, name, shorthand string, value uint32, usage string)

Uint32VarP is like Uint32Var, but accepts a shorthand letter that can be used after a single dash.

func (*FlagSet) Uint64

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

func (f *FlagSet) Uint64P(name, shorthand string, value uint64, usage string) *uint64

Uint64P is like Uint64, but accepts a shorthand letter that can be used after a single dash.

func (*FlagSet) Uint64Var

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

func (f *FlagSet) Uint64VarP(p *uint64, name, shorthand string, value uint64, usage string)

Uint64VarP is like Uint64Var, but accepts a shorthand letter that can be used after a single dash.

func (*FlagSet) Uint8

func (f *FlagSet) Uint8(name string, value uint8, usage string) *uint8

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

func (*FlagSet) Uint8P

func (f *FlagSet) Uint8P(name, shorthand string, value uint8, usage string) *uint8

Uint8P is like Uint8, but accepts a shorthand letter that can be used after a single dash.

func (*FlagSet) Uint8Var

func (f *FlagSet) Uint8Var(p *uint8, name string, value uint8, usage string)

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

func (*FlagSet) Uint8VarP

func (f *FlagSet) Uint8VarP(p *uint8, name, shorthand string, value uint8, usage string)

Uint8VarP is like Uint8Var, but accepts a shorthand letter that can be used after a single dash.

func (*FlagSet) UintP

func (f *FlagSet) UintP(name, shorthand string, value uint, usage string) *uint

UintP is like Uint, but accepts a shorthand letter that can be used after a single dash.

func (*FlagSet) UintVar

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

func (f *FlagSet) UintVarP(p *uint, name, shorthand string, value uint, usage string)

UintVarP is like UintVar, but accepts a shorthand letter that can be used after a single dash.

func (*FlagSet) Var

func (f *FlagSet) Var(value 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) VarP

func (f *FlagSet) VarP(value Value, name, shorthand, usage string)

VarP is like Var, but accepts a shorthand letter that can be used after a single dash.

func (*FlagSet) VarPF added in v1.0.5

func (f *FlagSet) VarPF(value Value, name, shorthand, usage string) *Flag

VarPF is like VarP, but returns the flag created

func (*FlagSet) Visit

func (f *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 (f *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 NormalizedName added in v0.5.1

type NormalizedName string

NormalizedName is a flag name that has been normalized according to rules for the FlagSet (e.g. making '-' and '_' equivalent).

type Value

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

Value is the interface to the dynamic value stored in a flag. (The default value is represented as a string.)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL