diff --git a/parse/args/args.go b/parse/args/args.go index 8235487..e6a0c4c 100644 --- a/parse/args/args.go +++ b/parse/args/args.go @@ -80,7 +80,7 @@ func (p *Parser) next() bool { name, value, hasValue := split(name, '=') if !hasValue && p.pos < len(p.Args) { value = p.Args[p.pos] - if len(value) > 0 && value[0] != '-' { + 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`. diff --git a/parse/args/args_test.go b/parse/args/args_test.go index b58dc2c..fb81580 100644 --- a/parse/args/args_test.go +++ b/parse/args/args_test.go @@ -67,6 +67,30 @@ func TestFlagsParseArgs(t *testing.T) { "arg1", "arg2", "arg3", }, }, + { + name: "empty arg", + flags: map[string]bool{ + "param": false, + }, + args: []string{ + "--param", + "", + }, + expPairs: [][2]string{ + {"param", ""}, + }, + }, + { + name: "empty bool arg", + flags: map[string]bool{ + "param": true, + }, + args: []string{ + "--param", + "", + }, + err: true, + }, { name: "basic error", flags: map[string]bool{ diff --git a/parse/pargs/posix.go b/parse/pargs/posix.go index 28d1fb0..6ab87b0 100644 --- a/parse/pargs/posix.go +++ b/parse/pargs/posix.go @@ -172,7 +172,7 @@ func (p *Parser) next() bool { name, value, hasValue := split(s, '=') if !hasValue && p.pos < len(p.Args) { value = p.Args[p.pos] - if len(value) > 0 && value[0] != '-' { + if len(value) == 0 || value[0] != '-' { if p.isBoolFlag(name) { dash := "--" if short { diff --git a/parse/pargs/posix_test.go b/parse/pargs/posix_test.go index 1685409..0ba9548 100644 --- a/parse/pargs/posix_test.go +++ b/parse/pargs/posix_test.go @@ -115,6 +115,28 @@ func TestPosixParse(t *testing.T) { {"bar", "false"}, }, }, + { + name: "empty parameter", + args: []string{ + "--foo", "", + }, + flags: map[string]bool{ + "foo": false, + }, + expPairs: [][2]string{ + {"foo", ""}, + }, + }, + { + name: "empty boolean", + args: []string{ + "--foo", "", + }, + flags: map[string]bool{ + "foo": true, + }, + err: true, + }, { name: "long ambiguous booleans", args: []string{