Skip to content
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

Fix dynamicvar incorrect parsing on bool values #151

Merged
merged 2 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions dynamic_var.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,24 @@ type dynamicFlag struct {

func (df *dynamicFlag) Set(value string) error {
fieldKind := reflect.TypeOf(df.field).Elem().Kind()
var isBoolValue bool
if _, err := strconv.ParseBool(value); err == nil {
isBoolValue = true
}
if fieldKind == reflect.Bool && isBoolValue {
boolField := df.field.(*bool)
*boolField = true
return nil
var (
RamanaReddy0M marked this conversation as resolved.
Show resolved Hide resolved
optionWithoutValue bool
pv bool
)
if value == "t" || value == "T" || value == "true" || value == "TRUE" {
pv = true
optionWithoutValue = true
} else if value == "f" || value == "F" || value == "false" || value == "FALSE" {
pv = false
}

switch fieldKind {
case reflect.Bool:
RamanaReddy0M marked this conversation as resolved.
Show resolved Hide resolved
boolField := df.field.(*bool)
*boolField = pv
case reflect.Int:
intField := df.field.(*int)
if isBoolValue {
if optionWithoutValue {
*intField = df.defaultValue.(int)
return nil
}
Expand All @@ -39,7 +44,7 @@ func (df *dynamicFlag) Set(value string) error {
*intField = newValue
case reflect.Float64:
floatField := df.field.(*float64)
if isBoolValue {
if optionWithoutValue {
*floatField = df.defaultValue.(float64)
return nil
}
Expand All @@ -50,14 +55,14 @@ func (df *dynamicFlag) Set(value string) error {
*floatField = newValue
case reflect.String:
stringField := df.field.(*string)
if isBoolValue {
if optionWithoutValue {
*stringField = df.defaultValue.(string)
return nil
}
*stringField = value
case reflect.Slice:
sliceField := df.field.(*[]string)
if isBoolValue {
if optionWithoutValue {
*sliceField = df.defaultValue.([]string)
return nil
}
Expand Down
107 changes: 107 additions & 0 deletions dynamic_var_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package goflags

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func TestDynamicVar(t *testing.T) {
t.Run("with bool as type", func(t *testing.T) {
var b bool
flagSet := NewFlagSet()
flagSet.CreateGroup("Option", "option",
flagSet.DynamicVar(&b, "kev", false, "kev with or without value"),
)
os.Args = []string{
os.Args[0],
"-kev=false",
}
err := flagSet.Parse()
assert.Nil(t, err)
assert.Equal(t, false, b)
tearDown(t.Name())
})

t.Run("without value for int as type", func(t *testing.T) {
var i int
flagSet := NewFlagSet()
flagSet.CreateGroup("Option", "option",
flagSet.DynamicVarP(&i, "concurrency", "c", 25, "concurrency for the process"),
)
os.Args = []string{
os.Args[0],
"-c",
}
err := flagSet.Parse()
assert.Nil(t, err)
assert.Equal(t, 25, i)
tearDown(t.Name())
})

t.Run("with value for int as type", func(t *testing.T) {
var i int
flagSet := NewFlagSet()
flagSet.CreateGroup("Option", "option",
flagSet.DynamicVarP(&i, "concurrency", "c", 25, "concurrency for the process"),
)
os.Args = []string{
os.Args[0],
"-c=100",
}
err := flagSet.Parse()
assert.Nil(t, err)
assert.Equal(t, 100, i)
tearDown(t.Name())
})

t.Run("with value for float64 as type", func(t *testing.T) {
var f float64
flagSet := NewFlagSet()
flagSet.CreateGroup("Option", "option",
flagSet.DynamicVarP(&f, "percentage", "p", 0.0, "percentage for the process"),
)
os.Args = []string{
os.Args[0],
"-p=100.0",
}
err := flagSet.Parse()
assert.Nil(t, err)
assert.Equal(t, 100.0, f)
tearDown(t.Name())
})

t.Run("with value for string as type", func(t *testing.T) {
var s string
flagSet := NewFlagSet()
flagSet.CreateGroup("Option", "option",
flagSet.DynamicVarP(&s, "name", "n", "", "name of the user"),
)
os.Args = []string{
os.Args[0],
"-n=test",
}
err := flagSet.Parse()
assert.Nil(t, err)
assert.Equal(t, "test", s)
tearDown(t.Name())
})

t.Run("with value for string slice as type", func(t *testing.T) {
var s []string
flagSet := NewFlagSet()
flagSet.CreateGroup("Option", "option",
flagSet.DynamicVarP(&s, "name", "n", []string{}, "name of the user"),
)
os.Args = []string{
os.Args[0],
"-n=test1,test2",
}
err := flagSet.Parse()
assert.Nil(t, err)
assert.Equal(t, []string{"test1", "test2"}, s)
tearDown(t.Name())
})

}