From 5a5789af7aecab045e5d5fed0d8e5b7c9c05df36 Mon Sep 17 00:00:00 2001 From: James He Date: Thu, 25 Aug 2022 16:48:26 -0500 Subject: [PATCH] adding int alias and test --- altsrc/flag.go | 16 ++++++++++------ altsrc/flag_test.go | 13 +++++++++++++ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/altsrc/flag.go b/altsrc/flag.go index 1783e1b..a57ed99 100644 --- a/altsrc/flag.go +++ b/altsrc/flag.go @@ -186,12 +186,16 @@ 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) { - value, err := isc.Int(f.IntFlag.Name) - if err != nil { - return err - } - for _, name := range f.Names() { - _ = f.set.Set(name, strconv.FormatInt(int64(value), 10)) + for _, name := range f.IntFlag.Names() { + if isc.isSet(name) { + value, err := isc.Int(name) + if err != nil { + return err + } + for _, n := range f.Names() { + _ = f.set.Set(n, strconv.FormatInt(int64(value), 10)) + } + } } } return nil diff --git a/altsrc/flag_test.go b/altsrc/flag_test.go index 47a280a..6b92d7f 100644 --- a/altsrc/flag_test.go +++ b/altsrc/flag_test.go @@ -332,6 +332,19 @@ func TestPathApplyInputSourceMethodEnvVarSet(t *testing.T) { refute(t, "goodbye", c.String("test")) } +func TestIntApplyInputSourceMethodSet_Alias(t *testing.T) { + tis := testApplyInputSource{ + Flag: NewIntFlag(&cli.IntFlag{Name: "test", Aliases: []string{"test_alias"}}), + FlagName: "test", + MapValue: 15, + } + c := runTest(t, tis) + expect(t, 15, c.Int("test_alias")) + + c = runRacyTest(t, tis) + refute(t, 15, c.Int("test_alias")) +} + func TestIntApplyInputSourceMethodSet(t *testing.T) { tis := testApplyInputSource{ Flag: NewIntFlag(&cli.IntFlag{Name: "test"}),