/
config_generated.go
77 lines (68 loc) · 1.58 KB
/
config_generated.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
// Code generated by "goconfig -type flag.ErrorHandling,CommandName|string -option -output config_generated.go -configOption Option"; DO NOT EDIT.
package fcli
import "flag"
type ConfigItem[T any] struct {
modified bool
value T
defaultValue T
}
func (s *ConfigItem[T]) Set(value T) {
s.modified = true
s.value = value
}
func (s *ConfigItem[T]) Get() T {
if s.modified {
return s.value
}
return s.defaultValue
}
func (s *ConfigItem[T]) Default() T {
return s.defaultValue
}
func (s *ConfigItem[T]) IsModified() bool {
return s.modified
}
func NewConfigItem[T any](defaultValue T) *ConfigItem[T] {
return &ConfigItem[T]{
defaultValue: defaultValue,
}
}
type Config struct {
ErrorHandling *ConfigItem[flag.ErrorHandling]
CommandName *ConfigItem[string]
}
type ConfigBuilder struct {
errorHandling flag.ErrorHandling
commandName string
}
func (s *ConfigBuilder) ErrorHandling(v flag.ErrorHandling) *ConfigBuilder {
s.errorHandling = v
return s
}
func (s *ConfigBuilder) CommandName(v string) *ConfigBuilder {
s.commandName = v
return s
}
func (s *ConfigBuilder) Build() *Config {
return &Config{
ErrorHandling: NewConfigItem(s.errorHandling),
CommandName: NewConfigItem(s.commandName),
}
}
func NewConfigBuilder() *ConfigBuilder { return &ConfigBuilder{} }
func (s *Config) Apply(opt ...Option) {
for _, x := range opt {
x(s)
}
}
type Option func(*Config)
func WithErrorHandling(v flag.ErrorHandling) Option {
return func(c *Config) {
c.ErrorHandling.Set(v)
}
}
func WithCommandName(v string) Option {
return func(c *Config) {
c.CommandName.Set(v)
}
}