pflagvalidator

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2024 License: MIT Imports: 6 Imported by: 1

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

func OverwriteDefaultMessages

func OverwriteDefaultMessages(messages map[string]string)

func When

func When(condition Condition) func(rules ...Rule) Rule

When validates the given list of rules when the condition is true.

Types

type Argument

type Argument struct {
	// contains filtered or unexported fields
}

Argument represents non-flag arguments.

func Arg

func Arg(index int) *Argument

Arg represents the index'th non-flag arguments.

func (*Argument) In

func (a *Argument) In(values ...string) Rule

In returns a validation rule that checks if a value is in the given list of values.

func (*Argument) Is

func (a *Argument) Is(is IsFunc) Rule

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

func (*Argument) IsSet

func (a *Argument) IsSet() Condition

type Condition

type Condition interface {
	Evaluate(fs *flag.FlagSet) bool
}

func Not

func Not(condition Condition) Condition

type ConditionBool

type ConditionBool bool

func (ConditionBool) Evaluate

func (c ConditionBool) Evaluate(fs *flag.FlagSet) bool

type ConditionFunc

type ConditionFunc func(fs *flag.FlagSet) bool

func (ConditionFunc) Evaluate

func (c ConditionFunc) Evaluate(fs *flag.FlagSet) bool

type Error

type Error interface {
	error
	SetMessage(message string) Error
	AddParams(params map[string]any) Error
}

func NewError

func NewError(message string, params ...map[string]any) Error

func WrapError

func WrapError(err error) Error

WrapError returns Error. When err is Error, return err as-is.

type Flags

type Flags struct {
	// contains filtered or unexported fields
}

Flags represents Flags. This struct has the methods returning Rule.

func AllOf

func AllOf(names ...string) *Flags

func AnyOf

func AnyOf(names ...string) *Flags

func Flag

func Flag(name string) *Flags

func (*Flags) DisplayStrings

func (f *Flags) DisplayStrings() []string

func (*Flags) IsSet

func (f *Flags) IsSet() Condition

func (*Flags) Required

func (f *Flags) Required() Rule
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

func (v IntValue) ExclusiveMax(max int) Rule

ExclusiveMax returns a validation rule that checks if a value is greater than the threshold value.

func (IntValue) ExclusiveMin

func (v IntValue) ExclusiveMin(min int) Rule

ExclusiveMin returns a validation rule that checks if a value is less than the threshold value.

func (IntValue) Max

func (v IntValue) Max(max int) Rule

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

func (IntValue) Min

func (v IntValue) Min(min int) Rule

Min returns a validation rule that checks if a value is less than or equal to the threshold value.

type IsFunc

type IsFunc func(value string) bool

func EqualTo

func EqualTo(want string) IsFunc

type Rule

type Rule interface {
	Validate(fs *flag.FlagSet) error
	Error(message string) Rule
}

func MutuallyExclusive

func MutuallyExclusive(flags ...*Flags) Rule

MutuallyExclusive returns the validation rule that checks if the given list of flags are not specified more than once.

func NumberOfArgs

func NumberOfArgs(n int) Rule

NumberOfArgs returns the validation rule that checks if the number of specified argments is equal to n.

type RuleFunc

type RuleFunc func(fs *flag.FlagSet) error

func (RuleFunc) Error

func (v RuleFunc) Error(message string) Rule

func (RuleFunc) Validate

func (v RuleFunc) Validate(fs *flag.FlagSet) error

type RuleSet

type RuleSet struct {
	// contains filtered or unexported fields
}

func NewRuleSet

func NewRuleSet(rules ...Rule) *RuleSet

func (*RuleSet) Validate

func (rs *RuleSet) Validate(fs *flag.FlagSet) error

type ValidationError

type ValidationError struct {
	// contains filtered or unexported fields
}

func (ValidationError) AddParams

func (a ValidationError) AddParams(params map[string]any) Error

func (ValidationError) Error

func (e ValidationError) Error() string

func (ValidationError) SetMessage

func (e ValidationError) SetMessage(message string) Error

type Value

type Value struct {
	// contains filtered or unexported fields
}

Value represents the value of a flag.

func ValueOf

func ValueOf(name string) *Value

func (Value) Is

func (v Value) Is(is IsFunc) Rule

Is returns a validation rule that checks if a value is a valid value.

func (Value) TypeInt

func (v Value) TypeInt() *IntValue

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL