From: qydysky Date: Sat, 1 Feb 2025 08:31:50 +0000 (+0800) Subject: 1 (#19) X-Git-Tag: v0.28.20250201083159 X-Git-Url: http://127.0.0.1:8081/?a=commitdiff_plain;h=1e7470d784ec9d929d6354c1fa308f6b7b7656e5;p=part%2F.git 1 (#19) --- diff --git a/.github/workflows/test1.yml b/.github/workflows/test1.yml index 5bd49c2..1c9565c 100644 --- a/.github/workflows/test1.yml +++ b/.github/workflows/test1.yml @@ -42,6 +42,7 @@ jobs: go test -count 1 -timeout 5s -v -race github.com/qydysky/part/bools go test -count 1 -timeout 5s -v -race github.com/qydysky/part/errors go test -count 1 -timeout 5s -v -race github.com/qydysky/part/crypto + go test -count 1 -timeout 5s -v -race github.com/qydysky/part/flag -sss=ss -i32=32 -f34=34 -btrue=true -d1m=1m w-test: name: w-test @@ -80,6 +81,7 @@ jobs: go test -count 1 -timeout 5s -v -race github.com/qydysky/part/bools go test -count 1 -timeout 5s -v -race github.com/qydysky/part/errors go test -count 1 -timeout 5s -v -race github.com/qydysky/part/crypto + go test -count 1 -timeout 5s -v -race github.com/qydysky/part/flag -sss=ss -i32=32 -f34=34 -btrue=true -d1m=1m u-test: name: u-test @@ -118,3 +120,4 @@ jobs: go test -count 1 -timeout 5s -v -race github.com/qydysky/part/bools go test -count 1 -timeout 5s -v -race github.com/qydysky/part/errors go test -count 1 -timeout 5s -v -race github.com/qydysky/part/crypto + go test -count 1 -timeout 5s -v -race github.com/qydysky/part/flag -sss=ss -i32=32 -f34=34 -btrue=true -d1m=1m diff --git a/flag/flag.go b/flag/flag.go new file mode 100644 index 0000000..eb90152 --- /dev/null +++ b/flag/flag.go @@ -0,0 +1,71 @@ +package part + +import ( + "flag" + "strconv" + "strings" + "time" + "unsafe" +) + +type flagType interface { + string | bool | float64 | int | int32 | int64 | uint | uint32 | uint64 | time.Duration +} + +func Lookup[T flagType](name string, defaultVal T) T { + if f := flag.Lookup(name); f != nil { + switch any(defaultVal).(type) { + case string: + return any(f.Value.String()).(T) + case bool: + return any(strings.ToLower(f.Value.String()) == "true").(T) + case float64: + if r, e := strconv.ParseFloat(f.Value.String(), 64); e == nil { + return any(r).(T) + } + case int: + switch unsafe.Sizeof(int(0)) { + case unsafe.Sizeof(int64(0)): + if r, e := strconv.ParseInt(f.Value.String(), 0, 64); e == nil { + return any(int(r)).(T) + } + case unsafe.Sizeof(int32(0)): + if r, e := strconv.ParseInt(f.Value.String(), 0, 32); e == nil { + return any(int(r)).(T) + } + } + case int32: + if r, e := strconv.ParseInt(f.Value.String(), 0, 32); e == nil { + return any(int32(r)).(T) + } + case int64: + if r, e := strconv.ParseInt(f.Value.String(), 0, 64); e == nil { + return any(r).(T) + } + case uint: + switch unsafe.Sizeof(uint(0)) { + case unsafe.Sizeof(uint64(0)): + if r, e := strconv.ParseUint(f.Value.String(), 0, 64); e == nil { + return any(uint(r)).(T) + } + case unsafe.Sizeof(uint32(0)): + if r, e := strconv.ParseUint(f.Value.String(), 0, 32); e == nil { + return any(uint(r)).(T) + } + } + case uint32: + if r, e := strconv.ParseUint(f.Value.String(), 0, 32); e == nil { + return any(uint32(r)).(T) + } + case uint64: + if r, e := strconv.ParseUint(f.Value.String(), 0, 64); e == nil { + return any(r).(T) + } + case time.Duration: + if r, e := time.ParseDuration(f.Value.String()); e == nil { + return any(r).(T) + } + } + } + return defaultVal +} diff --git a/flag/flag_test.go b/flag/flag_test.go new file mode 100644 index 0000000..0c8e168 --- /dev/null +++ b/flag/flag_test.go @@ -0,0 +1,38 @@ +package part + +import ( + "flag" + "testing" + "time" +) + +func init() { + flag.String("sss", "", "") + flag.Int("i32", 0, "") + flag.Int("f34", 0, "") + flag.Bool("btrue", false, "") + flag.Duration("d1m", time.Second, "") + testing.Init() + flag.Parse() +} + +func TestMain(t *testing.T) { + if Lookup("sss", "") != "ss" { + t.Fatal() + } + if Lookup("s", "") != "" { + t.Fatal() + } + if Lookup("i32", 0) != 32 { + t.Fatal() + } + if Lookup("f34", 0.0) != 34 { + t.Fatal() + } + if !Lookup("btrue", false) { + t.Fatal() + } + if Lookup("d1m", time.Second).Seconds() != 60 { + t.Fatal() + } +}