-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
49 lines (39 loc) · 1.01 KB
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package yag_test
import (
"fmt"
"os"
"time"
"github.com/zoido/yag-config"
)
type config struct {
Str string
Bool bool
Int int
Duration time.Duration
}
func Example() {
y := yag.New(yag.WithEnvPrefix("MY_APP_"))
cfg := &config{
Str: "default str value",
Int: 42,
}
y.String(&cfg.Str, "str", "sets Str")
y.Bool(&cfg.Bool, "bool", "sets Bool")
y.Duration(&cfg.Duration, "duration", "sets Duration", yag.FromEnv("MY_DURATION_VALUE"))
y.Int(&cfg.Int, "int", "sets Int")
args := []string{"-str=str flag value"}
_ = os.Setenv("MY_APP_STR", "str env value")
_ = os.Setenv("MY_APP_INT", "4")
_ = os.Setenv("MY_DURATION_VALUE", "1h")
err := y.Parse(args)
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
fmt.Printf("config.Str: %v, ", cfg.Str)
fmt.Printf("config.Int: %v, ", cfg.Int)
fmt.Printf("config.Bool: %v, ", cfg.Bool)
fmt.Printf("config.Duration: %v", cfg.Duration)
// Output:
// config.Str: str flag value, config.Int: 4, config.Bool: false, config.Duration: 1h0m0s
}