Update spf13/cobra and spf13/pflag

This commit is contained in:
Eric Paris 2015-08-20 15:00:41 -07:00
parent 2f1edf7db5
commit 681ee126ff
31 changed files with 436 additions and 216 deletions

5
Godeps/Godeps.json generated
View File

@ -476,12 +476,11 @@
}, },
{ {
"ImportPath": "github.com/spf13/cobra", "ImportPath": "github.com/spf13/cobra",
"Rev": "385fc87e4343efec233811d3d933509e8975d11a" "Rev": "db0518444643a7b170abb78164bbeaf5a2bb816f"
}, },
{ {
"ImportPath": "github.com/spf13/pflag", "ImportPath": "github.com/spf13/pflag",
"Comment": "v0.0.1-81-g4869ec2", "Rev": "112aaa578dfdd0e913663b05153be974bd72497a"
"Rev": "4869ec2ae0628354eaac5bf88fccf9a7265ae475"
}, },
{ {
"ImportPath": "github.com/stretchr/objx", "ImportPath": "github.com/stretchr/objx",

View File

@ -13,6 +13,7 @@ import (
const ( const (
BashCompFilenameExt = "cobra_annotation_bash_completion_filename_extentions" BashCompFilenameExt = "cobra_annotation_bash_completion_filename_extentions"
BashCompOneRequiredFlag = "cobra_annotation_bash_completion_one_required_flag" BashCompOneRequiredFlag = "cobra_annotation_bash_completion_one_required_flag"
BashCompSubdirsInDir = "cobra_annotation_bash_completion_subdirs_in_dir"
) )
func preamble(out *bytes.Buffer) { func preamble(out *bytes.Buffer) {
@ -100,6 +101,12 @@ __handle_filename_extension_flag()
_filedir "@(${ext})" _filedir "@(${ext})"
} }
__handle_subdirs_in_dir_flag()
{
local dir="$1"
pushd "${dir}" >/dev/null 2>&1 && _filedir -d && popd >/dev/null 2>&1
}
__handle_flag() __handle_flag()
{ {
__debug "${FUNCNAME}: c is $c words[c] is ${words[c]}" __debug "${FUNCNAME}: c is $c words[c] is ${words[c]}"
@ -226,6 +233,16 @@ func writeFlagHandler(name string, annotations map[string][]string, out *bytes.B
ext := "_filedir" ext := "_filedir"
fmt.Fprintf(out, " flags_completion+=(%q)\n", ext) fmt.Fprintf(out, " flags_completion+=(%q)\n", ext)
} }
case BashCompSubdirsInDir:
fmt.Fprintf(out, " flags_with_completion+=(%q)\n", name)
if len(value) == 1 {
ext := "__handle_subdirs_in_dir_flag " + value[0]
fmt.Fprintf(out, " flags_completion+=(%q)\n", ext)
} else {
ext := "_filedir -d"
fmt.Fprintf(out, " flags_completion+=(%q)\n", ext)
}
} }
} }
} }

View File

@ -56,6 +56,11 @@ func TestBashCompletions(t *testing.T) {
c.Flags().StringVar(&flagvalExt, "filename-ext", "", "Enter a filename (extension limited)") c.Flags().StringVar(&flagvalExt, "filename-ext", "", "Enter a filename (extension limited)")
c.MarkFlagFilename("filename-ext") c.MarkFlagFilename("filename-ext")
// subdirectories in a given directory
var flagvalTheme string
c.Flags().StringVar(&flagvalTheme, "theme", "", "theme to use (located in /themes/THEMENAME/)")
c.Flags().SetAnnotation("theme", BashCompSubdirsInDir, []string{"themes"})
out := new(bytes.Buffer) out := new(bytes.Buffer)
c.GenBashCompletion(out) c.GenBashCompletion(out)
str := out.String() str := out.String()
@ -75,6 +80,8 @@ func TestBashCompletions(t *testing.T) {
check(t, str, `flags_completion+=("_filedir")`) check(t, str, `flags_completion+=("_filedir")`)
// check for filename extension flags // check for filename extension flags
check(t, str, `flags_completion+=("__handle_filename_extension_flag json|yaml|yml")`) check(t, str, `flags_completion+=("__handle_filename_extension_flag json|yaml|yml")`)
// check for subdirs_in_dir flags
check(t, str, `flags_completion+=("__handle_subdirs_in_dir_flag themes")`)
checkOmit(t, str, cmdDeprecated.Name()) checkOmit(t, str, cmdDeprecated.Name())
} }

View File

@ -963,3 +963,11 @@ func TestGlobalNormFuncPropagation(t *testing.T) {
t.Error("cmdPrint and cmdEchoSub should had the normalization function of rootCmd") t.Error("cmdPrint and cmdEchoSub should had the normalization function of rootCmd")
} }
} }
func TestFlagOnPflagCommandLine(t *testing.T) {
flagName := "flagOnCommandLine"
pflag.CommandLine.String(flagName, "", "about my flag")
r := fullSetupTest("--help")
checkResultContains(t, r, flagName)
}

View File

