]> 127.0.0.1 Git - part/.git/commitdiff
1 (#19) v0.28.20250201083159
authorqydysky <qydysky@foxmail.com>
Sat, 1 Feb 2025 08:31:50 +0000 (16:31 +0800)
committerGitHub <noreply@github.com>
Sat, 1 Feb 2025 08:31:50 +0000 (16:31 +0800)
.github/workflows/test1.yml
flag/flag.go [new file with mode: 0644]
flag/flag_test.go [new file with mode: 0644]

index 5bd49c251b0be3fd212cd508216045741dbef12b..1c9565c3c086d43d00944014b7cdf80725848201 100644 (file)
@@ -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 (file)
index 0000000..eb90152
--- /dev/null
@@ -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 (file)
index 0000000..0c8e168
--- /dev/null
@@ -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()
+       }
+}