diff --git a/flag_bool.go b/flag_bool.go index ab27e111a8..8147f1cda0 100644 --- a/flag_bool.go +++ b/flag_bool.go @@ -112,6 +112,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 ec8faf9938..c612966df0 100644 --- a/flag_duration.go +++ b/flag_duration.go @@ -111,6 +111,11 @@ func (f *DurationFlag) RunAction(c *Context) 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 323b579439..5945c1b98a 100644 --- a/flag_float64.go +++ b/flag_float64.go @@ -111,6 +111,11 @@ func (f *Float64Flag) RunAction(c *Context) 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 df4f77a450..f52c1f1e3a 100644 --- a/flag_float64_slice.go +++ b/flag_float64_slice.go @@ -187,6 +187,11 @@ func (f *Float64SliceFlag) RunAction(c *Context) 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 8b92978147..88fc020df4 100644 --- a/flag_generic.go +++ b/flag_generic.go @@ -114,6 +114,11 @@ func (f *GenericFlag) RunAction(c *Context) 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 65d524fbfa..c51f31a0fc 100644 --- a/flag_int.go +++ b/flag_int.go @@ -112,6 +112,11 @@ func (f *IntFlag) RunAction(c *Context) 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 4493beb95d..887d7f8d56 100644 --- a/flag_int64.go +++ b/flag_int64.go @@ -111,6 +111,11 @@ func (f *Int64Flag) RunAction(c *Context) 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 f4acda7b15..b015d60f94 100644 --- a/flag_int64_slice.go +++ b/flag_int64_slice.go @@ -186,6 +186,11 @@ func (f *Int64SliceFlag) RunAction(c *Context) 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 c2b8ccc056..58da3671bc 100644 --- a/flag_int_slice.go +++ b/flag_int_slice.go @@ -197,6 +197,11 @@ func (f *IntSliceFlag) RunAction(c *Context) 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 8e2dd78756..be43247cf8 100644 --- a/flag_path.go +++ b/flag_path.go @@ -106,6 +106,11 @@ func (f *PathFlag) RunAction(c *Context) 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 a5fe2904df..1dddf5c67a 100644 --- a/flag_string.go +++ b/flag_string.go @@ -107,6 +107,11 @@ func (f *StringFlag) RunAction(c *Context) 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 f4f09763b9..bf6937d6ab 100644 --- a/flag_string_slice.go +++ b/flag_string_slice.go @@ -198,6 +198,11 @@ func (f *StringSliceFlag) RunAction(c *Context) 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 c7f18cbcb9..13109547ba 100644 --- a/flag_timestamp.go +++ b/flag_timestamp.go @@ -174,6 +174,11 @@ func (f *TimestampFlag) RunAction(c *Context) 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 f898d83d3b..9541692ed6 100644 --- a/flag_uint.go +++ b/flag_uint.go @@ -111,6 +111,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 e9ef45fc4b..dc578f9877 100644 --- a/flag_uint64.go +++ b/flag_uint64.go @@ -111,6 +111,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 {