@ -158,7 +158,6 @@ func (c *Command) SetHelpTemplate(s string) {
func (c *Command) SetGlobalNormalizationFunc(n func(f *flag.FlagSet, name string) flag.NormalizedName) { func (c *Command) SetGlobalNormalizationFunc(n func(f *flag.FlagSet, name string) flag.NormalizedName) {
c.Flags().SetNormalizeFunc(n) c.Flags().SetNormalizeFunc(n)
c.PersistentFlags().SetNormalizeFunc(n) c.PersistentFlags().SetNormalizeFunc(n)
c.LocalFlags().SetNormalizeFunc(n)
c.globNormFunc = n c.globNormFunc = n
for _, command := range c.commands { for _, command := range c.commands {
@ -873,6 +872,13 @@ func (c *Command) LocalFlags() *flag.FlagSet {
c.lflags.VisitAll(func(f *flag.Flag) { c.lflags.VisitAll(func(f *flag.Flag) {
local.AddFlag(f) local.AddFlag(f)
}) })
if !c.HasParent() {
flag.CommandLine.VisitAll(func(f *flag.Flag) {
if local.Lookup(f.Name) == nil {
local.AddFlag(f)
}
})
}
return local return local
} }

View File

@ -53,7 +53,7 @@ func (f *FlagSet) BoolVar(p *bool, name string, value bool, usage string) {
f.BoolVarP(p, name, "", value, usage) f.BoolVarP(p, name, "", value, usage)
} }
// Like BoolVar, but accepts a shorthand letter that can be used after a single dash. // BoolVarP is like BoolVar, but accepts a shorthand letter that can be used after a single dash.
func (f *FlagSet) BoolVarP(p *bool, name, shorthand string, value bool, usage string) { func (f *FlagSet) BoolVarP(p *bool, name, shorthand string, value bool, usage string) {
flag := f.VarPF(newBoolValue(value, p), name, shorthand, usage) flag := f.VarPF(newBoolValue(value, p), name, shorthand, usage)
flag.NoOptDefVal = "true" flag.NoOptDefVal = "true"
@ -65,7 +65,7 @@ func BoolVar(p *bool, name string, value bool, usage string) {
BoolVarP(p, name, "", value, usage) BoolVarP(p, name, "", value, usage)
} }
// Like BoolVar, but accepts a shorthand letter that can be used after a single dash. // BoolVarP is like BoolVar, but accepts a shorthand letter that can be used after a single dash.
func BoolVarP(p *bool, name, shorthand string, value bool, usage string) { func BoolVarP(p *bool, name, shorthand string, value bool, usage string) {
flag := CommandLine.VarPF(newBoolValue(value, p), name, shorthand, usage) flag := CommandLine.VarPF(newBoolValue(value, p), name, shorthand, usage)
flag.NoOptDefVal = "true" flag.NoOptDefVal = "true"
@ -77,7 +77,7 @@ func (f *FlagSet) Bool(name string, value bool, usage string) *bool {
return f.BoolP(name, "", value, usage) return f.BoolP(name, "", value, usage)
} }
// Like Bool, but accepts a shorthand letter that can be used after a single dash. // BoolP is like Bool, but accepts a shorthand letter that can be used after a single dash.
func (f *FlagSet) BoolP(name, shorthand string, value bool, usage string) *bool { func (f *FlagSet) BoolP(name, shorthand string, value bool, usage string) *bool {
p := new(bool) p := new(bool)
f.BoolVarP(p, name, shorthand, value, usage) f.BoolVarP(p, name, shorthand, value, usage)
@ -90,7 +90,7 @@ func Bool(name string, value bool, usage string) *bool {
return BoolP(name, "", value, usage) return BoolP(name, "", value, usage)
} }
// Like Bool, but accepts a shorthand letter that can be used after a single dash. // BoolP is like Bool, but accepts a shorthand letter that can be used after a single dash.
func BoolP(name, shorthand string, value bool, usage string) *bool { func BoolP(name, shorthand string, value bool, usage string) *bool {
b := CommandLine.BoolP(name, shorthand, value, usage) b := CommandLine.BoolP(name, shorthand, value, usage)
return b return b

View File

@ -38,6 +38,7 @@ func countConv(sval string) (interface{}, error) {
return i, nil return i, nil
} }
// GetCount return the int value of a flag with the given name
func (f *FlagSet) GetCount(name string) (int, error) { func (f *FlagSet) GetCount(name string) (int, error) {
val, err := f.getFlagType(name, "count", countConv) val, err := f.getFlagType(name, "count", countConv)
if err != nil { if err != nil {
@ -46,39 +47,51 @@ func (f *FlagSet) GetCount(name string) (int, error) {
return val.(int), nil return val.(int), nil
} }
// 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 (f *FlagSet) CountVar(p *int, name string, usage string) { func (f *FlagSet) CountVar(p *int, name string, usage string) {
f.CountVarP(p, name, "", usage) f.CountVarP(p, name, "", usage)
} }
// CountVarP is like CountVar only take a shorthand for the flag name.
func (f *FlagSet) CountVarP(p *int, name, shorthand string, usage string) { func (f *FlagSet) CountVarP(p *int, name, shorthand string, usage string) {
flag := f.VarPF(newCountValue(0, p), name, shorthand, usage) flag := f.VarPF(newCountValue(0, p), name, shorthand, usage)
flag.NoOptDefVal = "-1" flag.NoOptDefVal = "-1"
} }
// CountVar like CountVar only the flag is placed on the CommandLine instead of a given flag set
func CountVar(p *int, name string, usage string) { func CountVar(p *int, name string, usage string) {
CommandLine.CountVar(p, name, usage) CommandLine.CountVar(p, name, usage)
} }
// CountVarP is like CountVar only take a shorthand for the flag name.
func CountVarP(p *int, name, shorthand string, usage string) { func CountVarP(p *int, name, shorthand string, usage string) {
CommandLine.CountVarP(p, name, shorthand, usage) CommandLine.CountVarP(p, name, shorthand, usage)
} }
// 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 (f *FlagSet) Count(name string, usage string) *int { func (f *FlagSet) Count(name string, usage string) *int {
p := new(int) p := new(int)
f.CountVarP(p, name, "", usage) f.CountVarP(p, name, "", usage)
return p return p
} }
// CountP is like Count only takes a shorthand for the flag name.
func (f *FlagSet) CountP(name, shorthand string, usage string) *int { func (f *FlagSet) CountP(name, shorthand string, usage string) *int {
p := new(int) p := new(int)
f.CountVarP(p, name, shorthand, usage) f.CountVarP(p, name, shorthand, usage)
return p return p
} }
// Count like Count only the flag is placed on the CommandLine isntead of a given flag set
func Count(name string, usage string) *int { func Count(name string, usage string) *int {
return CommandLine.CountP(name, "", usage) return CommandLine.CountP(name, "", usage)
} }
// CountP is like Count only takes a shorthand for the flag name.
func CountP(name, shorthand string, usage string) *int { func CountP(name, shorthand string, usage string) *int {
return CommandLine.CountP(name, shorthand, usage) return CommandLine.CountP(name, shorthand, usage)
} }

View File

@ -43,7 +43,7 @@ func (f *FlagSet) DurationVar(p *time.Duration, name string, value time.Duration
f.VarP(newDurationValue(value, p), name, "", usage) f.VarP(newDurationValue(value, p), name, "", usage)
} }
// Like DurationVar, but accepts a shorthand letter that can be used after a single dash. // DurationVarP is like DurationVar, but accepts a shorthand letter that can be used after a single dash.
func (f *FlagSet) DurationVarP(p *time.Duration, name, shorthand string, value time.Duration, usage string) { func (f *FlagSet) DurationVarP(p *time.Duration, name, shorthand string, value time.Duration, usage string) {
f.VarP(newDurationValue(value, p), name, shorthand, usage) f.VarP(newDurationValue(value, p), name, shorthand, usage)
} }
@ -54,7 +54,7 @@ func DurationVar(p *time.Duration, name string, value time.Duration, usage strin
CommandLine.VarP(newDurationValue(value, p), name, "", usage) CommandLine.VarP(newDurationValue(value, p), name, "", usage)
} }
// Like DurationVar, but accepts a shorthand letter that can be used after a single dash. // DurationVarP is like DurationVar, but accepts a shorthand letter that can be used after a single dash.
func DurationVarP(p *time.Duration, name, shorthand string, value time.Duration, usage string) { func DurationVarP(p *time.Duration, name, shorthand string, value time.Duration, usage string) {
CommandLine.VarP(newDurationValue(value, p), name, shorthand, usage) CommandLine.VarP(newDurationValue(value, p), name, shorthand, usage)
} }
@ -67,7 +67,7 @@ func (f *FlagSet) Duration(name string, value time.Duration, usage string) *time
return p return p
} }
// Like Duration, but accepts a shorthand letter that can be used after a single dash. // DurationP is like Duration, but accepts a shorthand letter that can be used after a single dash.
func (f *FlagSet) DurationP(name, shorthand string, value time.Duration, usage string) *time.Duration { func (f *FlagSet) DurationP(name, shorthand string, value time.Duration, usage string) *time.Duration {
p := new(time.Duration) p := new(time.Duration)
f.DurationVarP(p, name, shorthand, value, usage) f.DurationVarP(p, name, shorthand, value, usage)
@ -80,7 +80,7 @@ func Duration(name string, value time.Duration, usage string) *time.Duration {
return CommandLine.DurationP(name, "", value, usage) return CommandLine.DurationP(name, "", value, usage)
} }
// Like Duration, but accepts a shorthand letter that can be used after a single dash. // DurationP is like Duration, but accepts a shorthand letter that can be used after a single dash.
func DurationP(name, shorthand string, value time.Duration, usage string) *time.Duration { func DurationP(name, shorthand string, value time.Duration, usage string) *time.Duration {
return CommandLine.DurationP(name, shorthand, value, usage) return CommandLine.DurationP(name, shorthand, value, usage)
} }

View File

@ -3,98 +3,98 @@
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
/* /*
pflag is a drop-in replacement for Go's flag package, implementing Package pflag is a drop-in replacement for Go's flag package, implementing
POSIX/GNU-style --flags. POSIX/GNU-style --flags.
pflag is compatible with the GNU extensions to the POSIX recommendations pflag is compatible with the GNU extensions to the POSIX recommendations
for command-line options. See for command-line options. See
http://www.gnu.org/software/libc/manual/html_node/Argument-Syntax.html http://www.gnu.org/software/libc/manual/html_node/Argument-Syntax.html
Usage: Usage:
pflag is a drop-in replacement of Go's native flag package. If you import 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 pflag under the name "flag" then all code should continue to function
with no changes. with no changes.
import flag "github.com/ogier/pflag" import flag "github.com/ogier/pflag"
There is one exception to this: if you directly instantiate the Flag struct 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. there is one more field "Shorthand" that you will need to set.
Most code never instantiates this struct directly, and instead uses Most code never instantiates this struct directly, and instead uses
functions such as String(), BoolVar(), and Var(), and is therefore functions such as String(), BoolVar(), and Var(), and is therefore
unaffected. unaffected.
Define flags using flag.String(), Bool(), Int(), etc. Define flags using flag.String(), Bool(), Int(), etc.
This declares an integer flag, -flagname, stored in the pointer ip, with type *int. This declares an integer flag, -flagname, stored in the pointer ip, with type *int.
var ip = flag.Int("flagname", 1234, "help message for flagname") 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. If you like, you can bind the flag to a variable using the Var() functions.
var flagvar int var flagvar int
func init() { func init() {
flag.IntVar(&flagvar, "flagname", 1234, "help message for flagname") flag.IntVar(&flagvar, "flagname", 1234, "help message for flagname")
} }
Or you can create custom flags that satisfy the Value interface (with Or you can create custom flags that satisfy the Value interface (with
pointer receivers) and couple them to flag parsing by pointer receivers) and couple them to flag parsing by
flag.Var(&flagVal, "name", "help message for flagname") flag.Var(&flagVal, "name", "help message for flagname")
For such flags, the default value is just the initial value of the variable. For such flags, the default value is just the initial value of the variable.
After all flags are defined, call After all flags are defined, call
flag.Parse() flag.Parse()
to parse the command line into the defined flags. to parse the command line into the defined flags.
Flags may then be used directly. If you're using the flags themselves, 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. they are all pointers; if you bind to variables, they're values.
fmt.Println("ip has value ", *ip) fmt.Println("ip has value ", *ip)
fmt.Println("flagvar has value ", flagvar) fmt.Println("flagvar has value ", flagvar)
After parsing, the arguments after the flag are available as the After parsing, the arguments after the flag are available as the
slice flag.Args() or individually as flag.Arg(i). slice flag.Args() or individually as flag.Arg(i).
The arguments are indexed from 0 through flag.NArg()-1. The arguments are indexed from 0 through flag.NArg()-1.
The pflag package also defines some new functions that are not in flag, 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 that give one-letter shorthands for flags. You can use these by appending
'P' to the name of any function that defines a flag. 'P' to the name of any function that defines a flag.
var ip = flag.IntP("flagname", "f", 1234, "help message") var ip = flag.IntP("flagname", "f", 1234, "help message")
var flagvar bool var flagvar bool
func init() { func init() {
flag.BoolVarP("boolname", "b", true, "help message") flag.BoolVarP("boolname", "b", true, "help message")
} }
flag.VarP(&flagVar, "varname", "v", 1234, "help message") flag.VarP(&flagVar, "varname", "v", 1234, "help message")
Shorthand letters can be used with single dashes on the command line. Shorthand letters can be used with single dashes on the command line.
Boolean shorthand flags can be combined with other shorthand flags. Boolean shorthand flags can be combined with other shorthand flags.
Command line flag syntax: Command line flag syntax:
--flag // boolean flags only --flag // boolean flags only
--flag=x --flag=x
Unlike the flag package, a single dash before an option means something Unlike the flag package, a single dash before an option means something
different than a double dash. Single dashes signify a series of shorthand 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. letters for flags. All but the last shorthand letter must be boolean flags.
// boolean flags // boolean flags
-f -f
-abc -abc
// non-boolean flags // non-boolean flags
-n 1234 -n 1234
-Ifile -Ifile
// mixed // mixed
-abcs "hello" -abcs "hello"
-abcn1234 -abcn1234
Flag parsing stops after the terminator "--". Unlike the flag package, Flag parsing stops after the terminator "--". Unlike the flag package,
flags can be interspersed with arguments anywhere on the command line flags can be interspersed with arguments anywhere on the command line
before this terminator. before this terminator.
Integer flags accept 1234, 0664, 0x1234 and may be negative. Integer flags accept 1234, 0664, 0x1234 and may be negative.
Boolean flags (in their long form) accept 1, 0, t, f, true, false, Boolean flags (in their long form) accept 1, 0, t, f, true, false,
TRUE, FALSE, True, False. TRUE, FALSE, True, False.
Duration flags accept any input valid for time.ParseDuration. Duration flags accept any input valid for time.ParseDuration.
The default set of command-line flags is controlled by The default set of command-line flags is controlled by
top-level functions. The FlagSet type allows one to define top-level functions. The FlagSet type allows one to define
independent sets of flags, such as to implement subcommands independent sets of flags, such as to implement subcommands
in a command-line interface. The methods of FlagSet are in a command-line interface. The methods of FlagSet are
analogous to the top-level functions for the command-line analogous to the top-level functions for the command-line
flag set. flag set.
*/ */
package pflag package pflag
@ -115,8 +115,11 @@ var ErrHelp = errors.New("pflag: help requested")
type ErrorHandling int type ErrorHandling int
const ( const (
// ContinueOnError will return an err from Parse() if an error is found
ContinueOnError ErrorHandling = iota ContinueOnError ErrorHandling = iota
// ExitOnError will call os.Exit(2) if an error is found when parsing
ExitOnError ExitOnError
// PanicOnError will panic() if an error is found when parsing flags
PanicOnError PanicOnError
) )
@ -181,6 +184,11 @@ func sortFlags(flags map[NormalizedName]*Flag) []*Flag {
return result return result
} }
// 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 (f *FlagSet) SetNormalizeFunc(n func(f *FlagSet, name string) NormalizedName) { func (f *FlagSet) SetNormalizeFunc(n func(f *FlagSet, name string) NormalizedName) {
f.normalizeNameFunc = n f.normalizeNameFunc = n
for k, v := range f.formal { for k, v := range f.formal {
@ -191,6 +199,8 @@ func (f *FlagSet) SetNormalizeFunc(n func(f *FlagSet, name string) NormalizedNam
} }
} }
// GetNormalizeFunc returns the previously set NormalizeFunc of a function which
// does no translation, if not set previously.
func (f *FlagSet) GetNormalizeFunc() func(f *FlagSet, name string) NormalizedName { func (f *FlagSet) GetNormalizeFunc() func(f *FlagSet, name string) NormalizedName {
if f.normalizeNameFunc != nil { if f.normalizeNameFunc != nil {
return f.normalizeNameFunc return f.normalizeNameFunc
@ -224,6 +234,7 @@ func (f *FlagSet) VisitAll(fn func(*Flag)) {
} }
} }
// HasFlags returns a bool to indicate if the FlagSet has any flags definied.
func (f *FlagSet) HasFlags() bool { func (f *FlagSet) HasFlags() bool {
return len(f.formal) > 0 return len(f.formal) > 0
} }
@ -279,7 +290,9 @@ func (f *FlagSet) getFlagType(name string, ftype string, convFunc func(sval stri
return result, nil return result, nil
} }
// Mark a flag deprecated in your program // 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 (f *FlagSet) MarkDeprecated(name string, usageMessage string) error { func (f *FlagSet) MarkDeprecated(name string, usageMessage string) error {
flag := f.Lookup(name) flag := f.Lookup(name)
if flag == nil { if flag == nil {
@ -317,6 +330,9 @@ func (f *FlagSet) Set(name, value string) error {
return nil return nil
} }
// 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 (f *FlagSet) SetAnnotation(name, key string, values []string) error { func (f *FlagSet) SetAnnotation(name, key string, values []string) error {
normalName := f.normalizeFlagName(name) normalName := f.normalizeFlagName(name)
flag, ok := f.formal[normalName] flag, ok := f.formal[normalName]
@ -330,6 +346,8 @@ func (f *FlagSet) SetAnnotation(name, key string, values []string) error {
return nil return nil
} }
// Changed returns true if the flag was explicitly set during Parse() and false
// otherwise
func (f *FlagSet) Changed(name string) bool { func (f *FlagSet) Changed(name string) bool {
flag := f.Lookup(name) flag := f.Lookup(name)
// If a flag doesn't exist, it wasn't changed.... // If a flag doesn't exist, it wasn't changed....
@ -347,12 +365,20 @@ func Set(name, value string) error {
// PrintDefaults prints, to standard error unless configured // PrintDefaults prints, to standard error unless configured
// otherwise, the default values of all defined flags in the set. // otherwise, the default values of all defined flags in the set.
func (f *FlagSet) PrintDefaults() { func (f *FlagSet) PrintDefaults() {
usages := f.FlagUsages()
fmt.Fprintf(f.out(), "%s", usages)
}
// FlagUsages Returns a string containing the usage information for all flags in
// the FlagSet
func (f *FlagSet) FlagUsages() string {
x := new(bytes.Buffer)
f.VisitAll(func(flag *Flag) { f.VisitAll(func(flag *Flag) {
if len(flag.Deprecated) > 0 { if len(flag.Deprecated) > 0 {
return return
} }
format := "" format := ""
// ex: w/ option string argument '-%s, --%s[=%q]: %s\n'
if len(flag.Shorthand) > 0 { if len(flag.Shorthand) > 0 {
format = " -%s, --%s" format = " -%s, --%s"
} else { } else {
@ -361,7 +387,8 @@ func (f *FlagSet) PrintDefaults() {
if len(flag.NoOptDefVal) > 0 { if len(flag.NoOptDefVal) > 0 {
format = format + "[" format = format + "["
} }
if _, ok := flag.Value.(*stringValue); ok { if flag.Value.Type() == "string" {
// put quotes on the value
format = format + "=%q" format = format + "=%q"
} else { } else {
format = format + "=%s" format = format + "=%s"
@ -370,27 +397,6 @@ func (f *FlagSet) PrintDefaults() {
format = format + "]" format = format + "]"
} }
format = format + ": %s\n" format = format + ": %s\n"
fmt.Fprintf(f.out(), format, flag.Shorthand, flag.Name, flag.DefValue, flag.Usage)
})
}
func (f *FlagSet) FlagUsages() string {
x := new(bytes.Buffer)
f.VisitAll(func(flag *Flag) {
if len(flag.Deprecated) > 0 {
return
}
format := "--%s=%s: %s\n"
if _, ok := flag.Value.(*stringValue); ok {
// put quotes on the value
format = "--%s=%q: %s\n"
}
if len(flag.Shorthand) > 0 {
format = " -%s, " + format
} else {
format = " %s " + format
}
fmt.Fprintf(x, format, flag.Shorthand, flag.Name, flag.DefValue, flag.Usage) fmt.Fprintf(x, format, flag.Shorthand, flag.Name, flag.DefValue, flag.Usage)
}) })
@ -462,7 +468,7 @@ func (f *FlagSet) Var(value Value, name string, usage string) {
f.VarP(value, name, "", usage) f.VarP(value, name, "", usage)
} }
// Like VarP, but returns the flag created // VarPF is like VarP, but returns the flag created
func (f *FlagSet) VarPF(value Value, name, shorthand, usage string) *Flag { func (f *FlagSet) VarPF(value Value, name, shorthand, usage string) *Flag {
// Remember the default value as a string; it won't change. // Remember the default value as a string; it won't change.
flag := &Flag{ flag := &Flag{
@ -476,14 +482,15 @@ func (f *FlagSet) VarPF(value Value, name, shorthand, usage string) *Flag {
return flag return flag
} }
// Like Var, but accepts a shorthand letter that can be used after a single dash. // VarP is like Var, but accepts a shorthand letter that can be used after a single dash.
func (f *FlagSet) VarP(value Value, name, shorthand, usage string) { func (f *FlagSet) VarP(value Value, name, shorthand, usage string) {
_ = f.VarPF(value, name, shorthand, usage) _ = f.VarPF(value, name, shorthand, usage)
} }
// AddFlag will add the flag to the FlagSet
func (f *FlagSet) AddFlag(flag *Flag) { func (f *FlagSet) AddFlag(flag *Flag) {
// Call normalizeFlagName function only once // Call normalizeFlagName function only once
var normalizedFlagName NormalizedName = f.normalizeFlagName(flag.Name) normalizedFlagName := f.normalizeFlagName(flag.Name)
_, alreadythere := f.formal[normalizedFlagName] _, alreadythere := f.formal[normalizedFlagName]
if alreadythere { if alreadythere {
@ -517,6 +524,19 @@ func (f *FlagSet) AddFlag(flag *Flag) {
f.shorthands[c] = flag f.shorthands[c] = flag
} }
// AddFlagSet adds one FlagSet to another. If a flag is already present in f
// the flag from newSet will be ignored
func (f *FlagSet) AddFlagSet(newSet *FlagSet) {
if newSet == nil {
return
}
newSet.VisitAll(func(flag *Flag) {
if f.Lookup(flag.Name) == nil {
f.AddFlag(flag)
}
})
}
// Var defines a flag with the specified name and usage string. The type and // 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 // 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 // typically holds a user-defined implementation of Value. For instance, the
@ -527,7 +547,7 @@ func Var(value Value, name string, usage string) {
CommandLine.VarP(value, name, "", usage) CommandLine.VarP(value, name, "", usage)
} }
// Like Var, but accepts a shorthand letter that can be used after a single dash. // VarP is like Var, but accepts a shorthand letter that can be used after a single dash.
func VarP(value Value, name, shorthand, usage string) { func VarP(value Value, name, shorthand, usage string) {
CommandLine.VarP(value, name, shorthand, usage) CommandLine.VarP(value, name, shorthand, usage)
} }
@ -720,7 +740,7 @@ func Parse() {
CommandLine.Parse(os.Args[1:]) CommandLine.Parse(os.Args[1:])
} }
// Whether to support interspersed option/non-option arguments. // SetInterspersed sets whether to support interspersed option/non-option arguments.
func SetInterspersed(interspersed bool) { func SetInterspersed(interspersed bool) {
CommandLine.SetInterspersed(interspersed) CommandLine.SetInterspersed(interspersed)
} }
@ -744,7 +764,7 @@ func NewFlagSet(name string, errorHandling ErrorHandling) *FlagSet {
return f return f
} }
// Whether to support interspersed option/non-option arguments. // SetIntersperesed sets whether to support interspersed option/non-option arguments.
func (f *FlagSet) SetInterspersed(interspersed bool) { func (f *FlagSet) SetInterspersed(interspersed bool) {
f.interspersed = interspersed f.interspersed = interspersed
} }

View File

@ -19,15 +19,15 @@ import (
) )
var ( var (
test_bool = Bool("test_bool", false, "bool value") testBool = Bool("test_bool", false, "bool value")
test_int = Int("test_int", 0, "int value") testInt = Int("test_int", 0, "int value")
test_int64 = Int64("test_int64", 0, "int64 value") testInt64 = Int64("test_int64", 0, "int64 value")
test_uint = Uint("test_uint", 0, "uint value") testUint = Uint("test_uint", 0, "uint value")
test_uint64 = Uint64("test_uint64", 0, "uint64 value") testUint64 = Uint64("test_uint64", 0, "uint64 value")
test_string = String("test_string", "0", "string value") testString = String("test_string", "0", "string value")
test_float64 = Float64("test_float64", 0, "float64 value") testFloat = Float64("test_float64", 0, "float64 value")
test_duration = Duration("test_duration", 0, "time.Duration value") testDuration = Duration("test_duration", 0, "time.Duration value")
test_optional_int = Int("test_optional_int", 0, "optional int value") testOptionalInt = Int("test_optional_int", 0, "optional int value")
normalizeFlagNameInvocations = 0 normalizeFlagNameInvocations = 0
) )
@ -110,6 +110,23 @@ func TestUsage(t *testing.T) {
} }
} }
func TestAddFlagSet(t *testing.T) {
oldSet := NewFlagSet("old", ContinueOnError)
newSet := NewFlagSet("new", ContinueOnError)
oldSet.String("flag1", "flag1", "flag1")
oldSet.String("flag2", "flag2", "flag2")
newSet.String("flag2", "flag2", "flag2")
newSet.String("flag3", "flag3", "flag3")
oldSet.AddFlagSet(newSet)
if len(oldSet.formal) != 3 {
t.Errorf("Unexpected result adding a FlagSet to a FlagSet %v", oldSet)
}
}
func TestAnnotation(t *testing.T) { func TestAnnotation(t *testing.T) {
f := NewFlagSet("shorthand", ContinueOnError) f := NewFlagSet("shorthand", ContinueOnError)
@ -291,7 +308,7 @@ func testParse(f *FlagSet, t *testing.T) {
t.Error("mask flag should be 255.255.255.0, is ", (*maskFlag).String()) t.Error("mask flag should be 255.255.255.0, is ", (*maskFlag).String())
} }
if v, err := f.GetIPv4Mask("mask"); err != nil || v.String() != (*maskFlag).String() { if v, err := f.GetIPv4Mask("mask"); err != nil || v.String() != (*maskFlag).String() {
t.Errorf("GetIP returned %v but maskFlag was %v", v, *maskFlag, err) t.Errorf("GetIP returned %v maskFlag was %v error was %v", v, *maskFlag, err)
} }
if *durationFlag != 2*time.Minute { if *durationFlag != 2*time.Minute {
t.Error("duration flag should be 2m, is ", *durationFlag) t.Error("duration flag should be 2m, is ", *durationFlag)

View File

@ -48,7 +48,7 @@ func (f *FlagSet) Float32Var(p *float32, name string, value float32, usage strin
f.VarP(newFloat32Value(value, p), name, "", usage) f.VarP(newFloat32Value(value, p), name, "", usage)
} }
// Like Float32Var, but accepts a shorthand letter that can be used after a single dash. // Float32VarP is like Float32Var, but accepts a shorthand letter that can be used after a single dash.
func (f *FlagSet) Float32VarP(p *float32, name, shorthand string, value float32, usage string) { func (f *FlagSet) Float32VarP(p *float32, name, shorthand string, value float32, usage string) {
f.VarP(newFloat32Value(value, p), name, shorthand, usage) f.VarP(newFloat32Value(value, p), name, shorthand, usage)
} }
@ -59,7 +59,7 @@ func Float32Var(p *float32, name string, value float32, usage string) {
CommandLine.VarP(newFloat32Value(value, p), name, "", usage) CommandLine.VarP(newFloat32Value(value, p), name, "", usage)
} }
// Like Float32Var, but accepts a shorthand letter that can be used after a single dash. // Float32VarP is like Float32Var, but accepts a shorthand letter that can be used after a single dash.
func Float32VarP(p *float32, name, shorthand string, value float32, usage string) { func Float32VarP(p *float32, name, shorthand string, value float32, usage string) {
CommandLine.VarP(newFloat32Value(value, p), name, shorthand, usage) CommandLine.VarP(newFloat32Value(value, p), name, shorthand, usage)
} }
@ -72,7 +72,7 @@ func (f *FlagSet) Float32(name string, value float32, usage string) *float32 {
return p return p
} }
// Like Float32, but accepts a shorthand letter that can be used after a single dash. // Float32P is like Float32, but accepts a shorthand letter that can be used after a single dash.
func (f *FlagSet) Float32P(name, shorthand string, value float32, usage string) *float32 { func (f *FlagSet) Float32P(name, shorthand string, value float32, usage string) *float32 {
p := new(float32) p := new(float32)
f.Float32VarP(p, name, shorthand, value, usage) f.Float32VarP(p, name, shorthand, value, usage)
@ -85,7 +85,7 @@ func Float32(name string, value float32, usage string) *float32 {
return CommandLine.Float32P(name, "", value, usage) return CommandLine.Float32P(name, "", value, usage)
} }
// Like Float32, but accepts a shorthand letter that can be used after a single dash. // Float32P is like Float32, but accepts a shorthand letter that can be used after a single dash.
func Float32P(name, shorthand string, value float32, usage string) *float32 { func Float32P(name, shorthand string, value float32, usage string) *float32 {
return CommandLine.Float32P(name, shorthand, value, usage) return CommandLine.Float32P(name, shorthand, value, usage)
} }

View File

@ -44,7 +44,7 @@ func (f *FlagSet) Float64Var(p *float64, name string, value float64, usage strin
f.VarP(newFloat64Value(value, p), name, "", usage) f.VarP(newFloat64Value(value, p), name, "", usage)
} }
// Like Float64Var, but accepts a shorthand letter that can be used after a single dash. // Float64VarP is like Float64Var, but accepts a shorthand letter that can be used after a single dash.
func (f *FlagSet) Float64VarP(p *float64, name, shorthand string, value float64, usage string) { func (f *FlagSet) Float64VarP(p *float64, name, shorthand string, value float64, usage string) {
f.VarP(newFloat64Value(value, p), name, shorthand, usage) f.VarP(newFloat64Value(value, p), name, shorthand, usage)
} }
@ -55,7 +55,7 @@ func Float64Var(p *float64, name string, value float64, usage string) {
CommandLine.VarP(newFloat64Value(value, p), name, "", usage) CommandLine.VarP(newFloat64Value(value, p), name, "", usage)
} }
// Like Float64Var, but accepts a shorthand letter that can be used after a single dash. // Float64VarP is like Float64Var, but accepts a shorthand letter that can be used after a single dash.
func Float64VarP(p *float64, name, shorthand string, value float64, usage string) { func Float64VarP(p *float64, name, shorthand string, value float64, usage string) {
CommandLine.VarP(newFloat64Value(value, p), name, shorthand, usage) CommandLine.VarP(newFloat64Value(value, p), name, shorthand, usage)
} }
@ -68,7 +68,7 @@ func (f *FlagSet) Float64(name string, value float64, usage string) *float64 {
return p return p
} }
// Like Float64, but accepts a shorthand letter that can be used after a single dash. // Float64P is like Float64, but accepts a shorthand letter that can be used after a single dash.
func (f *FlagSet) Float64P(name, shorthand string, value float64, usage string) *float64 { func (f *FlagSet) Float64P(name, shorthand string, value float64, usage string) *float64 {
p := new(float64) p := new(float64)
f.Float64VarP(p, name, shorthand, value, usage) f.Float64VarP(p, name, shorthand, value, usage)
@ -81,7 +81,7 @@ func Float64(name string, value float64, usage string) *float64 {
return CommandLine.Float64P(name, "", value, usage) return CommandLine.Float64P(name, "", value, usage)
} }
// Like Float64, but accepts a shorthand letter that can be used after a single dash. // Float64P is like Float64, but accepts a shorthand letter that can be used after a single dash.
func Float64P(name, shorthand string, value float64, usage string) *float64 { func Float64P(name, shorthand string, value float64, usage string) *float64 {
return CommandLine.Float64P(name, shorthand, value, usage) return CommandLine.Float64P(name, shorthand, value, usage)
} }

View File

@ -0,0 +1,94 @@
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package pflag
import (
goflag "flag"
"fmt"
"reflect"
"strings"
)
var _ = fmt.Print
// flagValueWrapper implements pflag.Value around a flag.Value. The main
// difference here is the addition of the Type method that returns a string
// name of the type. As this is generally unknown, we approximate that with
// reflection.
type flagValueWrapper struct {
inner goflag.Value
flagType string
}
// We are just copying the boolFlag interface out of goflag as that is what
// they use to decide if a flag should get "true" when no arg is given.
type goBoolFlag interface {
goflag.Value
IsBoolFlag() bool
}
func wrapFlagValue(v goflag.Value) Value {
// If the flag.Value happens to also be a pflag.Value, just use it directly.
if pv, ok := v.(Value); ok {
return pv
}
pv := &flagValueWrapper{
inner: v,
}
t := reflect.TypeOf(v)
if t.Kind() == reflect.Interface || t.Kind() == reflect.Ptr {
t = t.Elem()
}
pv.flagType = strings.TrimSuffix(t.Name(), "Value")
return pv
}
func (v *flagValueWrapper) String() string {
return v.inner.String()
}
func (v *flagValueWrapper) Set(s string) error {
return v.inner.Set(s)
}
func (v *flagValueWrapper) Type() string {
return v.flagType
}
func PFlagFromGoFlag(goflag *goflag.Flag) *Flag {
// Remember the default value as a string; it won't change.
flag := &Flag{
Name: goflag.Name,
Usage: goflag.Usage,
Value: wrapFlagValue(goflag.Value),
// Looks like golang flags don't set DefValue correctly :-(
//DefValue: goflag.DefValue,
DefValue: goflag.Value.String(),
}
if fv, ok := goflag.Value.(goBoolFlag); ok && fv.IsBoolFlag() {
flag.NoOptDefVal = "true"
}
return flag
}
func (f *FlagSet) AddGoFlag(goflag *goflag.Flag) {
if f.Lookup(goflag.Name) != nil {
return
}
newflag := PFlagFromGoFlag(goflag)
f.AddFlag(newflag)
}
func (f *FlagSet) AddGoFlagSet(newSet *goflag.FlagSet) {
if newSet == nil {
return
}
newSet.VisitAll(func(goflag *goflag.Flag) {
f.AddGoFlag(goflag)
})
}

View File

@ -0,0 +1,39 @@
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package pflag
import (
goflag "flag"
"testing"
)
func TestGoflags(t *testing.T) {
goflag.String("stringFlag", "stringFlag", "stringFlag")
goflag.Bool("boolFlag", false, "boolFlag")
f := NewFlagSet("test", ContinueOnError)
f.AddGoFlagSet(goflag.CommandLine)
err := f.Parse([]string{"--stringFlag=bob", "--boolFlag"})
if err != nil {
t.Fatal("expected no error; get", err)
}
getString, err := f.GetString("stringFlag")
if err != nil {
t.Fatal("expected no error; get", err)
}
if getString != "bob" {
t.Fatalf("expected getString=bob but got getString=%s", getString)
}
getBool, err := f.GetBool("boolFlag")
if err != nil {
t.Fatal("expected no error; get", err)
}
if getBool != true {
t.Fatalf("expected getBool=true but got getBool=%v", getBool)
}
}

View File

@ -44,7 +44,7 @@ func (f *FlagSet) IntVar(p *int, name string, value int, usage string) {
f.VarP(newIntValue(value, p), name, "", usage) f.VarP(newIntValue(value, p), name, "", usage)
} }
// Like IntVar, but accepts a shorthand letter that can be used after a single dash. // IntVarP is like IntVar, but accepts a shorthand letter that can be used after a single dash.
func (f *FlagSet) IntVarP(p *int, name, shorthand string, value int, usage string) { func (f *FlagSet) IntVarP(p *int, name, shorthand string, value int, usage string) {
f.VarP(newIntValue(value, p), name, shorthand, usage) f.VarP(newIntValue(value, p), name, shorthand, usage)
} }
@ -55,7 +55,7 @@ func IntVar(p *int, name string, value int, usage string) {
CommandLine.VarP(newIntValue(value, p), name, "", usage) CommandLine.VarP(newIntValue(value, p), name, "", usage)
} }
// Like IntVar, but accepts a shorthand letter that can be used after a single dash. // IntVarP is like IntVar, but accepts a shorthand letter that can be used after a single dash.
func IntVarP(p *int, name, shorthand string, value int, usage string) { func IntVarP(p *int, name, shorthand string, value int, usage string) {
CommandLine.VarP(newIntValue(value, p), name, shorthand, usage) CommandLine.VarP(newIntValue(value, p), name, shorthand, usage)
} }
@ -68,7 +68,7 @@ func (f *FlagSet) Int(name string, value int, usage string) *int {
return p return p
} }
// Like Int, but accepts a shorthand letter that can be used after a single dash. // IntP is like Int, but accepts a shorthand letter that can be used after a single dash.
func (f *FlagSet) IntP(name, shorthand string, value int, usage string) *int { func (f *FlagSet) IntP(name, shorthand string, value int, usage string) *int {
p := new(int) p := new(int)
f.IntVarP(p, name, shorthand, value, usage) f.IntVarP(p, name, shorthand, value, usage)
@ -81,7 +81,7 @@ func Int(name string, value int, usage string) *int {
return CommandLine.IntP(name, "", value, usage) return CommandLine.IntP(name, "", value, usage)
} }
// Like Int, but accepts a shorthand letter that can be used after a single dash. // IntP is like Int, but accepts a shorthand letter that can be used after a single dash.
func IntP(name, shorthand string, value int, usage string) *int { func IntP(name, shorthand string, value int, usage string) *int {
return CommandLine.IntP(name, shorthand, value, usage) return CommandLine.IntP(name, shorthand, value, usage)
} }

View File

@ -48,7 +48,7 @@ func (f *FlagSet) Int32Var(p *int32, name string, value int32, usage string) {
f.VarP(newInt32Value(value, p), name, "", usage) f.VarP(newInt32Value(value, p), name, "", usage)
} }
// Like Int32Var, but accepts a shorthand letter that can be used after a single dash. // Int32VarP is like Int32Var, but accepts a shorthand letter that can be used after a single dash.
func (f *FlagSet) Int32VarP(p *int32, name, shorthand string, value int32, usage string) { func (f *FlagSet) Int32VarP(p *int32, name, shorthand string, value int32, usage string) {
f.VarP(newInt32Value(value, p), name, shorthand, usage) f.VarP(newInt32Value(value, p), name, shorthand, usage)
} }
@ -59,7 +59,7 @@ func Int32Var(p *int32, name string, value int32, usage string) {
CommandLine.VarP(newInt32Value(value, p), name, "", usage) CommandLine.VarP(newInt32Value(value, p), name, "", usage)
} }
// Like Int32Var, but accepts a shorthand letter that can be used after a single dash. // Int32VarP is like Int32Var, but accepts a shorthand letter that can be used after a single dash.
func Int32VarP(p *int32, name, shorthand string, value int32, usage string) { func Int32VarP(p *int32, name, shorthand string, value int32, usage string) {
CommandLine.VarP(newInt32Value(value, p), name, shorthand, usage) CommandLine.VarP(newInt32Value(value, p), name, shorthand, usage)
} }
@ -72,7 +72,7 @@ func (f *FlagSet) Int32(name string, value int32, usage string) *int32 {
return p return p
} }
// Like Int32, but accepts a shorthand letter that can be used after a single dash. // Int32P is like Int32, but accepts a shorthand letter that can be used after a single dash.
func (f *FlagSet) Int32P(name, shorthand string, value int32, usage string) *int32 { func (f *FlagSet) Int32P(name, shorthand string, value int32, usage string) *int32 {
p := new(int32) p := new(int32)
f.Int32VarP(p, name, shorthand, value, usage) f.Int32VarP(p, name, shorthand, value, usage)
@ -85,7 +85,7 @@ func Int32(name string, value int32, usage string) *int32 {
return CommandLine.Int32P(name, "", value, usage) return CommandLine.Int32P(name, "", value, usage)
} }
// Like Int32, but accepts a shorthand letter that can be used after a single dash. // Int32P is like Int32, but accepts a shorthand letter that can be used after a single dash.
func Int32P(name, shorthand string, value int32, usage string) *int32 { func Int32P(name, shorthand string, value int32, usage string) *int32 {
return CommandLine.Int32P(name, shorthand, value, usage) return CommandLine.Int32P(name, shorthand, value, usage)
} }

View File

@ -44,7 +44,7 @@ func (f *FlagSet) Int64Var(p *int64, name string, value int64, usage string) {
f.VarP(newInt64Value(value, p), name, "", usage) f.VarP(newInt64Value(value, p), name, "", usage)
} }
// Like Int64Var, but accepts a shorthand letter that can be used after a single dash. // Int64VarP is like Int64Var, but accepts a shorthand letter that can be used after a single dash.
func (f *FlagSet) Int64VarP(p *int64, name, shorthand string, value int64, usage string) { func (f *FlagSet) Int64VarP(p *int64, name, shorthand string, value int64, usage string) {
f.VarP(newInt64Value(value, p), name, shorthand, usage) f.VarP(newInt64Value(value, p), name, shorthand, usage)
} }
@ -55,7 +55,7 @@ func Int64Var(p *int64, name string, value int64, usage string) {
CommandLine.VarP(newInt64Value(value, p), name, "", usage) CommandLine.VarP(newInt64Value(value, p), name, "", usage)
} }
// Like Int64Var, but accepts a shorthand letter that can be used after a single dash. // Int64VarP is like Int64Var, but accepts a shorthand letter that can be used after a single dash.
func Int64VarP(p *int64, name, shorthand string, value int64, usage string) { func Int64VarP(p *int64, name, shorthand string, value int64, usage string) {
CommandLine.VarP(newInt64Value(value, p), name, shorthand, usage) CommandLine.VarP(newInt64Value(value, p), name, shorthand, usage)
} }
@ -68,7 +68,7 @@ func (f *FlagSet) Int64(name string, value int64, usage string) *int64 {
return p return p
} }
// Like Int64, but accepts a shorthand letter that can be used after a single dash. // Int64P is like Int64, but accepts a shorthand letter that can be used after a single dash.
func (f *FlagSet) Int64P(name, shorthand string, value int64, usage string) *int64 { func (f *FlagSet) Int64P(name, shorthand string, value int64, usage string) *int64 {
p := new(int64) p := new(int64)
f.Int64VarP(p, name, shorthand, value, usage) f.Int64VarP(p, name, shorthand, value, usage)
@ -81,7 +81,7 @@ func Int64(name string, value int64, usage string) *int64 {
return CommandLine.Int64P(name, "", value, usage) return CommandLine.Int64P(name, "", value, usage)
} }
// Like Int64, but accepts a shorthand letter that can be used after a single dash. // Int64P is like Int64, but accepts a shorthand letter that can be used after a single dash.
func Int64P(name, shorthand string, value int64, usage string) *int64 { func Int64P(name, shorthand string, value int64, usage string) *int64 {
return CommandLine.Int64P(name, shorthand, value, usage) return CommandLine.Int64P(name, shorthand, value, usage)
} }

View File

@ -48,7 +48,7 @@ func (f *FlagSet) Int8Var(p *int8, name string, value int8, usage string) {
f.VarP(newInt8Value(value, p), name, "", usage) f.VarP(newInt8Value(value, p), name, "", usage)
} }
// Like Int8Var, but accepts a shorthand letter that can be used after a single dash. // Int8VarP is like Int8Var, but accepts a shorthand letter that can be used after a single dash.
func (f *FlagSet) Int8VarP(p *int8, name, shorthand string, value int8, usage string) { func (f *FlagSet) Int8VarP(p *int8, name, shorthand string, value int8, usage string) {
f.VarP(newInt8Value(value, p), name, shorthand, usage) f.VarP(newInt8Value(value, p), name, shorthand, usage)
} }
@ -59,7 +59,7 @@ func Int8Var(p *int8, name string, value int8, usage string) {
CommandLine.VarP(newInt8Value(value, p), name, "", usage) CommandLine.VarP(newInt8Value(value, p), name, "", usage)
} }
// Like Int8Var, but accepts a shorthand letter that can be used after a single dash. // Int8VarP is like Int8Var, but accepts a shorthand letter that can be used after a single dash.
func Int8VarP(p *int8, name, shorthand string, value int8, usage string) { func Int8VarP(p *int8, name, shorthand string, value int8, usage string) {
CommandLine.VarP(newInt8Value(value, p), name, shorthand, usage) CommandLine.VarP(newInt8Value(value, p), name, shorthand, usage)
} }
@ -72,7 +72,7 @@ func (f *FlagSet) Int8(name string, value int8, usage string) *int8 {
return p return p
} }
// Like Int8, but accepts a shorthand letter that can be used after a single dash. // Int8P is like Int8, but accepts a shorthand letter that can be used after a single dash.
func (f *FlagSet) Int8P(name, shorthand string, value int8, usage string) *int8 { func (f *FlagSet) Int8P(name, shorthand string, value int8, usage string) *int8 {
p := new(int8) p := new(int8)
f.Int8VarP(p, name, shorthand, value, usage) f.Int8VarP(p, name, shorthand, value, usage)
@ -85,7 +85,7 @@ func Int8(name string, value int8, usage string) *int8 {
return CommandLine.Int8P(name, "", value, usage) return CommandLine.Int8P(name, "", value, usage)
} }
// Like Int8, but accepts a shorthand letter that can be used after a single dash. // Int8P is like Int8, but accepts a shorthand letter that can be used after a single dash.
func Int8P(name, shorthand string, value int8, usage string) *int8 { func Int8P(name, shorthand string, value int8, usage string) *int8 {
return CommandLine.Int8P(name, shorthand, value, usage) return CommandLine.Int8P(name, shorthand, value, usage)
} }

View File

@ -85,7 +85,7 @@ func (f *FlagSet) IntSliceVar(p *[]int, name string, value []int, usage string)
f.VarP(newIntSliceValue(value, p), name, "", usage) f.VarP(newIntSliceValue(value, p), name, "", usage)
} }
// Like IntSliceVar, but accepts a shorthand letter that can be used after a single dash. // IntSliceVarP is like IntSliceVar, but accepts a shorthand letter that can be used after a single dash.
func (f *FlagSet) IntSliceVarP(p *[]int, name, shorthand string, value []int, usage string) { func (f *FlagSet) IntSliceVarP(p *[]int, name, shorthand string, value []int, usage string) {
f.VarP(newIntSliceValue(value, p), name, shorthand, usage) f.VarP(newIntSliceValue(value, p), name, shorthand, usage)
} }
@ -96,7 +96,7 @@ func IntSliceVar(p *[]int, name string, value []int, usage string) {
CommandLine.VarP(newIntSliceValue(value, p), name, "", usage) CommandLine.VarP(newIntSliceValue(value, p), name, "", usage)
} }
// Like IntSliceVar, but accepts a shorthand letter that can be used after a single dash. // IntSliceVarP is like IntSliceVar, but accepts a shorthand letter that can be used after a single dash.
func IntSliceVarP(p *[]int, name, shorthand string, value []int, usage string) { func IntSliceVarP(p *[]int, name, shorthand string, value []int, usage string) {
CommandLine.VarP(newIntSliceValue(value, p), name, shorthand, usage) CommandLine.VarP(newIntSliceValue(value, p), name, shorthand, usage)
} }
@ -104,14 +104,14 @@ func IntSliceVarP(p *[]int, name, shorthand string, value []int, usage string) {
// IntSlice defines a []int flag with specified name, default value, and usage string. // 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. // The return value is the address of a []int variable that stores the value of the flag.
func (f *FlagSet) IntSlice(name string, value []int, usage string) *[]int { func (f *FlagSet) IntSlice(name string, value []int, usage string) *[]int {
p := make([]int, 0) p := []int{}
f.IntSliceVarP(&p, name, "", value, usage) f.IntSliceVarP(&p, name, "", value, usage)
return &p return &p
} }
// Like IntSlice, but accepts a shorthand letter that can be used after a single dash. // IntSliceP is like IntSlice, but accepts a shorthand letter that can be used after a single dash.
func (f *FlagSet) IntSliceP(name, shorthand string, value []int, usage string) *[]int { func (f *FlagSet) IntSliceP(name, shorthand string, value []int, usage string) *[]int {
p := make([]int, 0) p := []int{}
f.IntSliceVarP(&p, name, shorthand, value, usage) f.IntSliceVarP(&p, name, shorthand, value, usage)
return &p return &p
} }
@ -122,7 +122,7 @@ func IntSlice(name string, value []int, usage string) *[]int {
return CommandLine.IntSliceP(name, "", value, usage) return CommandLine.IntSliceP(name, "", value, usage)
} }
// Like IntSlice, but accepts a shorthand letter that can be used after a single dash. // IntSliceP is like IntSlice, but accepts a shorthand letter that can be used after a single dash.
func IntSliceP(name, shorthand string, value []int, usage string) *[]int { func IntSliceP(name, shorthand string, value []int, usage string) *[]int {
return CommandLine.IntSliceP(name, shorthand, value, usage) return CommandLine.IntSliceP(name, shorthand, value, usage)
} }

