diff --git a/flag_bool.go b/flag_bool.go index 6e832f964b..018f9afccd 100644 --- a/flag_bool.go +++ b/flag_bool.go @@ -102,6 +102,11 @@ func (f *BoolFlag) Apply(set *flag.FlagSet) error { return nil } +// Get returns the flag’s value in the given Context. +func (f *BoolFlag) Get(ctx *Context) bool { + return ctx.Bool(f.Name) +} + // Bool looks up the value of a local BoolFlag, returns // false if not found func (cCtx *Context) Bool(name string) bool { diff --git a/flag_duration.go b/flag_duration.go index d30bd1d6ce..9c6dde7b9e 100644 --- a/flag_duration.go +++ b/flag_duration.go @@ -101,6 +101,11 @@ func (f *DurationFlag) Apply(set *flag.FlagSet) error { return nil } +// Get returns the flag’s value in the given Context. +func (f *DurationFlag) Get(ctx *Context) time.Duration { + return ctx.Duration(f.Name) +} + // Duration looks up the value of a local DurationFlag, returns // 0 if not found func (cCtx *Context) Duration(name string) time.Duration { diff --git a/flag_float64.go b/flag_float64.go index f11bef3a7b..62fdb13a48 100644 --- a/flag_float64.go +++ b/flag_float64.go @@ -101,6 +101,11 @@ func (f *Float64Flag) Apply(set *flag.FlagSet) error { return nil } +// Get returns the flag’s value in the given Context. +func (f *Float64Flag) Get(ctx *Context) float64 { + return ctx.Float64(f.Name) +} + // Float64 looks up the value of a local Float64Flag, returns // 0 if not found func (cCtx *Context) Float64(name string) float64 { diff --git a/flag_float64_slice.go b/flag_float64_slice.go index 219c50150c..8be586c40e 100644 --- a/flag_float64_slice.go +++ b/flag_float64_slice.go @@ -177,6 +177,11 @@ func (f *Float64SliceFlag) Apply(set *flag.FlagSet) error { return nil } +// Get returns the flag’s value in the given Context. +func (f *Float64SliceFlag) Get(ctx *Context) []float64 { + return ctx.Float64Slice(f.Name) +} + // Float64Slice looks up the value of a local Float64SliceFlag, returns // nil if not found func (cCtx *Context) Float64Slice(name string) []float64 { diff --git a/flag_generic.go b/flag_generic.go index e44e7d7517..ae594d4d84 100644 --- a/flag_generic.go +++ b/flag_generic.go @@ -104,6 +104,11 @@ func (f GenericFlag) Apply(set *flag.FlagSet) error { return nil } +// Get returns the flag’s value in the given Context. +func (f *GenericFlag) Get(ctx *Context) interface{} { + return ctx.Generic(f.Name) +} + // Generic looks up the value of a local GenericFlag, returns // nil if not found func (cCtx *Context) Generic(name string) interface{} { diff --git a/flag_int.go b/flag_int.go index 2c4e602711..63ec9a48df 100644 --- a/flag_int.go +++ b/flag_int.go @@ -102,6 +102,11 @@ func (f *IntFlag) Apply(set *flag.FlagSet) error { return nil } +// Get returns the flag’s value in the given Context. +func (f *IntFlag) Get(ctx *Context) int { + return ctx.Int(f.Name) +} + // Int looks up the value of a local IntFlag, returns // 0 if not found func (cCtx *Context) Int(name string) int { diff --git a/flag_int64.go b/flag_int64.go index f21e7e44f1..929e2d8d94 100644 --- a/flag_int64.go +++ b/flag_int64.go @@ -101,6 +101,11 @@ func (f *Int64Flag) Apply(set *flag.FlagSet) error { return nil } +// Get returns the flag’s value in the given Context. +func (f *Int64Flag) Get(ctx *Context) int64 { + return ctx.Int64(f.Name) +} + // Int64 looks up the value of a local Int64Flag, returns // 0 if not found func (cCtx *Context) Int64(name string) int64 { diff --git a/flag_int64_slice.go b/flag_int64_slice.go index 6ea7ae8b90..08f03ddfbe 100644 --- a/flag_int64_slice.go +++ b/flag_int64_slice.go @@ -176,6 +176,11 @@ func (f *Int64SliceFlag) Apply(set *flag.FlagSet) error { return nil } +// Get returns the flag’s value in the given Context. +func (f *Int64SliceFlag) Get(ctx *Context) []int64 { + return ctx.Int64Slice(f.Name) +} + // Int64Slice looks up the value of a local Int64SliceFlag, returns // nil if not found func (cCtx *Context) Int64Slice(name string) []int64 { diff --git a/flag_int_slice.go b/flag_int_slice.go index 38f6a33b06..e5b9e21e5e 100644 --- a/flag_int_slice.go +++ b/flag_int_slice.go @@ -187,6 +187,11 @@ func (f *IntSliceFlag) Apply(set *flag.FlagSet) error { return nil } +// Get returns the flag’s value in the given Context. +func (f *IntSliceFlag) Get(ctx *Context) []int { + return ctx.IntSlice(f.Name) +} + // IntSlice looks up the value of a local IntSliceFlag, returns // nil if not found func (cCtx *Context) IntSlice(name string) []int { diff --git a/flag_path.go b/flag_path.go index de19eea217..a7a213e877 100644 --- a/flag_path.go +++ b/flag_path.go @@ -96,6 +96,11 @@ func (f *PathFlag) Apply(set *flag.FlagSet) error { return nil } +// Get returns the flag’s value in the given Context. +func (f *PathFlag) Get(ctx *Context) string { + return ctx.Path(f.Name) +} + // Path looks up the value of a local PathFlag, returns // "" if not found func (cCtx *Context) Path(name string) string { diff --git a/flag_string.go b/flag_string.go index 936ce728a9..0365762d64 100644 --- a/flag_string.go +++ b/flag_string.go @@ -97,6 +97,11 @@ func (f *StringFlag) Apply(set *flag.FlagSet) error { return nil } +// Get returns the flag’s value in the given Context. +func (f *StringFlag) Get(ctx *Context) string { + return ctx.String(f.Name) +} + // String looks up the value of a local StringFlag, returns // "" if not found func (cCtx *Context) String(name string) string { diff --git a/flag_string_slice.go b/flag_string_slice.go index ff0a4b7da6..82aa1a492c 100644 --- a/flag_string_slice.go +++ b/flag_string_slice.go @@ -188,6 +188,11 @@ func (f *StringSliceFlag) Apply(set *flag.FlagSet) error { return nil } +// Get returns the flag’s value in the given Context. +func (f *StringSliceFlag) Get(ctx *Context) []string { + return ctx.StringSlice(f.Name) +} + // StringSlice looks up the value of a local StringSliceFlag, returns // nil if not found func (cCtx *Context) StringSlice(name string) []string { diff --git a/flag_test.go b/flag_test.go index e385276cd7..9433fc9d1c 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.Get(ctx), true) + expect(t, ff.Get(ctx), false) +} + func TestFlagsFromEnv(t *testing.T) { newSetFloat64Slice := func(defaults ...float64) Float64Slice { s := NewFloat64Slice(defaults...) @@ -439,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.Get(ctx), "foobar") +} + var pathFlagTests = []struct { name string aliases []string @@ -490,6 +509,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.Get(ctx), "/my/path") +} + var _ = []struct { name string env string @@ -603,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.Get(ctx), []string{"a", "b", "c"}) +} + var intFlagTests = []struct { name string expected string @@ -652,6 +687,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.Get(ctx), 42) +} + var int64FlagTests = []struct { name string expected string @@ -690,6 +733,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.Get(ctx), int64(42)) +} + var uintFlagTests = []struct { name string expected string @@ -728,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.Get(ctx), uint(42)) +} + var uint64FlagTests = []struct { name string expected string @@ -766,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.Get(ctx), uint64(42)) +} + var durationFlagTests = []struct { name string expected string @@ -815,6 +882,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.Get(ctx), 42*time.Second) +} + var intSliceFlagTests = []struct { name string aliases []string @@ -904,6 +979,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.Get(ctx), []int{1, 2, 3}) +} + var int64SliceFlagTests = []struct { name string aliases []string @@ -1000,6 +1083,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.Get(ctx), []int64{1, 2, 3}) +} + var float64FlagTests = []struct { name string expected string @@ -1049,6 +1140,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.Get(ctx), 1.23) +} + var float64SliceFlagTests = []struct { name string aliases []string @@ -1090,6 +1189,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.Get(ctx), []float64{1.23, 4.56}) +} + var genericFlagTests = []struct { name string value Generic @@ -1138,6 +1245,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.Get(ctx), &Parser{"abc", "def"}) +} + func TestParseMultiString(t *testing.T) { _ = (&App{ Flags: []Flag{ @@ -2165,6 +2280,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.Get(ctx), &now) +} + type flagDefaultTestCase struct { name string flag Flag diff --git a/flag_timestamp.go b/flag_timestamp.go index dcb9d84152..7bda8ee221 100644 --- a/flag_timestamp.go +++ b/flag_timestamp.go @@ -164,6 +164,11 @@ func (f *TimestampFlag) Apply(set *flag.FlagSet) error { return nil } +// Get returns the flag’s value in the given Context. +func (f *TimestampFlag) Get(ctx *Context) *time.Time { + return ctx.Timestamp(f.Name) +} + // Timestamp gets the timestamp from a flag name func (cCtx *Context) Timestamp(name string) *time.Time { if fs := cCtx.lookupFlagSet(name); fs != nil { diff --git a/flag_uint.go b/flag_uint.go index e9ce9fec7d..0fd0777d73 100644 --- a/flag_uint.go +++ b/flag_uint.go @@ -101,6 +101,11 @@ func (f *UintFlag) GetEnvVars() []string { return f.EnvVars } +// Get returns the flag’s value in the given Context. +func (f *UintFlag) Get(ctx *Context) uint { + return ctx.Uint(f.Name) +} + // Uint looks up the value of a local UintFlag, returns // 0 if not found func (cCtx *Context) Uint(name string) uint { diff --git a/flag_uint64.go b/flag_uint64.go index 2a2669fd24..c8314ac00f 100644 --- a/flag_uint64.go +++ b/flag_uint64.go @@ -101,6 +101,11 @@ func (f *Uint64Flag) GetEnvVars() []string { return f.EnvVars } +// Get returns the flag’s value in the given Context. +func (f *Uint64Flag) Get(ctx *Context) uint64 { + return ctx.Uint64(f.Name) +} + // Uint64 looks up the value of a local Uint64Flag, returns // 0 if not found func (cCtx *Context) Uint64(name string) uint64 {