-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathexample_test.go
37 lines (31 loc) · 866 Bytes
/
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
package options_test
import (
"flag"
"fmt"
"time"
"github.com/mreiferson/go-options"
)
type Options struct {
MaxSize int64 `flag:"max-size" cfg:"max_size"`
Timeout time.Duration `flag:"timeout" cfg:"timeout"`
Description string `flag:"description" cfg:"description"`
}
func ExampleResolve() {
flagSet := flag.NewFlagSet("example", flag.ExitOnError)
flagSet.Int64("max-size", 1024768, "maximum size")
flagSet.Duration("timeout", 1*time.Hour, "timeout setting")
flagSet.String("description", "", "description info")
// parse command line arguments here
// flagSet.Parse(os.Args[1:])
flagSet.Parse([]string{"-timeout=5s"})
opts := &Options{
MaxSize: 1,
Timeout: time.Second,
}
cfg := map[string]interface{}{
"timeout": "1h",
}
fmt.Printf("%#v", opts)
options.Resolve(opts, flagSet, cfg)
fmt.Printf("%#v", opts)
}