From 5dafdb1de66163e0149529468e66fe127dcd5866 Mon Sep 17 00:00:00 2001 From: Jim Powers Date: Wed, 2 May 2018 08:55:01 -0400 Subject: [PATCH 1/2] Exposed the `value` accessor in `Context` --- context.go | 2 +- flag_test.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/context.go b/context.go index 535c38818a..b88b703824 100644 --- a/context.go +++ b/context.go @@ -115,7 +115,7 @@ func (c *Context) Lineage() []*Context { } // value returns the value of the flag corresponding to `name` -func (c *Context) value(name string) interface{} { +func (c *Context) Value(name string) interface{} { return c.flagSet.Lookup(name).Value.(flag.Getter).Get() } diff --git a/flag_test.go b/flag_test.go index 5d0ecaea94..769f7bf687 100644 --- a/flag_test.go +++ b/flag_test.go @@ -121,8 +121,8 @@ func TestFlagsFromEnv(t *testing.T) { a := App{ Flags: []Flag{test.flag}, Action: func(ctx *Context) error { - if !reflect.DeepEqual(ctx.value(test.flag.Names()[0]), test.output) { - t.Errorf("ex:%01d expected %q to be parsed as %#v, instead was %#v", i, test.input, test.output, ctx.value(test.flag.Names()[0])) + if !reflect.DeepEqual(ctx.Value(test.flag.Names()[0]), test.output) { + t.Errorf("ex:%01d expected %q to be parsed as %#v, instead was %#v", i, test.input, test.output, ctx.Value(test.flag.Names()[0])) } return nil }, From 615e70ef221c7319679fbcafe151f34a75e26513 Mon Sep 17 00:00:00 2001 From: Jim Powers Date: Mon, 18 Nov 2019 07:38:48 -0500 Subject: [PATCH 2/2] Rebased upstream - Fixed NPE --- context.go | 2 +- context_test.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/context.go b/context.go index b88b703824..13fb52358d 100644 --- a/context.go +++ b/context.go @@ -114,7 +114,7 @@ func (c *Context) Lineage() []*Context { return lineage } -// value returns the value of the flag corresponding to `name` +// Value returns the value of the flag corresponding to `name` func (c *Context) Value(name string) interface{} { return c.flagSet.Lookup(name).Value.(flag.Getter).Get() } diff --git a/context_test.go b/context_test.go index 081a8c43b9..3cefcca02f 100644 --- a/context_test.go +++ b/context_test.go @@ -3,8 +3,8 @@ package cli import ( "context" "flag" - "sort" "os" + "sort" "strings" "testing" "time" @@ -328,7 +328,7 @@ func TestContextPropagation(t *testing.T) { parent := NewContext(nil, nil, nil) parent.Context = context.WithValue(context.Background(), "key", "val") ctx := NewContext(nil, nil, parent) - val := ctx.Value("key") + val := ctx.Context.Value("key") if val == nil { t.Fatal("expected a parent context to be inherited but got nil") }