Skip to content

Commit

Permalink
support VarPF variantes for options to return created flags
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mandelsoft committed Nov 3, 2022
1 parent d5e0c06 commit e79b13d
Show file tree
Hide file tree
Showing 35 changed files with 391 additions and 8 deletions.
13 changes: 13 additions & 0 deletions bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,14 @@ func (f *FlagSet) BoolVar(p *bool, name string, value bool, usage string) {

// BoolVarP is like BoolVar, but accepts a shorthand letter that can be used after a single dash.
func (f *FlagSet) BoolVarP(p *bool, name, shorthand string, value bool, usage string) {
f.BoolVarPF(p, name, shorthand, value, usage)
}

// BoolVarPF is like BoolVarP, but returns the created flag.
func (f *FlagSet) BoolVarPF(p *bool, name, shorthand string, value bool, usage string) *Flag {
flag := f.VarPF(newBoolValue(value, p), name, shorthand, usage)
flag.NoOptDefVal = "true"
return flag
}

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

// BoolVarPF is like BoolVarP, but returns the created flag.
func BoolVarPF(p *bool, name, shorthand string, value bool, usage string) *Flag {
flag := CommandLine.VarPF(newBoolValue(value, p), name, shorthand, usage)
flag.NoOptDefVal = "true"
return flag
}

// Bool defines a bool flag with specified name, default value, and usage string.
// The return value is the address of a bool variable that stores the value of the flag.
func (f *FlagSet) Bool(name string, value bool, usage string) *bool {
Expand Down
10 changes: 10 additions & 0 deletions bool_slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ func (f *FlagSet) BoolSliceVarP(p *[]bool, name, shorthand string, value []bool,
f.VarP(newBoolSliceValue(value, p), name, shorthand, usage)
}

// BoolSliceVarPF is like BoolSliceVarP, but returns the created flag.
func (f *FlagSet) BoolSliceVarPF(p *[]bool, name, shorthand string, value []bool, usage string) *Flag {
return f.VarPF(newBoolSliceValue(value, p), name, shorthand, usage)
}

// BoolSliceVar defines a []bool flag with specified name, default value, and usage string.
// The argument p points to a []bool variable in which to store the value of the flag.
func BoolSliceVar(p *[]bool, name string, value []bool, usage string) {
Expand All @@ -158,6 +163,11 @@ func BoolSliceVarP(p *[]bool, name, shorthand string, value []bool, usage string
CommandLine.VarP(newBoolSliceValue(value, p), name, shorthand, usage)
}

// BoolSliceVarPF is like BoolSliceVarP, but returns the created flag.
func BoolSliceVarPF(p *[]bool, name, shorthand string, value []bool, usage string) *Flag {
return CommandLine.VarPF(newBoolSliceValue(value, p), name, shorthand, usage)
}

// BoolSlice defines a []bool flag with specified name, default value, and usage string.
// The return value is the address of a []bool variable that stores the value of the flag.
func (f *FlagSet) BoolSlice(name string, value []bool, usage string) *[]bool {
Expand Down
20 changes: 20 additions & 0 deletions bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ func (f *FlagSet) BytesHexVarP(p *[]byte, name, shorthand string, value []byte,
f.VarP(newBytesHexValue(value, p), name, shorthand, usage)
}

// BytesHexVarPF is like BytesHexVarP, but returns the created flag.
func (f *FlagSet) BytesHexVarPF(p *[]byte, name, shorthand string, value []byte, usage string) *Flag {
return f.VarPF(newBytesHexValue(value, p), name, shorthand, usage)
}

// BytesHexVar defines an []byte flag with specified name, default value, and usage string.
// The argument p points to an []byte variable in which to store the value of the flag.
func BytesHexVar(p *[]byte, name string, value []byte, usage string) {
Expand All @@ -82,6 +87,11 @@ func BytesHexVarP(p *[]byte, name, shorthand string, value []byte, usage string)
CommandLine.VarP(newBytesHexValue(value, p), name, shorthand, usage)
}

// BytesHexVarPF is like BytesHexVarP, but returns the created flag.
func BytesHexVarPF(p *[]byte, name, shorthand string, value []byte, usage string) *Flag {
return CommandLine.VarPF(newBytesHexValue(value, p), name, shorthand, usage)
}

// BytesHex defines an []byte flag with specified name, default value, and usage string.
// The return value is the address of an []byte variable that stores the value of the flag.
func (f *FlagSet) BytesHex(name string, value []byte, usage string) *[]byte {
Expand Down Expand Up @@ -171,6 +181,11 @@ func (f *FlagSet) BytesBase64VarP(p *[]byte, name, shorthand string, value []byt
f.VarP(newBytesBase64Value(value, p), name, shorthand, usage)
}

// BytesBase64VarPF is like BytesBase64VarP, but returns the created flag.
func (f *FlagSet) BytesBase64VarPF(p *[]byte, name, shorthand string, value []byte, usage string) *Flag {
return f.VarPF(newBytesBase64Value(value, p), name, shorthand, usage)
}

// BytesBase64Var defines an []byte flag with specified name, default value, and usage string.
// The argument p points to an []byte variable in which to store the value of the flag.
func BytesBase64Var(p *[]byte, name string, value []byte, usage string) {
Expand All @@ -182,6 +197,11 @@ func BytesBase64VarP(p *[]byte, name, shorthand string, value []byte, usage stri
CommandLine.VarP(newBytesBase64Value(value, p), name, shorthand, usage)
}

// BytesBase64VarPF is like BytesBase64VarP, but returns the created flag.
func BytesBase64VarPF(p *[]byte, name, shorthand string, value []byte, usage string) *Flag {
return CommandLine.VarPF(newBytesBase64Value(value, p), name, shorthand, usage)
}

// BytesBase64 defines an []byte flag with specified name, default value, and usage string.
// The return value is the address of an []byte variable that stores the value of the flag.
func (f *FlagSet) BytesBase64(name string, value []byte, usage string) *[]byte {
Expand Down
11 changes: 11 additions & 0 deletions count.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,14 @@ func (f *FlagSet) CountVar(p *int, name string, usage string) {

// CountVarP is like CountVar only take a shorthand for the flag name.
func (f *FlagSet) CountVarP(p *int, name, shorthand string, usage string) {
f.CountVarPF(p, name, shorthand, usage)
}

// CountVarPF is like CountVarP, but returns the created flag.
func (f *FlagSet) CountVarPF(p *int, name, shorthand string, usage string) *Flag {
flag := f.VarPF(newCountValue(0, p), name, shorthand, usage)
flag.NoOptDefVal = "+1"
return flag
}

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

// CountVarPF is like CountVarP, but returns the created flag.
func CountVarPF(p *int, name, shorthand string, usage string) *Flag {
return CommandLine.CountVarPF(p, name, shorthand, usage)
}

// Count defines a count flag with specified name, default value, and usage string.
// The return value is the address of an int variable that stores the value of the flag.
// A count flag will add 1 to its value every time it is found on the command line
Expand Down
10 changes: 10 additions & 0 deletions duration.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ func (f *FlagSet) DurationVarP(p *time.Duration, name, shorthand string, value t
f.VarP(newDurationValue(value, p), name, shorthand, usage)
}

// DurationVarPF is like DurationVarP, but returns the created flag.
func (f *FlagSet) DurationVarPF(p *time.Duration, name, shorthand string, value time.Duration, usage string) *Flag {
return f.VarPF(newDurationValue(value, p), name, shorthand, usage)
}

// DurationVar defines a time.Duration flag with specified name, default value, and usage string.
// The argument p points to a time.Duration variable in which to store the value of the flag.
func DurationVar(p *time.Duration, name string, value time.Duration, usage string) {
Expand All @@ -59,6 +64,11 @@ func DurationVarP(p *time.Duration, name, shorthand string, value time.Duration,
CommandLine.VarP(newDurationValue(value, p), name, shorthand, usage)
}

// DurationVarPF is like DurationVarP, but returns the created flag.
func DurationVarPF(p *time.Duration, name, shorthand string, value time.Duration, usage string) *Flag {
return CommandLine.VarPF(newDurationValue(value, p), name, shorthand, usage)
}

// Duration defines a time.Duration flag with specified name, default value, and usage string.
// The return value is the address of a time.Duration variable that stores the value of the flag.
func (f *FlagSet) Duration(name string, value time.Duration, usage string) *time.Duration {
Expand Down
10 changes: 10 additions & 0 deletions duration_slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ func (f *FlagSet) DurationSliceVarP(p *[]time.Duration, name, shorthand string,
f.VarP(newDurationSliceValue(value, p), name, shorthand, usage)
}

// DurationSliceVarPF is like DurationSliceVarP, but returns the created flag.
func (f *FlagSet) DurationSliceVarPF(p *[]time.Duration, name, shorthand string, value []time.Duration, usage string) *Flag {
return f.VarPF(newDurationSliceValue(value, p), name, shorthand, usage)
}

// DurationSliceVar defines a duration[] flag with specified name, default value, and usage string.
// The argument p points to a duration[] variable in which to store the value of the flag.
func DurationSliceVar(p *[]time.Duration, name string, value []time.Duration, usage string) {
Expand All @@ -139,6 +144,11 @@ func DurationSliceVarP(p *[]time.Duration, name, shorthand string, value []time.
CommandLine.VarP(newDurationSliceValue(value, p), name, shorthand, usage)
}

// DurationSliceVarPF is like DurationSliceVarP, but returns the created flag.
func DurationSliceVarPF(p *[]time.Duration, name, shorthand string, value []time.Duration, usage string) *Flag {
return CommandLine.VarPF(newDurationSliceValue(value, p), name, shorthand, usage)
}

// DurationSlice defines a []time.Duration flag with specified name, default value, and usage string.
// The return value is the address of a []time.Duration variable that stores the value of the flag.
func (f *FlagSet) DurationSlice(name string, value []time.Duration, usage string) *[]time.Duration {
Expand Down
10 changes: 10 additions & 0 deletions float32.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ func (f *FlagSet) Float32VarP(p *float32, name, shorthand string, value float32,
f.VarP(newFloat32Value(value, p), name, shorthand, usage)
}

// Float32VarPF is like Float32VarP, but returns the created flag.
func (f *FlagSet) Float32VarPF(p *float32, name, shorthand string, value float32, usage string) *Flag {
return f.VarPF(newFloat32Value(value, p), name, shorthand, usage)
}

// Float32Var defines a float32 flag with specified name, default value, and usage string.
// The argument p points to a float32 variable in which to store the value of the flag.
func Float32Var(p *float32, name string, value float32, usage string) {
Expand All @@ -61,6 +66,11 @@ func Float32VarP(p *float32, name, shorthand string, value float32, usage string
CommandLine.VarP(newFloat32Value(value, p), name, shorthand, usage)
}

// Float32VarPF is like Float32VarP, but returns the created flag.
func Float32VarPF(p *float32, name, shorthand string, value float32, usage string) *Flag {
return CommandLine.VarPF(newFloat32Value(value, p), name, shorthand, usage)
}

// Float32 defines a float32 flag with specified name, default value, and usage string.
// The return value is the address of a float32 variable that stores the value of the flag.
func (f *FlagSet) Float32(name string, value float32, usage string) *float32 {
Expand Down
10 changes: 10 additions & 0 deletions float32_slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ func (f *FlagSet) Float32SliceVarP(p *[]float32, name, shorthand string, value [
f.VarP(newFloat32SliceValue(value, p), name, shorthand, usage)
}

// Float32SliceVarPF is like Float32SliceVarP, but returns the created flag.
func (f *FlagSet) Float32SliceVarPF(p *[]float32, name, shorthand string, value []float32, usage string) *Flag {
return f.VarPF(newFloat32SliceValue(value, p), name, shorthand, usage)
}

// Float32SliceVar defines a float32[] flag with specified name, default value, and usage string.
// The argument p points to a float32[] variable in which to store the value of the flag.
func Float32SliceVar(p *[]float32, name string, value []float32, usage string) {
Expand All @@ -147,6 +152,11 @@ func Float32SliceVarP(p *[]float32, name, shorthand string, value []float32, usa
CommandLine.VarP(newFloat32SliceValue(value, p), name, shorthand, usage)
}

// Float32SliceVarPF is like Float32SliceVarP, but returns the created flag.
func Float32SliceVarPF(p *[]float32, name, shorthand string, value []float32, usage string) *Flag {
return CommandLine.VarPF(newFloat32SliceValue(value, p), name, shorthand, usage)
}

// Float32Slice defines a []float32 flag with specified name, default value, and usage string.
// The return value is the address of a []float32 variable that stores the value of the flag.
func (f *FlagSet) Float32Slice(name string, value []float32, usage string) *[]float32 {
Expand Down
10 changes: 10 additions & 0 deletions float64.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ func (f *FlagSet) Float64VarP(p *float64, name, shorthand string, value float64,
f.VarP(newFloat64Value(value, p), name, shorthand, usage)
}

// Float64VarPF is like Float64VarP, but returns the created flag.
func (f *FlagSet) Float64VarPF(p *float64, name, shorthand string, value float64, usage string) *Flag {
return f.VarPF(newFloat64Value(value, p), name, shorthand, usage)
}

// Float64Var defines a float64 flag with specified name, default value, and usage string.
// The argument p points to a float64 variable in which to store the value of the flag.
func Float64Var(p *float64, name string, value float64, usage string) {
Expand All @@ -57,6 +62,11 @@ func Float64VarP(p *float64, name, shorthand string, value float64, usage string
CommandLine.VarP(newFloat64Value(value, p), name, shorthand, usage)
}

// Float64VarPF is like Float64VarP, bbut returns the created flag.
func Float64VarPF(p *float64, name, shorthand string, value float64, usage string) *Flag {
return CommandLine.VarPF(newFloat64Value(value, p), name, shorthand, usage)
}

// Float64 defines a float64 flag with specified name, default value, and usage string.
// The return value is the address of a float64 variable that stores the value of the flag.
func (f *FlagSet) Float64(name string, value float64, usage string) *float64 {
Expand Down
10 changes: 10 additions & 0 deletions float64_slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ func (f *FlagSet) Float64SliceVarP(p *[]float64, name, shorthand string, value [
f.VarP(newFloat64SliceValue(value, p), name, shorthand, usage)
}

// Float64SliceVarPF is like Float64SliceVarP, but returns the created flag.
func (f *FlagSet) Float64SliceVarPF(p *[]float64, name, shorthand string, value []float64, usage string) *Flag {
return f.VarPF(newFloat64SliceValue(value, p), name, shorthand, usage)
}

// Float64SliceVar defines a float64[] flag with specified name, default value, and usage string.
// The argument p points to a float64[] variable in which to store the value of the flag.
func Float64SliceVar(p *[]float64, name string, value []float64, usage string) {
Expand All @@ -139,6 +144,11 @@ func Float64SliceVarP(p *[]float64, name, shorthand string, value []float64, usa
CommandLine.VarP(newFloat64SliceValue(value, p), name, shorthand, usage)
}

// Float64SliceVarPF is like Float64SliceVarP, but returns the created flag.
func Float64SliceVarPF(p *[]float64, name, shorthand string, value []float64, usage string) *Flag {
return CommandLine.VarPF(newFloat64SliceValue(value, p), name, shorthand, usage)
}

// Float64Slice defines a []float64 flag with specified name, default value, and usage string.
// The return value is the address of a []float64 variable that stores the value of the flag.
func (f *FlagSet) Float64Slice(name string, value []float64, usage string) *[]float64 {
Expand Down
10 changes: 10 additions & 0 deletions int.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ func (f *FlagSet) IntVarP(p *int, name, shorthand string, value int, usage strin
f.VarP(newIntValue(value, p), name, shorthand, usage)
}

// IntVarPF is like IntVarP, but returns the created flag.
func (f *FlagSet) IntVarPF(p *int, name, shorthand string, value int, usage string) *Flag {
return f.VarPF(newIntValue(value, p), name, shorthand, usage)
}

// IntVar defines an int flag with specified name, default value, and usage string.
// The argument p points to an int variable in which to store the value of the flag.
func IntVar(p *int, name string, value int, usage string) {
Expand All @@ -57,6 +62,11 @@ func IntVarP(p *int, name, shorthand string, value int, usage string) {
CommandLine.VarP(newIntValue(value, p), name, shorthand, usage)
}

// IntVarPF is like IntVarP, but returns the created flag.
func IntVarPF(p *int, name, shorthand string, value int, usage string) *Flag {
return CommandLine.VarPF(newIntValue(value, p), name, shorthand, usage)
}

// Int defines an int flag with specified name, default value, and usage string.
// The return value is the address of an int variable that stores the value of the flag.
func (f *FlagSet) Int(name string, value int, usage string) *int {
Expand Down
10 changes: 10 additions & 0 deletions int16.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ func (f *FlagSet) Int16VarP(p *int16, name, shorthand string, value int16, usage
f.VarP(newInt16Value(value, p), name, shorthand, usage)
}

// Int16VarPF is like Int16VarP, but returns the created flag.
func (f *FlagSet) Int16VarPF(p *int16, name, shorthand string, value int16, usage string) *Flag {
return f.VarPF(newInt16Value(value, p), name, shorthand, usage)
}

// Int16Var defines an int16 flag with specified name, default value, and usage string.
// The argument p points to an int16 variable in which to store the value of the flag.
func Int16Var(p *int16, name string, value int16, usage string) {
Expand All @@ -61,6 +66,11 @@ func Int16VarP(p *int16, name, shorthand string, value int16, usage string) {
CommandLine.VarP(newInt16Value(value, p), name, shorthand, usage)
}

// Int16VarPF is like Int16VarP, but returns the created flag.
func Int16VarPF(p *int16, name, shorthand string, value int16, usage string) *Flag {
return CommandLine.VarPF(newInt16Value(value, p), name, shorthand, usage)
}

// Int16 defines an int16 flag with specified name, default value, and usage string.
// The return value is the address of an int16 variable that stores the value of the flag.
func (f *FlagSet) Int16(name string, value int16, usage string) *int16 {
Expand Down
10 changes: 10 additions & 0 deletions int32.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ func (f *FlagSet) Int32VarP(p *int32, name, shorthand string, value int32, usage
f.VarP(newInt32Value(value, p), name, shorthand, usage)
}

// Int32VarPF is like Int32VarP, but returns the created flag.
func (f *FlagSet) Int32VarPF(p *int32, name, shorthand string, value int32, usage string) *Flag {
return f.VarPF(newInt32Value(value, p), name, shorthand, usage)
}

// Int32Var defines an int32 flag with specified name, default value, and usage string.
// The argument p points to an int32 variable in which to store the value of the flag.
func Int32Var(p *int32, name string, value int32, usage string) {
Expand All @@ -61,6 +66,11 @@ func Int32VarP(p *int32, name, shorthand string, value int32, usage string) {
CommandLine.VarP(newInt32Value(value, p), name, shorthand, usage)
}

// Int32VarPF is like Int32VarP, but returns the created flag.
func Int32VarPF(p *int32, name, shorthand string, value int32, usage string) *Flag {
return CommandLine.VarPF(newInt32Value(value, p), name, shorthand, usage)
}

// Int32 defines an int32 flag with specified name, default value, and usage string.
// The return value is the address of an int32 variable that stores the value of the flag.
func (f *FlagSet) Int32(name string, value int32, usage string) *int32 {
Expand Down
10 changes: 10 additions & 0 deletions int32_slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ func (f *FlagSet) Int32SliceVarP(p *[]int32, name, shorthand string, value []int
f.VarP(newInt32SliceValue(value, p), name, shorthand, usage)
}

// Int32SliceVarPF is like Int32SliceVarP, but returns the created flag.
func (f *FlagSet) Int32SliceVarPF(p *[]int32, name, shorthand string, value []int32, usage string) *Flag {
return f.VarPF(newInt32SliceValue(value, p), name, shorthand, usage)
}

// Int32SliceVar defines a int32[] flag with specified name, default value, and usage string.
// The argument p points to a int32[] variable in which to store the value of the flag.
func Int32SliceVar(p *[]int32, name string, value []int32, usage string) {
Expand All @@ -147,6 +152,11 @@ func Int32SliceVarP(p *[]int32, name, shorthand string, value []int32, usage str
CommandLine.VarP(newInt32SliceValue(value, p), name, shorthand, usage)
}

// Int32SliceVarPF is like Int32SliceVarP, but returns the created flag.
func Int32SliceVarPF(p *[]int32, name, shorthand string, value []int32, usage string) *Flag {
return CommandLine.VarPF(newInt32SliceValue(value, p), name, shorthand, usage)
}

// Int32Slice defines a []int32 flag with specified name, default value, and usage string.
// The return value is the address of a []int32 variable that stores the value of the flag.
func (f *FlagSet) Int32Slice(name string, value []int32, usage string) *[]int32 {
Expand Down
Loading

0 comments on commit e79b13d

Please sign in to comment.