Skip to content

Commit e79b13d

Browse files
committed
support VarPF variantes for options to return created flags
The actuals variants of the flag creation do not report back the generated Flag object. But this would be reqired to do further tweaking according to the public fields of a Flag, like adding of annotations. Therefore it would be useful to have an additional variant of the option creation methods on a FlagSet, which returns the generated Flag object. The underlying method VarPF already exists and can directly be used to support appropriate VarPF variants for the type specific methods. For example func (f *FlagSet) StringVarPF(p *string, name, shorthand string, value string, usage string) *Flag
1 parent d5e0c06 commit e79b13d

35 files changed

+391
-8
lines changed

bool.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,14 @@ func (f *FlagSet) BoolVar(p *bool, name string, value bool, usage string) {
5252

5353
// BoolVarP is like BoolVar, but accepts a shorthand letter that can be used after a single dash.
5454
func (f *FlagSet) BoolVarP(p *bool, name, shorthand string, value bool, usage string) {
55+
f.BoolVarPF(p, name, shorthand, value, usage)
56+
}
57+
58+
// BoolVarPF is like BoolVarP, but returns the created flag.
59+
func (f *FlagSet) BoolVarPF(p *bool, name, shorthand string, value bool, usage string) *Flag {
5560
flag := f.VarPF(newBoolValue(value, p), name, shorthand, usage)
5661
flag.NoOptDefVal = "true"
62+
return flag
5763
}
5864

5965
// BoolVar defines a bool flag with specified name, default value, and usage string.
@@ -68,6 +74,13 @@ func BoolVarP(p *bool, name, shorthand string, value bool, usage string) {
6874
flag.NoOptDefVal = "true"
6975
}
7076

77+
// BoolVarPF is like BoolVarP, but returns the created flag.
78+
func BoolVarPF(p *bool, name, shorthand string, value bool, usage string) *Flag {
79+
flag := CommandLine.VarPF(newBoolValue(value, p), name, shorthand, usage)
80+
flag.NoOptDefVal = "true"
81+
return flag
82+
}
83+
7184
// Bool defines a bool flag with specified name, default value, and usage string.
7285
// The return value is the address of a bool variable that stores the value of the flag.
7386
func (f *FlagSet) Bool(name string, value bool, usage string) *bool {

bool_slice.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,11 @@ func (f *FlagSet) BoolSliceVarP(p *[]bool, name, shorthand string, value []bool,
147147
f.VarP(newBoolSliceValue(value, p), name, shorthand, usage)
148148
}
149149

150+
// BoolSliceVarPF is like BoolSliceVarP, but returns the created flag.
151+
func (f *FlagSet) BoolSliceVarPF(p *[]bool, name, shorthand string, value []bool, usage string) *Flag {
152+
return f.VarPF(newBoolSliceValue(value, p), name, shorthand, usage)
153+
}
154+
150155
// BoolSliceVar defines a []bool flag with specified name, default value, and usage string.
151156
// The argument p points to a []bool variable in which to store the value of the flag.
152157
func BoolSliceVar(p *[]bool, name string, value []bool, usage string) {
@@ -158,6 +163,11 @@ func BoolSliceVarP(p *[]bool, name, shorthand string, value []bool, usage string
158163
CommandLine.VarP(newBoolSliceValue(value, p), name, shorthand, usage)
159164
}
160165

166+
// BoolSliceVarPF is like BoolSliceVarP, but returns the created flag.
167+
func BoolSliceVarPF(p *[]bool, name, shorthand string, value []bool, usage string) *Flag {
168+
return CommandLine.VarPF(newBoolSliceValue(value, p), name, shorthand, usage)
169+
}
170+
161171
// BoolSlice defines a []bool flag with specified name, default value, and usage string.
162172
// The return value is the address of a []bool variable that stores the value of the flag.
163173
func (f *FlagSet) BoolSlice(name string, value []bool, usage string) *[]bool {

bytes.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ func (f *FlagSet) BytesHexVarP(p *[]byte, name, shorthand string, value []byte,
7171
f.VarP(newBytesHexValue(value, p), name, shorthand, usage)
7272
}
7373

74+
// BytesHexVarPF is like BytesHexVarP, but returns the created flag.
75+
func (f *FlagSet) BytesHexVarPF(p *[]byte, name, shorthand string, value []byte, usage string) *Flag {
76+
return f.VarPF(newBytesHexValue(value, p), name, shorthand, usage)
77+
}
78+
7479
// BytesHexVar defines an []byte flag with specified name, default value, and usage string.
7580
// The argument p points to an []byte variable in which to store the value of the flag.
7681
func BytesHexVar(p *[]byte, name string, value []byte, usage string) {
@@ -82,6 +87,11 @@ func BytesHexVarP(p *[]byte, name, shorthand string, value []byte, usage string)
8287
CommandLine.VarP(newBytesHexValue(value, p), name, shorthand, usage)
8388
}
8489

90+
// BytesHexVarPF is like BytesHexVarP, but returns the created flag.
91+
func BytesHexVarPF(p *[]byte, name, shorthand string, value []byte, usage string) *Flag {
92+
return CommandLine.VarPF(newBytesHexValue(value, p), name, shorthand, usage)
93+
}
94+
8595
// BytesHex defines an []byte flag with specified name, default value, and usage string.
8696
// The return value is the address of an []byte variable that stores the value of the flag.
8797
func (f *FlagSet) BytesHex(name string, value []byte, usage string) *[]byte {
@@ -171,6 +181,11 @@ func (f *FlagSet) BytesBase64VarP(p *[]byte, name, shorthand string, value []byt
171181
f.VarP(newBytesBase64Value(value, p), name, shorthand, usage)
172182
}
173183

184+
// BytesBase64VarPF is like BytesBase64VarP, but returns the created flag.
185+
func (f *FlagSet) BytesBase64VarPF(p *[]byte, name, shorthand string, value []byte, usage string) *Flag {
186+
return f.VarPF(newBytesBase64Value(value, p), name, shorthand, usage)
187+
}
188+
174189
// BytesBase64Var defines an []byte flag with specified name, default value, and usage string.
175190
// The argument p points to an []byte variable in which to store the value of the flag.
176191
func BytesBase64Var(p *[]byte, name string, value []byte, usage string) {
@@ -182,6 +197,11 @@ func BytesBase64VarP(p *[]byte, name, shorthand string, value []byte, usage stri
182197
CommandLine.VarP(newBytesBase64Value(value, p), name, shorthand, usage)
183198
}
184199

200+
// BytesBase64VarPF is like BytesBase64VarP, but returns the created flag.
201+
func BytesBase64VarPF(p *[]byte, name, shorthand string, value []byte, usage string) *Flag {
202+
return CommandLine.VarPF(newBytesBase64Value(value, p), name, shorthand, usage)
203+
}
204+
185205
// BytesBase64 defines an []byte flag with specified name, default value, and usage string.
186206
// The return value is the address of an []byte variable that stores the value of the flag.
187207
func (f *FlagSet) BytesBase64(name string, value []byte, usage string) *[]byte {

count.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,14 @@ func (f *FlagSet) CountVar(p *int, name string, usage string) {
5353

5454
// CountVarP is like CountVar only take a shorthand for the flag name.
5555
func (f *FlagSet) CountVarP(p *int, name, shorthand string, usage string) {
56+
f.CountVarPF(p, name, shorthand, usage)
57+
}
58+
59+
// CountVarPF is like CountVarP, but returns the created flag.
60+
func (f *FlagSet) CountVarPF(p *int, name, shorthand string, usage string) *Flag {
5661
flag := f.VarPF(newCountValue(0, p), name, shorthand, usage)
5762
flag.NoOptDefVal = "+1"
63+
return flag
5864
}
5965

6066
// CountVar like CountVar only the flag is placed on the CommandLine instead of a given flag set
@@ -67,6 +73,11 @@ func CountVarP(p *int, name, shorthand string, usage string) {
6773
CommandLine.CountVarP(p, name, shorthand, usage)
6874
}
6975

76+
// CountVarPF is like CountVarP, but returns the created flag.
77+
func CountVarPF(p *int, name, shorthand string, usage string) *Flag {
78+
return CommandLine.CountVarPF(p, name, shorthand, usage)
79+
}
80+
7081
// Count defines a count flag with specified name, default value, and usage string.
7182
// The return value is the address of an int variable that stores the value of the flag.
7283
// A count flag will add 1 to its value every time it is found on the command line

duration.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ func (f *FlagSet) DurationVarP(p *time.Duration, name, shorthand string, value t
4848
f.VarP(newDurationValue(value, p), name, shorthand, usage)
4949
}
5050

51+
// DurationVarPF is like DurationVarP, but returns the created flag.
52+
func (f *FlagSet) DurationVarPF(p *time.Duration, name, shorthand string, value time.Duration, usage string) *Flag {
53+
return f.VarPF(newDurationValue(value, p), name, shorthand, usage)
54+
}
55+
5156
// DurationVar defines a time.Duration flag with specified name, default value, and usage string.
5257
// The argument p points to a time.Duration variable in which to store the value of the flag.
5358
func DurationVar(p *time.Duration, name string, value time.Duration, usage string) {
@@ -59,6 +64,11 @@ func DurationVarP(p *time.Duration, name, shorthand string, value time.Duration,
5964
CommandLine.VarP(newDurationValue(value, p), name, shorthand, usage)
6065
}
6166

67+
// DurationVarPF is like DurationVarP, but returns the created flag.
68+
func DurationVarPF(p *time.Duration, name, shorthand string, value time.Duration, usage string) *Flag {
69+
return CommandLine.VarPF(newDurationValue(value, p), name, shorthand, usage)
70+
}
71+
6272
// Duration defines a time.Duration flag with specified name, default value, and usage string.
6373
// The return value is the address of a time.Duration variable that stores the value of the flag.
6474
func (f *FlagSet) Duration(name string, value time.Duration, usage string) *time.Duration {

duration_slice.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,11 @@ func (f *FlagSet) DurationSliceVarP(p *[]time.Duration, name, shorthand string,
128128
f.VarP(newDurationSliceValue(value, p), name, shorthand, usage)
129129
}
130130

131+
// DurationSliceVarPF is like DurationSliceVarP, but returns the created flag.
132+
func (f *FlagSet) DurationSliceVarPF(p *[]time.Duration, name, shorthand string, value []time.Duration, usage string) *Flag {
133+
return f.VarPF(newDurationSliceValue(value, p), name, shorthand, usage)
134+
}
135+
131136
// DurationSliceVar defines a duration[] flag with specified name, default value, and usage string.
132137
// The argument p points to a duration[] variable in which to store the value of the flag.
133138
func DurationSliceVar(p *[]time.Duration, name string, value []time.Duration, usage string) {
@@ -139,6 +144,11 @@ func DurationSliceVarP(p *[]time.Duration, name, shorthand string, value []time.
139144
CommandLine.VarP(newDurationSliceValue(value, p), name, shorthand, usage)
140145
}
141146

147+
// DurationSliceVarPF is like DurationSliceVarP, but returns the created flag.
148+
func DurationSliceVarPF(p *[]time.Duration, name, shorthand string, value []time.Duration, usage string) *Flag {
149+
return CommandLine.VarPF(newDurationSliceValue(value, p), name, shorthand, usage)
150+
}
151+
142152
// DurationSlice defines a []time.Duration flag with specified name, default value, and usage string.
143153
// The return value is the address of a []time.Duration variable that stores the value of the flag.
144154
func (f *FlagSet) DurationSlice(name string, value []time.Duration, usage string) *[]time.Duration {

float32.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ func (f *FlagSet) Float32VarP(p *float32, name, shorthand string, value float32,
5050
f.VarP(newFloat32Value(value, p), name, shorthand, usage)
5151
}
5252

53+
// Float32VarPF is like Float32VarP, but returns the created flag.
54+
func (f *FlagSet) Float32VarPF(p *float32, name, shorthand string, value float32, usage string) *Flag {
55+
return f.VarPF(newFloat32Value(value, p), name, shorthand, usage)
56+
}
57+
5358
// Float32Var defines a float32 flag with specified name, default value, and usage string.
5459
// The argument p points to a float32 variable in which to store the value of the flag.
5560
func Float32Var(p *float32, name string, value float32, usage string) {
@@ -61,6 +66,11 @@ func Float32VarP(p *float32, name, shorthand string, value float32, usage string
6166
CommandLine.VarP(newFloat32Value(value, p), name, shorthand, usage)
6267
}
6368

69+
// Float32VarPF is like Float32VarP, but returns the created flag.
70+
func Float32VarPF(p *float32, name, shorthand string, value float32, usage string) *Flag {
71+
return CommandLine.VarPF(newFloat32Value(value, p), name, shorthand, usage)
72+
}
73+
6474
// Float32 defines a float32 flag with specified name, default value, and usage string.
6575
// The return value is the address of a float32 variable that stores the value of the flag.
6676
func (f *FlagSet) Float32(name string, value float32, usage string) *float32 {

float32_slice.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@ func (f *FlagSet) Float32SliceVarP(p *[]float32, name, shorthand string, value [
136136
f.VarP(newFloat32SliceValue(value, p), name, shorthand, usage)
137137
}
138138

139+
// Float32SliceVarPF is like Float32SliceVarP, but returns the created flag.
140+
func (f *FlagSet) Float32SliceVarPF(p *[]float32, name, shorthand string, value []float32, usage string) *Flag {
141+
return f.VarPF(newFloat32SliceValue(value, p), name, shorthand, usage)
142+
}
143+
139144
// Float32SliceVar defines a float32[] flag with specified name, default value, and usage string.
140145
// The argument p points to a float32[] variable in which to store the value of the flag.
141146
func Float32SliceVar(p *[]float32, name string, value []float32, usage string) {
@@ -147,6 +152,11 @@ func Float32SliceVarP(p *[]float32, name, shorthand string, value []float32, usa
147152
CommandLine.VarP(newFloat32SliceValue(value, p), name, shorthand, usage)
148153
}
149154

155+
// Float32SliceVarPF is like Float32SliceVarP, but returns the created flag.
156+
func Float32SliceVarPF(p *[]float32, name, shorthand string, value []float32, usage string) *Flag {
157+
return CommandLine.VarPF(newFloat32SliceValue(value, p), name, shorthand, usage)
158+
}
159+
150160
// Float32Slice defines a []float32 flag with specified name, default value, and usage string.
151161
// The return value is the address of a []float32 variable that stores the value of the flag.
152162
func (f *FlagSet) Float32Slice(name string, value []float32, usage string) *[]float32 {

float64.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ func (f *FlagSet) Float64VarP(p *float64, name, shorthand string, value float64,
4646
f.VarP(newFloat64Value(value, p), name, shorthand, usage)
4747
}
4848

49+
// Float64VarPF is like Float64VarP, but returns the created flag.
50+
func (f *FlagSet) Float64VarPF(p *float64, name, shorthand string, value float64, usage string) *Flag {
51+
return f.VarPF(newFloat64Value(value, p), name, shorthand, usage)
52+
}
53+
4954
// Float64Var defines a float64 flag with specified name, default value, and usage string.
5055
// The argument p points to a float64 variable in which to store the value of the flag.
5156
func Float64Var(p *float64, name string, value float64, usage string) {
@@ -57,6 +62,11 @@ func Float64VarP(p *float64, name, shorthand string, value float64, usage string
5762
CommandLine.VarP(newFloat64Value(value, p), name, shorthand, usage)
5863
}
5964

65+
// Float64VarPF is like Float64VarP, bbut returns the created flag.
66+
func Float64VarPF(p *float64, name, shorthand string, value float64, usage string) *Flag {
67+
return CommandLine.VarPF(newFloat64Value(value, p), name, shorthand, usage)
68+
}
69+
6070
// Float64 defines a float64 flag with specified name, default value, and usage string.
6171
// The return value is the address of a float64 variable that stores the value of the flag.
6272
func (f *FlagSet) Float64(name string, value float64, usage string) *float64 {

float64_slice.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,11 @@ func (f *FlagSet) Float64SliceVarP(p *[]float64, name, shorthand string, value [
128128
f.VarP(newFloat64SliceValue(value, p), name, shorthand, usage)
129129
}
130130

131+
// Float64SliceVarPF is like Float64SliceVarP, but returns the created flag.
132+
func (f *FlagSet) Float64SliceVarPF(p *[]float64, name, shorthand string, value []float64, usage string) *Flag {
133+
return f.VarPF(newFloat64SliceValue(value, p), name, shorthand, usage)
134+
}
135+
131136
// Float64SliceVar defines a float64[] flag with specified name, default value, and usage string.
132137
// The argument p points to a float64[] variable in which to store the value of the flag.
133138
func Float64SliceVar(p *[]float64, name string, value []float64, usage string) {
@@ -139,6 +144,11 @@ func Float64SliceVarP(p *[]float64, name, shorthand string, value []float64, usa
139144
CommandLine.VarP(newFloat64SliceValue(value, p), name, shorthand, usage)
140145
}
141146

147+
// Float64SliceVarPF is like Float64SliceVarP, but returns the created flag.
148+
func Float64SliceVarPF(p *[]float64, name, shorthand string, value []float64, usage string) *Flag {
149+
return CommandLine.VarPF(newFloat64SliceValue(value, p), name, shorthand, usage)
150+
}
151+
142152
// Float64Slice defines a []float64 flag with specified name, default value, and usage string.
143153
// The return value is the address of a []float64 variable that stores the value of the flag.
144154
func (f *FlagSet) Float64Slice(name string, value []float64, usage string) *[]float64 {

0 commit comments

Comments
 (0)