Skip to content

Commit f394c37

Browse files
committed
Fix:(issue_1455) Allow bool flags from input altsrc
1 parent 7357e10 commit f394c37

File tree

5 files changed

+31
-20
lines changed

5 files changed

+31
-20
lines changed

altsrc/flag.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,14 @@ func (f *BoolFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceConte
124124
if f.set != nil && !cCtx.IsSet(f.Name) && !isEnvVarSet(f.EnvVars) && isc.isSet(f.BoolFlag.Name) {
125125
value, err := isc.Bool(f.BoolFlag.Name)
126126
if err != nil {
127+
fmt.Println(err)
127128
return err
128129
}
129-
if value {
130-
for _, name := range f.Names() {
131-
_ = f.set.Set(name, strconv.FormatBool(value))
132-
}
130+
for _, name := range f.Names() {
131+
_ = f.set.Set(name, strconv.FormatBool(value))
133132
}
133+
} else {
134+
fmt.Println("not fill")
134135
}
135136
return nil
136137
}

altsrc/json_command_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
const (
1313
fileName = "current.json"
14-
simpleJSON = `{"test": 15}`
14+
simpleJSON = `{"test": 15, "testb": false}`
1515
nestedJSON = `{"top": {"test": 15}}`
1616
)
1717

@@ -34,11 +34,16 @@ func TestCommandJSONFileTest(t *testing.T) {
3434
Action: func(c *cli.Context) error {
3535
val := c.Int("test")
3636
expect(t, val, 15)
37+
38+
valb := c.Bool("testb")
39+
expect(t, valb, false)
3740
return nil
3841
},
3942
Flags: []cli.Flag{
4043
NewIntFlag(&cli.IntFlag{Name: "test"}),
41-
&cli.StringFlag{Name: "load"}},
44+
&cli.StringFlag{Name: "load"},
45+
NewBoolFlag(&cli.BoolFlag{Name: "testb", Value: true}),
46+
},
4247
}
4348
command.Before = InitInputSourceWithContext(command.Flags, NewJSONSourceFromFlagFunc("load"))
4449
err := command.Run(c)

go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,7 @@ require (
99
gopkg.in/yaml.v3 v3.0.1
1010
)
1111

12-
require github.com/russross/blackfriday/v2 v2.1.0 // indirect
12+
require (
13+
github.com/russross/blackfriday/v2 v2.1.0 // indirect
14+
golang.org/x/text v0.3.7 // indirect
15+
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf
66
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
77
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
88
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
9+
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
10+
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
911
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
1012
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
1113
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

godoc-current.txt

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,24 @@ line Go applications. cli is designed to be easy to understand and write,
55
the most simple cli application can be written as follows:
66

77
func main() {
8-
(&cli.App{}).Run(os.Args)
8+
(&cli.App{}).Run(os.Args)
99
}
1010

1111
Of course this application does not do much, so let's make this an actual
1212
application:
1313

14-
func main() {
15-
app := &cli.App{
16-
Name: "greet",
17-
Usage: "say a greeting",
18-
Action: func(c *cli.Context) error {
19-
fmt.Println("Greetings")
20-
return nil
21-
},
22-
}
23-
24-
app.Run(os.Args)
25-
}
14+
func main() {
15+
app := &cli.App{
16+
Name: "greet",
17+
Usage: "say a greeting",
18+
Action: func(c *cli.Context) error {
19+
fmt.Println("Greetings")
20+
return nil
21+
},
22+
}
23+
24+
app.Run(os.Args)
25+
}
2626

2727
VARIABLES
2828

0 commit comments

Comments
 (0)