Skip to content

Commit

Permalink
fixes jessevdk#323 - Required slice option
Browse files Browse the repository at this point in the history
  • Loading branch information
raghur committed Dec 6, 2019
1 parent c0795c8 commit de746e6
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
5 changes: 4 additions & 1 deletion ini.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,9 @@ func (i *IniParser) matchingGroups(name string) []*Group {

func (i *IniParser) parse(ini *ini) error {
p := i.parser

p.eachOption(func(c *Command, g *Group, o *Option) {
o.isProcessingIni = true
})
var quotesLookup = make(map[*Option]bool)

for name, section := range ini.Sections {
Expand Down Expand Up @@ -579,6 +581,7 @@ func (i *IniParser) parse(ini *ini) error {
LineNumber: inival.LineNumber,
}
}
opt.setFromIni = true

// either all INI values are quoted or only values who need quoting
if _, ok := quotesLookup[opt]; !inival.Quoted || !ok {
Expand Down
25 changes: 25 additions & 0 deletions ini_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,31 @@ EnvDefault2 = env-def
}
}

func TestIniRequiredSlice_ShouldNotNeedToBeSpecifiedOnCli(t *testing.T) {
type options struct {
Items []string `long:"item" required:"true"`
}
var opts options
ini := `
[Application Options]
item=abc`
args := []string{}

parser := NewParser(&opts, Default)
inip := NewIniParser(parser)

inip.Parse(strings.NewReader(ini))
inip.ParseAsDefaults = false
_, err := parser.ParseArgs(args)
if err != nil {
t.Fatalf("Unexpected failure: %v", err)
}
if opts.Items[0] != "abc" {
t.Fatalf("Expected first option to be abc, but was %v", opts.Items[0])
}

}

func TestReadIni_flagEquivalent(t *testing.T) {
type options struct {
Opt1 bool `long:"opt1"`
Expand Down
6 changes: 5 additions & 1 deletion option.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ type Option struct {
preventDefault bool

defaultLiteral string

isProcessingIni bool
setFromIni bool
}

// LongNameWithNamespace returns the option's long name with the group namespaces
Expand Down Expand Up @@ -241,8 +244,9 @@ func (option *Option) IsSetDefault() bool {
func (option *Option) set(value *string) error {
kind := option.value.Type().Kind()

if (kind == reflect.Map || kind == reflect.Slice) && !option.isSet {
if (kind == reflect.Map || kind == reflect.Slice) && (!option.isSet || (!option.isProcessingIni && option.setFromIni)) {
option.empty()
option.setFromIni = false
}

option.isSet = true
Expand Down
6 changes: 5 additions & 1 deletion parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package flags

import (
"bytes"
"reflect"
"fmt"
"os"
"path"
Expand Down Expand Up @@ -207,7 +208,10 @@ func (p *Parser) ParseArgs(args []string) ([]string, error) {
}

p.eachOption(func(c *Command, g *Group, option *Option) {
option.isSet = false
option.isProcessingIni = false
if !(option.value.Kind() == reflect.Slice || option.value.Kind() == reflect.Map) {
option.isSet = false
}
option.isSetDefault = false
option.updateDefaultLiteral()
})
Expand Down

0 comments on commit de746e6

Please sign in to comment.