Skip to content

Commit

Permalink
Merge pull request #1773 from urfave/rename-float64
Browse files Browse the repository at this point in the history
Rename `float64` types from `Float64` to `Float`
  • Loading branch information
meatballhat authored Jun 23, 2023
2 parents de6dd28 + 89d5c1b commit 60ceaf5
Show file tree
Hide file tree
Showing 11 changed files with 151 additions and 167 deletions.
14 changes: 7 additions & 7 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1220,10 +1220,10 @@ func TestCommand_Float64Flag(t *testing.T) {

cmd := &Command{
Flags: []Flag{
&Float64Flag{Name: "height", Value: 1.5, Usage: "Set the height, in meters"},
&FloatFlag{Name: "height", Value: 1.5, Usage: "Set the height, in meters"},
},
Action: func(c *Context) error {
meters = c.Float64("height")
meters = c.Float("height")
return nil
},
}
Expand Down Expand Up @@ -2840,7 +2840,7 @@ func TestFlagAction(t *testing.T) {
return err
},
},
&Float64Flag{
&FloatFlag{
Name: "f_float64",
Action: func(cCtx *Context, v float64) error {
if v < 0 {
Expand All @@ -2850,7 +2850,7 @@ func TestFlagAction(t *testing.T) {
return err
},
},
&Float64SliceFlag{
&FloatSliceFlag{
Name: "f_float64_slice",
Action: func(cCtx *Context, v []float64) error {
if len(v) > 0 && v[0] < 0 {
Expand Down Expand Up @@ -2956,7 +2956,7 @@ func TestPersistentFlag(t *testing.T) {
Persistent: true,
Destination: &persistentCommandSliceInt,
},
&Float64SliceFlag{
&FloatSliceFlag{
Name: "persistentCommandFloatSliceFlag",
Persistent: true,
Value: []float64{11.3, 12.5},
Expand Down Expand Up @@ -2996,7 +2996,7 @@ func TestPersistentFlag(t *testing.T) {
},
},
Action: func(ctx *Context) error {
appSliceFloat64 = ctx.Float64Slice("persistentCommandFloatSliceFlag")
appSliceFloat64 = ctx.FloatSlice("persistentCommandFloatSliceFlag")
return nil
},
},
Expand Down Expand Up @@ -3077,7 +3077,7 @@ func TestFlagDuplicates(t *testing.T) {
&IntSliceFlag{
Name: "isflag",
},
&Float64SliceFlag{
&FloatSliceFlag{
Name: "fsflag",
OnlyOnce: true,
},
Expand Down
12 changes: 6 additions & 6 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestNewContext(t *testing.T) {
r := require.New(t)
r.Equal(int64(12), cCtx.Int("myflag"))
r.Equal(uint64(93), cCtx.Uint("myflagUint"))
r.Equal(float64(17), cCtx.Float64("myflag64"))
r.Equal(float64(17), cCtx.Float("myflag64"))
r.Equal("mycommand", cCtx.Command.Name)
}

Expand Down Expand Up @@ -71,8 +71,8 @@ func TestContext_Float64(t *testing.T) {
parentSet.Float64("top-flag", float64(18), "doc")
parentCtx := NewContext(nil, parentSet, nil)
c := NewContext(nil, set, parentCtx)
expect(t, c.Float64("myflag"), float64(17))
expect(t, c.Float64("top-flag"), float64(18))
expect(t, c.Float("myflag"), float64(17))
expect(t, c.Float("top-flag"), float64(18))
}

func TestContext_Duration(t *testing.T) {
Expand Down Expand Up @@ -203,10 +203,10 @@ func TestContext_IsSet_fromEnv(t *testing.T) {
_ = os.Setenv("APP_PASSWORD", "")
cmd := &Command{
Flags: []Flag{
&Float64Flag{Name: "timeout", Aliases: []string{"t"}, Sources: EnvVars("APP_TIMEOUT_SECONDS")},
&FloatFlag{Name: "timeout", Aliases: []string{"t"}, Sources: EnvVars("APP_TIMEOUT_SECONDS")},
&StringFlag{Name: "password", Aliases: []string{"p"}, Sources: EnvVars("APP_PASSWORD")},
&Float64Flag{Name: "unparsable", Aliases: []string{"u"}, Sources: EnvVars("APP_UNPARSABLE")},
&Float64Flag{Name: "no-env-var", Aliases: []string{"n"}},
&FloatFlag{Name: "unparsable", Aliases: []string{"u"}, Sources: EnvVars("APP_UNPARSABLE")},
&FloatFlag{Name: "no-env-var", Aliases: []string{"n"}},
},
Action: func(ctx *Context) error {
timeoutIsSet = ctx.IsSet("timeout")
Expand Down
2 changes: 1 addition & 1 deletion examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ func ExampleCommand_Run_sliceValues() {
Name: "multi_values",
Flags: []cli.Flag{
&cli.StringSliceFlag{Name: "stringSlice"},
&cli.Float64SliceFlag{Name: "float64Slice"},
&cli.FloatSliceFlag{Name: "float64Slice"},
&cli.IntSliceFlag{Name: "intSlice"},
},
Action: func(cCtx *cli.Context) error {
Expand Down
46 changes: 46 additions & 0 deletions flag_float.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package cli

import (
"fmt"
"strconv"
)

type FloatFlag = FlagBase[float64, NoConfig, floatValue]

// -- float64 Value
type floatValue float64

// Below functions are to satisfy the ValueCreator interface

func (f floatValue) Create(val float64, p *float64, c NoConfig) Value {
*p = val
return (*floatValue)(p)
}

func (f floatValue) ToString(b float64) string {
return fmt.Sprintf("%v", b)
}

// Below functions are to satisfy the flag.Value interface

func (f *floatValue) Set(s string) error {
v, err := strconv.ParseFloat(s, 64)
if err != nil {
return err
}
*f = floatValue(v)
return err
}

func (f *floatValue) Get() any { return float64(*f) }

func (f *floatValue) String() string { return strconv.FormatFloat(float64(*f), 'g', -1, 64) }

// Int looks up the value of a local IntFlag, returns
// 0 if not found
func (cCtx *Context) Float(name string) float64 {
if v, ok := cCtx.Value(name).(float64); ok {
return v
}
return 0
}
46 changes: 0 additions & 46 deletions flag_float64.go

This file was deleted.

29 changes: 0 additions & 29 deletions flag_float64_slice.go

This file was deleted.

29 changes: 29 additions & 0 deletions flag_float_slice.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package cli

import (
"flag"
)

type FloatSlice = SliceBase[float64, NoConfig, floatValue]
type FloatSliceFlag = FlagBase[[]float64, NoConfig, FloatSlice]

var NewFloatSlice = NewSliceBase[float64, NoConfig, floatValue]

// FloatSlice looks up the value of a local FloatSliceFlag, returns
// nil if not found
func (cCtx *Context) FloatSlice(name string) []float64 {
if fs := cCtx.lookupFlagSet(name); fs != nil {
return lookupFloatSlice(name, fs)
}
return nil
}

func lookupFloatSlice(name string, set *flag.FlagSet) []float64 {
f := set.Lookup(name)
if f != nil {
if slice, ok := f.Value.(flag.Getter).Get().([]float64); ok {
return slice
}
}
return nil
}
2 changes: 1 addition & 1 deletion flag_slice_impl.go → flag_slice_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (i SliceBase[T, C, VC]) Create(val []T, p *[]T, c C) Value {
}
}

// NewIntSlice makes an *IntSlice with default values
// NewSliceBase makes a *SliceBase with default values
func NewSliceBase[T any, C any, VC ValueCreator[T, C]](defaults ...T) *SliceBase[T, C, VC] {
return &SliceBase[T, C, VC]{
slice: &defaults,
Expand Down
Loading

0 comments on commit 60ceaf5

Please sign in to comment.