fix no set default value bug
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing

This commit is contained in:
Jay 2020-02-19 09:38:33 +00:00
parent a1eec53484
commit 5bb2198684
2 changed files with 9 additions and 3 deletions

View File

@ -129,8 +129,14 @@ func (p *Parser) parse(args *[]string) error {
func (p *Parser) setDefaultValue() { func (p *Parser) setDefaultValue() {
for _, v := range p.args { for _, v := range p.args {
// fmt.Printf("show %s , parsed: %v, defVal: %v\n", v.name(), v.parsed, v.defaultValue)
if !v.parsed { if !v.parsed {
v.value = v.defaultValue t := reflect.ValueOf(v.value)
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
t.Set(reflect.ValueOf(v.defaultValue))
// *v.value = v.defaultValue
} }
} }
} }

View File

@ -17,7 +17,7 @@ func main() {
// "-n", "name!!", // "-n", "name!!",
// "-i", "item1", // "-i", "item1",
// "-i", "item2", // "-i", "item2",
"-h", // "-h",
} }
p := argparse.New() p := argparse.New()
@ -37,7 +37,7 @@ func main() {
p.FloatVar(&floatVal, 1.2, "ff", "float", "float value", nil) p.FloatVar(&floatVal, 1.2, "ff", "float", "float value", nil)
p.StringSliceVar(&sarr, []string{}, "i", "item", "item list", nil) p.StringSliceVar(&sarr, []string{}, "i", "item", "item list", nil)
// uncomment to test required arg missing // uncomment to test required arg missing
p.IntVar(&missReq, -1, "m", "miss", "require value", &argparse.Option{Require: true}) // p.IntVar(&missReq, -1, "m", "miss", "require value", &argparse.Option{Require: true})
p.Help("h", "help") p.Help("h", "help")
name = p.String("", "n", "name", "show name", nil) name = p.String("", "n", "name", "show name", nil)