Skip to content

Commit

Permalink
Add support for float64
Browse files Browse the repository at this point in the history
  • Loading branch information
bamarni committed Sep 27, 2019
1 parent 0e777d3 commit 1903cca
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 3 deletions.
7 changes: 4 additions & 3 deletions README.md
Expand Up @@ -123,10 +123,11 @@ Here is a reference of the supported field types:

```go
type Options struct {
Name string `flaq:"-n, --name string name of the person to greet"`
Yell bool `flaq:" --yell --yell will set the value to true"`
String string `flaq:"-n, --name string a string eg. --string=world"`
Switch bool `flaq:" --switch --switch will set the value to true"`
Bool bool `flaq:" --bool bool --bool, --bool=true or --bool=false"`
Int int `flaq:" --int int an int value"`
Int int `flaq:" --int int an int value eg. --int=100"`
Float64 float64 `flaq:" --float64 float64 a float value eg. --float64=3.14159"`
Count int `flaq:"-c, --count count -ccc will set this count value to 3"`
Duration time.Duration `flaq:" --duration duration a duration eg. --duration=5min"`
}
Expand Down
19 changes: 19 additions & 0 deletions flag.go
Expand Up @@ -39,6 +39,11 @@ func Int(ivar *int, long, short, description string) {
flags.Int(ivar, long, short, description)
}

// Float64 adds a float64 flag with specified long/short form and description.
func Float64(fvar *float64, long, short, description string) {
flags.Float64(fvar, long, short, description)
}

// Count adds a count flag with specified long/short form and description.
func Count(cvar *int, long, short, description string) {
flags.Count(cvar, long, short, description)
Expand Down Expand Up @@ -180,6 +185,17 @@ func (f *FlagSet) Int(ivar *int, long, short, description string) {
})
}

// Float64 adds a float64 flag with specified long/short form and description.
func (f *FlagSet) Float64(fvar *float64, long, short, description string) {
f.Add(&Flag{
Long: long,
Short: short,
Description: description,
Value: (*float64Value)(fvar),
Arg: &FlagArg{Name: "float"},
})
}

// Count adds a count flag with specified long/short form and description.
func (f *FlagSet) Count(cvar *int, long, short, description string) {
f.Add(&Flag{
Expand Down Expand Up @@ -240,6 +256,9 @@ func (f *FlagSet) Struct(svar interface{}) {
case "duration":
flag.Value = (*durationValue)(val.(*time.Duration))
flag.Arg = &FlagArg{Name: "duration"}
case "float64":
flag.Value = (*float64Value)(val.(*float64))
flag.Arg = &FlagArg{Name: "float"}
case "int":
flag.Value = (*intValue)(val.(*int))
flag.Arg = &FlagArg{Name: "int"}
Expand Down
11 changes: 11 additions & 0 deletions flag_test.go
Expand Up @@ -20,6 +20,7 @@ func TestParse(t *testing.T) {
car bool
count int
number int
floatNumber float64
duration time.Duration
}{
{
Expand Down Expand Up @@ -101,6 +102,10 @@ func TestParse(t *testing.T) {
args: []string{"--number=50"},
number: 50,
},
{
args: []string{"--float-number=3.14159"},
floatNumber: 3.14159,
},
{
args: []string{"--duration=5m"},
duration: time.Duration(5 * time.Minute),
Expand All @@ -112,6 +117,7 @@ func TestParse(t *testing.T) {
var car, foo, fooBar bool
var bar string
var count, number int
var floatNumber float64
var duration time.Duration

flags := &FlagSet{}
Expand All @@ -121,6 +127,7 @@ func TestParse(t *testing.T) {
flags.Bool(&car, "car", "c", "", false)
flags.Count(&count, "count", "", "")
flags.Int(&number, "number", "", "")
flags.Float64(&floatNumber, "float-number", "", "")
flags.Duration(&duration, "duration", "", "")

err := flags.Parse(f.args)
Expand All @@ -136,6 +143,7 @@ func TestParse(t *testing.T) {
assert.Equal(t, f.car, car)
assert.Equal(t, f.count, count)
assert.Equal(t, f.number, number)
assert.Equal(t, f.floatNumber, floatNumber)
assert.Equal(t, f.duration, duration)
})
}
Expand Down Expand Up @@ -188,6 +196,7 @@ func TestParseStruct(t *testing.T) {
Yell bool `flaq:" --yell whether to yell or not"`
Bool bool `flaq:" --bool bool whether to bool or not"`
Int int `flaq:" --int int whether to int or not"`
Float64 float64 `flaq:" --float64 float64 whether to float64 or not"`
Count int `flaq:"-c, --count count whether to count or not"`
Duration time.Duration `flaq:" --duration duration whether to duration or not"`

Expand All @@ -202,6 +211,7 @@ func TestParseStruct(t *testing.T) {
"--yell",
"--bool=true",
"--duration=3s",
"--float64=3.14",
"--int=100",
"-ccc",
})
Expand All @@ -213,6 +223,7 @@ func TestParseStruct(t *testing.T) {
require.Equal(t, 3*time.Second, opts.Duration)
require.Equal(t, 100, opts.Int)
require.Equal(t, 3, opts.Count)
require.Equal(t, 3.14, opts.Float64)
}

func TestParseStructFieldTag(t *testing.T) {
Expand Down
8 changes: 8 additions & 0 deletions value.go
Expand Up @@ -51,3 +51,11 @@ func (d *durationValue) Set(val string) error {
*d = durationValue(v)
return err
}

type float64Value float64

func (f *float64Value) Set(val string) error {
v, err := strconv.ParseFloat(val, 64)
*f = float64Value(v)
return err
}
9 changes: 9 additions & 0 deletions value_test.go
Expand Up @@ -49,3 +49,12 @@ func TestDurationValue(t *testing.T) {
require.NoError(t, val.Set("5s"))
require.Equal(t, 5*time.Second, dvar)
}

func TestFloat64Value(t *testing.T) {
var fvar float64
val := (*float64Value)(&fvar)

require.Error(t, val.Set("invalid"))
require.NoError(t, val.Set("3.14159265358979323846264338327950288419716939937510"))
require.Equal(t, 3.14159265358979323846264338327950288419716939937510, fvar)
}

0 comments on commit 1903cca

Please sign in to comment.