forked from grailbio/base
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
59 lines (51 loc) · 1.6 KB
/
main.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
50
51
52
53
54
55
56
57
58
59
package main
import (
"flag"
"fmt"
"github.com/Schaudge/grailbase/config"
"github.com/Schaudge/grailbase/must"
)
type (
Fruit interface{ IsFruit() }
Apple struct{ color string }
Orange struct{}
)
func (Apple) IsFruit() {}
func (Orange) IsFruit() {}
func init() {
config.Register("fruits/apple-red", func(c *config.Constructor[Apple]) {
c.Doc = "Some people like apples."
c.New = func() (Apple, error) { return Apple{"red"}, nil }
})
config.Register("fruits/apple-green", func(c *config.Constructor[Apple]) {
c.Doc = "Another apple."
c.New = func() (Apple, error) { return Apple{"green"}, nil }
})
config.Register("fruits/orange", func(c *config.Constructor[Orange]) {
c.Doc = "Some people like oranges."
c.New = func() (Orange, error) { return Orange{}, nil }
})
config.Register("favorite", func(c *config.Constructor[Fruit]) {
c.Doc = "My favorite fruit."
var favorite Fruit
c.InstanceVar(&favorite, "is", "favorite-apple", "Favorite fruit?")
c.New = func() (Fruit, error) { return favorite, nil }
})
config.Register("favorite-apple", func(c *config.Constructor[Apple]) {
c.Doc = "My favorite apple."
var favorite Apple
c.InstanceVar(&favorite, "is", "fruits/apple-green", "Favorite apple?")
c.New = func() (Apple, error) { return favorite, nil }
})
}
func main() {
config.RegisterFlags("", "")
flag.Parse()
must.Nil(config.ProcessFlags())
var fruit Fruit
must.Nil(config.Instance("favorite", &fruit))
fmt.Printf("My favorite fruit is %#v.\n", fruit)
var apple Apple
must.Nil(config.Instance("favorite-apple", &apple))
fmt.Printf("My favorite apple is %#v.\n", apple)
}