Skip to content

Commit

Permalink
Fix panic when parsing unknown flag followed by empty argument (#173)
Browse files Browse the repository at this point in the history
Signed-off-by: Oren Shomron <shomron@gmail.com>
  • Loading branch information
shomron authored and eparis committed Aug 31, 2018
1 parent d929dcb commit 298182f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
7 changes: 5 additions & 2 deletions flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -925,13 +925,16 @@ func stripUnknownFlagValue(args []string) []string {
}

first := args[0]
if first[0] == '-' {
if len(first) > 0 && first[0] == '-' {
//--unknown --next-flag ...
return args
}

//--unknown arg ... (args will be arg ...)
return args[1:]
if len(args) > 1 {
return args[1:]
}
return nil
}

func (f *FlagSet) parseLongArg(s string, args []string, fn parseFunc) (a []string, err error) {
Expand Down
5 changes: 5 additions & 0 deletions flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,11 @@ func testParseWithUnknownFlags(f *FlagSet, t *testing.T) {
"--unknown8=unknown8value",
"--boole",
"--unknown6",
"",
"-uuuuu",
"",
"--unknown10",
"--unknown11",
}
want := []string{
"boola", "true",
Expand Down

0 comments on commit 298182f

Please sign in to comment.