Fix:(issue_1455) Allow bool flags from input altsrc

This commit is contained in:
Naveen Gogineni
2022-08-16 18:34:34 -04:00
committed by Dan Buch
parent 42eb492258
commit 78dac9c7af
5 changed files with 30 additions and 19 deletions

View File

@@ -124,13 +124,14 @@ func (f *BoolFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceConte
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 {
fmt.Println(err)
return err
}
if value {
for _, name := range f.Names() {
_ = f.set.Set(name, strconv.FormatBool(value))
}
for _, name := range f.Names() {
_ = f.set.Set(name, strconv.FormatBool(value))
}
} else {
fmt.Println("not fill")
}
return nil
}

View File

@@ -11,7 +11,7 @@ import (
const (
fileName = "current.json"
simpleJSON = `{"test": 15}`
simpleJSON = `{"test": 15, "testb": false}`
nestedJSON = `{"top": {"test": 15}}`
)
@@ -34,11 +34,16 @@ func TestCommandJSONFileTest(t *testing.T) {
Action: func(c *cli.Context) error {
val := c.Int("test")
expect(t, val, 15)
valb := c.Bool("testb")
expect(t, valb, false)
return nil
},
Flags: []cli.Flag{
NewIntFlag(&cli.IntFlag{Name: "test"}),
&cli.StringFlag{Name: "load"}},
&cli.StringFlag{Name: "load"},
NewBoolFlag(&cli.BoolFlag{Name: "testb", Value: true}),
},
}
command.Before = InitInputSourceWithContext(command.Flags, NewJSONSourceFromFlagFunc("load"))
err := command.Run(c)