Documentation ¶
Overview ¶
Code generated by flag-validator; DO NOT EDIT.
Code generated by flag-validator; DO NOT EDIT.
Code generated by flag-validator; DO NOT EDIT.
Code generated by flag-validator; DO NOT EDIT.
Code generated by flag-validator; DO NOT EDIT.
Code generated by flag-validator; DO NOT EDIT.
Code generated by flag-validator; DO NOT EDIT.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Argument ¶
type Argument struct {
// contains filtered or unexported fields
}
Argument represents non-flag arguments.
func (*Argument) In ¶
In returns a validation rule that checks if a value is in the given list of values.
func (*Argument) Is ¶
Is returns a validation rule that checks if a value is a valid value.
Example ¶
package main import ( "fmt" fv "github.com/kmio11/flag-validator/pflag-validator" flag "github.com/spf13/pflag" ) func main() { fs := flag.NewFlagSet("fs", flag.ContinueOnError) ruleSet := fv.NewRuleSet( fv.Arg(0).Is(fv.EqualTo("value_expected")), ) _ = fs.Parse([]string{"arg1"}) err := ruleSet.Validate(fs) if err != nil { fmt.Println(err) } }
Output: 0th argument must be a valid value
Example (Govalidator) ¶
package main import ( "fmt" "github.com/asaskevich/govalidator" fv "github.com/kmio11/flag-validator/pflag-validator" flag "github.com/spf13/pflag" ) func main() { fs := flag.NewFlagSet("fs", flag.ContinueOnError) ruleSet := fv.NewRuleSet( // you can use govalidator.IsXxx functions fv.Arg(0).Is(govalidator.IsEmail), fv.Arg(1).Is(govalidator.IsNumeric), ) _ = fs.Parse([]string{ "email@example.com", "invalid", }) err := ruleSet.Validate(fs) if err != nil { fmt.Println(err) } }
Output: 1th argument must be a valid value
type ConditionBool ¶
type ConditionBool bool
type ConditionFunc ¶
type Error ¶
type Flags ¶
type Flags struct {
// contains filtered or unexported fields
}
Flags represents Flags. This struct has the methods returning Rule.
func (*Flags) DisplayStrings ¶
func (*Flags) Required ¶
Example ¶
package main import ( "fmt" fv "github.com/kmio11/flag-validator/pflag-validator" flag "github.com/spf13/pflag" ) func main() { fs := flag.NewFlagSet("fs", flag.ContinueOnError) _ = fs.String("flag1", "", "") _ = fs.String("flag2", "", "") ruleSet := fv.NewRuleSet( fv.Flag("flag1").Required(), ) _ = fs.Parse([]string{"--flag2", "value2"}) err := ruleSet.Validate(fs) if err != nil { fmt.Println(err) } }
Output: The flag [--flag1] is required
Example (AllOf) ¶
package main import ( "fmt" fv "github.com/kmio11/flag-validator/pflag-validator" flag "github.com/spf13/pflag" ) func main() { fs := flag.NewFlagSet("fs", flag.ContinueOnError) _ = fs.String("flag1", "", "") _ = fs.String("flag2", "", "") _ = fs.String("flag3", "", "") ruleSet := fv.NewRuleSet( fv.AllOf("flag1", "flag2").Required(), ) _ = fs.Parse([]string{"--flag1", "value1"}) err := ruleSet.Validate(fs) if err != nil { fmt.Println(err) } }
Output: All the flags [--flag1 --flag2] is required
Example (AnyOf) ¶
package main import ( "fmt" fv "github.com/kmio11/flag-validator/pflag-validator" flag "github.com/spf13/pflag" ) func main() { fs := flag.NewFlagSet("fs", flag.ContinueOnError) _ = fs.String("flag1", "", "") _ = fs.String("flag2", "", "") _ = fs.String("flag3", "", "") ruleSet := fv.NewRuleSet( fv.AnyOf("flag1", "flag2").Required(), ) _ = fs.Parse([]string{"--flag3", "value3"}) err := ruleSet.Validate(fs) if err != nil { fmt.Println(err) } }
Output: At least one of the flags [--flag1 --flag2] is required
type IntValue ¶
type IntValue struct {
// contains filtered or unexported fields
}
func (IntValue) ExclusiveMax ¶
ExclusiveMax returns a validation rule that checks if a value is greater than the threshold value.
func (IntValue) ExclusiveMin ¶
ExclusiveMin returns a validation rule that checks if a value is less than the threshold value.
func (IntValue) Max ¶
Max returns a validation rule that checks if a value is greater than or equal to the threshold value.
Example ¶
package main import ( "fmt" fv "github.com/kmio11/flag-validator/pflag-validator" flag "github.com/spf13/pflag" ) func main() { fs := flag.NewFlagSet("fs", flag.ContinueOnError) _ = fs.Int("flag1", 0, "") ruleSet := fv.NewRuleSet( fv.ValueOf("flag1").TypeInt().Max(10), ) _ = fs.Parse([]string{"--flag1", "15"}) err := ruleSet.Validate(fs) if err != nil { fmt.Println(err) } }
Output: The value of flag1 must be less than or equal to 10
type Rule ¶
func MutuallyExclusive ¶
MutuallyExclusive returns the validation rule that checks if the given list of flags are not specified more than once.
func NumberOfArgs ¶
NumberOfArgs returns the validation rule that checks if the number of specified argments is equal to n.
type ValidationError ¶
type ValidationError struct {
// contains filtered or unexported fields
}
func (ValidationError) Error ¶
func (e ValidationError) Error() string
func (ValidationError) SetMessage ¶
func (e ValidationError) SetMessage(message string) Error