From 2144cc5ad452f481b653ad978718c06235f421c6 Mon Sep 17 00:00:00 2001 From: Andrew Nicoll Date: Wed, 9 Dec 2020 11:17:21 +0000 Subject: [PATCH 1/6] fix for parent context not setting child flags --- flag_int64_slice.go | 5 ++++- flag_int_slice.go | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) 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 } From a27ce0e881cf595969a2ff74beb7b6d5727f987c Mon Sep 17 00:00:00 2001 From: Andrew Nicoll Date: Wed, 9 Dec 2020 11:48:33 +0000 Subject: [PATCH 2/6] unable to see whats happening here with lack of debug but this seems to resolve the failing doc test --- docs/v2/manual.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/v2/manual.md b/docs/v2/manual.md index e99afc1fb3..fc3c1ba094 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")), From 55992853bf271b53549dc850f70344ffc9d9f3ea Mon Sep 17 00:00:00 2001 From: Andrew Nicoll Date: Wed, 9 Dec 2020 11:59:50 +0000 Subject: [PATCH 3/6] add flag tests --- flag_test.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/flag_test.go b/flag_test.go index b1fe70a4fd..99e6ea563b 100644 --- a/flag_test.go +++ b/flag_test.go @@ -674,6 +674,29 @@ 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"}) +} + var int64SliceFlagTests = []struct { name string aliases []string @@ -716,6 +739,29 @@ 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"}) +} + var float64FlagTests = []struct { name string expected string From 02f3866db27692ef9bf87fbb183ec72ad4e5a799 Mon Sep 17 00:00:00 2001 From: Andrew Nicoll Date: Wed, 9 Dec 2020 12:26:08 +0000 Subject: [PATCH 4/6] add nother test --- flag_test.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/flag_test.go b/flag_test.go index 99e6ea563b..5d94992afe 100644 --- a/flag_test.go +++ b/flag_test.go @@ -697,6 +697,22 @@ func TestIntSliceFlagApply_ParentContext(t *testing.T) { }).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 From ebe2c0ea70cee5e140c2a21670dbfc6fa5307ce6 Mon Sep 17 00:00:00 2001 From: Andrew Nicoll Date: Wed, 9 Dec 2020 12:27:56 +0000 Subject: [PATCH 5/6] add for int64 --- flag_test.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/flag_test.go b/flag_test.go index 5d94992afe..073e74ebab 100644 --- a/flag_test.go +++ b/flag_test.go @@ -778,6 +778,22 @@ func TestInt64SliceFlagApply_ParentContext(t *testing.T) { }).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")) + } +} + var float64FlagTests = []struct { name string expected string From 06e7bdec349457f1e05f8ef6b567b0214fdf8df6 Mon Sep 17 00:00:00 2001 From: Andrew Nicoll Date: Wed, 27 Jan 2021 18:41:52 +0000 Subject: [PATCH 6/6] add test for nil --- flag_test.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/flag_test.go b/flag_test.go index 073e74ebab..e00fd9f31c 100644 --- a/flag_test.go +++ b/flag_test.go @@ -793,6 +793,21 @@ func TestInt64SliceFlag_SetFromParentContext(t *testing.T) { 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