From e0db26749279d8a4696a3963ca5e408f7fdbf342 Mon Sep 17 00:00:00 2001 From: James He Date: Thu, 11 Aug 2022 15:49:12 -0500 Subject: [PATCH] adding in boolean alias support --- altsrc/flag.go | 20 +++++++++++++------- altsrc/flag_test.go | 13 +++++++++++++ 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/altsrc/flag.go b/altsrc/flag.go index 9801644..daac809 100644 --- a/altsrc/flag.go +++ b/altsrc/flag.go @@ -121,13 +121,19 @@ func (f *IntSliceFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceC // ApplyInputSourceValue applies a Bool value to the flagSet if required func (f *BoolFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContext) error { - if f.set != nil && !cCtx.IsSet(f.Name) && !isEnvVarSet(f.EnvVars) && isc.isSet(f.BoolFlag.Name) { - value, err := isc.Bool(f.BoolFlag.Name) - if err != nil { - return err - } - for _, name := range f.Names() { - _ = f.set.Set(name, strconv.FormatBool(value)) + if f.set != nil && !cCtx.IsSet(f.Name) && !isEnvVarSet(f.EnvVars) { + for _, name := range f.BoolFlag.Names() { + if isc.isSet(name) { + value, err := isc.Bool(name) + if err != nil { + return err + } + if value { + for _, n := range f.Names() { + _ = f.set.Set(n, strconv.FormatBool(value)) + } + } + } } } return nil diff --git a/altsrc/flag_test.go b/altsrc/flag_test.go index e3725f7..d97c184 100644 --- a/altsrc/flag_test.go +++ b/altsrc/flag_test.go @@ -178,6 +178,19 @@ func TestBoolApplyInputSourceMethodSet(t *testing.T) { refute(t, true, c.Bool("test")) } +func TestBoolApplyInputSourceMethodSet_Alias(t *testing.T) { + tis := testApplyInputSource{ + Flag: NewBoolFlag(&cli.BoolFlag{Name: "test", Aliases: []string{"test_alias"}}), + FlagName: "test_alias", + MapValue: true, + } + c := runTest(t, tis) + expect(t, true, c.Bool("test_alias")) + + c = runRacyTest(t, tis) + refute(t, true, c.Bool("test_alias")) +} + func TestBoolApplyInputSourceMethodContextSet(t *testing.T) { tis := testApplyInputSource{ Flag: NewBoolFlag(&cli.BoolFlag{Name: "test"}),