Skip to content

Commit cbd9bd9

Browse files
authored
Merge pull request #1367 from toaster/feature/1316-simplified_flag_value_access
Simplified flag value access
2 parents 76418f2 + 8cc4378 commit cbd9bd9

16 files changed

Lines changed: 199 additions & 0 deletions

flag_bool.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ func (f *BoolFlag) Apply(set *flag.FlagSet) error {
102102
return nil
103103
}
104104

105+
// Get returns the flag’s value in the given Context.
106+
func (f *BoolFlag) Get(ctx *Context) bool {
107+
return ctx.Bool(f.Name)
108+
}
109+
105110
// Bool looks up the value of a local BoolFlag, returns
106111
// false if not found
107112
func (cCtx *Context) Bool(name string) bool {

flag_duration.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ func (f *DurationFlag) Apply(set *flag.FlagSet) error {
101101
return nil
102102
}
103103

104+
// Get returns the flag’s value in the given Context.
105+
func (f *DurationFlag) Get(ctx *Context) time.Duration {
106+
return ctx.Duration(f.Name)
107+
}
108+
104109
// Duration looks up the value of a local DurationFlag, returns
105110
// 0 if not found
106111
func (cCtx *Context) Duration(name string) time.Duration {

flag_float64.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ func (f *Float64Flag) Apply(set *flag.FlagSet) error {
101101
return nil
102102
}
103103

104+
// Get returns the flag’s value in the given Context.
105+
func (f *Float64Flag) Get(ctx *Context) float64 {
106+
return ctx.Float64(f.Name)
107+
}
108+
104109
// Float64 looks up the value of a local Float64Flag, returns
105110
// 0 if not found
106111
func (cCtx *Context) Float64(name string) float64 {

flag_float64_slice.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,11 @@ func (f *Float64SliceFlag) Apply(set *flag.FlagSet) error {
177177
return nil
178178
}
179179

180+
// Get returns the flag’s value in the given Context.
181+
func (f *Float64SliceFlag) Get(ctx *Context) []float64 {
182+
return ctx.Float64Slice(f.Name)
183+
}
184+
180185
// Float64Slice looks up the value of a local Float64SliceFlag, returns
181186
// nil if not found
182187
func (cCtx *Context) Float64Slice(name string) []float64 {

flag_generic.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ func (f GenericFlag) Apply(set *flag.FlagSet) error {
104104
return nil
105105
}
106106

107+
// Get returns the flag’s value in the given Context.
108+
func (f *GenericFlag) Get(ctx *Context) interface{} {
109+
return ctx.Generic(f.Name)
110+
}
111+
107112
// Generic looks up the value of a local GenericFlag, returns
108113
// nil if not found
109114
func (cCtx *Context) Generic(name string) interface{} {

flag_int.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ func (f *IntFlag) Apply(set *flag.FlagSet) error {
102102
return nil
103103
}
104104

105+
// Get returns the flag’s value in the given Context.
106+
func (f *IntFlag) Get(ctx *Context) int {
107+
return ctx.Int(f.Name)
108+
}
109+
105110
// Int looks up the value of a local IntFlag, returns
106111
// 0 if not found
107112
func (cCtx *Context) Int(name string) int {

flag_int64.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ func (f *Int64Flag) Apply(set *flag.FlagSet) error {
101101
return nil
102102
}
103103

104+
// Get returns the flag’s value in the given Context.
105+
func (f *Int64Flag) Get(ctx *Context) int64 {
106+
return ctx.Int64(f.Name)
107+
}
108+
104109
// Int64 looks up the value of a local Int64Flag, returns
105110
// 0 if not found
106111
func (cCtx *Context) Int64(name string) int64 {

flag_int64_slice.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,11 @@ func (f *Int64SliceFlag) Apply(set *flag.FlagSet) error {
176176
return nil
177177
}
178178

179+
// Get returns the flag’s value in the given Context.
180+
func (f *Int64SliceFlag) Get(ctx *Context) []int64 {
181+
return ctx.Int64Slice(f.Name)
182+
}
183+
179184
// Int64Slice looks up the value of a local Int64SliceFlag, returns
180185
// nil if not found
181186
func (cCtx *Context) Int64Slice(name string) []int64 {

flag_int_slice.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,11 @@ func (f *IntSliceFlag) Apply(set *flag.FlagSet) error {
187187
return nil
188188
}
189189

190+
// Get returns the flag’s value in the given Context.
191+
func (f *IntSliceFlag) Get(ctx *Context) []int {
192+
return ctx.IntSlice(f.Name)
193+
}
194+
190195
// IntSlice looks up the value of a local IntSliceFlag, returns
191196
// nil if not found
192197
func (cCtx *Context) IntSlice(name string) []int {

flag_path.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ func (f *PathFlag) Apply(set *flag.FlagSet) error {
9696
return nil
9797
}
9898

99+
// Get returns the flag’s value in the given Context.
100+
func (f *PathFlag) Get(ctx *Context) string {
101+
return ctx.Path(f.Name)
102+
}
103+
99104
// Path looks up the value of a local PathFlag, returns
100105
// "" if not found
101106
func (cCtx *Context) Path(name string) string {

0 commit comments

Comments
 (0)