@@ -46,12 +46,7 @@ type Config map[string]*Section
46
46
type ConfigParser struct {
47
47
config Config
48
48
defaults * Section
49
- opt * Options
50
- }
51
-
52
- // Options allows to control parser behavior.
53
- type Options struct {
54
- AllowNoValue bool
49
+ opt * options
55
50
}
56
51
57
52
// Keys returns a sorted slice of keys
@@ -79,12 +74,17 @@ func New() *ConfigParser {
79
74
return & ConfigParser {
80
75
config : make (Config ),
81
76
defaults : newSection (defaultSectionName ),
82
- opt : & Options {},
77
+ opt : & options {},
83
78
}
84
79
}
85
80
86
81
// NewWithOptions creates a new ConfigParser with options.
87
- func NewWithOptions (opt * Options ) * ConfigParser {
82
+ func NewWithOptions (opts ... OptFunc ) * ConfigParser {
83
+ opt := & options {}
84
+ for _ , fn := range opts {
85
+ fn (opt )
86
+ }
87
+
88
88
return & ConfigParser {
89
89
config : make (Config ),
90
90
defaults : newSection (defaultSectionName ),
@@ -123,8 +123,8 @@ func ParseReader(in io.Reader) (*ConfigParser, error) {
123
123
}
124
124
125
125
// ParseReaderWithOptions parses a ConfigParser from the provided input with given options.
126
- func ParseReaderWithOptions (in io.Reader , opt * Options ) (* ConfigParser , error ) {
127
- p := NewWithOptions (opt )
126
+ func ParseReaderWithOptions (in io.Reader , opts ... OptFunc ) (* ConfigParser , error ) {
127
+ p := NewWithOptions (opts ... )
128
128
err := p .ParseReader (in )
129
129
130
130
return p , err
@@ -145,8 +145,8 @@ func Parse(filename string) (*ConfigParser, error) {
145
145
}
146
146
147
147
// ParseWithOptions takes a filename and parses it into a ConfigParser value with given options.
148
- func ParseWithOptions (filename string , opt * Options ) (* ConfigParser , error ) {
149
- p := NewWithOptions (opt )
148
+ func ParseWithOptions (filename string , opts ... OptFunc ) (* ConfigParser , error ) {
149
+ p := NewWithOptions (opts ... )
150
150
data , err := os .ReadFile (filename )
151
151
if err != nil {
152
152
return nil , err
@@ -237,7 +237,7 @@ func (p *ConfigParser) ParseReader(in io.Reader) error {
237
237
if err := curSect .Add (key , value ); err != nil {
238
238
return fmt .Errorf ("failed to add %q = %q: %w" , key , value , err )
239
239
}
240
- } else if match = keyWNoValue .FindStringSubmatch (line ); len (match ) > 0 && p .opt .AllowNoValue && curSect != nil {
240
+ } else if match = keyWNoValue .FindStringSubmatch (line ); len (match ) > 0 && p .opt .allowNoValue && curSect != nil {
241
241
key := strings .TrimSpace (match [1 ])
242
242
value := match [4 ]
243
243
if err := curSect .Add (key , value ); err != nil {
0 commit comments