From baf8ae98deb3bae30567f621d5167694da2a1402 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tilo=20Pr=C3=BCtz?= Date: Fri, 22 Apr 2022 15:48:26 +0200 Subject: [PATCH 01/16] BoolFlag.ValueFromContext() as convenient accessor --- flag_bool.go | 5 +++++ flag_test.go | 11 +++++++++++ 2 files changed, 16 insertions(+) diff --git a/flag_bool.go b/flag_bool.go index b8e625a1d1..bdca2b5886 100644 --- a/flag_bool.go +++ b/flag_bool.go @@ -102,6 +102,11 @@ func (f *BoolFlag) Apply(set *flag.FlagSet) error { return nil } +// ValueFromContext returns the flag’s value in the given Context. +func (f *BoolFlag) ValueFromContext(ctx *Context) bool { + return ctx.Bool(f.Name) +} + // Bool looks up the value of a local BoolFlag, returns // false if not found func (c *Context) Bool(name string) bool { diff --git a/flag_test.go b/flag_test.go index 44c3500a63..53cb60bd5c 100644 --- a/flag_test.go +++ b/flag_test.go @@ -51,6 +51,17 @@ func TestBoolFlagApply_SetsAllNames(t *testing.T) { expect(t, v, true) } +func TestBoolFlagValueFromContext(t *testing.T) { + set := flag.NewFlagSet("test", 0) + set.Bool("trueflag", true, "doc") + set.Bool("falseflag", false, "doc") + ctx := NewContext(nil, set, nil) + tf := &BoolFlag{Name: "trueflag"} + ff := &BoolFlag{Name: "falseflag"} + expect(t, tf.ValueFromContext(ctx), true) + expect(t, ff.ValueFromContext(ctx), false) +} + func TestFlagsFromEnv(t *testing.T) { newSetFloat64Slice := func(defaults ...float64) Float64Slice { s := NewFloat64Slice(defaults...) From 9eae255aac5411488cdebaf71efe73440fcdf929 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tilo=20Pr=C3=BCtz?= Date: Fri, 22 Apr 2022 15:50:13 +0200 Subject: [PATCH 02/16] DurationFlag.ValueFromContext() as convenient accessor --- flag_duration.go | 5 +++++ flag_test.go | 8 ++++++++ 2 files changed, 13 insertions(+) diff --git a/flag_duration.go b/flag_duration.go index e8ca15ec02..1de10b18dd 100644 --- a/flag_duration.go +++ b/flag_duration.go @@ -101,6 +101,11 @@ func (f *DurationFlag) Apply(set *flag.FlagSet) error { return nil } +// ValueFromContext returns the flag’s value in the given Context. +func (f *DurationFlag) ValueFromContext(ctx *Context) time.Duration { + return ctx.Duration(f.Name) +} + // Duration looks up the value of a local DurationFlag, returns // 0 if not found func (c *Context) Duration(name string) time.Duration { diff --git a/flag_test.go b/flag_test.go index 53cb60bd5c..2f41f42fcd 100644 --- a/flag_test.go +++ b/flag_test.go @@ -826,6 +826,14 @@ func TestDurationFlagApply_SetsAllNames(t *testing.T) { expect(t, v, time.Hour*30) } +func TestDurationFlagValueFromContext(t *testing.T) { + set := flag.NewFlagSet("test", 0) + set.Duration("myflag", 42*time.Second, "doc") + ctx := NewContext(nil, set, nil) + f := &DurationFlag{Name: "myflag"} + expect(t, f.ValueFromContext(ctx), 42*time.Second) +} + var intSliceFlagTests = []struct { name string aliases []string From 5047beb00185b71caa446147381f89a3178f575b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tilo=20Pr=C3=BCtz?= Date: Fri, 22 Apr 2022 15:55:37 +0200 Subject: [PATCH 03/16] Float64Flag.ValueFromContext() as convenient accessor --- flag_float64.go | 5 +++++ flag_test.go | 8 ++++++++ 2 files changed, 13 insertions(+) diff --git a/flag_float64.go b/flag_float64.go index 0ac5b43f0d..f2eb0400e1 100644 --- a/flag_float64.go +++ b/flag_float64.go @@ -101,6 +101,11 @@ func (f *Float64Flag) Apply(set *flag.FlagSet) error { return nil } +// ValueFromContext returns the flag’s value in the given Context. +func (f *Float64Flag) ValueFromContext(ctx *Context) float64 { + return ctx.Float64(f.Name) +} + // Float64 looks up the value of a local Float64Flag, returns // 0 if not found func (c *Context) Float64(name string) float64 { diff --git a/flag_test.go b/flag_test.go index 2f41f42fcd..edf9e8e1d7 100644 --- a/flag_test.go +++ b/flag_test.go @@ -1068,6 +1068,14 @@ func TestFloat64FlagApply_SetsAllNames(t *testing.T) { expect(t, v, float64(43.33333)) } +func TestFloat64FlagValueFromContext(t *testing.T) { + set := flag.NewFlagSet("test", 0) + set.Float64("myflag", 1.23, "doc") + ctx := NewContext(nil, set, nil) + f := &Float64Flag{Name: "myflag"} + expect(t, f.ValueFromContext(ctx), 1.23) +} + var float64SliceFlagTests = []struct { name string aliases []string From 2f92fc644ca176d529d751f5e19675bdebb85d06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tilo=20Pr=C3=BCtz?= Date: Fri, 22 Apr 2022 16:04:42 +0200 Subject: [PATCH 04/16] Float64SliceFlag.ValueFromContext() as convenient accessor --- flag_float64_slice.go | 5 +++++ flag_test.go | 8 ++++++++ 2 files changed, 13 insertions(+) diff --git a/flag_float64_slice.go b/flag_float64_slice.go index 984f77f7f3..c987688daf 100644 --- a/flag_float64_slice.go +++ b/flag_float64_slice.go @@ -175,6 +175,11 @@ func (f *Float64SliceFlag) Apply(set *flag.FlagSet) error { return nil } +// ValueFromContext returns the flag’s value in the given Context. +func (f *Float64SliceFlag) ValueFromContext(ctx *Context) []float64 { + return ctx.Float64Slice(f.Name) +} + // Float64Slice looks up the value of a local Float64SliceFlag, returns // nil if not found func (c *Context) Float64Slice(name string) []float64 { diff --git a/flag_test.go b/flag_test.go index edf9e8e1d7..ce9b5a3dd0 100644 --- a/flag_test.go +++ b/flag_test.go @@ -1117,6 +1117,14 @@ func TestFloat64SliceFlagWithEnvVarHelpOutput(t *testing.T) { } } +func TestFloat64SliceFlagValueFromContext(t *testing.T) { + set := flag.NewFlagSet("test", 0) + set.Var(NewFloat64Slice(1.23, 4.56), "myflag", "doc") + ctx := NewContext(nil, set, nil) + f := &Float64SliceFlag{Name: "myflag"} + expect(t, f.ValueFromContext(ctx), []float64{1.23, 4.56}) +} + var genericFlagTests = []struct { name string value Generic From 8bd5fb2390f6b8a4f2cba2376302cce8a545d5e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tilo=20Pr=C3=BCtz?= Date: Fri, 22 Apr 2022 16:10:09 +0200 Subject: [PATCH 05/16] GenericFlag.ValueFromContext() as convenient accessor --- flag_generic.go | 5 +++++ flag_test.go | 8 ++++++++ 2 files changed, 13 insertions(+) diff --git a/flag_generic.go b/flag_generic.go index d159507147..483c7d146a 100644 --- a/flag_generic.go +++ b/flag_generic.go @@ -104,6 +104,11 @@ func (f GenericFlag) Apply(set *flag.FlagSet) error { return nil } +// ValueFromContext returns the flag’s value in the given Context. +func (f *GenericFlag) ValueFromContext(ctx *Context) interface{} { + return ctx.Generic(f.Name) +} + // Generic looks up the value of a local GenericFlag, returns // nil if not found func (c *Context) Generic(name string) interface{} { diff --git a/flag_test.go b/flag_test.go index ce9b5a3dd0..7fa6673e77 100644 --- a/flag_test.go +++ b/flag_test.go @@ -1173,6 +1173,14 @@ func TestGenericFlagApply_SetsAllNames(t *testing.T) { expect(t, err, nil) } +func TestGenericFlagValueFromContext(t *testing.T) { + set := flag.NewFlagSet("test", 0) + set.Var(&Parser{"abc", "def"}, "myflag", "doc") + ctx := NewContext(nil, set, nil) + f := &GenericFlag{Name: "myflag"} + expect(t, f.ValueFromContext(ctx), &Parser{"abc", "def"}) +} + func TestParseMultiString(t *testing.T) { _ = (&App{ Flags: []Flag{ From bf18c00347867c9f8be0e872a82b4e1baed24d45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tilo=20Pr=C3=BCtz?= Date: Fri, 22 Apr 2022 16:12:11 +0200 Subject: [PATCH 06/16] IntFlag.ValueFromContext() as convenient accessor --- flag_int.go | 5 +++++ flag_test.go | 8 ++++++++ 2 files changed, 13 insertions(+) diff --git a/flag_int.go b/flag_int.go index 62c0848688..642a68b77f 100644 --- a/flag_int.go +++ b/flag_int.go @@ -102,6 +102,11 @@ func (f *IntFlag) Apply(set *flag.FlagSet) error { return nil } +// ValueFromContext returns the flag’s value in the given Context. +func (f *IntFlag) ValueFromContext(ctx *Context) int { + return ctx.Int(f.Name) +} + // Int looks up the value of a local IntFlag, returns // 0 if not found func (c *Context) Int(name string) int { diff --git a/flag_test.go b/flag_test.go index 7fa6673e77..86d281ed5a 100644 --- a/flag_test.go +++ b/flag_test.go @@ -663,6 +663,14 @@ func TestIntFlagApply_SetsAllNames(t *testing.T) { expect(t, v, 5) } +func TestIntFlagValueFromContext(t *testing.T) { + set := flag.NewFlagSet("test", 0) + set.Int("myflag", 42, "doc") + ctx := NewContext(nil, set, nil) + f := &IntFlag{Name: "myflag"} + expect(t, f.ValueFromContext(ctx), 42) +} + var int64FlagTests = []struct { name string expected string From 18b44dfb291d23ff4db2f388e746d88e6f7a310e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tilo=20Pr=C3=BCtz?= Date: Fri, 22 Apr 2022 16:45:10 +0200 Subject: [PATCH 07/16] Int64Flag.ValueFromContext() as convenient accessor --- flag_int64.go | 5 +++++ flag_test.go | 8 ++++++++ 2 files changed, 13 insertions(+) diff --git a/flag_int64.go b/flag_int64.go index 2f0be7aa06..6b40374cbd 100644 --- a/flag_int64.go +++ b/flag_int64.go @@ -101,6 +101,11 @@ func (f *Int64Flag) Apply(set *flag.FlagSet) error { return nil } +// ValueFromContext returns the flag’s value in the given Context. +func (f *Int64Flag) ValueFromContext(ctx *Context) int64 { + return ctx.Int64(f.Name) +} + // Int64 looks up the value of a local Int64Flag, returns // 0 if not found func (c *Context) Int64(name string) int64 { diff --git a/flag_test.go b/flag_test.go index 86d281ed5a..0b343e9666 100644 --- a/flag_test.go +++ b/flag_test.go @@ -709,6 +709,14 @@ func TestInt64FlagWithEnvVarHelpOutput(t *testing.T) { } } +func TestInt64FlagValueFromContext(t *testing.T) { + set := flag.NewFlagSet("test", 0) + set.Int64("myflag", 42, "doc") + ctx := NewContext(nil, set, nil) + f := &Int64Flag{Name: "myflag"} + expect(t, f.ValueFromContext(ctx), int64(42)) +} + var uintFlagTests = []struct { name string expected string From dcc47855b8d7685e47644ee6966df6dc7de8dfa4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tilo=20Pr=C3=BCtz?= Date: Fri, 22 Apr 2022 16:45:10 +0200 Subject: [PATCH 08/16] Int64SliceFlag.ValueFromContext() as convenient accessor --- flag_int64_slice.go | 5 +++++ flag_test.go | 8 ++++++++ 2 files changed, 13 insertions(+) diff --git a/flag_int64_slice.go b/flag_int64_slice.go index a53b185078..29e8dc46f1 100644 --- a/flag_int64_slice.go +++ b/flag_int64_slice.go @@ -174,6 +174,11 @@ func (f *Int64SliceFlag) Apply(set *flag.FlagSet) error { return nil } +// ValueFromContext returns the flag’s value in the given Context. +func (f *Int64SliceFlag) ValueFromContext(ctx *Context) []int64 { + return ctx.Int64Slice(f.Name) +} + // Int64Slice looks up the value of a local Int64SliceFlag, returns // nil if not found func (c *Context) Int64Slice(name string) []int64 { diff --git a/flag_test.go b/flag_test.go index 0b343e9666..d510235a6a 100644 --- a/flag_test.go +++ b/flag_test.go @@ -1035,6 +1035,14 @@ func TestInt64SliceFlag_ReturnNil(t *testing.T) { } } +func TestInt64SliceFlagValueFromContext(t *testing.T) { + set := flag.NewFlagSet("test", 0) + set.Var(NewInt64Slice(1, 2, 3), "myflag", "doc") + ctx := NewContext(nil, set, nil) + f := &Int64SliceFlag{Name: "myflag"} + expect(t, f.ValueFromContext(ctx), []int64{1, 2, 3}) +} + var float64FlagTests = []struct { name string expected string From 6d7f8590089276c1f11e91f93830f6ada78a035c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tilo=20Pr=C3=BCtz?= Date: Fri, 22 Apr 2022 16:49:38 +0200 Subject: [PATCH 09/16] IntSliceFlag.ValueFromContext() as convenient accessor --- flag_int_slice.go | 5 +++++ flag_test.go | 8 ++++++++ 2 files changed, 13 insertions(+) diff --git a/flag_int_slice.go b/flag_int_slice.go index 5f3bd88f05..4d1741c6a4 100644 --- a/flag_int_slice.go +++ b/flag_int_slice.go @@ -185,6 +185,11 @@ func (f *IntSliceFlag) Apply(set *flag.FlagSet) error { return nil } +// ValueFromContext returns the flag’s value in the given Context. +func (f *IntSliceFlag) ValueFromContext(ctx *Context) []int { + return ctx.IntSlice(f.Name) +} + // IntSlice looks up the value of a local IntSliceFlag, returns // nil if not found func (c *Context) IntSlice(name string) []int { diff --git a/flag_test.go b/flag_test.go index d510235a6a..e80231d7cc 100644 --- a/flag_test.go +++ b/flag_test.go @@ -939,6 +939,14 @@ func TestIntSliceFlag_SetFromParentContext(t *testing.T) { } } +func TestIntSliceFlagValueFromContext(t *testing.T) { + set := flag.NewFlagSet("test", 0) + set.Var(NewIntSlice(1, 2, 3), "myflag", "doc") + ctx := NewContext(nil, set, nil) + f := &IntSliceFlag{Name: "myflag"} + expect(t, f.ValueFromContext(ctx), []int{1, 2, 3}) +} + var int64SliceFlagTests = []struct { name string aliases []string From 660184dd92d1f13f1156dfceb1f22df327dfbd3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tilo=20Pr=C3=BCtz?= Date: Fri, 22 Apr 2022 16:59:19 +0200 Subject: [PATCH 10/16] PathFlag.ValueFromContext() as convenient accessor --- flag_path.go | 5 +++++ flag_test.go | 8 ++++++++ 2 files changed, 13 insertions(+) diff --git a/flag_path.go b/flag_path.go index 4010e84c62..40c90096f8 100644 --- a/flag_path.go +++ b/flag_path.go @@ -96,6 +96,11 @@ func (f *PathFlag) Apply(set *flag.FlagSet) error { return nil } +// ValueFromContext returns the flag’s value in the given Context. +func (f *PathFlag) ValueFromContext(ctx *Context) string { + return ctx.Path(f.Name) +} + // Path looks up the value of a local PathFlag, returns // "" if not found func (c *Context) Path(name string) string { diff --git a/flag_test.go b/flag_test.go index e80231d7cc..1ff56f82d2 100644 --- a/flag_test.go +++ b/flag_test.go @@ -501,6 +501,14 @@ func TestPathFlagApply_SetsAllNames(t *testing.T) { expect(t, v, "/path/to/file/PATH") } +func TestPathFlagValueFromContext(t *testing.T) { + set := flag.NewFlagSet("test", 0) + set.String("myflag", "/my/path", "doc") + ctx := NewContext(nil, set, nil) + f := &PathFlag{Name: "myflag"} + expect(t, f.ValueFromContext(ctx), "/my/path") +} + var envHintFlagTests = []struct { name string env string From ce4d9279c42ef762256900e52a58f8cc3ad48b08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tilo=20Pr=C3=BCtz?= Date: Fri, 22 Apr 2022 17:02:18 +0200 Subject: [PATCH 11/16] StringFlag.ValueFromContext() as convenient accessor --- flag_string.go | 5 +++++ flag_test.go | 8 ++++++++ 2 files changed, 13 insertions(+) diff --git a/flag_string.go b/flag_string.go index cd3c7dff4a..7464a2823f 100644 --- a/flag_string.go +++ b/flag_string.go @@ -97,6 +97,11 @@ func (f *StringFlag) Apply(set *flag.FlagSet) error { return nil } +// ValueFromContext returns the flag’s value in the given Context. +func (f *StringFlag) ValueFromContext(ctx *Context) string { + return ctx.String(f.Name) +} + // String looks up the value of a local StringFlag, returns // "" if not found func (c *Context) String(name string) string { diff --git a/flag_test.go b/flag_test.go index 1ff56f82d2..aa9fbecd29 100644 --- a/flag_test.go +++ b/flag_test.go @@ -450,6 +450,14 @@ func TestStringFlagApply_SetsAllNames(t *testing.T) { expect(t, v, "YUUUU") } +func TestStringFlagValueFromContext(t *testing.T) { + set := flag.NewFlagSet("test", 0) + set.String("myflag", "foobar", "doc") + ctx := NewContext(nil, set, nil) + f := &StringFlag{Name: "myflag"} + expect(t, f.ValueFromContext(ctx), "foobar") +} + var pathFlagTests = []struct { name string aliases []string From 6b336c478f198630e67e5adeb53fe8da24640165 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tilo=20Pr=C3=BCtz?= Date: Fri, 22 Apr 2022 17:04:13 +0200 Subject: [PATCH 12/16] StringSliceFlag.ValueFromContext() as convenient accessor --- flag_string_slice.go | 5 +++++ flag_test.go | 8 ++++++++ 2 files changed, 13 insertions(+) diff --git a/flag_string_slice.go b/flag_string_slice.go index 166424775f..90e6ebd63a 100644 --- a/flag_string_slice.go +++ b/flag_string_slice.go @@ -186,6 +186,11 @@ func (f *StringSliceFlag) Apply(set *flag.FlagSet) error { return nil } +// ValueFromContext returns the flag’s value in the given Context. +func (f *StringSliceFlag) ValueFromContext(ctx *Context) []string { + return ctx.StringSlice(f.Name) +} + // StringSlice looks up the value of a local StringSliceFlag, returns // nil if not found func (c *Context) StringSlice(name string) []string { diff --git a/flag_test.go b/flag_test.go index aa9fbecd29..cda34beca5 100644 --- a/flag_test.go +++ b/flag_test.go @@ -630,6 +630,14 @@ func TestStringSliceFlagApply_DefaultValueWithDestination(t *testing.T) { expect(t, defValue, fl.Destination.Value()) } +func TestStringSliceFlagValueFromContext(t *testing.T) { + set := flag.NewFlagSet("test", 0) + set.Var(NewStringSlice("a", "b", "c"), "myflag", "doc") + ctx := NewContext(nil, set, nil) + f := &StringSliceFlag{Name: "myflag"} + expect(t, f.ValueFromContext(ctx), []string{"a", "b", "c"}) +} + var intFlagTests = []struct { name string expected string From 889c7b5d7a4deb4f9aaaeed3a113933910253d08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tilo=20Pr=C3=BCtz?= Date: Fri, 22 Apr 2022 17:07:12 +0200 Subject: [PATCH 13/16] TimestampFlag.ValueFromContext() as convenient accessor --- flag_test.go | 9 +++++++++ flag_timestamp.go | 5 +++++ 2 files changed, 14 insertions(+) diff --git a/flag_test.go b/flag_test.go index cda34beca5..11b3265d5c 100644 --- a/flag_test.go +++ b/flag_test.go @@ -2264,6 +2264,15 @@ func TestTimestampFlagApply_Fail_Parse_Wrong_Time(t *testing.T) { expect(t, err, fmt.Errorf("invalid value \"2006-01-02T15:04:05Z\" for flag -time: parsing time \"2006-01-02T15:04:05Z\" as \"Jan 2, 2006 at 3:04pm (MST)\": cannot parse \"2006-01-02T15:04:05Z\" as \"Jan\"")) } +func TestTimestampFlagValueFromContext(t *testing.T) { + set := flag.NewFlagSet("test", 0) + now := time.Now() + set.Var(NewTimestamp(now), "myflag", "doc") + ctx := NewContext(nil, set, nil) + f := &TimestampFlag{Name: "myflag"} + expect(t, f.ValueFromContext(ctx), &now) +} + type flagDefaultTestCase struct { name string flag Flag diff --git a/flag_timestamp.go b/flag_timestamp.go index ed06418284..a3b230e2e9 100644 --- a/flag_timestamp.go +++ b/flag_timestamp.go @@ -164,6 +164,11 @@ func (f *TimestampFlag) Apply(set *flag.FlagSet) error { return nil } +// ValueFromContext returns the flag’s value in the given Context. +func (f *TimestampFlag) ValueFromContext(ctx *Context) *time.Time { + return ctx.Timestamp(f.Name) +} + // Timestamp gets the timestamp from a flag name func (c *Context) Timestamp(name string) *time.Time { if fs := c.lookupFlagSet(name); fs != nil { From 1f621059d32f9f1b4f66b8c719016eb119f0d816 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tilo=20Pr=C3=BCtz?= Date: Fri, 22 Apr 2022 17:09:09 +0200 Subject: [PATCH 14/16] UintFlag.ValueFromContext() as convenient accessor --- flag_test.go | 8 ++++++++ flag_uint.go | 5 +++++ 2 files changed, 13 insertions(+) diff --git a/flag_test.go b/flag_test.go index 11b3265d5c..9e6b335e1b 100644 --- a/flag_test.go +++ b/flag_test.go @@ -779,6 +779,14 @@ func TestUintFlagWithEnvVarHelpOutput(t *testing.T) { } } +func TestUintFlagValueFromContext(t *testing.T) { + set := flag.NewFlagSet("test", 0) + set.Uint("myflag", 42, "doc") + ctx := NewContext(nil, set, nil) + f := &UintFlag{Name: "myflag"} + expect(t, f.ValueFromContext(ctx), uint(42)) +} + var uint64FlagTests = []struct { name string expected string diff --git a/flag_uint.go b/flag_uint.go index dd10e1c01d..b074c4e931 100644 --- a/flag_uint.go +++ b/flag_uint.go @@ -101,6 +101,11 @@ func (f *UintFlag) GetEnvVars() []string { return f.EnvVars } +// ValueFromContext returns the flag’s value in the given Context. +func (f *UintFlag) ValueFromContext(ctx *Context) uint { + return ctx.Uint(f.Name) +} + // Uint looks up the value of a local UintFlag, returns // 0 if not found func (c *Context) Uint(name string) uint { From ca7f26ecb04f8092897b05cdc2ccb03827a15da9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tilo=20Pr=C3=BCtz?= Date: Fri, 22 Apr 2022 17:11:41 +0200 Subject: [PATCH 15/16] Uint64Flag.ValueFromContext() as convenient accessor --- flag_test.go | 8 ++++++++ flag_uint64.go | 5 +++++ 2 files changed, 13 insertions(+) diff --git a/flag_test.go b/flag_test.go index 9e6b335e1b..4862c032e7 100644 --- a/flag_test.go +++ b/flag_test.go @@ -825,6 +825,14 @@ func TestUint64FlagWithEnvVarHelpOutput(t *testing.T) { } } +func TestUint64FlagValueFromContext(t *testing.T) { + set := flag.NewFlagSet("test", 0) + set.Uint64("myflag", 42, "doc") + ctx := NewContext(nil, set, nil) + f := &Uint64Flag{Name: "myflag"} + expect(t, f.ValueFromContext(ctx), uint64(42)) +} + var durationFlagTests = []struct { name string expected string diff --git a/flag_uint64.go b/flag_uint64.go index 017db53c1d..e79b1a7aed 100644 --- a/flag_uint64.go +++ b/flag_uint64.go @@ -101,6 +101,11 @@ func (f *Uint64Flag) GetEnvVars() []string { return f.EnvVars } +// ValueFromContext returns the flag’s value in the given Context. +func (f *Uint64Flag) ValueFromContext(ctx *Context) uint64 { + return ctx.Uint64(f.Name) +} + // Uint64 looks up the value of a local Uint64Flag, returns // 0 if not found func (c *Context) Uint64(name string) uint64 { From 835bd32714ec77ac75a7dd66f616ce94dcfc8ed2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tilo=20Pr=C3=BCtz?= Date: Mon, 25 Apr 2022 07:59:10 +0200 Subject: [PATCH 16/16] =?UTF-8?q?rename=20flags=E2=80=99=20ValueFromContex?= =?UTF-8?q?t()=20to=20Get()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- flag_bool.go | 4 ++-- flag_duration.go | 4 ++-- flag_float64.go | 4 ++-- flag_float64_slice.go | 4 ++-- flag_generic.go | 4 ++-- flag_int.go | 4 ++-- flag_int64.go | 4 ++-- flag_int64_slice.go | 4 ++-- flag_int_slice.go | 4 ++-- flag_path.go | 4 ++-- flag_string.go | 4 ++-- flag_string_slice.go | 4 ++-- flag_test.go | 32 ++++++++++++++++---------------- flag_timestamp.go | 4 ++-- flag_uint.go | 4 ++-- flag_uint64.go | 4 ++-- 16 files changed, 46 insertions(+), 46 deletions(-) diff --git a/flag_bool.go b/flag_bool.go index bdca2b5886..3caeb0831c 100644 --- a/flag_bool.go +++ b/flag_bool.go @@ -102,8 +102,8 @@ func (f *BoolFlag) Apply(set *flag.FlagSet) error { return nil } -// ValueFromContext returns the flag’s value in the given Context. -func (f *BoolFlag) ValueFromContext(ctx *Context) bool { +// Get returns the flag’s value in the given Context. +func (f *BoolFlag) Get(ctx *Context) bool { return ctx.Bool(f.Name) } diff --git a/flag_duration.go b/flag_duration.go index 1de10b18dd..3452aac93f 100644 --- a/flag_duration.go +++ b/flag_duration.go @@ -101,8 +101,8 @@ func (f *DurationFlag) Apply(set *flag.FlagSet) error { return nil } -// ValueFromContext returns the flag’s value in the given Context. -func (f *DurationFlag) ValueFromContext(ctx *Context) time.Duration { +// Get returns the flag’s value in the given Context. +func (f *DurationFlag) Get(ctx *Context) time.Duration { return ctx.Duration(f.Name) } diff --git a/flag_float64.go b/flag_float64.go index f2eb0400e1..fc1b3b9913 100644 --- a/flag_float64.go +++ b/flag_float64.go @@ -101,8 +101,8 @@ func (f *Float64Flag) Apply(set *flag.FlagSet) error { return nil } -// ValueFromContext returns the flag’s value in the given Context. -func (f *Float64Flag) ValueFromContext(ctx *Context) float64 { +// Get returns the flag’s value in the given Context. +func (f *Float64Flag) Get(ctx *Context) float64 { return ctx.Float64(f.Name) } diff --git a/flag_float64_slice.go b/flag_float64_slice.go index c987688daf..35cc5354f5 100644 --- a/flag_float64_slice.go +++ b/flag_float64_slice.go @@ -175,8 +175,8 @@ func (f *Float64SliceFlag) Apply(set *flag.FlagSet) error { return nil } -// ValueFromContext returns the flag’s value in the given Context. -func (f *Float64SliceFlag) ValueFromContext(ctx *Context) []float64 { +// Get returns the flag’s value in the given Context. +func (f *Float64SliceFlag) Get(ctx *Context) []float64 { return ctx.Float64Slice(f.Name) } diff --git a/flag_generic.go b/flag_generic.go index 483c7d146a..74c896e861 100644 --- a/flag_generic.go +++ b/flag_generic.go @@ -104,8 +104,8 @@ func (f GenericFlag) Apply(set *flag.FlagSet) error { return nil } -// ValueFromContext returns the flag’s value in the given Context. -func (f *GenericFlag) ValueFromContext(ctx *Context) interface{} { +// Get returns the flag’s value in the given Context. +func (f *GenericFlag) Get(ctx *Context) interface{} { return ctx.Generic(f.Name) } diff --git a/flag_int.go b/flag_int.go index 642a68b77f..6929543a67 100644 --- a/flag_int.go +++ b/flag_int.go @@ -102,8 +102,8 @@ func (f *IntFlag) Apply(set *flag.FlagSet) error { return nil } -// ValueFromContext returns the flag’s value in the given Context. -func (f *IntFlag) ValueFromContext(ctx *Context) int { +// Get returns the flag’s value in the given Context. +func (f *IntFlag) Get(ctx *Context) int { return ctx.Int(f.Name) } diff --git a/flag_int64.go b/flag_int64.go index 6b40374cbd..55d1214895 100644 --- a/flag_int64.go +++ b/flag_int64.go @@ -101,8 +101,8 @@ func (f *Int64Flag) Apply(set *flag.FlagSet) error { return nil } -// ValueFromContext returns the flag’s value in the given Context. -func (f *Int64Flag) ValueFromContext(ctx *Context) int64 { +// Get returns the flag’s value in the given Context. +func (f *Int64Flag) Get(ctx *Context) int64 { return ctx.Int64(f.Name) } diff --git a/flag_int64_slice.go b/flag_int64_slice.go index 29e8dc46f1..212b47b380 100644 --- a/flag_int64_slice.go +++ b/flag_int64_slice.go @@ -174,8 +174,8 @@ func (f *Int64SliceFlag) Apply(set *flag.FlagSet) error { return nil } -// ValueFromContext returns the flag’s value in the given Context. -func (f *Int64SliceFlag) ValueFromContext(ctx *Context) []int64 { +// Get returns the flag’s value in the given Context. +func (f *Int64SliceFlag) Get(ctx *Context) []int64 { return ctx.Int64Slice(f.Name) } diff --git a/flag_int_slice.go b/flag_int_slice.go index 4d1741c6a4..82c045da17 100644 --- a/flag_int_slice.go +++ b/flag_int_slice.go @@ -185,8 +185,8 @@ func (f *IntSliceFlag) Apply(set *flag.FlagSet) error { return nil } -// ValueFromContext returns the flag’s value in the given Context. -func (f *IntSliceFlag) ValueFromContext(ctx *Context) []int { +// Get returns the flag’s value in the given Context. +func (f *IntSliceFlag) Get(ctx *Context) []int { return ctx.IntSlice(f.Name) } diff --git a/flag_path.go b/flag_path.go index 40c90096f8..82e540b525 100644 --- a/flag_path.go +++ b/flag_path.go @@ -96,8 +96,8 @@ func (f *PathFlag) Apply(set *flag.FlagSet) error { return nil } -// ValueFromContext returns the flag’s value in the given Context. -func (f *PathFlag) ValueFromContext(ctx *Context) string { +// Get returns the flag’s value in the given Context. +func (f *PathFlag) Get(ctx *Context) string { return ctx.Path(f.Name) } diff --git a/flag_string.go b/flag_string.go index 7464a2823f..258ca92b7d 100644 --- a/flag_string.go +++ b/flag_string.go @@ -97,8 +97,8 @@ func (f *StringFlag) Apply(set *flag.FlagSet) error { return nil } -// ValueFromContext returns the flag’s value in the given Context. -func (f *StringFlag) ValueFromContext(ctx *Context) string { +// Get returns the flag’s value in the given Context. +func (f *StringFlag) Get(ctx *Context) string { return ctx.String(f.Name) } diff --git a/flag_string_slice.go b/flag_string_slice.go index 90e6ebd63a..79257b6346 100644 --- a/flag_string_slice.go +++ b/flag_string_slice.go @@ -186,8 +186,8 @@ func (f *StringSliceFlag) Apply(set *flag.FlagSet) error { return nil } -// ValueFromContext returns the flag’s value in the given Context. -func (f *StringSliceFlag) ValueFromContext(ctx *Context) []string { +// Get returns the flag’s value in the given Context. +func (f *StringSliceFlag) Get(ctx *Context) []string { return ctx.StringSlice(f.Name) } diff --git a/flag_test.go b/flag_test.go index 4862c032e7..07f6f89a02 100644 --- a/flag_test.go +++ b/flag_test.go @@ -58,8 +58,8 @@ func TestBoolFlagValueFromContext(t *testing.T) { ctx := NewContext(nil, set, nil) tf := &BoolFlag{Name: "trueflag"} ff := &BoolFlag{Name: "falseflag"} - expect(t, tf.ValueFromContext(ctx), true) - expect(t, ff.ValueFromContext(ctx), false) + expect(t, tf.Get(ctx), true) + expect(t, ff.Get(ctx), false) } func TestFlagsFromEnv(t *testing.T) { @@ -455,7 +455,7 @@ func TestStringFlagValueFromContext(t *testing.T) { set.String("myflag", "foobar", "doc") ctx := NewContext(nil, set, nil) f := &StringFlag{Name: "myflag"} - expect(t, f.ValueFromContext(ctx), "foobar") + expect(t, f.Get(ctx), "foobar") } var pathFlagTests = []struct { @@ -514,7 +514,7 @@ func TestPathFlagValueFromContext(t *testing.T) { set.String("myflag", "/my/path", "doc") ctx := NewContext(nil, set, nil) f := &PathFlag{Name: "myflag"} - expect(t, f.ValueFromContext(ctx), "/my/path") + expect(t, f.Get(ctx), "/my/path") } var envHintFlagTests = []struct { @@ -635,7 +635,7 @@ func TestStringSliceFlagValueFromContext(t *testing.T) { set.Var(NewStringSlice("a", "b", "c"), "myflag", "doc") ctx := NewContext(nil, set, nil) f := &StringSliceFlag{Name: "myflag"} - expect(t, f.ValueFromContext(ctx), []string{"a", "b", "c"}) + expect(t, f.Get(ctx), []string{"a", "b", "c"}) } var intFlagTests = []struct { @@ -692,7 +692,7 @@ func TestIntFlagValueFromContext(t *testing.T) { set.Int("myflag", 42, "doc") ctx := NewContext(nil, set, nil) f := &IntFlag{Name: "myflag"} - expect(t, f.ValueFromContext(ctx), 42) + expect(t, f.Get(ctx), 42) } var int64FlagTests = []struct { @@ -738,7 +738,7 @@ func TestInt64FlagValueFromContext(t *testing.T) { set.Int64("myflag", 42, "doc") ctx := NewContext(nil, set, nil) f := &Int64Flag{Name: "myflag"} - expect(t, f.ValueFromContext(ctx), int64(42)) + expect(t, f.Get(ctx), int64(42)) } var uintFlagTests = []struct { @@ -784,7 +784,7 @@ func TestUintFlagValueFromContext(t *testing.T) { set.Uint("myflag", 42, "doc") ctx := NewContext(nil, set, nil) f := &UintFlag{Name: "myflag"} - expect(t, f.ValueFromContext(ctx), uint(42)) + expect(t, f.Get(ctx), uint(42)) } var uint64FlagTests = []struct { @@ -830,7 +830,7 @@ func TestUint64FlagValueFromContext(t *testing.T) { set.Uint64("myflag", 42, "doc") ctx := NewContext(nil, set, nil) f := &Uint64Flag{Name: "myflag"} - expect(t, f.ValueFromContext(ctx), uint64(42)) + expect(t, f.Get(ctx), uint64(42)) } var durationFlagTests = []struct { @@ -887,7 +887,7 @@ func TestDurationFlagValueFromContext(t *testing.T) { set.Duration("myflag", 42*time.Second, "doc") ctx := NewContext(nil, set, nil) f := &DurationFlag{Name: "myflag"} - expect(t, f.ValueFromContext(ctx), 42*time.Second) + expect(t, f.Get(ctx), 42*time.Second) } var intSliceFlagTests = []struct { @@ -984,7 +984,7 @@ func TestIntSliceFlagValueFromContext(t *testing.T) { set.Var(NewIntSlice(1, 2, 3), "myflag", "doc") ctx := NewContext(nil, set, nil) f := &IntSliceFlag{Name: "myflag"} - expect(t, f.ValueFromContext(ctx), []int{1, 2, 3}) + expect(t, f.Get(ctx), []int{1, 2, 3}) } var int64SliceFlagTests = []struct { @@ -1088,7 +1088,7 @@ func TestInt64SliceFlagValueFromContext(t *testing.T) { set.Var(NewInt64Slice(1, 2, 3), "myflag", "doc") ctx := NewContext(nil, set, nil) f := &Int64SliceFlag{Name: "myflag"} - expect(t, f.ValueFromContext(ctx), []int64{1, 2, 3}) + expect(t, f.Get(ctx), []int64{1, 2, 3}) } var float64FlagTests = []struct { @@ -1145,7 +1145,7 @@ func TestFloat64FlagValueFromContext(t *testing.T) { set.Float64("myflag", 1.23, "doc") ctx := NewContext(nil, set, nil) f := &Float64Flag{Name: "myflag"} - expect(t, f.ValueFromContext(ctx), 1.23) + expect(t, f.Get(ctx), 1.23) } var float64SliceFlagTests = []struct { @@ -1194,7 +1194,7 @@ func TestFloat64SliceFlagValueFromContext(t *testing.T) { set.Var(NewFloat64Slice(1.23, 4.56), "myflag", "doc") ctx := NewContext(nil, set, nil) f := &Float64SliceFlag{Name: "myflag"} - expect(t, f.ValueFromContext(ctx), []float64{1.23, 4.56}) + expect(t, f.Get(ctx), []float64{1.23, 4.56}) } var genericFlagTests = []struct { @@ -1250,7 +1250,7 @@ func TestGenericFlagValueFromContext(t *testing.T) { set.Var(&Parser{"abc", "def"}, "myflag", "doc") ctx := NewContext(nil, set, nil) f := &GenericFlag{Name: "myflag"} - expect(t, f.ValueFromContext(ctx), &Parser{"abc", "def"}) + expect(t, f.Get(ctx), &Parser{"abc", "def"}) } func TestParseMultiString(t *testing.T) { @@ -2286,7 +2286,7 @@ func TestTimestampFlagValueFromContext(t *testing.T) { set.Var(NewTimestamp(now), "myflag", "doc") ctx := NewContext(nil, set, nil) f := &TimestampFlag{Name: "myflag"} - expect(t, f.ValueFromContext(ctx), &now) + expect(t, f.Get(ctx), &now) } type flagDefaultTestCase struct { diff --git a/flag_timestamp.go b/flag_timestamp.go index a3b230e2e9..872f855e26 100644 --- a/flag_timestamp.go +++ b/flag_timestamp.go @@ -164,8 +164,8 @@ func (f *TimestampFlag) Apply(set *flag.FlagSet) error { return nil } -// ValueFromContext returns the flag’s value in the given Context. -func (f *TimestampFlag) ValueFromContext(ctx *Context) *time.Time { +// Get returns the flag’s value in the given Context. +func (f *TimestampFlag) Get(ctx *Context) *time.Time { return ctx.Timestamp(f.Name) } diff --git a/flag_uint.go b/flag_uint.go index b074c4e931..358e22f52f 100644 --- a/flag_uint.go +++ b/flag_uint.go @@ -101,8 +101,8 @@ func (f *UintFlag) GetEnvVars() []string { return f.EnvVars } -// ValueFromContext returns the flag’s value in the given Context. -func (f *UintFlag) ValueFromContext(ctx *Context) uint { +// Get returns the flag’s value in the given Context. +func (f *UintFlag) Get(ctx *Context) uint { return ctx.Uint(f.Name) } diff --git a/flag_uint64.go b/flag_uint64.go index e79b1a7aed..844dfc97d2 100644 --- a/flag_uint64.go +++ b/flag_uint64.go @@ -101,8 +101,8 @@ func (f *Uint64Flag) GetEnvVars() []string { return f.EnvVars } -// ValueFromContext returns the flag’s value in the given Context. -func (f *Uint64Flag) ValueFromContext(ctx *Context) uint64 { +// Get returns the flag’s value in the given Context. +func (f *Uint64Flag) Get(ctx *Context) uint64 { return ctx.Uint64(f.Name) }