/
option.go
84 lines (67 loc) · 2.19 KB
/
option.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package libuconf
import "strings"
/*
EnvOpt describes an option that can be set from the environment.
It is used by ParseEnv() to determine what environment variables to read.
For example, if the app name is "APP" and Env() returns "FOO", we will look for
the environment variable "APP_FOO".
*/
type EnvOpt interface {
Setter
Env() string // example: A_B_C
}
/*
FlagOpt describes an option that can be set using a flag.
It is used by ParseFlags.
If Bool returns true, ParseFlags will know that a value is technically optional
for the given flag.
If it cannot find a value for it, it will thus pass "true" (bool) to Set().
Flag and ShortFlag determine what ParseFlags looks for on the command line, as
well as what Usage will print out.
Finally, Help changes the help string in Usage.
*/
type FlagOpt interface {
Setter
Bool() bool
Flag() string // example: a.b.c
Help() string
ShortFlag() rune // can be 0: means disabled; example: x
}
/*
Getter describes an option that can return its *current* value.
This is used in tests, as well as in Usage.
Usage in particular allows you to observe the current value of your flags, which
is useful to see whether or not your config files (for example) are being
applied.
It may also be of interest to consumers of the library, via Visit.
*/
type Getter interface {
Get() interface{}
}
/*
Setter describes a type that can be Set.
All options must implement Setter.
Set takes an interface, and should use type switch and/or reflection to take the
appropriate course of action based on what ended up being passed to it.
It is highly recommended to at least handle strings.
If your option implements FlagOpt and Bool returns true, Set is also expected to
handle booleans.
*/
type Setter interface {
Set(interface{}) error
}
/*
TomlOpt describes an option that can be set by a value in a TOML file.
The return value of Toml should be the exact search query to plug in.
Set will be called with whatever the get call returns (unless it's nil).
*/
type TomlOpt interface {
Setter
Toml() string // a TOML query string; example: a.b.c
}
func env(f FlagOpt) string {
return strings.ToUpper(strings.ReplaceAll(f.Flag(), ".", "_"))
}
func _toml(f FlagOpt) string {
return f.Flag()
}