Skip to content

Commit

Permalink
Override default options from ini
Browse files Browse the repository at this point in the history
  • Loading branch information
pierrec authored and Pierre CURTO committed Jun 27, 2016
1 parent 106a254 commit ca8cdbe
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
6 changes: 4 additions & 2 deletions ini.go
Expand Up @@ -58,6 +58,8 @@ const (
// IniParser is a utility to read and write flags options from and to ini
// formatted strings.
type IniParser struct {
ParseAsDefaults bool // override default flags

parser *Parser
}

Expand Down Expand Up @@ -539,9 +541,9 @@ func (i *IniParser) parse(ini *ini) error {
continue
}

// ini value is ignored if override is not set or
// ini value is ignored if override is set and
// value was previously set from non default
if len(opt.tag.Get("ini-override")) > 0 && !opt.isSetDefault {
if i.ParseAsDefaults && !opt.isSetDefault {
continue
}

Expand Down
9 changes: 6 additions & 3 deletions ini_test.go
Expand Up @@ -988,8 +988,8 @@ func TestIniOverwriteOptions(t *testing.T) {
for _, test := range tests {
var opts struct {
Config string `long:"config" no-ini:"true"`
Value string `long:"value" default:"from default" ini-override:"true"`
Toggle bool `long:"toggle" ini-override:"true"`
Value string `long:"value" default:"from default"`
Toggle bool `long:"toggle"`
}

p := NewParser(&opts, Default)
Expand All @@ -1000,7 +1000,10 @@ func TestIniOverwriteOptions(t *testing.T) {
}

if opts.Config != "" {
err = NewIniParser(p).Parse(bytes.NewBufferString("value = from INI\ntoggle = true"))
inip := NewIniParser(p)
inip.ParseAsDefaults = true

err = inip.Parse(bytes.NewBufferString("value = from INI\ntoggle = true"))
if err != nil {
t.Fatalf("Unexpected error %s with args %+v", err, test.args)
}
Expand Down

0 comments on commit ca8cdbe

Please sign in to comment.