View File

@ -13,13 +13,13 @@ import (
func setUpISFlagSet(isp *[]int) *FlagSet { func setUpISFlagSet(isp *[]int) *FlagSet {
f := NewFlagSet("test", ContinueOnError) f := NewFlagSet("test", ContinueOnError)
f.IntSliceVar(isp, "is", []int{}, "Command seperated list!") f.IntSliceVar(isp, "is", []int{}, "Command separated list!")
return f return f
} }
func setUpISFlagSetWithDefault(isp *[]int) *FlagSet { func setUpISFlagSetWithDefault(isp *[]int) *FlagSet {
f := NewFlagSet("test", ContinueOnError) f := NewFlagSet("test", ContinueOnError)
f.IntSliceVar(isp, "is", []int{0, 1}, "Command seperated list!") f.IntSliceVar(isp, "is", []int{0, 1}, "Command separated list!")
return f return f
} }
@ -87,7 +87,7 @@ func TestISDefault(t *testing.T) {
t.Fatalf("got error: %v", err) t.Fatalf("got error: %v", err)
} }
if d != v { if d != v {
t.Fatalf("expected is[%d] to be %s but got: %s", i, d, v) t.Fatalf("expected is[%d] to be %d but got: %d", i, d, v)
} }
} }
@ -101,7 +101,7 @@ func TestISDefault(t *testing.T) {
t.Fatal("got an error from GetIntSlice():", err) t.Fatal("got an error from GetIntSlice():", err)
} }
if d != v { if d != v {
t.Fatalf("expected is[%d] to be %s from GetIntSlice but got: %s", i, d, v) t.Fatalf("expected is[%d] to be %d from GetIntSlice but got: %d", i, d, v)
} }
} }
} }
@ -122,7 +122,7 @@ func TestISWithDefault(t *testing.T) {
t.Fatalf("got error: %v", err) t.Fatalf("got error: %v", err)
} }
if d != v { if d != v {
t.Fatalf("expected is[%d] to be %s but got: %s", i, d, v) t.Fatalf("expected is[%d] to be %d but got: %d", i, d, v)
} }
} }
@ -136,7 +136,7 @@ func TestISWithDefault(t *testing.T) {
t.Fatalf("got error: %v", err) t.Fatalf("got error: %v", err)
} }
if d != v { if d != v {
t.Fatalf("expected is[%d] to be %s from GetIntSlice but got: %s", i, d, v) t.Fatalf("expected is[%d] to be %d from GetIntSlice but got: %d", i, d, v)
} }
} }
} }
@ -156,7 +156,7 @@ func TestISCalledTwice(t *testing.T) {
} }
for i, v := range is { for i, v := range is {
if expected[i] != v { if expected[i] != v {
t.Fatalf("expected is[%d] to be %s but got: %s", i, expected[i], v) t.Fatalf("expected is[%d] to be %d but got: %d", i, expected[i], v)
} }
} }
} }

