-
Notifications
You must be signed in to change notification settings - Fork 348
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding custom validation for any type of flag's value #374
base: master
Are you sure you want to change the base?
Conversation
Erfan Momeni seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account. You have signed the CLA already but the status is still pending? Let us recheck it. |
This reverts commit c1513ce.
This change doesn't affect the existing code; as you see, previous tests pass and the last parameter (validation func) is also optional. for implementation, I think this feature is not like shorthand (that adds a new |
I see now that it is indeed optional, so i apologize for my misteak.
Except it currently doesn't work for package main
import (
"errors"
flag "github.com/erfanmomeniii/pflag"
)
type Foo struct{}
func validateFoo(v interface{}) error {
return errors.New("you will never see this error")
}
func (Foo) Set(string) error {
return nil
}
func (Foo) String() string {
return "Foo!"
}
func (Foo) Type() string {
return "Foo"
}
func main() {
var foo Foo
flag.Var(foo, "foo", "usage for --foo", validateFoo)
flag.CommandLine.Parse([]string{"--foo=FOO"})
} Also, it's important to note that func setValue(value flag.Value, newVal string, validation func(newValue flag.Value) error) (err error) {
oldVal := value.String()
defer func() {
if r := recover(); r != nil {
value.Set(oldVal)
err = fmt.Errorf("%v", r)
}
}()
if err = value.Set(newVal); err != nil {
value.Set(oldVal)
} else if err = validation(value); err != nil {
value.Set(oldVal)
}
return
} Notice the validation function accepts a func (f *FlagSet) BoolVarP(p *bool, name, shorthand string, value bool, usage string, validation ...func(value bool) error) {
var flag *Flag
if len(validation) == 0 {
flag = f.VarPF(newBoolValue(value, p), name, shorthand, usage)
} else {
validationFunc := func(v Value) error {
return validation[0](*v.(*boolValue))
}
flag = f.VarPF(newBoolValue(value, p), name, shorthand, usage, validationFunc)
}
flag.NoOptDefVal = "true"
} Otherwise, |
I also forgot to mention that If you insist on validating before an attempt to set the value, it's worth considering the addition of an optional You might also consider reverting the changes to |
fixes #236,
fixes#293,
fixes #299
Hi, After merging this pull request we can add custom validation for any type of flag.
It is optional and for this operation, you should define a function and pass it as the last parameter to the flag definition function.
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.