Merge pull request #229 from saromanov/fix-missing-value

Fix panic if Valus in Int/StringSliceFlasg is missing
This commit is contained in:
Jesse Szwedko 2015-06-01 20:39:11 -07:00
commit 5d58d5b313
2 changed files with 38 additions and 0 deletions

View File

@ -342,6 +342,38 @@ func TestApp_ParseSliceFlags(t *testing.T) {
} }
} }
func TestApp_ParseSliceFlagsWithMissingValue(t *testing.T) {
var parsedIntSlice []int
var parsedStringSlice []string
app := cli.NewApp()
command := cli.Command{
Name: "cmd",
Flags: []cli.Flag{
cli.IntSliceFlag{Name: "a", Usage: "set numbers"},
cli.StringSliceFlag{Name: "str", Usage: "set strings"},
},
Action: func(c *cli.Context) {
parsedIntSlice = c.IntSlice("a")
parsedStringSlice = c.StringSlice("str")
},
}
app.Commands = []cli.Command{command}
app.Run([]string{"", "cmd", "my-arg", "-a", "2", "-str", "A"})
var expectedIntSlice = []int{2}
var expectedStringSlice = []string{"A"}
if parsedIntSlice[0] != expectedIntSlice[0] {
t.Errorf("%v does not match %v", parsedIntSlice[0], expectedIntSlice[0])
}
if parsedStringSlice[0] != expectedStringSlice[0] {
t.Errorf("%v does not match %v", parsedIntSlice[0], expectedIntSlice[0])
}
}
func TestApp_DefaultStdout(t *testing.T) { func TestApp_DefaultStdout(t *testing.T) {
app := cli.NewApp() app := cli.NewApp()

View File

@ -144,6 +144,9 @@ func (f StringSliceFlag) Apply(set *flag.FlagSet) {
} }
eachName(f.Name, func(name string) { eachName(f.Name, func(name string) {
if f.Value == nil {
f.Value = &StringSlice{}
}
set.Var(f.Value, name, f.Usage) set.Var(f.Value, name, f.Usage)
}) })
} }
@ -206,6 +209,9 @@ func (f IntSliceFlag) Apply(set *flag.FlagSet) {
} }
eachName(f.Name, func(name string) { eachName(f.Name, func(name string) {
if f.Value == nil {
f.Value = &IntSlice{}
}
set.Var(f.Value, name, f.Usage) set.Var(f.Value, name, f.Usage)
}) })
} }