Skip to content

Commit

Permalink
Int flags added
Browse files Browse the repository at this point in the history
  • Loading branch information
markdicksonjr committed Nov 11, 2020
1 parent 422f193 commit 912f0ce
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,19 @@ type stringVar struct {
Name string
}

type intVar struct {
Ptr *int
Key string
Name string
}

// TODO: ALLOW DESCS TO BE STATED SOMEHOW
func applyFlags(flagCfg interface{}) error {
keys := dot.KeysRecursiveLeaves(flagCfg)
var boolFlags []boolVar
var boolPtrFlags []boolVar
var intFlags []intVar
var intPtrFlags []intVar
var stringFlags []stringVar
var stringPtrFlags []stringVar

Expand Down Expand Up @@ -178,6 +186,30 @@ func applyFlags(flagCfg interface{}) error {
continue
}

if dv, ok := dotVal.(int); ok {
intFlag := intVar{
Name: camelKey,
Key: key,
Ptr: nil,
}
intFlag.Ptr = flag.Int(intFlag.Name, dv, "")
intFlags = append(intFlags, intFlag)
continue
}

if _, ok := dotVal.(*int); ok {
empty := 0

intFlag := intVar{
Name: camelKey,
Key: key,
Ptr: &empty,
}
flag.IntVar(&empty, intFlag.Name, 0, "")
intPtrFlags = append(intPtrFlags, intFlag)
continue
}

if _, ok := dotVal.(*string); ok {
empty := ""

Expand Down Expand Up @@ -209,6 +241,19 @@ func applyFlags(flagCfg interface{}) error {
}
}

// loop through ints and apply them
for _, intKey := range intFlags {
if err := dot.Set(flagCfg, intKey.Key, *intKey.Ptr); err != nil {
return err
}
}

for _, intPtrKey := range intPtrFlags {
if err := dot.Set(flagCfg, intPtrKey.Key, intPtrKey.Ptr); err != nil {
return err
}
}

// loop through strings and apply them
for _, stringKey := range stringFlags {
if err := dot.Set(flagCfg, stringKey.Key, *stringKey.Ptr); err != nil {
Expand Down

0 comments on commit 912f0ce

Please sign in to comment.