View File

@ -53,7 +53,7 @@ func (f *FlagSet) IPVar(p *net.IP, name string, value net.IP, usage string) {
f.VarP(newIPValue(value, p), name, "", usage) f.VarP(newIPValue(value, p), name, "", usage)
} }
// Like IPVar, but accepts a shorthand letter that can be used after a single dash. // IPVarP is like IPVar, but accepts a shorthand letter that can be used after a single dash.
func (f *FlagSet) IPVarP(p *net.IP, name, shorthand string, value net.IP, usage string) { func (f *FlagSet) IPVarP(p *net.IP, name, shorthand string, value net.IP, usage string) {
f.VarP(newIPValue(value, p), name, shorthand, usage) f.VarP(newIPValue(value, p), name, shorthand, usage)
} }
@ -64,7 +64,7 @@ func IPVar(p *net.IP, name string, value net.IP, usage string) {
CommandLine.VarP(newIPValue(value, p), name, "", usage) CommandLine.VarP(newIPValue(value, p), name, "", usage)
} }
// Like IPVar, but accepts a shorthand letter that can be used after a single dash. // IPVarP is like IPVar, but accepts a shorthand letter that can be used after a single dash.
func IPVarP(p *net.IP, name, shorthand string, value net.IP, usage string) { func IPVarP(p *net.IP, name, shorthand string, value net.IP, usage string) {
CommandLine.VarP(newIPValue(value, p), name, shorthand, usage) CommandLine.VarP(newIPValue(value, p), name, shorthand, usage)
} }
@ -77,7 +77,7 @@ func (f *FlagSet) IP(name string, value net.IP, usage string) *net.IP {
return p return p
} }
// Like IP, but accepts a shorthand letter that can be used after a single dash. // IPP is like IP, but accepts a shorthand letter that can be used after a single dash.
func (f *FlagSet) IPP(name, shorthand string, value net.IP, usage string) *net.IP { func (f *FlagSet) IPP(name, shorthand string, value net.IP, usage string) *net.IP {
p := new(net.IP) p := new(net.IP)
f.IPVarP(p, name, shorthand, value, usage) f.IPVarP(p, name, shorthand, value, usage)
@ -90,7 +90,7 @@ func IP(name string, value net.IP, usage string) *net.IP {
return CommandLine.IPP(name, "", value, usage) return CommandLine.IPP(name, "", value, usage)
} }
// Like IP, but accepts a shorthand letter that can be used after a single dash. // IPP is like IP, but accepts a shorthand letter that can be used after a single dash.
func IPP(name, shorthand string, value net.IP, usage string) *net.IP { func IPP(name, shorthand string, value net.IP, usage string) *net.IP {
return CommandLine.IPP(name, shorthand, value, usage) return CommandLine.IPP(name, shorthand, value, usage)
} }

View File

@ -28,7 +28,7 @@ func (i *ipMaskValue) Type() string {
return "ipMask" return "ipMask"
} }
// Parse IPv4 netmask written in IP form (e.g. 255.255.255.0). // ParseIPv4Mask written in IP form (e.g. 255.255.255.0).
// This function should really belong to the net package. // This function should really belong to the net package.
func ParseIPv4Mask(s string) net.IPMask { func ParseIPv4Mask(s string) net.IPMask {
mask := net.ParseIP(s) mask := net.ParseIP(s)
@ -79,7 +79,7 @@ func (f *FlagSet) IPMaskVar(p *net.IPMask, name string, value net.IPMask, usage
f.VarP(newIPMaskValue(value, p), name, "", usage) f.VarP(newIPMaskValue(value, p), name, "", usage)
} }
// Like IPMaskVar, but accepts a shorthand letter that can be used after a single dash. // IPMaskVarP is like IPMaskVar, but accepts a shorthand letter that can be used after a single dash.
func (f *FlagSet) IPMaskVarP(p *net.IPMask, name, shorthand string, value net.IPMask, usage string) { func (f *FlagSet) IPMaskVarP(p *net.IPMask, name, shorthand string, value net.IPMask, usage string) {
f.VarP(newIPMaskValue(value, p), name, shorthand, usage) f.VarP(newIPMaskValue(value, p), name, shorthand, usage)
} }
@ -90,7 +90,7 @@ func IPMaskVar(p *net.IPMask, name string, value net.IPMask, usage string) {
CommandLine.VarP(newIPMaskValue(value, p), name, "", usage) CommandLine.VarP(newIPMaskValue(value, p), name, "", usage)
} }
// Like IPMaskVar, but accepts a shorthand letter that can be used after a single dash. // IPMaskVarP is like IPMaskVar, but accepts a shorthand letter that can be used after a single dash.
func IPMaskVarP(p *net.IPMask, name, shorthand string, value net.IPMask, usage string) { func IPMaskVarP(p *net.IPMask, name, shorthand string, value net.IPMask, usage string) {
CommandLine.VarP(newIPMaskValue(value, p), name, shorthand, usage) CommandLine.VarP(newIPMaskValue(value, p), name, shorthand, usage)
} }
@ -103,7 +103,7 @@ func (f *FlagSet) IPMask(name string, value net.IPMask, usage string) *net.IPMas
return p return p
} }
// Like IPMask, but accepts a shorthand letter that can be used after a single dash. // IPMaskP is like IPMask, but accepts a shorthand letter that can be used after a single dash.
func (f *FlagSet) IPMaskP(name, shorthand string, value net.IPMask, usage string) *net.IPMask { func (f *FlagSet) IPMaskP(name, shorthand string, value net.IPMask, usage string) *net.IPMask {
p := new(net.IPMask) p := new(net.IPMask)
f.IPMaskVarP(p, name, shorthand, value, usage) f.IPMaskVarP(p, name, shorthand, value, usage)
@ -116,7 +116,7 @@ func IPMask(name string, value net.IPMask, usage string) *net.IPMask {
return CommandLine.IPMaskP(name, "", value, usage) return CommandLine.IPMaskP(name, "", value, usage)
} }
// Like IP, but accepts a shorthand letter that can be used after a single dash. // IPMaskP is like IP, but accepts a shorthand letter that can be used after a single dash.
func IPMaskP(name, shorthand string, value net.IPMask, usage string) *net.IPMask { func IPMaskP(name, shorthand string, value net.IPMask, usage string) *net.IPMask {
return CommandLine.IPMaskP(name, shorthand, value, usage) return CommandLine.IPMaskP(name, shorthand, value, usage)
} }

View File

@ -7,31 +7,31 @@ import (
) )
// IPNet adapts net.IPNet for use as a flag. // IPNet adapts net.IPNet for use as a flag.
type IPNetValue net.IPNet type ipNetValue net.IPNet
func (ipnet IPNetValue) String() string { func (ipnet ipNetValue) String() string {
n := net.IPNet(ipnet) n := net.IPNet(ipnet)
return n.String() return n.String()
} }
func (ipnet *IPNetValue) Set(value string) error { func (ipnet *ipNetValue) Set(value string) error {
_, n, err := net.ParseCIDR(strings.TrimSpace(value)) _, n, err := net.ParseCIDR(strings.TrimSpace(value))
if err != nil { if err != nil {
return err return err
} }
*ipnet = IPNetValue(*n) *ipnet = ipNetValue(*n)
return nil return nil
} }
func (*IPNetValue) Type() string { func (*ipNetValue) Type() string {
return "ipNet" return "ipNet"
} }
var _ = strings.TrimSpace var _ = strings.TrimSpace
func newIPNetValue(val net.IPNet, p *net.IPNet) *IPNetValue { func newIPNetValue(val net.IPNet, p *net.IPNet) *ipNetValue {
*p = val *p = val
return (*IPNetValue)(p) return (*ipNetValue)(p)
} }
func ipNetConv(sval string) (interface{}, error) { func ipNetConv(sval string) (interface{}, error) {
@ -57,7 +57,7 @@ func (f *FlagSet) IPNetVar(p *net.IPNet, name string, value net.IPNet, usage str
f.VarP(newIPNetValue(value, p), name, "", usage) f.VarP(newIPNetValue(value, p), name, "", usage)
} }
// Like IPNetVar, but accepts a shorthand letter that can be used after a single dash. // IPNetVarP is like IPNetVar, but accepts a shorthand letter that can be used after a single dash.
func (f *FlagSet) IPNetVarP(p *net.IPNet, name, shorthand string, value net.IPNet, usage string) { func (f *FlagSet) IPNetVarP(p *net.IPNet, name, shorthand string, value net.IPNet, usage string) {
f.VarP(newIPNetValue(value, p), name, shorthand, usage) f.VarP(newIPNetValue(value, p), name, shorthand, usage)
} }
@ -68,7 +68,7 @@ func IPNetVar(p *net.IPNet, name string, value net.IPNet, usage string) {
CommandLine.VarP(newIPNetValue(value, p), name, "", usage) CommandLine.VarP(newIPNetValue(value, p), name, "", usage)
} }
// Like IPNetVar, but accepts a shorthand letter that can be used after a single dash. // IPNetVarP is like IPNetVar, but accepts a shorthand letter that can be used after a single dash.
func IPNetVarP(p *net.IPNet, name, shorthand string, value net.IPNet, usage string) { func IPNetVarP(p *net.IPNet, name, shorthand string, value net.IPNet, usage string) {
CommandLine.VarP(newIPNetValue(value, p), name, shorthand, usage) CommandLine.VarP(newIPNetValue(value, p), name, shorthand, usage)
} }
@ -81,7 +81,7 @@ func (f *FlagSet) IPNet(name string, value net.IPNet, usage string) *net.IPNet {
return p return p
} }
// Like IPNet, but accepts a shorthand letter that can be used after a single dash. // IPNetP is like IPNet, but accepts a shorthand letter that can be used after a single dash.
func (f *FlagSet) IPNetP(name, shorthand string, value net.IPNet, usage string) *net.IPNet { func (f *FlagSet) IPNetP(name, shorthand string, value net.IPNet, usage string) *net.IPNet {
p := new(net.IPNet) p := new(net.IPNet)
f.IPNetVarP(p, name, shorthand, value, usage) f.IPNetVarP(p, name, shorthand, value, usage)
@ -94,7 +94,7 @@ func IPNet(name string, value net.IPNet, usage string) *net.IPNet {
return CommandLine.IPNetP(name, "", value, usage) return CommandLine.IPNetP(name, "", value, usage)
} }
// Like IPNet, but accepts a shorthand letter that can be used after a single dash. // IPNetP is like IPNet, but accepts a shorthand letter that can be used after a single dash.
func IPNetP(name, shorthand string, value net.IPNet, usage string) *net.IPNet { func IPNetP(name, shorthand string, value net.IPNet, usage string) *net.IPNet {
return CommandLine.IPNetP(name, shorthand, value, usage) return CommandLine.IPNetP(name, shorthand, value, usage)
} }

View File

@ -39,7 +39,7 @@ func (f *FlagSet) StringVar(p *string, name string, value string, usage string)
f.VarP(newStringValue(value, p), name, "", usage) f.VarP(newStringValue(value, p), name, "", usage)
} }
// Like StringVar, but accepts a shorthand letter that can be used after a single dash. // StringVarP is like StringVar, but accepts a shorthand letter that can be used after a single dash.
func (f *FlagSet) StringVarP(p *string, name, shorthand string, value string, usage string) { func (f *FlagSet) StringVarP(p *string, name, shorthand string, value string, usage string) {
f.VarP(newStringValue(value, p), name, shorthand, usage) f.VarP(newStringValue(value, p), name, shorthand, usage)
} }
@ -50,7 +50,7 @@ func StringVar(p *string, name string, value string, usage string) {
CommandLine.VarP(newStringValue(value, p), name, "", usage) CommandLine.VarP(newStringValue(value, p), name, "", usage)
} }
// Like StringVar, but accepts a shorthand letter that can be used after a single dash. // StringVarP is like StringVar, but accepts a shorthand letter that can be used after a single dash.
func StringVarP(p *string, name, shorthand string, value string, usage string) { func StringVarP(p *string, name, shorthand string, value string, usage string) {
CommandLine.VarP(newStringValue(value, p), name, shorthand, usage) CommandLine.VarP(newStringValue(value, p), name, shorthand, usage)
} }
@ -63,7 +63,7 @@ func (f *FlagSet) String(name string, value string, usage string) *string {
return p return p
} }
// Like String, but accepts a shorthand letter that can be used after a single dash. // StringP is like String, but accepts a shorthand letter that can be used after a single dash.
func (f *FlagSet) StringP(name, shorthand string, value string, usage string) *string { func (f *FlagSet) StringP(name, shorthand string, value string, usage string) *string {
p := new(string) p := new(string)
f.StringVarP(p, name, shorthand, value, usage) f.StringVarP(p, name, shorthand, value, usage)
@ -76,7 +76,7 @@ func String(name string, value string, usage string) *string {
return CommandLine.StringP(name, "", value, usage) return CommandLine.StringP(name, "", value, usage)
} }
// Like String, but accepts a shorthand letter that can be used after a single dash. // StringP is like String, but accepts a shorthand letter that can be used after a single dash.
func StringP(name, shorthand string, value string, usage string) *string { func StringP(name, shorthand string, value string, usage string) *string {
return CommandLine.StringP(name, shorthand, value, usage) return CommandLine.StringP(name, shorthand, value, usage)
} }

View File

@ -62,7 +62,7 @@ func (f *FlagSet) StringSliceVar(p *[]string, name string, value []string, usage
f.VarP(newStringSliceValue(value, p), name, "", usage) f.VarP(newStringSliceValue(value, p), name, "", usage)
} }
// Like StringSliceVar, but accepts a shorthand letter that can be used after a single dash. // StringSliceVarP is like StringSliceVar, but accepts a shorthand letter that can be used after a single dash.
func (f *FlagSet) StringSliceVarP(p *[]string, name, shorthand string, value []string, usage string) { func (f *FlagSet) StringSliceVarP(p *[]string, name, shorthand string, value []string, usage string) {
f.VarP(newStringSliceValue(value, p), name, shorthand, usage) f.VarP(newStringSliceValue(value, p), name, shorthand, usage)
} }
@ -73,7 +73,7 @@ func StringSliceVar(p *[]string, name string, value []string, usage string) {
CommandLine.VarP(newStringSliceValue(value, p), name, "", usage) CommandLine.VarP(newStringSliceValue(value, p), name, "", usage)
} }
// Like StringSliceVar, but accepts a shorthand letter that can be used after a single dash. // StringSliceVarP is like StringSliceVar, but accepts a shorthand letter that can be used after a single dash.
func StringSliceVarP(p *[]string, name, shorthand string, value []string, usage string) { func StringSliceVarP(p *[]string, name, shorthand string, value []string, usage string) {
CommandLine.VarP(newStringSliceValue(value, p), name, shorthand, usage) CommandLine.VarP(newStringSliceValue(value, p), name, shorthand, usage)
} }
@ -81,14 +81,14 @@ func StringSliceVarP(p *[]string, name, shorthand string, value []string, usage
// StringSlice defines a string flag with specified name, default value, and usage 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. // The return value is the address of a []string variable that stores the value of the flag.
func (f *FlagSet) StringSlice(name string, value []string, usage string) *[]string { func (f *FlagSet) StringSlice(name string, value []string, usage string) *[]string {
p := make([]string, 0) p := []string{}
f.StringSliceVarP(&p, name, "", value, usage) f.StringSliceVarP(&p, name, "", value, usage)
return &p return &p
} }
// Like StringSlice, but accepts a shorthand letter that can be used after a single dash. // StringSliceP is like StringSlice, but accepts a shorthand letter that can be used after a single dash.
func (f *FlagSet) StringSliceP(name, shorthand string, value []string, usage string) *[]string { func (f *FlagSet) StringSliceP(name, shorthand string, value []string, usage string) *[]string {
p := make([]string, 0) p := []string{}
f.StringSliceVarP(&p, name, shorthand, value, usage) f.StringSliceVarP(&p, name, shorthand, value, usage)
return &p return &p
} }
@ -99,7 +99,7 @@ func StringSlice(name string, value []string, usage string) *[]string {
return CommandLine.StringSliceP(name, "", value, usage) return CommandLine.StringSliceP(name, "", value, usage)
} }
// Like StringSlice, but accepts a shorthand letter that can be used after a single dash. // StringSliceP is like StringSlice, but accepts a shorthand letter that can be used after a single dash.
func StringSliceP(name, shorthand string, value []string, usage string) *[]string { func StringSliceP(name, shorthand string, value []string, usage string) *[]string {
return CommandLine.StringSliceP(name, shorthand, value, usage) return CommandLine.StringSliceP(name, shorthand, value, usage)
} }

