aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGrégoire Duchêne <gduchene@awhk.org>2022-12-03 12:01:45 +0000
committerGrégoire Duchêne <gduchene@awhk.org>2022-12-03 12:01:45 +0000
commita3ef784a3b5af5dd98ab8d9a44404f2ef240a626 (patch)
treef70615a096a8a36b32b6f9ea618bf310d204fea0
parent552c8aaa156f39725329c6d00ea715e0312c6fb8 (diff)
Drop T from the name of the Flag functions
Kind of redundant given that there’s a type parameter right after the function name.
-rw-r--r--flag.go24
-rw-r--r--flag_test.go18
2 files changed, 21 insertions, 21 deletions
diff --git a/flag.go b/flag.go
index d48c091..6fe41f3 100644
--- a/flag.go
+++ b/flag.go
@@ -11,23 +11,23 @@ import (
"time"
)
-// FlagT works like other flag.FlagSet methods, except it is generic.
-// The 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 FlagT[T any](fs *flag.FlagSet, name string, value T, usage string, parse ParseFunc[T]) *T {
+// Flag works like other flag.FlagSet methods, except it is generic. The
+// 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)
- FlagTVar(fs, p, name, value, usage, parse)
+ FlagVar(fs, p, name, value, usage, parse)
return p
}
-// FlagTVar works like FlagT, except it is up to the caller to supply a
+// FlagVar works like FlagT, except it is up to the caller to supply a
// valid *T.
-func FlagTVar[T any](fs *flag.FlagSet, p *T, name string, value T, usage string, parse ParseFunc[T]) {
+func FlagVar[T any](fs *flag.FlagSet, p *T, name string, value T, usage string, parse ParseFunc[T]) {
*p = value
fs.Var(&flagValue[T]{Parse: parse, Value: p}, name, usage)
}
-// FlagTSlice works like FlagT, except slices are created; flags created
+// FlagSlice works like FlagT, except slices are created; flags created
// that way can therefore be repeated. A valid *[]T is returned for use
// by the caller.
//
@@ -39,15 +39,15 @@ func FlagTVar[T any](fs *flag.FlagSet, p *T, name string, value T, usage string,
// - -flag=val -flag=val-2 -flag=val-3
// - -flag=val,val-2 -flag=val-3
// - -flag=val,val-2,val-3
-func FlagTSlice[T any](fs *flag.FlagSet, name string, values []T, usage string, parse ParseFunc[T], sep string) *[]T {
+func FlagSlice[T any](fs *flag.FlagSet, name string, values []T, usage string, parse ParseFunc[T], sep string) *[]T {
p := new([]T)
- FlagTSliceVar(fs, p, name, values, usage, parse, sep)
+ FlagSliceVar(fs, p, name, values, usage, parse, sep)
return p
}
-// FlagTSliceVar works like FlagTSlice, except it is up to the caller to
+// FlagSliceVar works like FlagTSlice, except it is up to the caller to
// supply a valid *[]T.
-func FlagTSliceVar[T any](fs *flag.FlagSet, p *[]T, name string, values []T, usage string, parse ParseFunc[T], sep string) {
+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)
diff --git a/flag_test.go b/flag_test.go
index 584a2c9..26568d6 100644
--- a/flag_test.go
+++ b/flag_test.go
@@ -11,43 +11,43 @@ import (
"go.awhk.org/core"
)
-func TestFlagT(s *testing.T) {
+func TestFlag(s *testing.T) {
t := core.T{T: s}
fs := flag.NewFlagSet("", flag.PanicOnError)
- fl := core.FlagT(fs, "test", 42, "", strconv.Atoi)
+ fl := core.Flag(fs, "test", 42, "", strconv.Atoi)
t.AssertEqual(42, *fl)
t.AssertErrorIs(nil, fs.Parse([]string{"-test=84"}))
t.AssertEqual(84, *fl)
}
-func TestFlagTVar(s *testing.T) {
+func TestFlagVar(s *testing.T) {
t := core.T{T: s}
fs := flag.NewFlagSet("", flag.PanicOnError)
var fl int
- core.FlagTVar(fs, &fl, "test", 42, "", strconv.Atoi)
+ core.FlagVar(fs, &fl, "test", 42, "", strconv.Atoi)
t.AssertEqual(42, fl)
t.AssertErrorIs(nil, fs.Parse([]string{"-test=84"}))
t.AssertEqual(84, fl)
}
-func TestFlagTSlice(s *testing.T) {
+func TestFlagSlice(s *testing.T) {
t := core.T{T: s}
fs := flag.NewFlagSet("", flag.PanicOnError)
- fl := core.FlagTSlice(fs, "test", []int{42}, "", strconv.Atoi, ",")
+ fl := core.FlagSlice(fs, "test", []int{42}, "", 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)
}
-func TestFlagTSliceVar(s *testing.T) {
+func TestFlagSliceVar(s *testing.T) {
t := core.T{T: s}
fs := flag.NewFlagSet("", flag.PanicOnError)
var fl []int
- core.FlagTSliceVar(fs, &fl, "test", []int{42}, "", strconv.Atoi, ",")
+ core.FlagSliceVar(fs, &fl, "test", []int{42}, "", 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)
@@ -102,7 +102,7 @@ func TestInitFlagSet(s *testing.T) {
t.Run(tc.name, func(t *core.T) {
fs := flag.NewFlagSet("", flag.PanicOnError)
fi := fs.Int("int", 0, "")
- fl := core.FlagTSlice(fs, "int-slice", nil, "", strconv.Atoi, ",")
+ fl := core.FlagSlice(fs, "int-slice", nil, "", strconv.Atoi, ",")
fm := fs.String("string", "", "")
t.AssertErrorIs(tc.expErr, core.InitFlagSet(fs, tc.env, tc.cfg, tc.args))
t.AssertEqual(tc.expInt, *fi)