diff --git a/README.md b/README.md index 7eacc5bd..618f5828 100644 --- a/README.md +++ b/README.md @@ -122,6 +122,25 @@ 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. +## Adding custom validation for flags + +You can also add custom validation for validating the flag's value. + +It is optional and for this operation, you should define a function and pass it as the last parameter to the flag definition function. + +```go +var flagvar int +func init() { + validation := func(value int) error { + if value <= 4 { + return errors.New("int value should be greater than 4") + } + } + flag.IntVar(&flagvar, "flagname", 1234, "help message for flagname", validation) +} +```` + +In the above example if you pass an integer value greater than 4, it returned an error and the value of the flag doesn't set. ## Setting no option default values for flags diff --git a/bool.go b/bool.go index c4c5c0bf..22518d60 100644 --- a/bool.go +++ b/bool.go @@ -1,6 +1,8 @@ package pflag -import "strconv" +import ( + "strconv" +) // optional interface to indicate boolean flags that can be // supplied without "=value" text @@ -46,49 +48,61 @@ func (f *FlagSet) GetBool(name string) (bool, error) { // 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 (f *FlagSet) BoolVar(p *bool, name string, value bool, usage string) { - f.BoolVarP(p, name, "", value, usage) +func (f *FlagSet) BoolVar(p *bool, name string, value bool, usage string, validation ...func(value bool) error) { + f.BoolVarP(p, name, "", value, usage, validation...) } // 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, validation ...func(value bool) error) { + if len(validation) > 0 { + validationFunc := (interface{})(validation[0]) + flag := f.VarPF(newBoolValue(value, p), name, shorthand, usage, validationFunc) + flag.NoOptDefVal = "true" + return + } flag := f.VarPF(newBoolValue(value, p), name, shorthand, usage) flag.NoOptDefVal = "true" } // 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 BoolVar(p *bool, name string, value bool, usage string) { - BoolVarP(p, name, "", value, usage) +func BoolVar(p *bool, name string, value bool, usage string, validation ...func(value bool) error) { + BoolVarP(p, name, "", value, usage, validation...) } // 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, validation ...func(value bool) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]).(func(value interface{}) error) + flag := CommandLine.VarPF(newBoolValue(value, p), name, shorthand, usage, validationFunc) + flag.NoOptDefVal = "true" + return + } flag := CommandLine.VarPF(newBoolValue(value, p), name, shorthand, usage) flag.NoOptDefVal = "true" } // 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 (f *FlagSet) Bool(name string, value bool, usage string) *bool { - return f.BoolP(name, "", value, usage) +func (f *FlagSet) Bool(name string, value bool, usage string, validation ...func(value bool) error) *bool { + return f.BoolP(name, "", value, usage, validation...) } // 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, validation ...func(value bool) error) *bool { p := new(bool) - f.BoolVarP(p, name, shorthand, value, usage) + f.BoolVarP(p, name, shorthand, value, usage, validation...) return p } // 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 Bool(name string, value bool, usage string) *bool { - return BoolP(name, "", value, usage) +func Bool(name string, value bool, usage string, validation ...func(value bool) error) *bool { + return BoolP(name, "", value, usage, validation...) } // 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 { - b := CommandLine.BoolP(name, shorthand, value, usage) +func BoolP(name, shorthand string, value bool, usage string, validation ...func(value bool) error) *bool { + b := CommandLine.BoolP(name, shorthand, value, usage, validation...) return b } diff --git a/bool_slice.go b/bool_slice.go index 3731370d..661d8108 100644 --- a/bool_slice.go +++ b/bool_slice.go @@ -138,48 +138,68 @@ func (f *FlagSet) GetBoolSlice(name string) ([]bool, error) { // BoolSliceVar defines a boolSlice 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 (f *FlagSet) BoolSliceVar(p *[]bool, name string, value []bool, usage string) { +func (f *FlagSet) BoolSliceVar(p *[]bool, name string, value []bool, usage string, validation ...func(value []bool) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + f.VarP(newBoolSliceValue(value, p), name, "", usage, validationFunc) + return + } f.VarP(newBoolSliceValue(value, p), name, "", usage) } // BoolSliceVarP is like BoolSliceVar, but accepts a shorthand letter that can be used after a single dash. -func (f *FlagSet) BoolSliceVarP(p *[]bool, name, shorthand string, value []bool, usage string) { +func (f *FlagSet) BoolSliceVarP(p *[]bool, name, shorthand string, value []bool, usage string, validation ...func(value []bool) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + f.VarP(newBoolSliceValue(value, p), name, shorthand, usage, validationFunc) + return + } f.VarP(newBoolSliceValue(value, p), name, shorthand, usage) } // BoolSliceVar 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 BoolSliceVar(p *[]bool, name string, value []bool, usage string) { +func BoolSliceVar(p *[]bool, name string, value []bool, usage string, validation ...func(value []bool) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + CommandLine.VarP(newBoolSliceValue(value, p), name, "", usage, validationFunc) + return + } CommandLine.VarP(newBoolSliceValue(value, p), name, "", usage) } // BoolSliceVarP is like BoolSliceVar, but accepts a shorthand letter that can be used after a single dash. -func BoolSliceVarP(p *[]bool, name, shorthand string, value []bool, usage string) { +func BoolSliceVarP(p *[]bool, name, shorthand string, value []bool, usage string, validation ...func(value []bool) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + CommandLine.VarP(newBoolSliceValue(value, p), name, shorthand, usage, validationFunc) + return + } CommandLine.VarP(newBoolSliceValue(value, p), name, shorthand, usage) } // BoolSlice 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 (f *FlagSet) BoolSlice(name string, value []bool, usage string) *[]bool { +func (f *FlagSet) BoolSlice(name string, value []bool, usage string, validation ...func(value []bool) error) *[]bool { p := []bool{} - f.BoolSliceVarP(&p, name, "", value, usage) + f.BoolSliceVarP(&p, name, "", value, usage, validation...) return &p } // BoolSliceP is like BoolSlice, but accepts a shorthand letter that can be used after a single dash. -func (f *FlagSet) BoolSliceP(name, shorthand string, value []bool, usage string) *[]bool { +func (f *FlagSet) BoolSliceP(name, shorthand string, value []bool, usage string, validation ...func(value []bool) error) *[]bool { p := []bool{} - f.BoolSliceVarP(&p, name, shorthand, value, usage) + f.BoolSliceVarP(&p, name, shorthand, value, usage, validation...) return &p } // BoolSlice 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 BoolSlice(name string, value []bool, usage string) *[]bool { - return CommandLine.BoolSliceP(name, "", value, usage) +func BoolSlice(name string, value []bool, usage string, validation ...func(value []bool) error) *[]bool { + return CommandLine.BoolSliceP(name, "", value, usage, validation...) } // BoolSliceP is like BoolSlice, but accepts a shorthand letter that can be used after a single dash. -func BoolSliceP(name, shorthand string, value []bool, usage string) *[]bool { - return CommandLine.BoolSliceP(name, shorthand, value, usage) +func BoolSliceP(name, shorthand string, value []bool, usage string, validation ...func(value []bool) error) *[]bool { + return CommandLine.BoolSliceP(name, shorthand, value, usage, validation...) } diff --git a/bytes.go b/bytes.go index 67d53045..5c31ce42 100644 --- a/bytes.go +++ b/bytes.go @@ -62,50 +62,70 @@ func (f *FlagSet) GetBytesHex(name string) ([]byte, error) { // BytesHexVar defines an []byte flag with specified name, default value, and usage string. // The argument p points to an []byte variable in which to store the value of the flag. -func (f *FlagSet) BytesHexVar(p *[]byte, name string, value []byte, usage string) { +func (f *FlagSet) BytesHexVar(p *[]byte, name string, value []byte, usage string, validation ...func(value []byte) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + f.VarP(newBytesHexValue(value, p), name, "", usage, validationFunc) + return + } f.VarP(newBytesHexValue(value, p), name, "", usage) } // BytesHexVarP is like BytesHexVar, but accepts a shorthand letter that can be used after a single dash. -func (f *FlagSet) BytesHexVarP(p *[]byte, name, shorthand string, value []byte, usage string) { +func (f *FlagSet) BytesHexVarP(p *[]byte, name, shorthand string, value []byte, usage string, validation ...func(value []byte) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + f.VarP(newBytesHexValue(value, p), name, shorthand, usage, validationFunc) + return + } f.VarP(newBytesHexValue(value, p), name, shorthand, usage) } // BytesHexVar defines an []byte flag with specified name, default value, and usage string. // The argument p points to an []byte variable in which to store the value of the flag. -func BytesHexVar(p *[]byte, name string, value []byte, usage string) { +func BytesHexVar(p *[]byte, name string, value []byte, usage string, validation ...func(value []byte) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + CommandLine.VarP(newBytesHexValue(value, p), name, "", usage, validationFunc) + return + } CommandLine.VarP(newBytesHexValue(value, p), name, "", usage) } // BytesHexVarP is like BytesHexVar, but accepts a shorthand letter that can be used after a single dash. -func BytesHexVarP(p *[]byte, name, shorthand string, value []byte, usage string) { +func BytesHexVarP(p *[]byte, name, shorthand string, value []byte, usage string, validation ...func(value []byte) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + CommandLine.VarP(newBytesHexValue(value, p), name, shorthand, usage, validationFunc) + return + } CommandLine.VarP(newBytesHexValue(value, p), name, shorthand, usage) } // BytesHex defines an []byte flag with specified name, default value, and usage string. // The return value is the address of an []byte variable that stores the value of the flag. -func (f *FlagSet) BytesHex(name string, value []byte, usage string) *[]byte { +func (f *FlagSet) BytesHex(name string, value []byte, usage string, validation ...func(value []byte) error) *[]byte { p := new([]byte) - f.BytesHexVarP(p, name, "", value, usage) + f.BytesHexVarP(p, name, "", value, usage, validation...) return p } // BytesHexP is like BytesHex, but accepts a shorthand letter that can be used after a single dash. -func (f *FlagSet) BytesHexP(name, shorthand string, value []byte, usage string) *[]byte { +func (f *FlagSet) BytesHexP(name, shorthand string, value []byte, usage string, validation ...func(value []byte) error) *[]byte { p := new([]byte) - f.BytesHexVarP(p, name, shorthand, value, usage) + f.BytesHexVarP(p, name, shorthand, value, usage, validation...) return p } // BytesHex defines an []byte flag with specified name, default value, and usage string. // The return value is the address of an []byte variable that stores the value of the flag. -func BytesHex(name string, value []byte, usage string) *[]byte { - return CommandLine.BytesHexP(name, "", value, usage) +func BytesHex(name string, value []byte, usage string, validation ...func(value []byte) error) *[]byte { + return CommandLine.BytesHexP(name, "", value, usage, validation...) } // BytesHexP is like BytesHex, but accepts a shorthand letter that can be used after a single dash. -func BytesHexP(name, shorthand string, value []byte, usage string) *[]byte { - return CommandLine.BytesHexP(name, shorthand, value, usage) +func BytesHexP(name, shorthand string, value []byte, usage string, validation ...func(value []byte) error) *[]byte { + return CommandLine.BytesHexP(name, shorthand, value, usage, validation...) } // BytesBase64 adapts []byte for use as a flag. Value of flag is Base64 encoded @@ -162,48 +182,64 @@ func (f *FlagSet) GetBytesBase64(name string) ([]byte, error) { // BytesBase64Var defines an []byte flag with specified name, default value, and usage string. // The argument p points to an []byte variable in which to store the value of the flag. -func (f *FlagSet) BytesBase64Var(p *[]byte, name string, value []byte, usage string) { +func (f *FlagSet) BytesBase64Var(p *[]byte, name string, value []byte, usage string, validation ...func(value []byte) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]).(func(value interface{}) error) + f.VarP(newBytesBase64Value(value, p), name, "", usage, validationFunc) + } f.VarP(newBytesBase64Value(value, p), name, "", usage) } // BytesBase64VarP is like BytesBase64Var, but accepts a shorthand letter that can be used after a single dash. -func (f *FlagSet) BytesBase64VarP(p *[]byte, name, shorthand string, value []byte, usage string) { +func (f *FlagSet) BytesBase64VarP(p *[]byte, name, shorthand string, value []byte, usage string, validation ...func(value []byte) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]).(func(value interface{}) error) + f.VarP(newBytesBase64Value(value, p), name, shorthand, usage, validationFunc) + } f.VarP(newBytesBase64Value(value, p), name, shorthand, usage) } // BytesBase64Var defines an []byte flag with specified name, default value, and usage string. // The argument p points to an []byte variable in which to store the value of the flag. -func BytesBase64Var(p *[]byte, name string, value []byte, usage string) { +func BytesBase64Var(p *[]byte, name string, value []byte, usage string, validation ...func(value []byte) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]).(func(value interface{}) error) + CommandLine.VarP(newBytesBase64Value(value, p), name, "", usage, validationFunc) + } CommandLine.VarP(newBytesBase64Value(value, p), name, "", usage) } // BytesBase64VarP is like BytesBase64Var, but accepts a shorthand letter that can be used after a single dash. -func BytesBase64VarP(p *[]byte, name, shorthand string, value []byte, usage string) { +func BytesBase64VarP(p *[]byte, name, shorthand string, value []byte, usage string, validation ...func(value []byte) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]).(func(value interface{}) error) + CommandLine.VarP(newBytesBase64Value(value, p), name, shorthand, usage, validationFunc) + } CommandLine.VarP(newBytesBase64Value(value, p), name, shorthand, usage) } // BytesBase64 defines an []byte flag with specified name, default value, and usage string. // The return value is the address of an []byte variable that stores the value of the flag. -func (f *FlagSet) BytesBase64(name string, value []byte, usage string) *[]byte { +func (f *FlagSet) BytesBase64(name string, value []byte, usage string, validation ...func(value []byte) error) *[]byte { p := new([]byte) - f.BytesBase64VarP(p, name, "", value, usage) + f.BytesBase64VarP(p, name, "", value, usage, validation...) return p } // BytesBase64P is like BytesBase64, but accepts a shorthand letter that can be used after a single dash. -func (f *FlagSet) BytesBase64P(name, shorthand string, value []byte, usage string) *[]byte { +func (f *FlagSet) BytesBase64P(name, shorthand string, value []byte, usage string, validation ...func(value []byte) error) *[]byte { p := new([]byte) - f.BytesBase64VarP(p, name, shorthand, value, usage) + f.BytesBase64VarP(p, name, shorthand, value, usage, validation...) return p } // BytesBase64 defines an []byte flag with specified name, default value, and usage string. // The return value is the address of an []byte variable that stores the value of the flag. -func BytesBase64(name string, value []byte, usage string) *[]byte { - return CommandLine.BytesBase64P(name, "", value, usage) +func BytesBase64(name string, value []byte, usage string, validation ...func(value []byte) error) *[]byte { + return CommandLine.BytesBase64P(name, "", value, usage, validation...) } // BytesBase64P is like BytesBase64, but accepts a shorthand letter that can be used after a single dash. -func BytesBase64P(name, shorthand string, value []byte, usage string) *[]byte { - return CommandLine.BytesBase64P(name, shorthand, value, usage) +func BytesBase64P(name, shorthand string, value []byte, usage string, validation ...func(value []byte) error) *[]byte { + return CommandLine.BytesBase64P(name, shorthand, value, usage, validation...) } diff --git a/count.go b/count.go index a0b2679f..948f44bc 100644 --- a/count.go +++ b/count.go @@ -47,50 +47,56 @@ func (f *FlagSet) GetCount(name string) (int, error) { // 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 every time it is found on the command line -func (f *FlagSet) CountVar(p *int, name string, usage string) { - f.CountVarP(p, name, "", usage) +func (f *FlagSet) CountVar(p *int, name string, usage string, validation ...func(value int) error) { + f.CountVarP(p, name, "", usage, validation...) } // 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, validation ...func(value int) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + flag := f.VarPF(newCountValue(0, p), name, shorthand, usage, validationFunc) + flag.NoOptDefVal = "+1" + return + } flag := f.VarPF(newCountValue(0, p), name, shorthand, usage) 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) { - CommandLine.CountVar(p, name, usage) +func CountVar(p *int, name string, usage string, validation ...func(value int) error) { + CommandLine.CountVar(p, name, usage, validation...) } // CountVarP is like CountVar only take a shorthand for the flag name. -func CountVarP(p *int, name, shorthand string, usage string) { - CommandLine.CountVarP(p, name, shorthand, usage) +func CountVarP(p *int, name, shorthand string, usage string, validation ...func(value int) error) { + CommandLine.CountVarP(p, name, shorthand, usage, validation...) } // 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 every 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, validation ...func(value int) error) *int { p := new(int) - f.CountVarP(p, name, "", usage) + f.CountVarP(p, name, "", usage, validation...) 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, validation ...func(value int) error) *int { p := new(int) - f.CountVarP(p, name, shorthand, usage) + f.CountVarP(p, name, shorthand, usage, validation...) return p } // 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 Count(name string, usage string) *int { - return CommandLine.CountP(name, "", usage) +func Count(name string, usage string, validation ...func(value int) error) *int { + return CommandLine.CountP(name, "", usage, validation...) } // CountP is like Count only takes a shorthand for the flag name. -func CountP(name, shorthand string, usage string) *int { - return CommandLine.CountP(name, shorthand, usage) +func CountP(name, shorthand string, usage string, validation ...func(value int) error) *int { + return CommandLine.CountP(name, shorthand, usage, validation...) } diff --git a/duration.go b/duration.go index e9debef8..628788ef 100644 --- a/duration.go +++ b/duration.go @@ -39,48 +39,68 @@ func (f *FlagSet) GetDuration(name string) (time.Duration, error) { // 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 (f *FlagSet) DurationVar(p *time.Duration, name string, value time.Duration, usage string) { +func (f *FlagSet) DurationVar(p *time.Duration, name string, value time.Duration, usage string, validation ...func(value time.Duration) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + f.VarP(newDurationValue(value, p), name, "", usage, validationFunc) + return + } f.VarP(newDurationValue(value, p), name, "", usage) } // 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, validation ...func(value time.Duration) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + f.VarP(newDurationValue(value, p), name, shorthand, usage, validationFunc) + return + } f.VarP(newDurationValue(value, p), name, shorthand, usage) } // 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 DurationVar(p *time.Duration, name string, value time.Duration, usage string) { +func DurationVar(p *time.Duration, name string, value time.Duration, usage string, validation ...func(value time.Duration) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + CommandLine.VarP(newDurationValue(value, p), name, "", usage, validationFunc) + return + } CommandLine.VarP(newDurationValue(value, p), name, "", usage) } // 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, validation ...func(value time.Duration) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + CommandLine.VarP(newDurationValue(value, p), name, shorthand, usage, validationFunc) + return + } CommandLine.VarP(newDurationValue(value, p), name, shorthand, usage) } // 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 (f *FlagSet) Duration(name string, value time.Duration, usage string) *time.Duration { +func (f *FlagSet) Duration(name string, value time.Duration, usage string, validation ...func(value time.Duration) error) *time.Duration { p := new(time.Duration) - f.DurationVarP(p, name, "", value, usage) + f.DurationVarP(p, name, "", value, usage, validation...) return p } // 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, validation ...func(value time.Duration) error) *time.Duration { p := new(time.Duration) - f.DurationVarP(p, name, shorthand, value, usage) + f.DurationVarP(p, name, shorthand, value, usage, validation...) return p } // 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 Duration(name string, value time.Duration, usage string) *time.Duration { - return CommandLine.DurationP(name, "", value, usage) +func Duration(name string, value time.Duration, usage string, validation ...func(value time.Duration) error) *time.Duration { + return CommandLine.DurationP(name, "", value, usage, validation...) } // 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 { - return CommandLine.DurationP(name, shorthand, value, usage) +func DurationP(name, shorthand string, value time.Duration, usage string, validation ...func(value time.Duration) error) *time.Duration { + return CommandLine.DurationP(name, shorthand, value, usage, validation...) } diff --git a/duration_slice.go b/duration_slice.go index badadda5..9b815706 100644 --- a/duration_slice.go +++ b/duration_slice.go @@ -119,48 +119,68 @@ func (f *FlagSet) GetDurationSlice(name string) ([]time.Duration, error) { // DurationSliceVar defines a durationSlice 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 (f *FlagSet) DurationSliceVar(p *[]time.Duration, name string, value []time.Duration, usage string) { +func (f *FlagSet) DurationSliceVar(p *[]time.Duration, name string, value []time.Duration, usage string, validation ...func(value []time.Duration) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + f.VarP(newDurationSliceValue(value, p), name, "", usage, validationFunc) + return + } f.VarP(newDurationSliceValue(value, p), name, "", usage) } // DurationSliceVarP is like DurationSliceVar, but accepts a shorthand letter that can be used after a single dash. -func (f *FlagSet) DurationSliceVarP(p *[]time.Duration, name, shorthand string, value []time.Duration, usage string) { +func (f *FlagSet) DurationSliceVarP(p *[]time.Duration, name, shorthand string, value []time.Duration, usage string, validation ...func(value []time.Duration) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + f.VarP(newDurationSliceValue(value, p), name, shorthand, usage, validationFunc) + return + } f.VarP(newDurationSliceValue(value, p), name, shorthand, usage) } // DurationSliceVar defines a duration[] flag with specified name, default value, and usage string. // The argument p points to a duration[] variable in which to store the value of the flag. -func DurationSliceVar(p *[]time.Duration, name string, value []time.Duration, usage string) { +func DurationSliceVar(p *[]time.Duration, name string, value []time.Duration, usage string, validation ...func(value []time.Duration) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + CommandLine.VarP(newDurationSliceValue(value, p), name, "", usage, validationFunc) + return + } CommandLine.VarP(newDurationSliceValue(value, p), name, "", usage) } // DurationSliceVarP is like DurationSliceVar, but accepts a shorthand letter that can be used after a single dash. -func DurationSliceVarP(p *[]time.Duration, name, shorthand string, value []time.Duration, usage string) { +func DurationSliceVarP(p *[]time.Duration, name, shorthand string, value []time.Duration, usage string, validation ...func(value []time.Duration) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + CommandLine.VarP(newDurationSliceValue(value, p), name, shorthand, usage, validationFunc) + return + } CommandLine.VarP(newDurationSliceValue(value, p), name, shorthand, usage) } // DurationSlice 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 (f *FlagSet) DurationSlice(name string, value []time.Duration, usage string) *[]time.Duration { +func (f *FlagSet) DurationSlice(name string, value []time.Duration, usage string, validation ...func(value []time.Duration) error) *[]time.Duration { p := []time.Duration{} - f.DurationSliceVarP(&p, name, "", value, usage) + f.DurationSliceVarP(&p, name, "", value, usage, validation...) return &p } // DurationSliceP is like DurationSlice, but accepts a shorthand letter that can be used after a single dash. -func (f *FlagSet) DurationSliceP(name, shorthand string, value []time.Duration, usage string) *[]time.Duration { +func (f *FlagSet) DurationSliceP(name, shorthand string, value []time.Duration, usage string, validation ...func(value []time.Duration) error) *[]time.Duration { p := []time.Duration{} - f.DurationSliceVarP(&p, name, shorthand, value, usage) + f.DurationSliceVarP(&p, name, shorthand, value, usage, validation...) return &p } // DurationSlice 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 DurationSlice(name string, value []time.Duration, usage string) *[]time.Duration { - return CommandLine.DurationSliceP(name, "", value, usage) +func DurationSlice(name string, value []time.Duration, usage string, validation ...func(value []time.Duration) error) *[]time.Duration { + return CommandLine.DurationSliceP(name, "", value, usage, validation...) } // DurationSliceP is like DurationSlice, but accepts a shorthand letter that can be used after a single dash. -func DurationSliceP(name, shorthand string, value []time.Duration, usage string) *[]time.Duration { - return CommandLine.DurationSliceP(name, shorthand, value, usage) +func DurationSliceP(name, shorthand string, value []time.Duration, usage string, validation ...func(value []time.Duration) error) *[]time.Duration { + return CommandLine.DurationSliceP(name, shorthand, value, usage, validation...) } diff --git a/flag.go b/flag.go index 7c058de3..627b06d3 100644 --- a/flag.go +++ b/flag.go @@ -27,23 +27,32 @@ 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) @@ -54,22 +63,26 @@ 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(&flagvar, "boolname", "b", true, "help message") } flag.VarP(&flagval, "varname", "v", "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 @@ -104,9 +117,11 @@ import ( goflag "flag" "fmt" "io" + "net" "os" "sort" "strings" + "time" ) // ErrHelp is the error returned if the flag -help is invoked but no such flag is defined. @@ -180,6 +195,7 @@ type Flag struct { 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 + Validation interface{} // If you want to add custom validation for the value of the flag } // Value is the interface to the dynamic value stored in a flag. @@ -467,14 +483,22 @@ func (f *FlagSet) Set(name, value string) error { return fmt.Errorf("no such flag -%v", name) } + var flagName string + if flag.Shorthand != "" && flag.ShorthandDeprecated == "" { + flagName = fmt.Sprintf("-%s, --%s", flag.Shorthand, flag.Name) + } else { + flagName = fmt.Sprintf("--%s", flag.Name) + } + + if flag.Validation != nil { + err := validate(flag.Validation, value) + if err != nil { + return fmt.Errorf("invalid argument %q for %q flag: %v", value, flagName, err) + } + } + err := flag.Value.Set(value) if err != nil { - var flagName string - if flag.Shorthand != "" && flag.ShorthandDeprecated == "" { - flagName = fmt.Sprintf("-%s, --%s", flag.Shorthand, flag.Name) - } else { - flagName = fmt.Sprintf("--%s", flag.Name) - } return fmt.Errorf("invalid argument %q for %q flag: %v", value, flagName, err) } @@ -821,27 +845,34 @@ func Args() []string { return CommandLine.args } // 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 (f *FlagSet) Var(value Value, name string, usage string) { - f.VarP(value, name, "", usage) +func (f *FlagSet) Var(value Value, name string, usage string, validation ...interface{}) { + f.VarP(value, name, "", usage, validation...) } // 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, validation ...interface{}) *Flag { + var validationFunc interface{} + + if len(validation) > 0 { + validationFunc = validation[0] + } + // Remember the default value as a string; it won't change. flag := &Flag{ - Name: name, - Shorthand: shorthand, - Usage: usage, - Value: value, - DefValue: value.String(), + Name: name, + Shorthand: shorthand, + Usage: usage, + Value: value, + DefValue: value.String(), + Validation: validationFunc, } f.AddFlag(flag) return flag } // 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) { - f.VarPF(value, name, shorthand, usage) +func (f *FlagSet) VarP(value Value, name, shorthand, usage string, validation ...interface{}) { + f.VarPF(value, name, shorthand, usage, validation...) } // AddFlag will add the flag to the FlagSet @@ -902,13 +933,13 @@ func (f *FlagSet) AddFlagSet(newSet *FlagSet) { // 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 Var(value Value, name string, usage string) { - CommandLine.VarP(value, name, "", usage) +func Var(value Value, name string, usage string, validation ...func(value interface{}) error) { + CommandLine.VarP(value, name, "", usage, validation[0]) } // 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) { - CommandLine.VarP(value, name, shorthand, usage) +func VarP(value Value, name, shorthand, usage string, validation ...func(value interface{}) error) { + CommandLine.VarP(value, name, shorthand, usage, validation[0]) } // failf prints to standard error a formatted error and usage message and @@ -934,9 +965,9 @@ func (f *FlagSet) usage() { } } -//--unknown (args will be empty) -//--unknown --next-flag ... (args will be --next-flag ...) -//--unknown arg ... (args will be arg ...) +// --unknown (args will be empty) +// --unknown --next-flag ... (args will be --next-flag ...) +// --unknown arg ... (args will be arg ...) func stripUnknownFlagValue(args []string) []string { if len(args) == 0 { //--unknown @@ -1244,3 +1275,112 @@ func (f *FlagSet) Init(name string, errorHandling ErrorHandling) { f.errorHandling = errorHandling f.argsLenAtDash = -1 } + +func validate(validation interface{}, value string) error { + if validation == nil { + return nil + } + + switch validation.(type) { + case func(value bool) error: + v, _ := boolConv(value) + return validation.(func(value bool) error)(v.(bool)) + case func(value []bool) error: + v, _ := boolSliceConv(value) + return validation.(func(value []bool) error)(v.([]bool)) + case func(value []byte) error: + v, _ := bytesHexConv(value) + return validation.(func(value []byte) error)(v.([]byte)) + case func(value time.Duration) error: + v, _ := durationConv(value) + return validation.(func(value time.Duration) error)(v.(time.Duration)) + case func(value []time.Duration) error: + v, _ := durationSliceConv(value) + return validation.(func(value []time.Duration) error)(v.([]time.Duration)) + case func(value float32) error: + v, _ := float32Conv(value) + return validation.(func(value float32) error)(v.(float32)) + case func(value []float32) error: + v, _ := float32SliceConv(value) + return validation.(func(value []float32) error)(v.([]float32)) + case func(value float64) error: + v, _ := float64Conv(value) + return validation.(func(value float64) error)(v.(float64)) + case func(value []float64) error: + v, _ := float64SliceConv(value) + return validation.(func(value []float64) error)(v.([]float64)) + case func(value int) error: + v, _ := intConv(value) + return validation.(func(value int) error)(v.(int)) + case func(value []int) error: + v, _ := intSliceConv(value) + return validation.(func(value []int) error)(v.([]int)) + case func(value int8) error: + v, _ := int8Conv(value) + return validation.(func(value int8) error)(v.(int8)) + case func(value int16) error: + v, _ := int16Conv(value) + return validation.(func(value int16) error)(v.(int16)) + case func(value int32) error: + v, _ := int32Conv(value) + return validation.(func(value int32) error)(v.(int32)) + case func(value []int32) error: + v, _ := int32SliceConv(value) + return validation.(func(value []int32) error)(v.([]int32)) + case func(value int64) error: + v, _ := int64Conv(value) + return validation.(func(value int64) error)(v.(int64)) + case func(value []int64) error: + v, _ := int64SliceConv(value) + return validation.(func(value []int64) error)(v.([]int64)) + case func(value net.IP) error: + v, _ := ipConv(value) + return validation.(func(value net.IP) error)(v.(net.IP)) + case func(value []net.IP) error: + v, _ := ipSliceConv(value) + return validation.(func(value []net.IP) error)(v.([]net.IP)) + case func(value net.IPMask) error: + v, _ := parseIPv4Mask(value) + return validation.(func(value net.IPMask) error)(v.(net.IPMask)) + case func(value net.IPNet) error: + v, _ := ipNetConv(value) + return validation.(func(value net.IPNet) error)(v.(net.IPNet)) + case func(value []net.IPNet) error: + v, _ := ipNetSliceConv(value) + return validation.(func(value []net.IPNet) error)(v.([]net.IPNet)) + case func(value string) error: + v, _ := stringConv(value) + return validation.(func(value string) error)(v.(string)) + case func(value []string) error: + v, _ := stringSliceConv(value) + return validation.(func(value []string) error)(v.([]string)) + case func(value map[string]int) error: + v, _ := stringToIntConv(value) + return validation.(func(value map[string]int) error)(v.(map[string]int)) + case func(value map[string]int64) error: + v, _ := stringToInt64Conv(value) + return validation.(func(value map[string]int64) error)(v.(map[string]int64)) + case func(value map[string]string) error: + v, _ := stringToStringConv(value) + return validation.(func(value map[string]string) error)(v.(map[string]string)) + case func(value uint) error: + v, _ := uintConv(value) + return validation.(func(value uint) error)(v.(uint)) + case func(value uint8) error: + v, _ := uint8Conv(value) + return validation.(func(value uint8) error)(v.(uint8)) + case func(value uint16) error: + v, _ := uint16Conv(value) + return validation.(func(value uint16) error)(v.(uint16)) + case func(value uint32) error: + v, _ := uint32Conv(value) + return validation.(func(value uint32) error)(v.(uint32)) + case func(value uint64) error: + v, _ := uint64Conv(value) + return validation.(func(value uint64) error)(v.(uint64)) + case func(value []uint) error: + v, _ := uintSliceConv(value) + return validation.(func(value []uint) error)(v.([]uint)) + } + return nil +} diff --git a/flag_test.go b/flag_test.go index 58a5d25a..5f1a70ed 100644 --- a/flag_test.go +++ b/flag_test.go @@ -6,6 +6,7 @@ package pflag import ( "bytes" + "errors" "fmt" "io" "io/ioutil" @@ -404,6 +405,73 @@ func testParseAll(f *FlagSet, t *testing.T) { } } +func testParseFlagWithValidation(f *FlagSet, t *testing.T) { + if f.Parsed() { + t.Error("f.Parse() = true before Parse") + } + + boolValidationFunc := func(value bool) error { + if value { + return errors.New("validation failed") + } + return nil + } + + intValidationFunc := func(value int) error { + if value > 10 || value < 0 { + return errors.New("validation failed") + } + return nil + } + + stringValidationFunc := func(value string) error { + if len(value) < 4 { + return errors.New("validation failed") + } + return nil + } + + uintValidationFunc := func(value uint) error { + if value < 4 { + return errors.New("validation failed") + } + return nil + } + + boolFlag1 := f.Bool("bool", false, "bool value", boolValidationFunc) + intFlag := f.Int("int", 0, "int value", intValidationFunc) + stringFlag := f.String("string", "0", "string value", stringValidationFunc) + uintFlag := f.Uint("uint", 10, "uint value", uintValidationFunc) + args := []string{ + "--bool=true", + "--int11=", + "--string=hello", + "--uint=3", + } + + f.Parse(args) + + if !f.Parsed() { + t.Error("f.Parse() = false after Parse") + } + + if *boolFlag1 != false { + t.Error("bool flag should be true, is ", *boolFlag1) + } + + if *intFlag != 0 { + t.Error("int flag should be 0, is ", *intFlag) + } + + if *stringFlag != "0" { + t.Error("string flag should be 0, is ", *stringFlag) + } + + if *uintFlag != 10 { + t.Error("uint flag should be 10, is ", *stringFlag) + } +} + func testParseWithUnknownFlags(f *FlagSet, t *testing.T) { if f.Parsed() { t.Error("f.Parse() = true before Parse") @@ -586,6 +654,11 @@ func TestParseAll(t *testing.T) { testParseAll(GetCommandLine(), t) } +func TestParseFlagWithValidation(t *testing.T) { + ResetForTesting(func() { t.Error("bad parse") }) + testParseFlagWithValidation(GetCommandLine(), t) +} + func TestIgnoreUnknownFlags(t *testing.T) { ResetForTesting(func() { t.Error("bad parse") }) testParseWithUnknownFlags(GetCommandLine(), t) @@ -1134,7 +1207,6 @@ func TestMultipleNormalizeFlagNameInvocations(t *testing.T) { } } -// func TestHiddenFlagInUsage(t *testing.T) { f := NewFlagSet("bob", ContinueOnError) f.Bool("secretFlag", true, "shhh") @@ -1149,7 +1221,6 @@ func TestHiddenFlagInUsage(t *testing.T) { } } -// func TestHiddenFlagUsage(t *testing.T) { f := NewFlagSet("bob", ContinueOnError) f.Bool("secretFlag", true, "shhh") @@ -1239,7 +1310,7 @@ func TestPrintDefaults(t *testing.T) { got := buf.String() if got != defaultOutput { fmt.Println("\n" + got) - fmt.Println("\n" + defaultOutput) + // fmt.Println("\n" + defaultOutput) t.Errorf("got %q want %q\n", got, defaultOutput) } } diff --git a/float32.go b/float32.go index a243f81f..4bc263b3 100644 --- a/float32.go +++ b/float32.go @@ -41,48 +41,68 @@ func (f *FlagSet) GetFloat32(name string) (float32, error) { // 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 (f *FlagSet) Float32Var(p *float32, name string, value float32, usage string) { +func (f *FlagSet) Float32Var(p *float32, name string, value float32, usage string, validation ...func(value float32) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + f.VarP(newFloat32Value(value, p), name, "", usage, validationFunc) + return + } f.VarP(newFloat32Value(value, p), name, "", usage) } // 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, validation ...func(value float32) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + f.VarP(newFloat32Value(value, p), name, shorthand, usage, validationFunc) + return + } f.VarP(newFloat32Value(value, p), name, shorthand, usage) } // 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 Float32Var(p *float32, name string, value float32, usage string) { +func Float32Var(p *float32, name string, value float32, usage string, validation ...func(value float32) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + CommandLine.VarP(newFloat32Value(value, p), name, "", usage, validationFunc) + return + } CommandLine.VarP(newFloat32Value(value, p), name, "", usage) } // 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, validation ...func(value float32) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + CommandLine.VarP(newFloat32Value(value, p), name, shorthand, usage, validationFunc) + return + } CommandLine.VarP(newFloat32Value(value, p), name, shorthand, usage) } // 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 (f *FlagSet) Float32(name string, value float32, usage string) *float32 { +func (f *FlagSet) Float32(name string, value float32, usage string, validation ...func(value float32) error) *float32 { p := new(float32) - f.Float32VarP(p, name, "", value, usage) + f.Float32VarP(p, name, "", value, usage, validation...) return p } // 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, validation ...func(value float32) error) *float32 { p := new(float32) - f.Float32VarP(p, name, shorthand, value, usage) + f.Float32VarP(p, name, shorthand, value, usage, validation...) return p } // 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 Float32(name string, value float32, usage string) *float32 { - return CommandLine.Float32P(name, "", value, usage) +func Float32(name string, value float32, usage string, validation ...func(value float32) error) *float32 { + return CommandLine.Float32P(name, "", value, usage, validation...) } // 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 { - return CommandLine.Float32P(name, shorthand, value, usage) +func Float32P(name, shorthand string, value float32, usage string, validation ...func(value float32) error) *float32 { + return CommandLine.Float32P(name, shorthand, value, usage, validation...) } diff --git a/float32_slice.go b/float32_slice.go index caa35274..12d9e023 100644 --- a/float32_slice.go +++ b/float32_slice.go @@ -127,48 +127,68 @@ func (f *FlagSet) GetFloat32Slice(name string) ([]float32, error) { // Float32SliceVar defines a float32Slice 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 (f *FlagSet) Float32SliceVar(p *[]float32, name string, value []float32, usage string) { +func (f *FlagSet) Float32SliceVar(p *[]float32, name string, value []float32, usage string, validation ...func(value []float32) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + f.VarP(newFloat32SliceValue(value, p), name, "", usage, validationFunc) + return + } f.VarP(newFloat32SliceValue(value, p), name, "", usage) } // Float32SliceVarP is like Float32SliceVar, but accepts a shorthand letter that can be used after a single dash. -func (f *FlagSet) Float32SliceVarP(p *[]float32, name, shorthand string, value []float32, usage string) { +func (f *FlagSet) Float32SliceVarP(p *[]float32, name, shorthand string, value []float32, usage string, validation ...func(value []float32) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + f.VarP(newFloat32SliceValue(value, p), name, shorthand, usage, validationFunc) + return + } f.VarP(newFloat32SliceValue(value, p), name, shorthand, usage) } // Float32SliceVar 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 Float32SliceVar(p *[]float32, name string, value []float32, usage string) { +func Float32SliceVar(p *[]float32, name string, value []float32, usage string, validation ...func(value []float32) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + CommandLine.VarP(newFloat32SliceValue(value, p), name, "", usage, validationFunc) + return + } CommandLine.VarP(newFloat32SliceValue(value, p), name, "", usage) } // Float32SliceVarP is like Float32SliceVar, but accepts a shorthand letter that can be used after a single dash. -func Float32SliceVarP(p *[]float32, name, shorthand string, value []float32, usage string) { +func Float32SliceVarP(p *[]float32, name, shorthand string, value []float32, usage string, validation ...func(value []float32) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + CommandLine.VarP(newFloat32SliceValue(value, p), name, shorthand, usage, validationFunc) + return + } CommandLine.VarP(newFloat32SliceValue(value, p), name, shorthand, usage) } // Float32Slice 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 (f *FlagSet) Float32Slice(name string, value []float32, usage string) *[]float32 { +func (f *FlagSet) Float32Slice(name string, value []float32, usage string, validation ...func(value []float32) error) *[]float32 { p := []float32{} - f.Float32SliceVarP(&p, name, "", value, usage) + f.Float32SliceVarP(&p, name, "", value, usage, validation...) return &p } // Float32SliceP is like Float32Slice, but accepts a shorthand letter that can be used after a single dash. -func (f *FlagSet) Float32SliceP(name, shorthand string, value []float32, usage string) *[]float32 { +func (f *FlagSet) Float32SliceP(name, shorthand string, value []float32, usage string, validation ...func(value []float32) error) *[]float32 { p := []float32{} - f.Float32SliceVarP(&p, name, shorthand, value, usage) + f.Float32SliceVarP(&p, name, shorthand, value, usage, validation...) return &p } // Float32Slice 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 Float32Slice(name string, value []float32, usage string) *[]float32 { - return CommandLine.Float32SliceP(name, "", value, usage) +func Float32Slice(name string, value []float32, usage string, validation ...func(value []float32) error) *[]float32 { + return CommandLine.Float32SliceP(name, "", value, usage, validation...) } // Float32SliceP is like Float32Slice, but accepts a shorthand letter that can be used after a single dash. -func Float32SliceP(name, shorthand string, value []float32, usage string) *[]float32 { - return CommandLine.Float32SliceP(name, shorthand, value, usage) +func Float32SliceP(name, shorthand string, value []float32, usage string, validation ...func(value []float32) error) *[]float32 { + return CommandLine.Float32SliceP(name, shorthand, value, usage, validation...) } diff --git a/float64.go b/float64.go index 04b5492a..2b435d9a 100644 --- a/float64.go +++ b/float64.go @@ -37,48 +37,68 @@ func (f *FlagSet) GetFloat64(name string) (float64, error) { // 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 (f *FlagSet) Float64Var(p *float64, name string, value float64, usage string) { +func (f *FlagSet) Float64Var(p *float64, name string, value float64, usage string, validation ...func(value float64) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + f.VarP(newFloat64Value(value, p), name, "", usage, validationFunc) + return + } f.VarP(newFloat64Value(value, p), name, "", usage) } // 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, validation ...func(value float64) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + f.VarP(newFloat64Value(value, p), name, shorthand, usage, validationFunc) + return + } f.VarP(newFloat64Value(value, p), name, shorthand, usage) } // 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 Float64Var(p *float64, name string, value float64, usage string) { +func Float64Var(p *float64, name string, value float64, usage string, validation ...func(value float64) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + CommandLine.VarP(newFloat64Value(value, p), name, "", usage, validationFunc) + return + } CommandLine.VarP(newFloat64Value(value, p), name, "", usage) } // 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, validation ...func(value float64) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + CommandLine.VarP(newFloat64Value(value, p), name, shorthand, usage, validationFunc) + return + } CommandLine.VarP(newFloat64Value(value, p), name, shorthand, usage) } // 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 (f *FlagSet) Float64(name string, value float64, usage string) *float64 { +func (f *FlagSet) Float64(name string, value float64, usage string, validation ...func(value float64) error) *float64 { p := new(float64) - f.Float64VarP(p, name, "", value, usage) + f.Float64VarP(p, name, "", value, usage, validation...) return p } // 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, validation ...func(value float64) error) *float64 { p := new(float64) - f.Float64VarP(p, name, shorthand, value, usage) + f.Float64VarP(p, name, shorthand, value, usage, validation...) return p } // 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 Float64(name string, value float64, usage string) *float64 { - return CommandLine.Float64P(name, "", value, usage) +func Float64(name string, value float64, usage string, validation ...func(value float64) error) *float64 { + return CommandLine.Float64P(name, "", value, usage, validation...) } // 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 { - return CommandLine.Float64P(name, shorthand, value, usage) +func Float64P(name, shorthand string, value float64, usage string, validation ...func(value float64) error) *float64 { + return CommandLine.Float64P(name, shorthand, value, usage, validation...) } diff --git a/float64_slice.go b/float64_slice.go index 85bf3073..a9617c5b 100644 --- a/float64_slice.go +++ b/float64_slice.go @@ -119,48 +119,68 @@ func (f *FlagSet) GetFloat64Slice(name string) ([]float64, error) { // Float64SliceVar defines a float64Slice 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 (f *FlagSet) Float64SliceVar(p *[]float64, name string, value []float64, usage string) { +func (f *FlagSet) Float64SliceVar(p *[]float64, name string, value []float64, usage string, validation ...func(value []float64) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + f.VarP(newFloat64SliceValue(value, p), name, "", usage, validationFunc) + return + } f.VarP(newFloat64SliceValue(value, p), name, "", usage) } // Float64SliceVarP is like Float64SliceVar, but accepts a shorthand letter that can be used after a single dash. -func (f *FlagSet) Float64SliceVarP(p *[]float64, name, shorthand string, value []float64, usage string) { +func (f *FlagSet) Float64SliceVarP(p *[]float64, name, shorthand string, value []float64, usage string, validation ...func(value []float64) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + f.VarP(newFloat64SliceValue(value, p), name, shorthand, usage, validationFunc) + return + } f.VarP(newFloat64SliceValue(value, p), name, shorthand, usage) } // Float64SliceVar 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 Float64SliceVar(p *[]float64, name string, value []float64, usage string) { +func Float64SliceVar(p *[]float64, name string, value []float64, usage string, validation ...func(value []float64) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + CommandLine.VarP(newFloat64SliceValue(value, p), name, "", usage, validationFunc) + return + } CommandLine.VarP(newFloat64SliceValue(value, p), name, "", usage) } // Float64SliceVarP is like Float64SliceVar, but accepts a shorthand letter that can be used after a single dash. -func Float64SliceVarP(p *[]float64, name, shorthand string, value []float64, usage string) { +func Float64SliceVarP(p *[]float64, name, shorthand string, value []float64, usage string, validation ...func(value []float64) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + CommandLine.VarP(newFloat64SliceValue(value, p), name, shorthand, usage, validationFunc) + return + } CommandLine.VarP(newFloat64SliceValue(value, p), name, shorthand, usage) } // Float64Slice 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 (f *FlagSet) Float64Slice(name string, value []float64, usage string) *[]float64 { +func (f *FlagSet) Float64Slice(name string, value []float64, usage string, validation ...func(value []float64) error) *[]float64 { p := []float64{} - f.Float64SliceVarP(&p, name, "", value, usage) + f.Float64SliceVarP(&p, name, "", value, usage, validation...) return &p } // Float64SliceP is like Float64Slice, but accepts a shorthand letter that can be used after a single dash. -func (f *FlagSet) Float64SliceP(name, shorthand string, value []float64, usage string) *[]float64 { +func (f *FlagSet) Float64SliceP(name, shorthand string, value []float64, usage string, validation ...func(value []float64) error) *[]float64 { p := []float64{} - f.Float64SliceVarP(&p, name, shorthand, value, usage) + f.Float64SliceVarP(&p, name, shorthand, value, usage, validation...) return &p } // Float64Slice 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 Float64Slice(name string, value []float64, usage string) *[]float64 { - return CommandLine.Float64SliceP(name, "", value, usage) +func Float64Slice(name string, value []float64, usage string, validation ...func(value []float64) error) *[]float64 { + return CommandLine.Float64SliceP(name, "", value, usage, validation...) } // Float64SliceP is like Float64Slice, but accepts a shorthand letter that can be used after a single dash. -func Float64SliceP(name, shorthand string, value []float64, usage string) *[]float64 { - return CommandLine.Float64SliceP(name, shorthand, value, usage) +func Float64SliceP(name, shorthand string, value []float64, usage string, validation ...func(value []float64) error) *[]float64 { + return CommandLine.Float64SliceP(name, shorthand, value, usage, validation...) } diff --git a/int.go b/int.go index 1474b89d..15a26715 100644 --- a/int.go +++ b/int.go @@ -37,48 +37,68 @@ func (f *FlagSet) GetInt(name string) (int, error) { // 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 (f *FlagSet) IntVar(p *int, name string, value int, usage string) { +func (f *FlagSet) IntVar(p *int, name string, value int, usage string, validation ...func(value int) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + f.VarP(newIntValue(value, p), name, "", usage, validationFunc) + return + } f.VarP(newIntValue(value, p), name, "", usage) } // 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, validation ...func(value int) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + f.VarP(newIntValue(value, p), name, shorthand, usage, validationFunc) + return + } f.VarP(newIntValue(value, p), name, shorthand, usage) } // 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 IntVar(p *int, name string, value int, usage string) { +func IntVar(p *int, name string, value int, usage string, validation ...func(value int) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + CommandLine.VarP(newIntValue(value, p), name, "", usage, validationFunc) + return + } CommandLine.VarP(newIntValue(value, p), name, "", usage) } // 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, validation ...func(value int) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + CommandLine.VarP(newIntValue(value, p), name, shorthand, usage, validationFunc) + return + } CommandLine.VarP(newIntValue(value, p), name, shorthand, usage) } // 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 (f *FlagSet) Int(name string, value int, usage string) *int { +func (f *FlagSet) Int(name string, value int, usage string, validation ...func(value int) error) *int { p := new(int) - f.IntVarP(p, name, "", value, usage) + f.IntVarP(p, name, "", value, usage, validation...) return p } // 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, validation ...func(value int) error) *int { p := new(int) - f.IntVarP(p, name, shorthand, value, usage) + f.IntVarP(p, name, shorthand, value, usage, validation...) return p } // 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 Int(name string, value int, usage string) *int { - return CommandLine.IntP(name, "", value, usage) +func Int(name string, value int, usage string, validation ...func(value int) error) *int { + return CommandLine.IntP(name, "", value, usage, validation...) } // 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 { - return CommandLine.IntP(name, shorthand, value, usage) +func IntP(name, shorthand string, value int, usage string, validation ...func(value int) error) *int { + return CommandLine.IntP(name, shorthand, value, usage, validation...) } diff --git a/int16.go b/int16.go index f1a01d05..7ab0e2fd 100644 --- a/int16.go +++ b/int16.go @@ -41,48 +41,68 @@ func (f *FlagSet) GetInt16(name string) (int16, error) { // Int16Var defines an int16 flag with specified name, default value, and usage string. // The argument p points to an int16 variable in which to store the value of the flag. -func (f *FlagSet) Int16Var(p *int16, name string, value int16, usage string) { +func (f *FlagSet) Int16Var(p *int16, name string, value int16, usage string, validation ...func(value int16) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + f.VarP(newInt16Value(value, p), name, "", usage, validationFunc) + return + } f.VarP(newInt16Value(value, p), name, "", usage) } // Int16VarP is like Int16Var, but accepts a shorthand letter that can be used after a single dash. -func (f *FlagSet) Int16VarP(p *int16, name, shorthand string, value int16, usage string) { +func (f *FlagSet) Int16VarP(p *int16, name, shorthand string, value int16, usage string, validation ...func(value int16) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + f.VarP(newInt16Value(value, p), name, shorthand, usage, validationFunc) + return + } f.VarP(newInt16Value(value, p), name, shorthand, usage) } // Int16Var defines an int16 flag with specified name, default value, and usage string. // The argument p points to an int16 variable in which to store the value of the flag. -func Int16Var(p *int16, name string, value int16, usage string) { +func Int16Var(p *int16, name string, value int16, usage string, validation ...func(value int16) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + CommandLine.VarP(newInt16Value(value, p), name, "", usage, validationFunc) + return + } CommandLine.VarP(newInt16Value(value, p), name, "", usage) } // Int16VarP is like Int16Var, but accepts a shorthand letter that can be used after a single dash. -func Int16VarP(p *int16, name, shorthand string, value int16, usage string) { +func Int16VarP(p *int16, name, shorthand string, value int16, usage string, validation ...func(value int16) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + CommandLine.VarP(newInt16Value(value, p), name, shorthand, usage, validationFunc) + return + } CommandLine.VarP(newInt16Value(value, p), name, shorthand, usage) } // Int16 defines an int16 flag with specified name, default value, and usage string. // The return value is the address of an int16 variable that stores the value of the flag. -func (f *FlagSet) Int16(name string, value int16, usage string) *int16 { +func (f *FlagSet) Int16(name string, value int16, usage string, validation ...func(value int16) error) *int16 { p := new(int16) - f.Int16VarP(p, name, "", value, usage) + f.Int16VarP(p, name, "", value, usage, validation...) return p } // Int16P is like Int16, but accepts a shorthand letter that can be used after a single dash. -func (f *FlagSet) Int16P(name, shorthand string, value int16, usage string) *int16 { +func (f *FlagSet) Int16P(name, shorthand string, value int16, usage string, validation ...func(value int16) error) *int16 { p := new(int16) - f.Int16VarP(p, name, shorthand, value, usage) + f.Int16VarP(p, name, shorthand, value, usage, validation...) return p } // Int16 defines an int16 flag with specified name, default value, and usage string. // The return value is the address of an int16 variable that stores the value of the flag. -func Int16(name string, value int16, usage string) *int16 { - return CommandLine.Int16P(name, "", value, usage) +func Int16(name string, value int16, usage string, validation ...func(value int16) error) *int16 { + return CommandLine.Int16P(name, "", value, usage, validation...) } // Int16P is like Int16, but accepts a shorthand letter that can be used after a single dash. -func Int16P(name, shorthand string, value int16, usage string) *int16 { - return CommandLine.Int16P(name, shorthand, value, usage) +func Int16P(name, shorthand string, value int16, usage string, validation ...func(value int16) error) *int16 { + return CommandLine.Int16P(name, shorthand, value, usage, validation...) } diff --git a/int32.go b/int32.go index 9b95944f..4126da49 100644 --- a/int32.go +++ b/int32.go @@ -41,48 +41,68 @@ func (f *FlagSet) GetInt32(name string) (int32, error) { // 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 (f *FlagSet) Int32Var(p *int32, name string, value int32, usage string) { +func (f *FlagSet) Int32Var(p *int32, name string, value int32, usage string, validation ...func(value int32) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + f.VarP(newInt32Value(value, p), name, "", usage, validationFunc) + return + } f.VarP(newInt32Value(value, p), name, "", usage) } // 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, validation ...func(value int32) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + f.VarP(newInt32Value(value, p), name, shorthand, usage, validationFunc) + return + } f.VarP(newInt32Value(value, p), name, shorthand, usage) } // 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 Int32Var(p *int32, name string, value int32, usage string) { +func Int32Var(p *int32, name string, value int32, usage string, validation ...func(value int32) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + CommandLine.VarP(newInt32Value(value, p), name, "", usage, validationFunc) + return + } CommandLine.VarP(newInt32Value(value, p), name, "", usage) } // 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, validation ...func(value int32) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + CommandLine.VarP(newInt32Value(value, p), name, shorthand, usage, validationFunc) + return + } CommandLine.VarP(newInt32Value(value, p), name, shorthand, usage) } // 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 (f *FlagSet) Int32(name string, value int32, usage string) *int32 { +func (f *FlagSet) Int32(name string, value int32, usage string, validation ...func(value int32) error) *int32 { p := new(int32) - f.Int32VarP(p, name, "", value, usage) + f.Int32VarP(p, name, "", value, usage, validation...) return p } // 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, validation ...func(value int32) error) *int32 { p := new(int32) - f.Int32VarP(p, name, shorthand, value, usage) + f.Int32VarP(p, name, shorthand, value, usage, validation...) return p } // 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 Int32(name string, value int32, usage string) *int32 { - return CommandLine.Int32P(name, "", value, usage) +func Int32(name string, value int32, usage string, validation ...func(value int32) error) *int32 { + return CommandLine.Int32P(name, "", value, usage, validation...) } // 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 { - return CommandLine.Int32P(name, shorthand, value, usage) +func Int32P(name, shorthand string, value int32, usage string, validation ...func(value int32) error) *int32 { + return CommandLine.Int32P(name, shorthand, value, usage, validation...) } diff --git a/int32_slice.go b/int32_slice.go index ff128ff0..d3ae36eb 100644 --- a/int32_slice.go +++ b/int32_slice.go @@ -127,48 +127,68 @@ func (f *FlagSet) GetInt32Slice(name string) ([]int32, error) { // Int32SliceVar defines a int32Slice flag with specified name, default value, and usage string. // The argument p points to a []int32 variable in which to store the value of the flag. -func (f *FlagSet) Int32SliceVar(p *[]int32, name string, value []int32, usage string) { +func (f *FlagSet) Int32SliceVar(p *[]int32, name string, value []int32, usage string, validation ...func(value []int32) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + f.VarP(newInt32SliceValue(value, p), name, "", usage, validationFunc) + return + } f.VarP(newInt32SliceValue(value, p), name, "", usage) } // Int32SliceVarP is like Int32SliceVar, but accepts a shorthand letter that can be used after a single dash. -func (f *FlagSet) Int32SliceVarP(p *[]int32, name, shorthand string, value []int32, usage string) { +func (f *FlagSet) Int32SliceVarP(p *[]int32, name, shorthand string, value []int32, usage string, validation ...func(value []int32) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + f.VarP(newInt32SliceValue(value, p), name, shorthand, usage, validationFunc) + return + } f.VarP(newInt32SliceValue(value, p), name, shorthand, usage) } // Int32SliceVar defines a int32[] flag with specified name, default value, and usage string. // The argument p points to a int32[] variable in which to store the value of the flag. -func Int32SliceVar(p *[]int32, name string, value []int32, usage string) { +func Int32SliceVar(p *[]int32, name string, value []int32, usage string, validation ...func(value []int32) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + CommandLine.VarP(newInt32SliceValue(value, p), name, "", usage, validationFunc) + return + } CommandLine.VarP(newInt32SliceValue(value, p), name, "", usage) } // Int32SliceVarP is like Int32SliceVar, but accepts a shorthand letter that can be used after a single dash. -func Int32SliceVarP(p *[]int32, name, shorthand string, value []int32, usage string) { +func Int32SliceVarP(p *[]int32, name, shorthand string, value []int32, usage string, validation ...func(value []int32) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + CommandLine.VarP(newInt32SliceValue(value, p), name, shorthand, usage, validationFunc) + return + } CommandLine.VarP(newInt32SliceValue(value, p), name, shorthand, usage) } // Int32Slice defines a []int32 flag with specified name, default value, and usage string. // The return value is the address of a []int32 variable that stores the value of the flag. -func (f *FlagSet) Int32Slice(name string, value []int32, usage string) *[]int32 { +func (f *FlagSet) Int32Slice(name string, value []int32, usage string, validation ...func(value []int32) error) *[]int32 { p := []int32{} - f.Int32SliceVarP(&p, name, "", value, usage) + f.Int32SliceVarP(&p, name, "", value, usage, validation...) return &p } // Int32SliceP is like Int32Slice, but accepts a shorthand letter that can be used after a single dash. -func (f *FlagSet) Int32SliceP(name, shorthand string, value []int32, usage string) *[]int32 { +func (f *FlagSet) Int32SliceP(name, shorthand string, value []int32, usage string, validation ...func(value []int32) error) *[]int32 { p := []int32{} - f.Int32SliceVarP(&p, name, shorthand, value, usage) + f.Int32SliceVarP(&p, name, shorthand, value, usage, validation...) return &p } // Int32Slice defines a []int32 flag with specified name, default value, and usage string. // The return value is the address of a []int32 variable that stores the value of the flag. -func Int32Slice(name string, value []int32, usage string) *[]int32 { - return CommandLine.Int32SliceP(name, "", value, usage) +func Int32Slice(name string, value []int32, usage string, validation ...func(value []int32) error) *[]int32 { + return CommandLine.Int32SliceP(name, "", value, usage, validation...) } // Int32SliceP is like Int32Slice, but accepts a shorthand letter that can be used after a single dash. -func Int32SliceP(name, shorthand string, value []int32, usage string) *[]int32 { - return CommandLine.Int32SliceP(name, shorthand, value, usage) +func Int32SliceP(name, shorthand string, value []int32, usage string, validation ...func(value []int32) error) *[]int32 { + return CommandLine.Int32SliceP(name, shorthand, value, usage, validation...) } diff --git a/int64.go b/int64.go index 0026d781..5cf9ad0b 100644 --- a/int64.go +++ b/int64.go @@ -37,48 +37,68 @@ func (f *FlagSet) GetInt64(name string) (int64, error) { // 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 (f *FlagSet) Int64Var(p *int64, name string, value int64, usage string) { +func (f *FlagSet) Int64Var(p *int64, name string, value int64, usage string, validation ...func(value int64) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + f.VarP(newInt64Value(value, p), name, "", usage, validationFunc) + return + } f.VarP(newInt64Value(value, p), name, "", usage) } // 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, validation ...func(value int64) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + f.VarP(newInt64Value(value, p), name, shorthand, usage, validationFunc) + return + } f.VarP(newInt64Value(value, p), name, shorthand, usage) } // 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 Int64Var(p *int64, name string, value int64, usage string) { +func Int64Var(p *int64, name string, value int64, usage string, validation ...func(value int64) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + CommandLine.VarP(newInt64Value(value, p), name, "", usage, validationFunc) + return + } CommandLine.VarP(newInt64Value(value, p), name, "", usage) } // 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, validation ...func(value int64) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + CommandLine.VarP(newInt64Value(value, p), name, shorthand, usage, validationFunc) + return + } CommandLine.VarP(newInt64Value(value, p), name, shorthand, usage) } // 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 (f *FlagSet) Int64(name string, value int64, usage string) *int64 { +func (f *FlagSet) Int64(name string, value int64, usage string, validation ...func(value int64) error) *int64 { p := new(int64) - f.Int64VarP(p, name, "", value, usage) + f.Int64VarP(p, name, "", value, usage, validation...) return p } // 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, validation ...func(value int64) error) *int64 { p := new(int64) - f.Int64VarP(p, name, shorthand, value, usage) + f.Int64VarP(p, name, shorthand, value, usage, validation...) return p } // 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 Int64(name string, value int64, usage string) *int64 { - return CommandLine.Int64P(name, "", value, usage) +func Int64(name string, value int64, usage string, validation ...func(value int64) error) *int64 { + return CommandLine.Int64P(name, "", value, usage, validation...) } // 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 { - return CommandLine.Int64P(name, shorthand, value, usage) +func Int64P(name, shorthand string, value int64, usage string, validation ...func(value int64) error) *int64 { + return CommandLine.Int64P(name, shorthand, value, usage, validation...) } diff --git a/int64_slice.go b/int64_slice.go index 25464638..32299865 100644 --- a/int64_slice.go +++ b/int64_slice.go @@ -119,48 +119,68 @@ func (f *FlagSet) GetInt64Slice(name string) ([]int64, error) { // Int64SliceVar defines a int64Slice flag with specified name, default value, and usage string. // The argument p points to a []int64 variable in which to store the value of the flag. -func (f *FlagSet) Int64SliceVar(p *[]int64, name string, value []int64, usage string) { +func (f *FlagSet) Int64SliceVar(p *[]int64, name string, value []int64, usage string, validation ...func(value []int64) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + f.VarP(newInt64SliceValue(value, p), name, "", usage, validationFunc) + return + } f.VarP(newInt64SliceValue(value, p), name, "", usage) } // Int64SliceVarP is like Int64SliceVar, but accepts a shorthand letter that can be used after a single dash. -func (f *FlagSet) Int64SliceVarP(p *[]int64, name, shorthand string, value []int64, usage string) { +func (f *FlagSet) Int64SliceVarP(p *[]int64, name, shorthand string, value []int64, usage string, validation ...func(value []int64) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + f.VarP(newInt64SliceValue(value, p), name, shorthand, usage, validationFunc) + return + } f.VarP(newInt64SliceValue(value, p), name, shorthand, usage) } // Int64SliceVar defines a int64[] flag with specified name, default value, and usage string. // The argument p points to a int64[] variable in which to store the value of the flag. -func Int64SliceVar(p *[]int64, name string, value []int64, usage string) { +func Int64SliceVar(p *[]int64, name string, value []int64, usage string, validation ...func(value []int64) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + CommandLine.VarP(newInt64SliceValue(value, p), name, "", usage, validationFunc) + return + } CommandLine.VarP(newInt64SliceValue(value, p), name, "", usage) } // Int64SliceVarP is like Int64SliceVar, but accepts a shorthand letter that can be used after a single dash. -func Int64SliceVarP(p *[]int64, name, shorthand string, value []int64, usage string) { +func Int64SliceVarP(p *[]int64, name, shorthand string, value []int64, usage string, validation ...func(value []int64) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + CommandLine.VarP(newInt64SliceValue(value, p), name, shorthand, usage, validationFunc) + return + } CommandLine.VarP(newInt64SliceValue(value, p), name, shorthand, usage) } // Int64Slice defines a []int64 flag with specified name, default value, and usage string. // The return value is the address of a []int64 variable that stores the value of the flag. -func (f *FlagSet) Int64Slice(name string, value []int64, usage string) *[]int64 { +func (f *FlagSet) Int64Slice(name string, value []int64, usage string, validation ...func(value []int64) error) *[]int64 { p := []int64{} - f.Int64SliceVarP(&p, name, "", value, usage) + f.Int64SliceVarP(&p, name, "", value, usage, validation...) return &p } // Int64SliceP is like Int64Slice, but accepts a shorthand letter that can be used after a single dash. -func (f *FlagSet) Int64SliceP(name, shorthand string, value []int64, usage string) *[]int64 { +func (f *FlagSet) Int64SliceP(name, shorthand string, value []int64, usage string, validation ...func(value []int64) error) *[]int64 { p := []int64{} - f.Int64SliceVarP(&p, name, shorthand, value, usage) + f.Int64SliceVarP(&p, name, shorthand, value, usage, validation...) return &p } // Int64Slice defines a []int64 flag with specified name, default value, and usage string. // The return value is the address of a []int64 variable that stores the value of the flag. -func Int64Slice(name string, value []int64, usage string) *[]int64 { - return CommandLine.Int64SliceP(name, "", value, usage) +func Int64Slice(name string, value []int64, usage string, validation ...func(value []int64) error) *[]int64 { + return CommandLine.Int64SliceP(name, "", value, usage, validation...) } // Int64SliceP is like Int64Slice, but accepts a shorthand letter that can be used after a single dash. -func Int64SliceP(name, shorthand string, value []int64, usage string) *[]int64 { - return CommandLine.Int64SliceP(name, shorthand, value, usage) +func Int64SliceP(name, shorthand string, value []int64, usage string, validation ...func(value []int64) error) *[]int64 { + return CommandLine.Int64SliceP(name, shorthand, value, usage, validation...) } diff --git a/int8.go b/int8.go index 4da92228..2644aadf 100644 --- a/int8.go +++ b/int8.go @@ -41,48 +41,68 @@ func (f *FlagSet) GetInt8(name string) (int8, error) { // 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 (f *FlagSet) Int8Var(p *int8, name string, value int8, usage string) { +func (f *FlagSet) Int8Var(p *int8, name string, value int8, usage string, validation ...func(value int8) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + f.VarP(newInt8Value(value, p), name, "", usage, validationFunc) + return + } f.VarP(newInt8Value(value, p), name, "", usage) } // 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, validation ...func(value int8) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + f.VarP(newInt8Value(value, p), name, shorthand, usage, validationFunc) + return + } f.VarP(newInt8Value(value, p), name, shorthand, usage) } // 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 Int8Var(p *int8, name string, value int8, usage string) { +func Int8Var(p *int8, name string, value int8, usage string, validation ...func(value int8) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + CommandLine.VarP(newInt8Value(value, p), name, "", usage, validationFunc) + return + } CommandLine.VarP(newInt8Value(value, p), name, "", usage) } // 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, validation ...func(value int8) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + CommandLine.VarP(newInt8Value(value, p), name, shorthand, usage, validationFunc) + return + } CommandLine.VarP(newInt8Value(value, p), name, shorthand, usage) } // 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 (f *FlagSet) Int8(name string, value int8, usage string) *int8 { +func (f *FlagSet) Int8(name string, value int8, usage string, validation ...func(value int8) error) *int8 { p := new(int8) - f.Int8VarP(p, name, "", value, usage) + f.Int8VarP(p, name, "", value, usage, validation...) return p } // 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, validation ...func(value int8) error) *int8 { p := new(int8) - f.Int8VarP(p, name, shorthand, value, usage) + f.Int8VarP(p, name, shorthand, value, usage, validation...) return p } // 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 Int8(name string, value int8, usage string) *int8 { - return CommandLine.Int8P(name, "", value, usage) +func Int8(name string, value int8, usage string, validation ...func(value int8) error) *int8 { + return CommandLine.Int8P(name, "", value, usage, validation...) } // 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 { - return CommandLine.Int8P(name, shorthand, value, usage) +func Int8P(name, shorthand string, value int8, usage string, validation ...func(value int8) error) *int8 { + return CommandLine.Int8P(name, shorthand, value, usage, validation...) } diff --git a/int_slice.go b/int_slice.go index e71c39d9..c3464a39 100644 --- a/int_slice.go +++ b/int_slice.go @@ -111,48 +111,68 @@ func (f *FlagSet) GetIntSlice(name string) ([]int, error) { // 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 (f *FlagSet) IntSliceVar(p *[]int, name string, value []int, usage string) { +func (f *FlagSet) IntSliceVar(p *[]int, name string, value []int, usage string, validation ...func(value []int) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + f.VarP(newIntSliceValue(value, p), name, "", usage, validationFunc) + return + } f.VarP(newIntSliceValue(value, p), name, "", usage) } // 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, validation ...func(value []int) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + f.VarP(newIntSliceValue(value, p), name, shorthand, usage, validationFunc) + return + } f.VarP(newIntSliceValue(value, p), name, shorthand, usage) } // 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 IntSliceVar(p *[]int, name string, value []int, usage string) { +func IntSliceVar(p *[]int, name string, value []int, usage string, validation ...func(value []int) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + CommandLine.VarP(newIntSliceValue(value, p), name, "", usage, validationFunc) + return + } CommandLine.VarP(newIntSliceValue(value, p), name, "", usage) } // 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, validation ...func(value []int) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + CommandLine.VarP(newIntSliceValue(value, p), name, shorthand, usage, validationFunc) + return + } CommandLine.VarP(newIntSliceValue(value, p), name, shorthand, usage) } // 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 (f *FlagSet) IntSlice(name string, value []int, usage string) *[]int { +func (f *FlagSet) IntSlice(name string, value []int, usage string, validation ...func(value []int) error) *[]int { p := []int{} - f.IntSliceVarP(&p, name, "", value, usage) + f.IntSliceVarP(&p, name, "", value, usage, validation...) return &p } // 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, validation ...func(value []int) error) *[]int { p := []int{} - f.IntSliceVarP(&p, name, shorthand, value, usage) + f.IntSliceVarP(&p, name, shorthand, value, usage, validation...) return &p } // 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 IntSlice(name string, value []int, usage string) *[]int { - return CommandLine.IntSliceP(name, "", value, usage) +func IntSlice(name string, value []int, usage string, validation ...func(value []int) error) *[]int { + return CommandLine.IntSliceP(name, "", value, usage, validation...) } // 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 { - return CommandLine.IntSliceP(name, shorthand, value, usage) +func IntSliceP(name, shorthand string, value []int, usage string, validation ...func(value []int) error) *[]int { + return CommandLine.IntSliceP(name, shorthand, value, usage, validation...) } diff --git a/ip.go b/ip.go index 06b8bcb5..ea43f728 100644 --- a/ip.go +++ b/ip.go @@ -50,48 +50,68 @@ func (f *FlagSet) GetIP(name string) (net.IP, error) { // 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 (f *FlagSet) IPVar(p *net.IP, name string, value net.IP, usage string) { +func (f *FlagSet) IPVar(p *net.IP, name string, value net.IP, usage string, validation ...func(value net.IP) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + f.VarP(newIPValue(value, p), name, "", usage, validationFunc) + return + } f.VarP(newIPValue(value, p), name, "", usage) } // 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, validation ...func(value net.IP) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + f.VarP(newIPValue(value, p), name, shorthand, usage, validationFunc) + return + } f.VarP(newIPValue(value, p), name, shorthand, usage) } // 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 IPVar(p *net.IP, name string, value net.IP, usage string) { +func IPVar(p *net.IP, name string, value net.IP, usage string, validation ...func(value net.IP) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + CommandLine.VarP(newIPValue(value, p), name, "", usage, validationFunc) + return + } CommandLine.VarP(newIPValue(value, p), name, "", usage) } // 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, validation ...func(value net.IP) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + CommandLine.VarP(newIPValue(value, p), name, shorthand, usage, validationFunc) + return + } CommandLine.VarP(newIPValue(value, p), name, shorthand, usage) } // 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 (f *FlagSet) IP(name string, value net.IP, usage string) *net.IP { +func (f *FlagSet) IP(name string, value net.IP, usage string, validation ...func(value net.IP) error) *net.IP { p := new(net.IP) - f.IPVarP(p, name, "", value, usage) + f.IPVarP(p, name, "", value, usage, validation...) return p } // 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, validation ...func(value net.IP) error) *net.IP { p := new(net.IP) - f.IPVarP(p, name, shorthand, value, usage) + f.IPVarP(p, name, shorthand, value, usage, validation...) return p } // 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 IP(name string, value net.IP, usage string) *net.IP { - return CommandLine.IPP(name, "", value, usage) +func IP(name string, value net.IP, usage string, validation ...func(value net.IP) error) *net.IP { + return CommandLine.IPP(name, "", value, usage, validation...) } // 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 { - return CommandLine.IPP(name, shorthand, value, usage) +func IPP(name, shorthand string, value net.IP, usage string, validation ...func(value net.IP) error) *net.IP { + return CommandLine.IPP(name, shorthand, value, usage, validation...) } diff --git a/ip_slice.go b/ip_slice.go index 775faae4..e74edc1e 100644 --- a/ip_slice.go +++ b/ip_slice.go @@ -139,48 +139,68 @@ func (f *FlagSet) GetIPSlice(name string) ([]net.IP, error) { // IPSliceVar defines a ipSlice flag with specified name, default value, and usage string. // The argument p points to a []net.IP variable in which to store the value of the flag. -func (f *FlagSet) IPSliceVar(p *[]net.IP, name string, value []net.IP, usage string) { +func (f *FlagSet) IPSliceVar(p *[]net.IP, name string, value []net.IP, usage string, validation ...func(value []net.IP) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + f.VarP(newIPSliceValue(value, p), name, "", usage, validationFunc) + return + } f.VarP(newIPSliceValue(value, p), name, "", usage) } // IPSliceVarP is like IPSliceVar, but accepts a shorthand letter that can be used after a single dash. -func (f *FlagSet) IPSliceVarP(p *[]net.IP, name, shorthand string, value []net.IP, usage string) { +func (f *FlagSet) IPSliceVarP(p *[]net.IP, name, shorthand string, value []net.IP, usage string, validation ...func(value []net.IP) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + f.VarP(newIPSliceValue(value, p), name, shorthand, usage, validationFunc) + return + } f.VarP(newIPSliceValue(value, p), name, shorthand, usage) } // IPSliceVar defines a []net.IP flag with specified name, default value, and usage string. // The argument p points to a []net.IP variable in which to store the value of the flag. -func IPSliceVar(p *[]net.IP, name string, value []net.IP, usage string) { +func IPSliceVar(p *[]net.IP, name string, value []net.IP, usage string, validation ...func(value []net.IP) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + CommandLine.VarP(newIPSliceValue(value, p), name, "", usage, validationFunc) + return + } CommandLine.VarP(newIPSliceValue(value, p), name, "", usage) } // IPSliceVarP is like IPSliceVar, but accepts a shorthand letter that can be used after a single dash. -func IPSliceVarP(p *[]net.IP, name, shorthand string, value []net.IP, usage string) { +func IPSliceVarP(p *[]net.IP, name, shorthand string, value []net.IP, usage string, validation ...func(value []net.IP) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + CommandLine.VarP(newIPSliceValue(value, p), name, shorthand, usage, validationFunc) + return + } CommandLine.VarP(newIPSliceValue(value, p), name, shorthand, usage) } // IPSlice defines a []net.IP flag with specified name, default value, and usage string. // The return value is the address of a []net.IP variable that stores the value of that flag. -func (f *FlagSet) IPSlice(name string, value []net.IP, usage string) *[]net.IP { +func (f *FlagSet) IPSlice(name string, value []net.IP, usage string, validation ...func(value []net.IP) error) *[]net.IP { p := []net.IP{} - f.IPSliceVarP(&p, name, "", value, usage) + f.IPSliceVarP(&p, name, "", value, usage, validation...) return &p } // IPSliceP is like IPSlice, but accepts a shorthand letter that can be used after a single dash. -func (f *FlagSet) IPSliceP(name, shorthand string, value []net.IP, usage string) *[]net.IP { +func (f *FlagSet) IPSliceP(name, shorthand string, value []net.IP, usage string, validation ...func(value []net.IP) error) *[]net.IP { p := []net.IP{} - f.IPSliceVarP(&p, name, shorthand, value, usage) + f.IPSliceVarP(&p, name, shorthand, value, usage, validation...) return &p } // IPSlice defines a []net.IP flag with specified name, default value, and usage string. // The return value is the address of a []net.IP variable that stores the value of the flag. -func IPSlice(name string, value []net.IP, usage string) *[]net.IP { - return CommandLine.IPSliceP(name, "", value, usage) +func IPSlice(name string, value []net.IP, usage string, validation ...func(value []net.IP) error) *[]net.IP { + return CommandLine.IPSliceP(name, "", value, usage, validation...) } // IPSliceP is like IPSlice, but accepts a shorthand letter that can be used after a single dash. -func IPSliceP(name, shorthand string, value []net.IP, usage string) *[]net.IP { - return CommandLine.IPSliceP(name, shorthand, value, usage) +func IPSliceP(name, shorthand string, value []net.IP, usage string, validation ...func(value []net.IP) error) *[]net.IP { + return CommandLine.IPSliceP(name, shorthand, value, usage, validation...) } diff --git a/ipmask.go b/ipmask.go index 5bd44bd2..288c7e52 100644 --- a/ipmask.go +++ b/ipmask.go @@ -75,48 +75,68 @@ func (f *FlagSet) GetIPv4Mask(name string) (net.IPMask, error) { // 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 (f *FlagSet) IPMaskVar(p *net.IPMask, name string, value net.IPMask, usage string) { +func (f *FlagSet) IPMaskVar(p *net.IPMask, name string, value net.IPMask, usage string, validation ...func(value net.IPMask) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + f.VarP(newIPMaskValue(value, p), name, "", usage, validationFunc) + return + } f.VarP(newIPMaskValue(value, p), name, "", usage) } // 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, validation ...func(value net.IPMask) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + f.VarP(newIPMaskValue(value, p), name, shorthand, usage, validationFunc) + return + } f.VarP(newIPMaskValue(value, p), name, shorthand, usage) } // 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 IPMaskVar(p *net.IPMask, name string, value net.IPMask, usage string) { +func IPMaskVar(p *net.IPMask, name string, value net.IPMask, usage string, validation ...func(value net.IPMask) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + CommandLine.VarP(newIPMaskValue(value, p), name, "", usage, validationFunc) + return + } CommandLine.VarP(newIPMaskValue(value, p), name, "", usage) } // 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, validation ...func(value net.IPMask) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + CommandLine.VarP(newIPMaskValue(value, p), name, shorthand, usage, validationFunc) + return + } CommandLine.VarP(newIPMaskValue(value, p), name, shorthand, usage) } // 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 (f *FlagSet) IPMask(name string, value net.IPMask, usage string) *net.IPMask { +func (f *FlagSet) IPMask(name string, value net.IPMask, usage string, validation ...func(value net.IPMask) error) *net.IPMask { p := new(net.IPMask) - f.IPMaskVarP(p, name, "", value, usage) + f.IPMaskVarP(p, name, "", value, usage, validation...) return p } // 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, validation ...func(value net.IPMask) error) *net.IPMask { p := new(net.IPMask) - f.IPMaskVarP(p, name, shorthand, value, usage) + f.IPMaskVarP(p, name, shorthand, value, usage, validation...) return p } // 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 IPMask(name string, value net.IPMask, usage string) *net.IPMask { - return CommandLine.IPMaskP(name, "", value, usage) +func IPMask(name string, value net.IPMask, usage string, validation ...func(value net.IPMask) error) *net.IPMask { + return CommandLine.IPMaskP(name, "", value, usage, validation...) } // 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 { - return CommandLine.IPMaskP(name, shorthand, value, usage) +func IPMaskP(name, shorthand string, value net.IPMask, usage string, validation ...func(value net.IPMask) error) *net.IPMask { + return CommandLine.IPMaskP(name, shorthand, value, usage, validation...) } diff --git a/ipnet.go b/ipnet.go index e2c1b8bc..3b265c9e 100644 --- a/ipnet.go +++ b/ipnet.go @@ -51,48 +51,64 @@ func (f *FlagSet) GetIPNet(name string) (net.IPNet, error) { // 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 (f *FlagSet) IPNetVar(p *net.IPNet, name string, value net.IPNet, usage string) { +func (f *FlagSet) IPNetVar(p *net.IPNet, name string, value net.IPNet, usage string, validation ...func(value net.IPNet) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]).(func(value interface{}) error) + f.VarP(newIPNetValue(value, p), name, "", usage, validationFunc) + } f.VarP(newIPNetValue(value, p), name, "", usage) } // 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, validation ...func(value net.IPNet) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]).(func(value interface{}) error) + f.VarP(newIPNetValue(value, p), name, shorthand, usage, validationFunc) + } f.VarP(newIPNetValue(value, p), name, shorthand, usage) } // 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 IPNetVar(p *net.IPNet, name string, value net.IPNet, usage string) { +func IPNetVar(p *net.IPNet, name string, value net.IPNet, usage string, validation ...func(value net.IPNet) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]).(func(value interface{}) error) + CommandLine.VarP(newIPNetValue(value, p), name, "", usage, validationFunc) + } CommandLine.VarP(newIPNetValue(value, p), name, "", usage) } // 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, validation ...func(value net.IPNet) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]).(func(value interface{}) error) + CommandLine.VarP(newIPNetValue(value, p), name, shorthand, usage, validationFunc) + } CommandLine.VarP(newIPNetValue(value, p), name, shorthand, usage) } // 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 (f *FlagSet) IPNet(name string, value net.IPNet, usage string) *net.IPNet { +func (f *FlagSet) IPNet(name string, value net.IPNet, usage string, validation ...func(value net.IPNet) error) *net.IPNet { p := new(net.IPNet) - f.IPNetVarP(p, name, "", value, usage) + f.IPNetVarP(p, name, "", value, usage, validation...) return p } // 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, validation ...func(value net.IPNet) error) *net.IPNet { p := new(net.IPNet) - f.IPNetVarP(p, name, shorthand, value, usage) + f.IPNetVarP(p, name, shorthand, value, usage, validation...) return p } // 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 IPNet(name string, value net.IPNet, usage string) *net.IPNet { - return CommandLine.IPNetP(name, "", value, usage) +func IPNet(name string, value net.IPNet, usage string, validation ...func(value net.IPNet) error) *net.IPNet { + return CommandLine.IPNetP(name, "", value, usage, validation...) } // 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 { - return CommandLine.IPNetP(name, shorthand, value, usage) +func IPNetP(name, shorthand string, value net.IPNet, usage string, validation ...func(value net.IPNet) error) *net.IPNet { + return CommandLine.IPNetP(name, shorthand, value, usage, validation...) } diff --git a/ipnet_slice.go b/ipnet_slice.go index 6b541aa8..6f5afb34 100644 --- a/ipnet_slice.go +++ b/ipnet_slice.go @@ -100,48 +100,64 @@ func (f *FlagSet) GetIPNetSlice(name string) ([]net.IPNet, error) { // IPNetSliceVar defines a ipNetSlice flag with specified name, default value, and usage string. // The argument p points to a []net.IPNet variable in which to store the value of the flag. -func (f *FlagSet) IPNetSliceVar(p *[]net.IPNet, name string, value []net.IPNet, usage string) { +func (f *FlagSet) IPNetSliceVar(p *[]net.IPNet, name string, value []net.IPNet, usage string, validation ...func(value []net.IPNet) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]).(func(value interface{}) error) + f.VarP(newIPNetSliceValue(value, p), name, "", usage, validationFunc) + } f.VarP(newIPNetSliceValue(value, p), name, "", usage) } // IPNetSliceVarP is like IPNetSliceVar, but accepts a shorthand letter that can be used after a single dash. -func (f *FlagSet) IPNetSliceVarP(p *[]net.IPNet, name, shorthand string, value []net.IPNet, usage string) { +func (f *FlagSet) IPNetSliceVarP(p *[]net.IPNet, name, shorthand string, value []net.IPNet, usage string, validation ...func(value []net.IPNet) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]).(func(value interface{}) error) + f.VarP(newIPNetSliceValue(value, p), name, shorthand, usage, validationFunc) + } f.VarP(newIPNetSliceValue(value, p), name, shorthand, usage) } // IPNetSliceVar defines a []net.IPNet flag with specified name, default value, and usage string. // The argument p points to a []net.IPNet variable in which to store the value of the flag. -func IPNetSliceVar(p *[]net.IPNet, name string, value []net.IPNet, usage string) { +func IPNetSliceVar(p *[]net.IPNet, name string, value []net.IPNet, usage string, validation ...func(value []net.IPNet) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]).(func(value interface{}) error) + CommandLine.VarP(newIPNetSliceValue(value, p), name, "", usage, validationFunc) + } CommandLine.VarP(newIPNetSliceValue(value, p), name, "", usage) } // IPNetSliceVarP is like IPNetSliceVar, but accepts a shorthand letter that can be used after a single dash. -func IPNetSliceVarP(p *[]net.IPNet, name, shorthand string, value []net.IPNet, usage string) { +func IPNetSliceVarP(p *[]net.IPNet, name, shorthand string, value []net.IPNet, usage string, validation ...func(value []net.IPNet) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]).(func(value interface{}) error) + CommandLine.VarP(newIPNetSliceValue(value, p), name, shorthand, usage, validationFunc) + } CommandLine.VarP(newIPNetSliceValue(value, p), name, shorthand, usage) } // IPNetSlice defines a []net.IPNet flag with specified name, default value, and usage string. // The return value is the address of a []net.IPNet variable that stores the value of that flag. -func (f *FlagSet) IPNetSlice(name string, value []net.IPNet, usage string) *[]net.IPNet { +func (f *FlagSet) IPNetSlice(name string, value []net.IPNet, usage string, validation ...func(value []net.IPNet) error) *[]net.IPNet { p := []net.IPNet{} - f.IPNetSliceVarP(&p, name, "", value, usage) + f.IPNetSliceVarP(&p, name, "", value, usage, validation...) return &p } // IPNetSliceP is like IPNetSlice, but accepts a shorthand letter that can be used after a single dash. -func (f *FlagSet) IPNetSliceP(name, shorthand string, value []net.IPNet, usage string) *[]net.IPNet { +func (f *FlagSet) IPNetSliceP(name, shorthand string, value []net.IPNet, usage string, validation ...func(value []net.IPNet) error) *[]net.IPNet { p := []net.IPNet{} - f.IPNetSliceVarP(&p, name, shorthand, value, usage) + f.IPNetSliceVarP(&p, name, shorthand, value, usage, validation...) return &p } // IPNetSlice defines a []net.IPNet flag with specified name, default value, and usage string. // The return value is the address of a []net.IP variable that stores the value of the flag. -func IPNetSlice(name string, value []net.IPNet, usage string) *[]net.IPNet { - return CommandLine.IPNetSliceP(name, "", value, usage) +func IPNetSlice(name string, value []net.IPNet, usage string, validation ...func(value []net.IPNet) error) *[]net.IPNet { + return CommandLine.IPNetSliceP(name, "", value, usage, validation...) } // IPNetSliceP is like IPNetSlice, but accepts a shorthand letter that can be used after a single dash. -func IPNetSliceP(name, shorthand string, value []net.IPNet, usage string) *[]net.IPNet { - return CommandLine.IPNetSliceP(name, shorthand, value, usage) +func IPNetSliceP(name, shorthand string, value []net.IPNet, usage string, validation ...func(value []net.IPNet) error) *[]net.IPNet { + return CommandLine.IPNetSliceP(name, shorthand, value, usage, validation...) } diff --git a/string.go b/string.go index 04e0a26f..56559835 100644 --- a/string.go +++ b/string.go @@ -33,48 +33,68 @@ func (f *FlagSet) GetString(name string) (string, error) { // 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 (f *FlagSet) StringVar(p *string, name string, value string, usage string) { +func (f *FlagSet) StringVar(p *string, name string, value string, usage string, validation ...func(value string) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + f.VarP(newStringValue(value, p), name, "", usage, validationFunc) + return + } f.VarP(newStringValue(value, p), name, "", usage) } // 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, validation ...func(value string) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + f.VarP(newStringValue(value, p), name, shorthand, usage, validationFunc) + return + } f.VarP(newStringValue(value, p), name, shorthand, usage) } // 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 StringVar(p *string, name string, value string, usage string) { +func StringVar(p *string, name string, value string, usage string, validation ...func(value string) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + CommandLine.VarP(newStringValue(value, p), name, "", usage, validationFunc) + return + } CommandLine.VarP(newStringValue(value, p), name, "", usage) } // 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, validation ...func(value string) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + CommandLine.VarP(newStringValue(value, p), name, shorthand, usage, validationFunc) + return + } CommandLine.VarP(newStringValue(value, p), name, shorthand, usage) } // 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 (f *FlagSet) String(name string, value string, usage string) *string { +func (f *FlagSet) String(name string, value string, usage string, validation ...func(value string) error) *string { p := new(string) - f.StringVarP(p, name, "", value, usage) + f.StringVarP(p, name, "", value, usage, validation...) return p } // 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, validation ...func(value string) error) *string { p := new(string) - f.StringVarP(p, name, shorthand, value, usage) + f.StringVarP(p, name, shorthand, value, usage, validation...) return p } // 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 String(name string, value string, usage string) *string { - return CommandLine.StringP(name, "", value, usage) +func String(name string, value string, usage string, validation ...func(value string) error) *string { + return CommandLine.StringP(name, "", value, usage, validation...) } // 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 { - return CommandLine.StringP(name, shorthand, value, usage) +func StringP(name, shorthand string, value string, usage string, validation ...func(value string) error) *string { + return CommandLine.StringP(name, shorthand, value, usage, validation...) } diff --git a/string_array.go b/string_array.go index d1ff0a96..6dfcf3d7 100644 --- a/string_array.go +++ b/string_array.go @@ -75,51 +75,71 @@ func (f *FlagSet) GetStringArray(name string) ([]string, error) { // StringArrayVar 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 values of the multiple flags. // The value of each argument will not try to be separated by comma. Use a StringSlice for that. -func (f *FlagSet) StringArrayVar(p *[]string, name string, value []string, usage string) { +func (f *FlagSet) StringArrayVar(p *[]string, name string, value []string, usage string, validation ...func(value []string) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + f.VarP(newStringArrayValue(value, p), name, "", usage, validationFunc) + return + } f.VarP(newStringArrayValue(value, p), name, "", usage) } // StringArrayVarP is like StringArrayVar, but accepts a shorthand letter that can be used after a single dash. -func (f *FlagSet) StringArrayVarP(p *[]string, name, shorthand string, value []string, usage string) { +func (f *FlagSet) StringArrayVarP(p *[]string, name, shorthand string, value []string, usage string, validation ...func(value []string) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + f.VarP(newStringArrayValue(value, p), name, shorthand, usage, validationFunc) + return + } f.VarP(newStringArrayValue(value, p), name, shorthand, usage) } // StringArrayVar 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. // The value of each argument will not try to be separated by comma. Use a StringSlice for that. -func StringArrayVar(p *[]string, name string, value []string, usage string) { +func StringArrayVar(p *[]string, name string, value []string, usage string, validation ...func(value []string) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + CommandLine.VarP(newStringArrayValue(value, p), name, "", usage, validationFunc) + return + } CommandLine.VarP(newStringArrayValue(value, p), name, "", usage) } // StringArrayVarP is like StringArrayVar, but accepts a shorthand letter that can be used after a single dash. -func StringArrayVarP(p *[]string, name, shorthand string, value []string, usage string) { +func StringArrayVarP(p *[]string, name, shorthand string, value []string, usage string, validation ...func(value []string) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + CommandLine.VarP(newStringArrayValue(value, p), name, shorthand, usage, validationFunc) + return + } CommandLine.VarP(newStringArrayValue(value, p), name, shorthand, usage) } // StringArray 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 value of each argument will not try to be separated by comma. Use a StringSlice for that. -func (f *FlagSet) StringArray(name string, value []string, usage string) *[]string { +func (f *FlagSet) StringArray(name string, value []string, usage string, validation ...func(value []string) error) *[]string { p := []string{} - f.StringArrayVarP(&p, name, "", value, usage) + f.StringArrayVarP(&p, name, "", value, usage, validation...) return &p } // StringArrayP is like StringArray, but accepts a shorthand letter that can be used after a single dash. -func (f *FlagSet) StringArrayP(name, shorthand string, value []string, usage string) *[]string { +func (f *FlagSet) StringArrayP(name, shorthand string, value []string, usage string, validation ...func(value []string) error) *[]string { p := []string{} - f.StringArrayVarP(&p, name, shorthand, value, usage) + f.StringArrayVarP(&p, name, shorthand, value, usage, validation...) return &p } // StringArray 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 value of each argument will not try to be separated by comma. Use a StringSlice for that. -func StringArray(name string, value []string, usage string) *[]string { - return CommandLine.StringArrayP(name, "", value, usage) +func StringArray(name string, value []string, usage string, validation ...func(value []string) error) *[]string { + return CommandLine.StringArrayP(name, "", value, usage, validation...) } // StringArrayP is like StringArray, but accepts a shorthand letter that can be used after a single dash. -func StringArrayP(name, shorthand string, value []string, usage string) *[]string { - return CommandLine.StringArrayP(name, shorthand, value, usage) +func StringArrayP(name, shorthand string, value []string, usage string, validation ...func(value []string) error) *[]string { + return CommandLine.StringArrayP(name, shorthand, value, usage, validation...) } diff --git a/string_slice.go b/string_slice.go index 3cb2e69d..6c42176c 100644 --- a/string_slice.go +++ b/string_slice.go @@ -98,15 +98,28 @@ func (f *FlagSet) GetStringSlice(name string) ([]string, error) { // The argument p points to a []string variable in which to store the value of the flag. // Compared to StringArray flags, StringSlice flags take comma-separated value as arguments and split them accordingly. // For example: -// --ss="v1,v2" --ss="v3" +// +// --ss="v1,v2" --ss="v3" +// // will result in -// []string{"v1", "v2", "v3"} -func (f *FlagSet) StringSliceVar(p *[]string, name string, value []string, usage string) { +// +// []string{"v1", "v2", "v3"} +func (f *FlagSet) StringSliceVar(p *[]string, name string, value []string, usage string, validation ...func(value []string) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + f.VarP(newStringSliceValue(value, p), name, "", usage, validationFunc) + return + } f.VarP(newStringSliceValue(value, p), name, "", usage) } // 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, validation ...func(value []string) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + f.VarP(newStringSliceValue(value, p), name, shorthand, usage, validationFunc) + return + } f.VarP(newStringSliceValue(value, p), name, shorthand, usage) } @@ -114,15 +127,28 @@ func (f *FlagSet) StringSliceVarP(p *[]string, name, shorthand string, value []s // The argument p points to a []string variable in which to store the value of the flag. // Compared to StringArray flags, StringSlice flags take comma-separated value as arguments and split them accordingly. // For example: -// --ss="v1,v2" --ss="v3" +// +// --ss="v1,v2" --ss="v3" +// // will result in -// []string{"v1", "v2", "v3"} -func StringSliceVar(p *[]string, name string, value []string, usage string) { +// +// []string{"v1", "v2", "v3"} +func StringSliceVar(p *[]string, name string, value []string, usage string, validation ...func(value []string) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + CommandLine.VarP(newStringSliceValue(value, p), name, "", usage, validationFunc) + return + } CommandLine.VarP(newStringSliceValue(value, p), name, "", usage) } // 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, validation ...func(value []string) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + CommandLine.VarP(newStringSliceValue(value, p), name, shorthand, usage, validationFunc) + return + } CommandLine.VarP(newStringSliceValue(value, p), name, shorthand, usage) } @@ -130,19 +156,22 @@ func StringSliceVarP(p *[]string, name, shorthand string, value []string, usage // The return value is the address of a []string variable that stores the value of the flag. // Compared to StringArray flags, StringSlice flags take comma-separated value as arguments and split them accordingly. // For example: -// --ss="v1,v2" --ss="v3" +// +// --ss="v1,v2" --ss="v3" +// // will result in -// []string{"v1", "v2", "v3"} -func (f *FlagSet) StringSlice(name string, value []string, usage string) *[]string { +// +// []string{"v1", "v2", "v3"} +func (f *FlagSet) StringSlice(name string, value []string, usage string, validation ...func(value []string) error) *[]string { p := []string{} - f.StringSliceVarP(&p, name, "", value, usage) + f.StringSliceVarP(&p, name, "", value, usage, validation...) return &p } // 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, validation ...func(value []string) error) *[]string { p := []string{} - f.StringSliceVarP(&p, name, shorthand, value, usage) + f.StringSliceVarP(&p, name, shorthand, value, usage, validation...) return &p } @@ -150,14 +179,17 @@ func (f *FlagSet) StringSliceP(name, shorthand string, value []string, usage str // The return value is the address of a []string variable that stores the value of the flag. // Compared to StringArray flags, StringSlice flags take comma-separated value as arguments and split them accordingly. // For example: -// --ss="v1,v2" --ss="v3" +// +// --ss="v1,v2" --ss="v3" +// // will result in -// []string{"v1", "v2", "v3"} -func StringSlice(name string, value []string, usage string) *[]string { - return CommandLine.StringSliceP(name, "", value, usage) +// +// []string{"v1", "v2", "v3"} +func StringSlice(name string, value []string, usage string, validation ...func(value []string) error) *[]string { + return CommandLine.StringSliceP(name, "", value, usage, validation...) } // 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 { - return CommandLine.StringSliceP(name, shorthand, value, usage) +func StringSliceP(name, shorthand string, value []string, usage string, validation ...func(value []string) error) *[]string { + return CommandLine.StringSliceP(name, shorthand, value, usage, validation...) } diff --git a/string_to_int.go b/string_to_int.go index 5ceda396..18a5fb48 100644 --- a/string_to_int.go +++ b/string_to_int.go @@ -99,51 +99,71 @@ func (f *FlagSet) GetStringToInt(name string) (map[string]int, error) { // StringToIntVar defines a string flag with specified name, default value, and usage string. // The argument p points to a map[string]int variable in which to store the values of the multiple flags. // The value of each argument will not try to be separated by comma -func (f *FlagSet) StringToIntVar(p *map[string]int, name string, value map[string]int, usage string) { +func (f *FlagSet) StringToIntVar(p *map[string]int, name string, value map[string]int, usage string, validation ...func(value map[string]int) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + f.VarP(newStringToIntValue(value, p), name, "", usage, validationFunc) + return + } f.VarP(newStringToIntValue(value, p), name, "", usage) } // StringToIntVarP is like StringToIntVar, but accepts a shorthand letter that can be used after a single dash. -func (f *FlagSet) StringToIntVarP(p *map[string]int, name, shorthand string, value map[string]int, usage string) { +func (f *FlagSet) StringToIntVarP(p *map[string]int, name, shorthand string, value map[string]int, usage string, validation ...func(value map[string]int) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + f.VarP(newStringToIntValue(value, p), name, shorthand, usage, validationFunc) + return + } f.VarP(newStringToIntValue(value, p), name, shorthand, usage) } // StringToIntVar defines a string flag with specified name, default value, and usage string. // The argument p points to a map[string]int variable in which to store the value of the flag. // The value of each argument will not try to be separated by comma -func StringToIntVar(p *map[string]int, name string, value map[string]int, usage string) { +func StringToIntVar(p *map[string]int, name string, value map[string]int, usage string, validation ...func(value map[string]int) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + CommandLine.VarP(newStringToIntValue(value, p), name, "", usage, validationFunc) + return + } CommandLine.VarP(newStringToIntValue(value, p), name, "", usage) } // StringToIntVarP is like StringToIntVar, but accepts a shorthand letter that can be used after a single dash. -func StringToIntVarP(p *map[string]int, name, shorthand string, value map[string]int, usage string) { +func StringToIntVarP(p *map[string]int, name, shorthand string, value map[string]int, usage string, validation ...func(value map[string]int) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + CommandLine.VarP(newStringToIntValue(value, p), name, shorthand, usage, validationFunc) + return + } CommandLine.VarP(newStringToIntValue(value, p), name, shorthand, usage) } // StringToInt defines a string flag with specified name, default value, and usage string. // The return value is the address of a map[string]int variable that stores the value of the flag. // The value of each argument will not try to be separated by comma -func (f *FlagSet) StringToInt(name string, value map[string]int, usage string) *map[string]int { +func (f *FlagSet) StringToInt(name string, value map[string]int, usage string, validation ...func(value map[string]int) error) *map[string]int { p := map[string]int{} - f.StringToIntVarP(&p, name, "", value, usage) + f.StringToIntVarP(&p, name, "", value, usage, validation...) return &p } // StringToIntP is like StringToInt, but accepts a shorthand letter that can be used after a single dash. -func (f *FlagSet) StringToIntP(name, shorthand string, value map[string]int, usage string) *map[string]int { +func (f *FlagSet) StringToIntP(name, shorthand string, value map[string]int, usage string, validation ...func(value map[string]int) error) *map[string]int { p := map[string]int{} - f.StringToIntVarP(&p, name, shorthand, value, usage) + f.StringToIntVarP(&p, name, shorthand, value, usage, validation...) return &p } // StringToInt defines a string flag with specified name, default value, and usage string. // The return value is the address of a map[string]int variable that stores the value of the flag. // The value of each argument will not try to be separated by comma -func StringToInt(name string, value map[string]int, usage string) *map[string]int { - return CommandLine.StringToIntP(name, "", value, usage) +func StringToInt(name string, value map[string]int, usage string, validation ...func(value map[string]int) error) *map[string]int { + return CommandLine.StringToIntP(name, "", value, usage, validation...) } // StringToIntP is like StringToInt, but accepts a shorthand letter that can be used after a single dash. -func StringToIntP(name, shorthand string, value map[string]int, usage string) *map[string]int { - return CommandLine.StringToIntP(name, shorthand, value, usage) +func StringToIntP(name, shorthand string, value map[string]int, usage string, validation ...func(value map[string]int) error) *map[string]int { + return CommandLine.StringToIntP(name, shorthand, value, usage, validation...) } diff --git a/string_to_int64.go b/string_to_int64.go index a807a04a..fa83bb2d 100644 --- a/string_to_int64.go +++ b/string_to_int64.go @@ -99,51 +99,71 @@ func (f *FlagSet) GetStringToInt64(name string) (map[string]int64, error) { // StringToInt64Var defines a string flag with specified name, default value, and usage string. // The argument p point64s to a map[string]int64 variable in which to store the values of the multiple flags. // The value of each argument will not try to be separated by comma -func (f *FlagSet) StringToInt64Var(p *map[string]int64, name string, value map[string]int64, usage string) { +func (f *FlagSet) StringToInt64Var(p *map[string]int64, name string, value map[string]int64, usage string, validation ...func(value map[string]int64) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + f.VarP(newStringToInt64Value(value, p), name, "", usage, validationFunc) + return + } f.VarP(newStringToInt64Value(value, p), name, "", usage) } // StringToInt64VarP is like StringToInt64Var, but accepts a shorthand letter that can be used after a single dash. -func (f *FlagSet) StringToInt64VarP(p *map[string]int64, name, shorthand string, value map[string]int64, usage string) { +func (f *FlagSet) StringToInt64VarP(p *map[string]int64, name, shorthand string, value map[string]int64, usage string, validation ...func(value map[string]int64) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + f.VarP(newStringToInt64Value(value, p), name, shorthand, usage, validationFunc) + return + } f.VarP(newStringToInt64Value(value, p), name, shorthand, usage) } // StringToInt64Var defines a string flag with specified name, default value, and usage string. // The argument p point64s to a map[string]int64 variable in which to store the value of the flag. // The value of each argument will not try to be separated by comma -func StringToInt64Var(p *map[string]int64, name string, value map[string]int64, usage string) { +func StringToInt64Var(p *map[string]int64, name string, value map[string]int64, usage string, validation ...func(value map[string]int64) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + CommandLine.VarP(newStringToInt64Value(value, p), name, "", usage, validationFunc) + return + } CommandLine.VarP(newStringToInt64Value(value, p), name, "", usage) } // StringToInt64VarP is like StringToInt64Var, but accepts a shorthand letter that can be used after a single dash. -func StringToInt64VarP(p *map[string]int64, name, shorthand string, value map[string]int64, usage string) { +func StringToInt64VarP(p *map[string]int64, name, shorthand string, value map[string]int64, usage string, validation ...func(value map[string]int64) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + CommandLine.VarP(newStringToInt64Value(value, p), name, shorthand, usage, validationFunc) + return + } CommandLine.VarP(newStringToInt64Value(value, p), name, shorthand, usage) } // StringToInt64 defines a string flag with specified name, default value, and usage string. // The return value is the address of a map[string]int64 variable that stores the value of the flag. // The value of each argument will not try to be separated by comma -func (f *FlagSet) StringToInt64(name string, value map[string]int64, usage string) *map[string]int64 { +func (f *FlagSet) StringToInt64(name string, value map[string]int64, usage string, validation ...func(value map[string]int64) error) *map[string]int64 { p := map[string]int64{} - f.StringToInt64VarP(&p, name, "", value, usage) + f.StringToInt64VarP(&p, name, "", value, usage, validation...) return &p } // StringToInt64P is like StringToInt64, but accepts a shorthand letter that can be used after a single dash. -func (f *FlagSet) StringToInt64P(name, shorthand string, value map[string]int64, usage string) *map[string]int64 { +func (f *FlagSet) StringToInt64P(name, shorthand string, value map[string]int64, usage string, validation ...func(value map[string]int64) error) *map[string]int64 { p := map[string]int64{} - f.StringToInt64VarP(&p, name, shorthand, value, usage) + f.StringToInt64VarP(&p, name, shorthand, value, usage, validation...) return &p } // StringToInt64 defines a string flag with specified name, default value, and usage string. // The return value is the address of a map[string]int64 variable that stores the value of the flag. // The value of each argument will not try to be separated by comma -func StringToInt64(name string, value map[string]int64, usage string) *map[string]int64 { - return CommandLine.StringToInt64P(name, "", value, usage) +func StringToInt64(name string, value map[string]int64, usage string, validation ...func(value map[string]int64) error) *map[string]int64 { + return CommandLine.StringToInt64P(name, "", value, usage, validation...) } // StringToInt64P is like StringToInt64, but accepts a shorthand letter that can be used after a single dash. -func StringToInt64P(name, shorthand string, value map[string]int64, usage string) *map[string]int64 { - return CommandLine.StringToInt64P(name, shorthand, value, usage) +func StringToInt64P(name, shorthand string, value map[string]int64, usage string, validation ...func(value map[string]int64) error) *map[string]int64 { + return CommandLine.StringToInt64P(name, shorthand, value, usage, validation...) } diff --git a/string_to_string.go b/string_to_string.go index 890a01af..bedd471f 100644 --- a/string_to_string.go +++ b/string_to_string.go @@ -110,51 +110,71 @@ func (f *FlagSet) GetStringToString(name string) (map[string]string, error) { // StringToStringVar defines a string flag with specified name, default value, and usage string. // The argument p points to a map[string]string variable in which to store the values of the multiple flags. // The value of each argument will not try to be separated by comma -func (f *FlagSet) StringToStringVar(p *map[string]string, name string, value map[string]string, usage string) { +func (f *FlagSet) StringToStringVar(p *map[string]string, name string, value map[string]string, usage string, validation ...func(value map[string]string) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + f.VarP(newStringToStringValue(value, p), name, "", usage, validationFunc) + return + } f.VarP(newStringToStringValue(value, p), name, "", usage) } // StringToStringVarP is like StringToStringVar, but accepts a shorthand letter that can be used after a single dash. -func (f *FlagSet) StringToStringVarP(p *map[string]string, name, shorthand string, value map[string]string, usage string) { +func (f *FlagSet) StringToStringVarP(p *map[string]string, name, shorthand string, value map[string]string, usage string, validation ...func(value map[string]string) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + f.VarP(newStringToStringValue(value, p), name, shorthand, usage, validationFunc) + return + } f.VarP(newStringToStringValue(value, p), name, shorthand, usage) } // StringToStringVar defines a string flag with specified name, default value, and usage string. // The argument p points to a map[string]string variable in which to store the value of the flag. // The value of each argument will not try to be separated by comma -func StringToStringVar(p *map[string]string, name string, value map[string]string, usage string) { +func StringToStringVar(p *map[string]string, name string, value map[string]string, usage string, validation ...func(value map[string]string) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + CommandLine.VarP(newStringToStringValue(value, p), name, "", usage, validationFunc) + return + } CommandLine.VarP(newStringToStringValue(value, p), name, "", usage) } // StringToStringVarP is like StringToStringVar, but accepts a shorthand letter that can be used after a single dash. -func StringToStringVarP(p *map[string]string, name, shorthand string, value map[string]string, usage string) { +func StringToStringVarP(p *map[string]string, name, shorthand string, value map[string]string, usage string, validation ...func(value map[string]string) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + CommandLine.VarP(newStringToStringValue(value, p), name, shorthand, usage, validationFunc) + return + } CommandLine.VarP(newStringToStringValue(value, p), name, shorthand, usage) } // StringToString defines a string flag with specified name, default value, and usage string. // The return value is the address of a map[string]string variable that stores the value of the flag. // The value of each argument will not try to be separated by comma -func (f *FlagSet) StringToString(name string, value map[string]string, usage string) *map[string]string { +func (f *FlagSet) StringToString(name string, value map[string]string, usage string, validation ...func(value map[string]string) error) *map[string]string { p := map[string]string{} - f.StringToStringVarP(&p, name, "", value, usage) + f.StringToStringVarP(&p, name, "", value, usage, validation...) return &p } // StringToStringP is like StringToString, but accepts a shorthand letter that can be used after a single dash. -func (f *FlagSet) StringToStringP(name, shorthand string, value map[string]string, usage string) *map[string]string { +func (f *FlagSet) StringToStringP(name, shorthand string, value map[string]string, usage string, validation ...func(value map[string]string) error) *map[string]string { p := map[string]string{} - f.StringToStringVarP(&p, name, shorthand, value, usage) + f.StringToStringVarP(&p, name, shorthand, value, usage, validation...) return &p } // StringToString defines a string flag with specified name, default value, and usage string. // The return value is the address of a map[string]string variable that stores the value of the flag. // The value of each argument will not try to be separated by comma -func StringToString(name string, value map[string]string, usage string) *map[string]string { - return CommandLine.StringToStringP(name, "", value, usage) +func StringToString(name string, value map[string]string, usage string, validation ...func(value map[string]string) error) *map[string]string { + return CommandLine.StringToStringP(name, "", value, usage, validation...) } // StringToStringP is like StringToString, but accepts a shorthand letter that can be used after a single dash. -func StringToStringP(name, shorthand string, value map[string]string, usage string) *map[string]string { - return CommandLine.StringToStringP(name, shorthand, value, usage) +func StringToStringP(name, shorthand string, value map[string]string, usage string, validation ...func(value map[string]string) error) *map[string]string { + return CommandLine.StringToStringP(name, shorthand, value, usage, validation...) } diff --git a/uint.go b/uint.go index dcbc2b75..30296711 100644 --- a/uint.go +++ b/uint.go @@ -41,48 +41,68 @@ func (f *FlagSet) GetUint(name string) (uint, error) { // 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 (f *FlagSet) UintVar(p *uint, name string, value uint, usage string) { +func (f *FlagSet) UintVar(p *uint, name string, value uint, usage string, validation ...func(value uint) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + f.VarP(newUintValue(value, p), name, "", usage, validationFunc) + return + } f.VarP(newUintValue(value, p), name, "", usage) } // 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, validation ...func(value uint) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + f.VarP(newUintValue(value, p), name, shorthand, usage, validationFunc) + return + } f.VarP(newUintValue(value, p), name, shorthand, usage) } // 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 UintVar(p *uint, name string, value uint, usage string) { +func UintVar(p *uint, name string, value uint, usage string, validation ...func(value uint) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + CommandLine.VarP(newUintValue(value, p), name, "", usage, validationFunc) + return + } CommandLine.VarP(newUintValue(value, p), name, "", usage) } // 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, validation ...func(value uint) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + CommandLine.VarP(newUintValue(value, p), name, shorthand, usage, validationFunc) + return + } CommandLine.VarP(newUintValue(value, p), name, shorthand, usage) } // 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 (f *FlagSet) Uint(name string, value uint, usage string) *uint { +func (f *FlagSet) Uint(name string, value uint, usage string, validation ...func(value uint) error) *uint { p := new(uint) - f.UintVarP(p, name, "", value, usage) + f.UintVarP(p, name, "", value, usage, validation...) return p } // 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, validation ...func(value uint) error) *uint { p := new(uint) - f.UintVarP(p, name, shorthand, value, usage) + f.UintVarP(p, name, shorthand, value, usage, validation...) return p } // 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 Uint(name string, value uint, usage string) *uint { - return CommandLine.UintP(name, "", value, usage) +func Uint(name string, value uint, usage string, validation ...func(value uint) error) *uint { + return CommandLine.UintP(name, "", value, usage, validation...) } // 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 { - return CommandLine.UintP(name, shorthand, value, usage) +func UintP(name, shorthand string, value uint, usage string, validation ...func(value uint) error) *uint { + return CommandLine.UintP(name, shorthand, value, usage, validation...) } diff --git a/uint16.go b/uint16.go index 7e9914ed..2549eecc 100644 --- a/uint16.go +++ b/uint16.go @@ -41,48 +41,68 @@ func (f *FlagSet) GetUint16(name string) (uint16, error) { // 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 (f *FlagSet) Uint16Var(p *uint16, name string, value uint16, usage string) { +func (f *FlagSet) Uint16Var(p *uint16, name string, value uint16, usage string, validation ...func(value uint16) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + f.VarP(newUint16Value(value, p), name, "", usage, validationFunc) + return + } f.VarP(newUint16Value(value, p), name, "", usage) } // 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, validation ...func(value uint16) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + f.VarP(newUint16Value(value, p), name, shorthand, usage, validationFunc) + return + } f.VarP(newUint16Value(value, p), name, shorthand, usage) } // 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 Uint16Var(p *uint16, name string, value uint16, usage string) { +func Uint16Var(p *uint16, name string, value uint16, usage string, validation ...func(value uint16) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + CommandLine.VarP(newUint16Value(value, p), name, "", usage, validationFunc) + return + } CommandLine.VarP(newUint16Value(value, p), name, "", usage) } // 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, validation ...func(value uint16) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + CommandLine.VarP(newUint16Value(value, p), name, shorthand, usage, validationFunc) + return + } CommandLine.VarP(newUint16Value(value, p), name, shorthand, usage) } // 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 (f *FlagSet) Uint16(name string, value uint16, usage string) *uint16 { +func (f *FlagSet) Uint16(name string, value uint16, usage string, validation ...func(value uint16) error) *uint16 { p := new(uint16) - f.Uint16VarP(p, name, "", value, usage) + f.Uint16VarP(p, name, "", value, usage, validation...) return p } // 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, validation ...func(value uint16) error) *uint16 { p := new(uint16) - f.Uint16VarP(p, name, shorthand, value, usage) + f.Uint16VarP(p, name, shorthand, value, usage, validation...) return p } // 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 Uint16(name string, value uint16, usage string) *uint16 { - return CommandLine.Uint16P(name, "", value, usage) +func Uint16(name string, value uint16, usage string, validation ...func(value uint16) error) *uint16 { + return CommandLine.Uint16P(name, "", value, usage, validation...) } // 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 { - return CommandLine.Uint16P(name, shorthand, value, usage) +func Uint16P(name, shorthand string, value uint16, usage string, validation ...func(value uint16) error) *uint16 { + return CommandLine.Uint16P(name, shorthand, value, usage, validation...) } diff --git a/uint32.go b/uint32.go index d8024539..4f29e77e 100644 --- a/uint32.go +++ b/uint32.go @@ -41,48 +41,68 @@ func (f *FlagSet) GetUint32(name string) (uint32, error) { // 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 (f *FlagSet) Uint32Var(p *uint32, name string, value uint32, usage string) { +func (f *FlagSet) Uint32Var(p *uint32, name string, value uint32, usage string, validation ...func(value uint32) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + f.VarP(newUint32Value(value, p), name, "", usage, validationFunc) + return + } f.VarP(newUint32Value(value, p), name, "", usage) } // 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, validation ...func(value uint32) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + f.VarP(newUint32Value(value, p), name, shorthand, usage, validationFunc) + return + } f.VarP(newUint32Value(value, p), name, shorthand, usage) } // 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 Uint32Var(p *uint32, name string, value uint32, usage string) { +func Uint32Var(p *uint32, name string, value uint32, usage string, validation ...func(value uint32) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + CommandLine.VarP(newUint32Value(value, p), name, "", usage, validationFunc) + return + } CommandLine.VarP(newUint32Value(value, p), name, "", usage) } // 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, validation ...func(value uint32) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + CommandLine.VarP(newUint32Value(value, p), name, shorthand, usage, validationFunc) + return + } CommandLine.VarP(newUint32Value(value, p), name, shorthand, usage) } // 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 (f *FlagSet) Uint32(name string, value uint32, usage string) *uint32 { +func (f *FlagSet) Uint32(name string, value uint32, usage string, validation ...func(value uint32) error) *uint32 { p := new(uint32) - f.Uint32VarP(p, name, "", value, usage) + f.Uint32VarP(p, name, "", value, usage, validation...) return p } // 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, validation ...func(value uint32) error) *uint32 { p := new(uint32) - f.Uint32VarP(p, name, shorthand, value, usage) + f.Uint32VarP(p, name, shorthand, value, usage, validation...) return p } // 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 Uint32(name string, value uint32, usage string) *uint32 { - return CommandLine.Uint32P(name, "", value, usage) +func Uint32(name string, value uint32, usage string, validation ...func(value uint32) error) *uint32 { + return CommandLine.Uint32P(name, "", value, usage, validation...) } // 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 { - return CommandLine.Uint32P(name, shorthand, value, usage) +func Uint32P(name, shorthand string, value uint32, usage string, validation ...func(value uint32) error) *uint32 { + return CommandLine.Uint32P(name, shorthand, value, usage, validation...) } diff --git a/uint64.go b/uint64.go index f62240f2..1cd118c4 100644 --- a/uint64.go +++ b/uint64.go @@ -41,48 +41,68 @@ func (f *FlagSet) GetUint64(name string) (uint64, error) { // 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 (f *FlagSet) Uint64Var(p *uint64, name string, value uint64, usage string) { +func (f *FlagSet) Uint64Var(p *uint64, name string, value uint64, usage string, validation ...func(value uint64) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + f.VarP(newUint64Value(value, p), name, "", usage, validationFunc) + return + } f.VarP(newUint64Value(value, p), name, "", usage) } // 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, validation ...func(value uint64) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + f.VarP(newUint64Value(value, p), name, shorthand, usage, validationFunc) + return + } f.VarP(newUint64Value(value, p), name, shorthand, usage) } // 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 Uint64Var(p *uint64, name string, value uint64, usage string) { +func Uint64Var(p *uint64, name string, value uint64, usage string, validation ...func(value uint64) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + CommandLine.VarP(newUint64Value(value, p), name, "", usage, validationFunc) + return + } CommandLine.VarP(newUint64Value(value, p), name, "", usage) } // 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, validation ...func(value uint64) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + CommandLine.VarP(newUint64Value(value, p), name, shorthand, usage, validationFunc) + return + } CommandLine.VarP(newUint64Value(value, p), name, shorthand, usage) } // 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 (f *FlagSet) Uint64(name string, value uint64, usage string) *uint64 { +func (f *FlagSet) Uint64(name string, value uint64, usage string, validation ...func(value uint64) error) *uint64 { p := new(uint64) - f.Uint64VarP(p, name, "", value, usage) + f.Uint64VarP(p, name, "", value, usage, validation...) return p } // 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, validation ...func(value uint64) error) *uint64 { p := new(uint64) - f.Uint64VarP(p, name, shorthand, value, usage) + f.Uint64VarP(p, name, shorthand, value, usage, validation...) return p } // 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 Uint64(name string, value uint64, usage string) *uint64 { - return CommandLine.Uint64P(name, "", value, usage) +func Uint64(name string, value uint64, usage string, validation ...func(value uint64) error) *uint64 { + return CommandLine.Uint64P(name, "", value, usage, validation...) } // 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 { - return CommandLine.Uint64P(name, shorthand, value, usage) +func Uint64P(name, shorthand string, value uint64, usage string, validation ...func(value uint64) error) *uint64 { + return CommandLine.Uint64P(name, shorthand, value, usage, validation...) } diff --git a/uint8.go b/uint8.go index bb0e83c1..af93f4da 100644 --- a/uint8.go +++ b/uint8.go @@ -41,48 +41,68 @@ func (f *FlagSet) GetUint8(name string) (uint8, error) { // 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 (f *FlagSet) Uint8Var(p *uint8, name string, value uint8, usage string) { +func (f *FlagSet) Uint8Var(p *uint8, name string, value uint8, usage string, validation ...func(value uint8) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + f.VarP(newUint8Value(value, p), name, "", usage, validationFunc) + return + } f.VarP(newUint8Value(value, p), name, "", usage) } // 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, validation ...func(value uint8) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + f.VarP(newUint8Value(value, p), name, shorthand, usage, validationFunc) + return + } f.VarP(newUint8Value(value, p), name, shorthand, usage) } // 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 Uint8Var(p *uint8, name string, value uint8, usage string) { +func Uint8Var(p *uint8, name string, value uint8, usage string, validation ...func(value uint8) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + CommandLine.VarP(newUint8Value(value, p), name, "", usage, validationFunc) + return + } CommandLine.VarP(newUint8Value(value, p), name, "", usage) } // 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, validation ...func(value uint8) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + CommandLine.VarP(newUint8Value(value, p), name, shorthand, usage, validationFunc) + return + } CommandLine.VarP(newUint8Value(value, p), name, shorthand, usage) } // 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 (f *FlagSet) Uint8(name string, value uint8, usage string) *uint8 { +func (f *FlagSet) Uint8(name string, value uint8, usage string, validation ...func(value uint8) error) *uint8 { p := new(uint8) - f.Uint8VarP(p, name, "", value, usage) + f.Uint8VarP(p, name, "", value, usage, validation...) return p } // 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, validation ...func(value uint8) error) *uint8 { p := new(uint8) - f.Uint8VarP(p, name, shorthand, value, usage) + f.Uint8VarP(p, name, shorthand, value, usage, validation...) return p } // 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 Uint8(name string, value uint8, usage string) *uint8 { - return CommandLine.Uint8P(name, "", value, usage) +func Uint8(name string, value uint8, usage string, validation ...func(value uint8) error) *uint8 { + return CommandLine.Uint8P(name, "", value, usage, validation...) } // 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 { - return CommandLine.Uint8P(name, shorthand, value, usage) +func Uint8P(name, shorthand string, value uint8, usage string, validation ...func(value uint8) error) *uint8 { + return CommandLine.Uint8P(name, shorthand, value, usage, validation...) } diff --git a/uint_slice.go b/uint_slice.go index 5fa92483..3b54992b 100644 --- a/uint_slice.go +++ b/uint_slice.go @@ -121,48 +121,68 @@ func (f *FlagSet) GetUintSlice(name string) ([]uint, error) { // UintSliceVar defines a uintSlice 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 (f *FlagSet) UintSliceVar(p *[]uint, name string, value []uint, usage string) { +func (f *FlagSet) UintSliceVar(p *[]uint, name string, value []uint, usage string, validation ...func(value []uint) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + f.VarP(newUintSliceValue(value, p), name, "", usage, validationFunc) + return + } f.VarP(newUintSliceValue(value, p), name, "", usage) } // UintSliceVarP is like UintSliceVar, but accepts a shorthand letter that can be used after a single dash. -func (f *FlagSet) UintSliceVarP(p *[]uint, name, shorthand string, value []uint, usage string) { +func (f *FlagSet) UintSliceVarP(p *[]uint, name, shorthand string, value []uint, usage string, validation ...func(value []uint) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + f.VarP(newUintSliceValue(value, p), name, shorthand, usage, validationFunc) + return + } f.VarP(newUintSliceValue(value, p), name, shorthand, usage) } // UintSliceVar 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 UintSliceVar(p *[]uint, name string, value []uint, usage string) { +func UintSliceVar(p *[]uint, name string, value []uint, usage string, validation ...func(value []uint) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + CommandLine.VarP(newUintSliceValue(value, p), name, "", usage, validationFunc) + return + } CommandLine.VarP(newUintSliceValue(value, p), name, "", usage) } // UintSliceVarP is like the UintSliceVar, but accepts a shorthand letter that can be used after a single dash. -func UintSliceVarP(p *[]uint, name, shorthand string, value []uint, usage string) { +func UintSliceVarP(p *[]uint, name, shorthand string, value []uint, usage string, validation ...func(value []uint) error) { + if len(validation) > 0 { + validationFunc := interface{}(validation[0]) + CommandLine.VarP(newUintSliceValue(value, p), name, shorthand, usage, validationFunc) + return + } CommandLine.VarP(newUintSliceValue(value, p), name, shorthand, usage) } // UintSlice 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 (f *FlagSet) UintSlice(name string, value []uint, usage string) *[]uint { +func (f *FlagSet) UintSlice(name string, value []uint, usage string, validation ...func(value []uint) error) *[]uint { p := []uint{} - f.UintSliceVarP(&p, name, "", value, usage) + f.UintSliceVarP(&p, name, "", value, usage, validation...) return &p } // UintSliceP is like UintSlice, but accepts a shorthand letter that can be used after a single dash. -func (f *FlagSet) UintSliceP(name, shorthand string, value []uint, usage string) *[]uint { +func (f *FlagSet) UintSliceP(name, shorthand string, value []uint, usage string, validation ...func(value []uint) error) *[]uint { p := []uint{} - f.UintSliceVarP(&p, name, shorthand, value, usage) + f.UintSliceVarP(&p, name, shorthand, value, usage, validation...) return &p } // UintSlice 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 UintSlice(name string, value []uint, usage string) *[]uint { - return CommandLine.UintSliceP(name, "", value, usage) +func UintSlice(name string, value []uint, usage string, validation ...func(value []uint) error) *[]uint { + return CommandLine.UintSliceP(name, "", value, usage, validation...) } // UintSliceP is like UintSlice, but accepts a shorthand letter that can be used after a single dash. -func UintSliceP(name, shorthand string, value []uint, usage string) *[]uint { - return CommandLine.UintSliceP(name, shorthand, value, usage) +func UintSliceP(name, shorthand string, value []uint, usage string, validation ...func(value []uint) error) *[]uint { + return CommandLine.UintSliceP(name, shorthand, value, usage, validation...) }