View File

@ -12,13 +12,13 @@ import (
func setUpSSFlagSet(ssp *[]string) *FlagSet { func setUpSSFlagSet(ssp *[]string) *FlagSet {
f := NewFlagSet("test", ContinueOnError) f := NewFlagSet("test", ContinueOnError)
f.StringSliceVar(ssp, "ss", []string{}, "Command seperated list!") f.StringSliceVar(ssp, "ss", []string{}, "Command separated list!")
return f return f
} }
func setUpSSFlagSetWithDefault(ssp *[]string) *FlagSet { func setUpSSFlagSetWithDefault(ssp *[]string) *FlagSet {
f := NewFlagSet("test", ContinueOnError) f := NewFlagSet("test", ContinueOnError)
f.StringSliceVar(ssp, "ss", []string{"default", "values"}, "Command seperated list!") f.StringSliceVar(ssp, "ss", []string{"default", "values"}, "Command separated list!")
return f return f
} }

View File

@ -48,7 +48,7 @@ func (f *FlagSet) UintVar(p *uint, name string, value uint, usage string) {
f.VarP(newUintValue(value, p), name, "", usage) f.VarP(newUintValue(value, p), name, "", usage)
} }
// Like UintVar, but accepts a shorthand letter that can be used after a single dash. // UintVarP is like UintVar, but accepts a shorthand letter that can be used after a single dash.
func (f *FlagSet) UintVarP(p *uint, name, shorthand string, value uint, usage string) { func (f *FlagSet) UintVarP(p *uint, name, shorthand string, value uint, usage string) {
f.VarP(newUintValue(value, p), name, shorthand, usage) f.VarP(newUintValue(value, p), name, shorthand, usage)
} }
@ -59,7 +59,7 @@ func UintVar(p *uint, name string, value uint, usage string) {
CommandLine.VarP(newUintValue(value, p), name, "", usage) CommandLine.VarP(newUintValue(value, p), name, "", usage)
} }
// Like UintVar, but accepts a shorthand letter that can be used after a single dash. // UintVarP is like UintVar, but accepts a shorthand letter that can be used after a single dash.
func UintVarP(p *uint, name, shorthand string, value uint, usage string) { func UintVarP(p *uint, name, shorthand string, value uint, usage string) {
CommandLine.VarP(newUintValue(value, p), name, shorthand, usage) CommandLine.VarP(newUintValue(value, p), name, shorthand, usage)
} }
@ -72,7 +72,7 @@ func (f *FlagSet) Uint(name string, value uint, usage string) *uint {
return p return p
} }
// Like Uint, but accepts a shorthand letter that can be used after a single dash. // UintP is like Uint, but accepts a shorthand letter that can be used after a single dash.
func (f *FlagSet) UintP(name, shorthand string, value uint, usage string) *uint { func (f *FlagSet) UintP(name, shorthand string, value uint, usage string) *uint {
p := new(uint) p := new(uint)
f.UintVarP(p, name, shorthand, value, usage) f.UintVarP(p, name, shorthand, value, usage)
@ -85,7 +85,7 @@ func Uint(name string, value uint, usage string) *uint {
return CommandLine.UintP(name, "", value, usage) return CommandLine.UintP(name, "", value, usage)
} }
// Like Uint, but accepts a shorthand letter that can be used after a single dash. // UintP is like Uint, but accepts a shorthand letter that can be used after a single dash.
func UintP(name, shorthand string, value uint, usage string) *uint { func UintP(name, shorthand string, value uint, usage string) *uint {
return CommandLine.UintP(name, shorthand, value, usage) return CommandLine.UintP(name, shorthand, value, usage)
} }

View File

@ -46,7 +46,7 @@ func (f *FlagSet) Uint16Var(p *uint16, name string, value uint16, usage string)
f.VarP(newUint16Value(value, p), name, "", usage) f.VarP(newUint16Value(value, p), name, "", usage)
} }
// Like Uint16Var, but accepts a shorthand letter that can be used after a single dash. // Uint16VarP is like Uint16Var, but accepts a shorthand letter that can be used after a single dash.
func (f *FlagSet) Uint16VarP(p *uint16, name, shorthand string, value uint16, usage string) { func (f *FlagSet) Uint16VarP(p *uint16, name, shorthand string, value uint16, usage string) {
f.VarP(newUint16Value(value, p), name, shorthand, usage) f.VarP(newUint16Value(value, p), name, shorthand, usage)
} }
@ -57,7 +57,7 @@ func Uint16Var(p *uint16, name string, value uint16, usage string) {
CommandLine.VarP(newUint16Value(value, p), name, "", usage) CommandLine.VarP(newUint16Value(value, p), name, "", usage)
} }
// Like Uint16Var, but accepts a shorthand letter that can be used after a single dash. // Uint16VarP is like Uint16Var, but accepts a shorthand letter that can be used after a single dash.
func Uint16VarP(p *uint16, name, shorthand string, value uint16, usage string) { func Uint16VarP(p *uint16, name, shorthand string, value uint16, usage string) {
CommandLine.VarP(newUint16Value(value, p), name, shorthand, usage) CommandLine.VarP(newUint16Value(value, p), name, shorthand, usage)
} }
@ -70,7 +70,7 @@ func (f *FlagSet) Uint16(name string, value uint16, usage string) *uint16 {
return p return p
} }
// Like Uint16, but accepts a shorthand letter that can be used after a single dash. // Uint16P is like Uint16, but accepts a shorthand letter that can be used after a single dash.
func (f *FlagSet) Uint16P(name, shorthand string, value uint16, usage string) *uint16 { func (f *FlagSet) Uint16P(name, shorthand string, value uint16, usage string) *uint16 {
p := new(uint16) p := new(uint16)
f.Uint16VarP(p, name, shorthand, value, usage) f.Uint16VarP(p, name, shorthand, value, usage)
@ -83,7 +83,7 @@ func Uint16(name string, value uint16, usage string) *uint16 {
return CommandLine.Uint16P(name, "", value, usage) return CommandLine.Uint16P(name, "", value, usage)
} }
// Like Uint16, but accepts a shorthand letter that can be used after a single dash. // Uint16P is like Uint16, but accepts a shorthand letter that can be used after a single dash.
func Uint16P(name, shorthand string, value uint16, usage string) *uint16 { func Uint16P(name, shorthand string, value uint16, usage string) *uint16 {
return CommandLine.Uint16P(name, shorthand, value, usage) return CommandLine.Uint16P(name, shorthand, value, usage)
} }

View File

@ -46,7 +46,7 @@ func (f *FlagSet) Uint32Var(p *uint32, name string, value uint32, usage string)
f.VarP(newUint32Value(value, p), name, "", usage) f.VarP(newUint32Value(value, p), name, "", usage)
} }
// Like Uint32Var, but accepts a shorthand letter that can be used after a single dash. // Uint32VarP is like Uint32Var, but accepts a shorthand letter that can be used after a single dash.
func (f *FlagSet) Uint32VarP(p *uint32, name, shorthand string, value uint32, usage string) { func (f *FlagSet) Uint32VarP(p *uint32, name, shorthand string, value uint32, usage string) {
f.VarP(newUint32Value(value, p), name, shorthand, usage) f.VarP(newUint32Value(value, p), name, shorthand, usage)
} }
@ -57,7 +57,7 @@ func Uint32Var(p *uint32, name string, value uint32, usage string) {
CommandLine.VarP(newUint32Value(value, p), name, "", usage) CommandLine.VarP(newUint32Value(value, p), name, "", usage)
} }
// Like Uint32Var, but accepts a shorthand letter that can be used after a single dash. // Uint32VarP is like Uint32Var, but accepts a shorthand letter that can be used after a single dash.
func Uint32VarP(p *uint32, name, shorthand string, value uint32, usage string) { func Uint32VarP(p *uint32, name, shorthand string, value uint32, usage string) {
CommandLine.VarP(newUint32Value(value, p), name, shorthand, usage) CommandLine.VarP(newUint32Value(value, p), name, shorthand, usage)
} }
@ -70,7 +70,7 @@ func (f *FlagSet) Uint32(name string, value uint32, usage string) *uint32 {
return p return p
} }
// Like Uint32, but accepts a shorthand letter that can be used after a single dash. // Uint32P is like Uint32, but accepts a shorthand letter that can be used after a single dash.
func (f *FlagSet) Uint32P(name, shorthand string, value uint32, usage string) *uint32 { func (f *FlagSet) Uint32P(name, shorthand string, value uint32, usage string) *uint32 {
p := new(uint32) p := new(uint32)
f.Uint32VarP(p, name, shorthand, value, usage) f.Uint32VarP(p, name, shorthand, value, usage)
@ -83,7 +83,7 @@ func Uint32(name string, value uint32, usage string) *uint32 {
return CommandLine.Uint32P(name, "", value, usage) return CommandLine.Uint32P(name, "", value, usage)
} }
// Like Uint32, but accepts a shorthand letter that can be used after a single dash. // Uint32P is like Uint32, but accepts a shorthand letter that can be used after a single dash.
func Uint32P(name, shorthand string, value uint32, usage string) *uint32 { func Uint32P(name, shorthand string, value uint32, usage string) *uint32 {
return CommandLine.Uint32P(name, shorthand, value, usage) return CommandLine.Uint32P(name, shorthand, value, usage)
} }

View File

@ -48,7 +48,7 @@ func (f *FlagSet) Uint64Var(p *uint64, name string, value uint64, usage string)
f.VarP(newUint64Value(value, p), name, "", usage) f.VarP(newUint64Value(value, p), name, "", usage)
} }
// Like Uint64Var, but accepts a shorthand letter that can be used after a single dash. // Uint64VarP is like Uint64Var, but accepts a shorthand letter that can be used after a single dash.
func (f *FlagSet) Uint64VarP(p *uint64, name, shorthand string, value uint64, usage string) { func (f *FlagSet) Uint64VarP(p *uint64, name, shorthand string, value uint64, usage string) {
f.VarP(newUint64Value(value, p), name, shorthand, usage) f.VarP(newUint64Value(value, p), name, shorthand, usage)
} }
@ -59,7 +59,7 @@ func Uint64Var(p *uint64, name string, value uint64, usage string) {
CommandLine.VarP(newUint64Value(value, p), name, "", usage) CommandLine.VarP(newUint64Value(value, p), name, "", usage)
} }
// Like Uint64Var, but accepts a shorthand letter that can be used after a single dash. // Uint64VarP is like Uint64Var, but accepts a shorthand letter that can be used after a single dash.
func Uint64VarP(p *uint64, name, shorthand string, value uint64, usage string) { func Uint64VarP(p *uint64, name, shorthand string, value uint64, usage string) {
CommandLine.VarP(newUint64Value(value, p), name, shorthand, usage) CommandLine.VarP(newUint64Value(value, p), name, shorthand, usage)
} }
@ -72,7 +72,7 @@ func (f *FlagSet) Uint64(name string, value uint64, usage string) *uint64 {
return p return p
} }
// Like Uint64, but accepts a shorthand letter that can be used after a single dash. // Uint64P is like Uint64, but accepts a shorthand letter that can be used after a single dash.
func (f *FlagSet) Uint64P(name, shorthand string, value uint64, usage string) *uint64 { func (f *FlagSet) Uint64P(name, shorthand string, value uint64, usage string) *uint64 {
p := new(uint64) p := new(uint64)
f.Uint64VarP(p, name, shorthand, value, usage) f.Uint64VarP(p, name, shorthand, value, usage)
@ -85,7 +85,7 @@ func Uint64(name string, value uint64, usage string) *uint64 {
return CommandLine.Uint64P(name, "", value, usage) return CommandLine.Uint64P(name, "", value, usage)
} }
// Like Uint64, but accepts a shorthand letter that can be used after a single dash. // Uint64P is like Uint64, but accepts a shorthand letter that can be used after a single dash.
func Uint64P(name, shorthand string, value uint64, usage string) *uint64 { func Uint64P(name, shorthand string, value uint64, usage string) *uint64 {
return CommandLine.Uint64P(name, shorthand, value, usage) return CommandLine.Uint64P(name, shorthand, value, usage)
} }

View File

@ -48,7 +48,7 @@ func (f *FlagSet) Uint8Var(p *uint8, name string, value uint8, usage string) {
f.VarP(newUint8Value(value, p), name, "", usage) f.VarP(newUint8Value(value, p), name, "", usage)
} }
// Like Uint8Var, but accepts a shorthand letter that can be used after a single dash. // Uint8VarP is like Uint8Var, but accepts a shorthand letter that can be used after a single dash.
func (f *FlagSet) Uint8VarP(p *uint8, name, shorthand string, value uint8, usage string) { func (f *FlagSet) Uint8VarP(p *uint8, name, shorthand string, value uint8, usage string) {
f.VarP(newUint8Value(value, p), name, shorthand, usage) f.VarP(newUint8Value(value, p), name, shorthand, usage)
} }
@ -59,7 +59,7 @@ func Uint8Var(p *uint8, name string, value uint8, usage string) {
CommandLine.VarP(newUint8Value(value, p), name, "", usage) CommandLine.VarP(newUint8Value(value, p), name, "", usage)
} }
// Like Uint8Var, but accepts a shorthand letter that can be used after a single dash. // Uint8VarP is like Uint8Var, but accepts a shorthand letter that can be used after a single dash.
func Uint8VarP(p *uint8, name, shorthand string, value uint8, usage string) { func Uint8VarP(p *uint8, name, shorthand string, value uint8, usage string) {
CommandLine.VarP(newUint8Value(value, p), name, shorthand, usage) CommandLine.VarP(newUint8Value(value, p), name, shorthand, usage)
} }
@ -72,7 +72,7 @@ func (f *FlagSet) Uint8(name string, value uint8, usage string) *uint8 {
return p return p
} }
// Like Uint8, but accepts a shorthand letter that can be used after a single dash. // Uint8P is like Uint8, but accepts a shorthand letter that can be used after a single dash.
func (f *FlagSet) Uint8P(name, shorthand string, value uint8, usage string) *uint8 { func (f *FlagSet) Uint8P(name, shorthand string, value uint8, usage string) *uint8 {
p := new(uint8) p := new(uint8)
f.Uint8VarP(p, name, shorthand, value, usage) f.Uint8VarP(p, name, shorthand, value, usage)
@ -85,7 +85,7 @@ func Uint8(name string, value uint8, usage string) *uint8 {
return CommandLine.Uint8P(name, "", value, usage) return CommandLine.Uint8P(name, "", value, usage)
} }
// Like Uint8, but accepts a shorthand letter that can be used after a single dash. // Uint8P is like Uint8, but accepts a shorthand letter that can be used after a single dash.
func Uint8P(name, shorthand string, value uint8, usage string) *uint8 { func Uint8P(name, shorthand string, value uint8, usage string) *uint8 {
return CommandLine.Uint8P(name, shorthand, value, usage) return CommandLine.Uint8P(name, shorthand, value, usage)
} }