Skip to content

Commit

Permalink
Create new buffer if not present yet (#549)
Browse files Browse the repository at this point in the history
Fixes a nil dereference when TraverseChildren is used
with multiple subcommands.
  • Loading branch information
thcyron authored and n10v committed Oct 12, 2017
1 parent 7cd9cc6 commit 7b2c5ac
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
3 changes: 3 additions & 0 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -1360,6 +1360,9 @@ func (c *Command) ParseFlags(args []string) error {
return nil
}

if c.flagErrorBuf == nil {
c.flagErrorBuf = new(bytes.Buffer)
}
beforeErrorBufLen := c.flagErrorBuf.Len()
c.mergePersistentFlags()
err := c.Flags().Parse(args)
Expand Down
26 changes: 26 additions & 0 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,32 @@ func TestTraverseWithBadChildFlag(t *testing.T) {
}
}

func TestTraverseWithTwoSubcommands(t *testing.T) {
cmd := &Command{
Use: "do",
TraverseChildren: true,
}

sub := &Command{
Use: "sub",
TraverseChildren: true,
}
cmd.AddCommand(sub)

subsub := &Command{
Use: "subsub",
}
sub.AddCommand(subsub)

c, _, err := cmd.Traverse([]string{"sub", "subsub"})
if err != nil {
t.Fatalf("Expected no error: %s", err)
}
if c.Name() != subsub.Name() {
t.Fatalf("wrong command %q expected %q", c.Name(), subsub.Name())
}
}

func TestRequiredFlags(t *testing.T) {
c := &Command{Use: "c", Run: func(*Command, []string) {}}
output := new(bytes.Buffer)
Expand Down

0 comments on commit 7b2c5ac

Please sign in to comment.