diff --git a/docs/v2/manual.md b/docs/v2/manual.md index 2243c6d86e..af09010b9a 100644 --- a/docs/v2/manual.md +++ b/docs/v2/manual.md @@ -649,7 +649,7 @@ func main() { app := &cli.App{ Action: func(c *cli.Context) error { - fmt.Println("yaml ist rad") + fmt.Println("--test value.*default: 0") return nil }, Before: altsrc.InitInputSourceWithContext(flags, altsrc.NewYamlSourceFromFlagFunc("load")), diff --git a/flag_int64_slice.go b/flag_int64_slice.go index 6c7fd9376d..2c9a15af3e 100644 --- a/flag_int64_slice.go +++ b/flag_int64_slice.go @@ -145,7 +145,10 @@ func (f *Int64SliceFlag) Apply(set *flag.FlagSet) error { // Int64Slice looks up the value of a local Int64SliceFlag, returns // nil if not found func (c *Context) Int64Slice(name string) []int64 { - return lookupInt64Slice(name, c.flagSet) + if fs := lookupFlagSet(name, c); fs != nil { + return lookupInt64Slice(name, fs) + } + return nil } func lookupInt64Slice(name string, set *flag.FlagSet) []int64 { diff --git a/flag_int_slice.go b/flag_int_slice.go index 4e0afc0210..a73ca6b81c 100644 --- a/flag_int_slice.go +++ b/flag_int_slice.go @@ -157,7 +157,7 @@ func (f *IntSliceFlag) Apply(set *flag.FlagSet) error { // nil if not found func (c *Context) IntSlice(name string) []int { if fs := lookupFlagSet(name, c); fs != nil { - return lookupIntSlice(name, c.flagSet) + return lookupIntSlice(name, fs) } return nil } diff --git a/flag_test.go b/flag_test.go index b1fe70a4fd..e00fd9f31c 100644 --- a/flag_test.go +++ b/flag_test.go @@ -674,6 +674,45 @@ func TestIntSliceFlagApply_SetsAllNames(t *testing.T) { expect(t, err, nil) } +func TestIntSliceFlagApply_ParentContext(t *testing.T) { + _ = (&App{ + Flags: []Flag{ + &IntSliceFlag{Name: "numbers", Aliases: []string{"n"}, Value: NewIntSlice(1, 2, 3)}, + }, + Commands: []*Command{ + { + Name: "child", + Action: func(ctx *Context) error { + expected := []int{1, 2, 3} + if !reflect.DeepEqual(ctx.IntSlice("numbers"), expected) { + t.Errorf("child context unable to view parent flag: %v != %v", expected, ctx.IntSlice("numbers")) + } + if !reflect.DeepEqual(ctx.IntSlice("n"), expected) { + t.Errorf("child context unable to view parent flag: %v != %v", expected, ctx.IntSlice("n")) + } + return nil + }, + }, + }, + }).Run([]string{"run", "child"}) +} + +func TestIntSliceFlag_SetFromParentContext(t *testing.T) { + fl := &IntSliceFlag{Name: "numbers", Aliases: []string{"n"}, Value: NewIntSlice(1, 2, 3, 4)} + set := flag.NewFlagSet("test", 0) + _ = fl.Apply(set) + ctx := &Context{ + parentContext: &Context{ + flagSet: set, + }, + flagSet: flag.NewFlagSet("empty", 0), + } + expected := []int{1, 2, 3, 4} + if !reflect.DeepEqual(ctx.IntSlice("numbers"), expected) { + t.Errorf("child context unable to view parent flag: %v != %v", expected, ctx.IntSlice("numbers")) + } +} + var int64SliceFlagTests = []struct { name string aliases []string @@ -716,6 +755,60 @@ func TestInt64SliceFlagWithEnvVarHelpOutput(t *testing.T) { } } +func TestInt64SliceFlagApply_ParentContext(t *testing.T) { + _ = (&App{ + Flags: []Flag{ + &Int64SliceFlag{Name: "numbers", Aliases: []string{"n"}, Value: NewInt64Slice(1, 2, 3)}, + }, + Commands: []*Command{ + { + Name: "child", + Action: func(ctx *Context) error { + expected := []int64{1, 2, 3} + if !reflect.DeepEqual(ctx.Int64Slice("numbers"), expected) { + t.Errorf("child context unable to view parent flag: %v != %v", expected, ctx.Int64Slice("numbers")) + } + if !reflect.DeepEqual(ctx.Int64Slice("n"), expected) { + t.Errorf("child context unable to view parent flag: %v != %v", expected, ctx.Int64Slice("n")) + } + return nil + }, + }, + }, + }).Run([]string{"run", "child"}) +} + +func TestInt64SliceFlag_SetFromParentContext(t *testing.T) { + fl := &Int64SliceFlag{Name: "numbers", Aliases: []string{"n"}, Value: NewInt64Slice(1, 2, 3, 4)} + set := flag.NewFlagSet("test", 0) + _ = fl.Apply(set) + ctx := &Context{ + parentContext: &Context{ + flagSet: set, + }, + flagSet: flag.NewFlagSet("empty", 0), + } + expected := []int64{1, 2, 3, 4} + if !reflect.DeepEqual(ctx.Int64Slice("numbers"), expected) { + t.Errorf("child context unable to view parent flag: %v != %v", expected, ctx.Int64Slice("numbers")) + } +} +func TestInt64SliceFlag_ReturnNil(t *testing.T) { + fl := &Int64SliceFlag{} + set := flag.NewFlagSet("test", 0) + _ = fl.Apply(set) + ctx := &Context{ + parentContext: &Context{ + flagSet: set, + }, + flagSet: flag.NewFlagSet("empty", 0), + } + expected := []int64(nil) + if !reflect.DeepEqual(ctx.Int64Slice("numbers"), expected) { + t.Errorf("child context unable to view parent flag: %v != %v", expected, ctx.Int64Slice("numbers")) + } +} + var float64FlagTests = []struct { name string expected string