summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--flag.go27
-rw-r--r--flag_test.go8
2 files changed, 15 insertions, 20 deletions
diff --git a/flag.go b/flag.go
index 1931077..4506683 100644
--- a/flag.go
+++ b/flag.go
@@ -15,15 +15,14 @@ import (
// passed ParseFunc will be used to parse raw arguments into a useful T
// value. A valid *T is returned for use by the caller.
func Flag[T any](fs *flag.FlagSet, name string, value T, usage string, parse ParseFunc[T]) *T {
- p := new(T)
- FlagVar(fs, p, name, value, usage, parse)
- return p
+ p := value
+ FlagVar(fs, &p, name, usage, parse)
+ return &p
}
// FlagVar works like FlagT, except it is up to the caller to supply a
// valid *T.
-func FlagVar[T any](fs *flag.FlagSet, p *T, name string, value T, usage string, parse ParseFunc[T]) {
- *p = value
+func FlagVar[T any](fs *flag.FlagSet, p *T, name string, usage string, parse ParseFunc[T]) {
fs.Var(&flagValue[T]{Parse: parse, Value: p}, name, usage)
}
@@ -40,18 +39,15 @@ func FlagVar[T any](fs *flag.FlagSet, p *T, name string, value T, usage string,
// - -flag=val,val-2 -flag=val-3
// - -flag=val,val-2,val-3
func FlagSlice[T any](fs *flag.FlagSet, name string, values []T, usage string, parse ParseFunc[T], sep string) *[]T {
- p := new([]T)
- FlagSliceVar(fs, p, name, values, usage, parse, sep)
- return p
+ p := make([]T, len(values))
+ copy(p, values)
+ FlagSliceVar(fs, &p, name, usage, parse, sep)
+ return &p
}
// FlagSliceVar works like FlagTSlice, except it is up to the caller to
// supply a valid *[]T.
-func FlagSliceVar[T any](fs *flag.FlagSet, p *[]T, name string, values []T, usage string, parse ParseFunc[T], sep string) {
- if values != nil {
- *p = make([]T, len(values))
- copy(*p, values)
- }
+func FlagSliceVar[T any](fs *flag.FlagSet, p *[]T, name string, usage string, parse ParseFunc[T], sep string) {
fs.Var(&flagValueSlice[T]{Parse: parse, Separator: sep, Values: p}, name, usage)
}
@@ -59,9 +55,8 @@ func FlagSliceVar[T any](fs *flag.FlagSet, p *[]T, name string, values []T, usag
// following order: environment variables, then an arbitrary map, then
// command line arguments.
//
-// Note that InitFlagSet does not require the use of any of the Flag
-// functions defined in this package. Standard flags will work just as
-// well.
+// Note that InitFlagSet does not require the use of the Flag functions
+// defined in this package. Standard flags will work just as well.
func InitFlagSet(fs *flag.FlagSet, env []string, cfg map[string]string, args []string) (err error) {
var environ map[string]string
if env != nil {
diff --git a/flag_test.go b/flag_test.go
index 1f36b63..91c6caa 100644
--- a/flag_test.go
+++ b/flag_test.go
@@ -25,8 +25,8 @@ func TestFlagVar(s *testing.T) {
t := core.T{T: s}
fs := flag.NewFlagSet("", flag.PanicOnError)
- var fl int
- core.FlagVar(fs, &fl, "test", 42, "", strconv.Atoi)
+ fl := 42
+ core.FlagVar(fs, &fl, "test", "", strconv.Atoi)
t.AssertEqual(42, fl)
t.AssertErrorIs(nil, fs.Parse([]string{"-test=84"}))
t.AssertEqual(84, fl)
@@ -46,8 +46,8 @@ func TestFlagSliceVar(s *testing.T) {
t := core.T{T: s}
fs := flag.NewFlagSet("", flag.PanicOnError)
- var fl []int
- core.FlagSliceVar(fs, &fl, "test", []int{42}, "", strconv.Atoi, ",")
+ fl := []int{42}
+ core.FlagSliceVar(fs, &fl, "test", "", strconv.Atoi, ",")
t.AssertEqual([]int{42}, fl)
t.AssertErrorIs(nil, fs.Parse([]string{"-test=1", "-test=2", "-test=42,84"}))
t.AssertEqual([]int{1, 2, 42, 84}, fl)