Skip to content

Commit

Permalink
parse/args: do not guess on boolean flags with values
Browse files Browse the repository at this point in the history
  • Loading branch information
gobwas committed Sep 6, 2020
1 parent c8899d4 commit d83b9d5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
12 changes: 12 additions & 0 deletions parse/args/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@ func (p *Parser) next() bool {
if !hasValue && p.pos < len(p.Args) {
value = p.Args[p.pos]
if len(value) > 0 && value[0] != '-' {
// NOTE: this is NOT the same behaviour as for flag.Parse().
// flag.Parse() works well if we pass `-flag=true`, but not
// if we pass `-flag true`.
if p.isBoolFlag(name) {
p.fail(""+
"ambiguous boolean flag -%s value: can't guess whether "+
"the %q is the flag value or the non-flag argument "+
"(consider using `=` or `--`)",
name, value,
)
return false
}
hasValue = true
p.pos++
}
Expand Down
4 changes: 2 additions & 2 deletions parse/args/args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ func TestFlagsParseArgs(t *testing.T) {
},
args: []string{
"-a",
"-b", "true",
"-c", "false",
"-b=true",
"-c=false",
"-foo", "value",
"--bar", "value",
"arg1", "arg2", "arg3",
Expand Down

0 comments on commit d83b9d5

Please sign in to comment.