Skip to content

Commit

Permalink
Merge pull request #1367 from toaster/feature/1316-simplified_flag_va…
Browse files Browse the repository at this point in the history
…lue_access

Simplified flag value access
  • Loading branch information
meatballhat committed May 1, 2022
2 parents 76418f2 + 8cc4378 commit cbd9bd9
Show file tree
Hide file tree
Showing 16 changed files with 199 additions and 0 deletions.
5 changes: 5 additions & 0 deletions flag_bool.go
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions flag_duration.go
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions flag_float64.go
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions flag_float64_slice.go
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions flag_generic.go
Expand Up @@ -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{} {
Expand Down
5 changes: 5 additions & 0 deletions flag_int.go
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions flag_int64.go
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions flag_int64_slice.go
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions flag_int_slice.go
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions flag_path.go
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions flag_string.go
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions flag_string_slice.go
Expand Up @@ -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 {
Expand Down
124 changes: 124 additions & 0 deletions flag_test.go
Expand Up @@ -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...)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions flag_timestamp.go
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions flag_uint.go
Expand Up @@ -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 {
Expand Down

0 comments on commit cbd9bd9

Please sign in to comment.