Skip to content

Commit

Permalink
parse: fix parsing empty argument for both posix and args
Browse files Browse the repository at this point in the history
  • Loading branch information
gobwas committed Sep 25, 2020
1 parent de4ead0 commit 9dbc793
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
2 changes: 1 addition & 1 deletion parse/args/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
24 changes: 24 additions & 0 deletions parse/args/args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion parse/pargs/posix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
22 changes: 22 additions & 0 deletions parse/pargs/posix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit 9dbc793

Please sign in to comment.