From 126d238fc9403128ddcc4ec98b24876a5886130c Mon Sep 17 00:00:00 2001 From: James He Date: Thu, 25 Aug 2022 16:59:57 -0500 Subject: [PATCH] adding float flag alias support --- altsrc/flag.go | 28 ++++++++++++++++------------ altsrc/flag_test.go | 13 +++++++++++++ 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/altsrc/flag.go b/altsrc/flag.go index 48b394d..37e9b03 100644 --- a/altsrc/flag.go +++ b/altsrc/flag.go @@ -141,7 +141,7 @@ func (f *BoolFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceConte // ApplyInputSourceValue applies a String value to the flagSet if required func (f *StringFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContext) error { - if f.set != nil && !(cCtx.IsSet(f.Name) || isEnvVarSet(f.EnvVars)) && isc.isSet(f.StringFlag.Name) { + if f.set != nil && !(cCtx.IsSet(f.Name) || isEnvVarSet(f.EnvVars)) { for _, name := range f.StringFlag.Names(){ if isc.isSet(name) { value, err := isc.String(name) @@ -159,7 +159,7 @@ func (f *StringFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceCon // ApplyInputSourceValue applies a Path value to the flagSet if required func (f *PathFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContext) error { - if f.set != nil && !(cCtx.IsSet(f.Name) || isEnvVarSet(f.EnvVars)) && isc.isSet(f.PathFlag.Name) { + if f.set != nil && !(cCtx.IsSet(f.Name) || isEnvVarSet(f.EnvVars)) { value, err := isc.String(f.PathFlag.Name) if err != nil { return err @@ -185,7 +185,7 @@ func (f *PathFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceConte // ApplyInputSourceValue applies a int value to the flagSet if required func (f *IntFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContext) error { - if f.set != nil && !(cCtx.IsSet(f.Name) || isEnvVarSet(f.EnvVars)) && isc.isSet(f.IntFlag.Name) { + if f.set != nil && !(cCtx.IsSet(f.Name) || isEnvVarSet(f.EnvVars)) { for _, name := range f.IntFlag.Names() { if isc.isSet(name) { value, err := isc.Int(name) @@ -203,7 +203,7 @@ func (f *IntFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContex // ApplyInputSourceValue applies a Duration value to the flagSet if required func (f *DurationFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContext) error { - if f.set != nil && !(cCtx.IsSet(f.Name) || isEnvVarSet(f.EnvVars)) && isc.isSet(f.DurationFlag.Name) { + if f.set != nil && !(cCtx.IsSet(f.Name) || isEnvVarSet(f.EnvVars)) { for _, name := range f.DurationFlag.Names() { if isc.isSet(name) { value, err := isc.Duration(name) @@ -221,14 +221,18 @@ func (f *DurationFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceC // ApplyInputSourceValue applies a Float64 value to the flagSet if required func (f *Float64Flag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContext) error { - if f.set != nil && !(cCtx.IsSet(f.Name) || isEnvVarSet(f.EnvVars)) && isc.isSet(f.Float64Flag.Name) { - value, err := isc.Float64(f.Float64Flag.Name) - if err != nil { - return err - } - floatStr := float64ToString(value) - for _, name := range f.Names() { - _ = f.set.Set(name, floatStr) + if f.set != nil && !(cCtx.IsSet(f.Name) || isEnvVarSet(f.EnvVars)) { + for _, name := range f.Float64Flag.Names() { + if isc.isSet(name) { + value, err := isc.Float64(name) + if err != nil { + return err + } + floatStr := float64ToString(value) + for _, n := range f.Names() { + _ = f.set.Set(n, floatStr) + } + } } } return nil diff --git a/altsrc/flag_test.go b/altsrc/flag_test.go index 43f26bd..bd6745e 100644 --- a/altsrc/flag_test.go +++ b/altsrc/flag_test.go @@ -481,6 +481,19 @@ func TestFloat64ApplyInputSourceMethodSet(t *testing.T) { refute(t, 1.3, c.Float64("test")) } +func TestFloat64ApplyInputSourceMethodSetNegativeValue_Alias(t *testing.T) { + tis := testApplyInputSource{ + Flag: NewFloat64Flag(&cli.Float64Flag{Name: "test", Aliases: []string{"test_alias"}}), + FlagName: "test", + MapValue: -1.3, + } + c := runTest(t, tis) + expect(t, -1.3, c.Float64("test_alias")) + + c = runRacyTest(t, tis) + refute(t, -1.3, c.Float64("test_alias")) +} + func TestFloat64ApplyInputSourceMethodSetNegativeValue(t *testing.T) { tis := testApplyInputSource{ Flag: NewFloat64Flag(&cli.Float64Flag{Name: "test"}),