Skip to content

Commit

Permalink
Add validator
Browse files Browse the repository at this point in the history
  • Loading branch information
dearchap committed Jun 26, 2023
1 parent 983228e commit a0f03ca
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 18 deletions.
11 changes: 2 additions & 9 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"flag"
"fmt"
"log"
"strings"
)

Expand Down Expand Up @@ -153,14 +152,8 @@ func (cCtx *Context) Count(name string) int {

// Value returns the value of the flag corresponding to `name`
func (cCtx *Context) Value(name string) interface{} {
if f := cCtx.lookupFlag(name); f != nil {
if g, ok := f.(ValueGetter); ok {
return g.GetFlagValue().Get()
}
if g, ok := f.(flag.Getter); ok {
log.Printf("%v", g.Get())
return g.Get()
}
if fs := cCtx.lookupFlagSet(name); fs != nil {
return fs.Lookup(name).Value.(flag.Getter).Get()
}
return nil
}
Expand Down
17 changes: 12 additions & 5 deletions flag_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ type Value interface {
flag.Getter
}

type ValueGetter interface {
GetFlagValue() Value
}

type boolFlag interface {
IsBoolFlag() bool
}
Expand All @@ -25,6 +21,7 @@ type fnValue struct {
fn func(string) error
isBool bool
v Value
count *int
}

func (f fnValue) Get() any { return f.v.Get() }
Expand All @@ -45,6 +42,7 @@ func (f fnValue) Serialize() string {

func (f fnValue) IsBoolFlag() bool { return f.isBool }
func (f fnValue) GetFlagValue() Value { return f.v }
func (f fnValue) Count() int { return *f.count }

// ValueCreator is responsible for creating a flag.Value emulation
// as well as custom formatting
Expand Down Expand Up @@ -92,6 +90,8 @@ type FlagBase[T any, C any, VC ValueCreator[T, C]] struct {

OnlyOnce bool // whether this flag can be duplicated on the command line

Validator func(T) error // custom function to validate this flag value

// unexported fields for internal use
count int // number of times the flag has been set
hasBeenSet bool // whether the flag has been set from env or file
Expand Down Expand Up @@ -168,10 +168,17 @@ func (f *FlagBase[T, C, V]) Apply(set *flag.FlagSet) error {
return fmt.Errorf("cant duplicate this flag")
}
f.count++
return f.value.Set(val)
if err := f.value.Set(val); err != nil {
return err
}
if f.Validator != nil {
return f.Validator(f.value.Get().(T))
}
return nil
},
isBool: isBool,
v: f.value,
count: &f.count,
}, name, f.Usage)
}

Expand Down
6 changes: 2 additions & 4 deletions godoc-current.txt
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,8 @@ type FlagBase[T any, C any, VC ValueCreator[T, C]] struct {

OnlyOnce bool // whether this flag can be duplicated on the command line

Validator func(T) error // custom function to validate this flag value

// Has unexported fields.
}
FlagBase[T,C,VC] is a generic flag base which can be used as a boilerplate
Expand Down Expand Up @@ -1073,10 +1075,6 @@ type ValueCreator[T any, C any] interface {
T specifies the type
C specifies the config for the type

type ValueGetter interface {
GetFlagValue() Value
}

type ValueSource interface {
fmt.Stringer
fmt.GoStringer
Expand Down

0 comments on commit a0f03ca

Please sign in to comment.