fix opt cleared with defualt

This commit is contained in:
Boshi Lian 2019-01-06 13:59:51 +00:00
parent 650023f363
commit fa85805222
3 changed files with 22 additions and 6 deletions

6
Gopkg.lock generated
View file

@ -14,10 +14,10 @@
revision = "d32a9ef172a1ec91efdf3ce26b240c946285bfe7"
[[projects]]
branch = "ignore_unknown_ini"
branch = "hack_global_ref"
name = "github.com/jessevdk/go-flags"
packages = ["."]
revision = "f0dfbc82957e3db331cb27732d270e275614b005"
revision = "3e2b0d3b82050426db6ebde363a66b2c7711d8fc"
source = "https://github.com/tg123/go-flags"
[[projects]]
@ -66,6 +66,6 @@
[solve-meta]
analyzer-name = "dep"
analyzer-version = 1
inputs-digest = "534da9de5467ab2fd3960701473a3fd7da82684491b7b65f21644b0a6b7147f5"
inputs-digest = "9e42cf42e27d0d44f175e90958f545127d53a6ba490856c10094dffdb54e79f1"
solver-name = "gps-cdcl"
solver-version = 1

View file

@ -30,6 +30,6 @@
name = "github.com/jinzhu/gorm"
[[constraint]]
branch = "ignore_unknown_ini"
branch = "hack_global_ref"
name = "github.com/jessevdk/go-flags"
source = "https://github.com/tg123/go-flags"

View file

@ -9,6 +9,7 @@ import (
"fmt"
"os"
"path"
"reflect"
"sort"
"strings"
"unicode/utf8"
@ -123,6 +124,8 @@ type parseState struct {
command *Command
lookup lookup
setcalled map[reflect.Value]bool
}
// Parse is a convenience function to parse command line options with default
@ -230,8 +233,9 @@ func (p *Parser) ParseArgs(args []string) ([]string, error) {
}
s := &parseState{
args: args,
retargs: make([]string, 0, len(args)),
args: args,
retargs: make([]string, 0, len(args)),
setcalled: make(map[reflect.Value]bool),
}
p.fillParseState(s)
@ -292,11 +296,16 @@ func (p *Parser) ParseArgs(args []string) ([]string, error) {
}
if s.err == nil {
p.eachOption(func(c *Command, g *Group, option *Option) {
if option.preventDefault {
return
}
if _, ok := s.setcalled[option.value]; ok {
return
}
option.clearDefault()
})
@ -501,6 +510,13 @@ func (p *parseState) estimateCommand() error {
}
func (p *Parser) parseOption(s *parseState, name string, option *Option, canarg bool, argument *string) (err error) {
defer func() {
if err == nil {
s.setcalled[option.value] = true
}
}()
if !option.canArgument() {
if argument != nil {
return newErrorf(ErrNoArgumentForBool, "bool flag `%s' cannot have an argument", option)