Skip to content

Commit

Permalink
Lookup options in all parent commands
Browse files Browse the repository at this point in the history
  • Loading branch information
jessevdk committed Oct 14, 2015
1 parent eff4073 commit 2e3d1bd
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 5 deletions.
13 changes: 8 additions & 5 deletions command_private.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,18 +156,21 @@ func (c *Command) makeLookup() lookup {

parent := c.parent

var parents []*Command

for parent != nil {
if cmd, ok := parent.(*Command); ok {
cmd.fillLookup(&ret, true)
}

if grp, ok := parent.(*Group); ok {
parent = grp
parents = append(parents, cmd)
parent = cmd.parent
} else {
parent = nil
}
}

for i := len(parents) - 1; i >= 0; i-- {
parents[i].fillLookup(&ret, true)
}

c.fillLookup(&ret, false)
return ret
}
Expand Down
80 changes: 80 additions & 0 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,34 @@ func TestCommandFlagOrder2(t *testing.T) {
}
}

func TestCommandFlagOrderSub(t *testing.T) {
var opts = struct {
Value bool `short:"v"`

Command struct {
G bool `short:"g"`

SubCommand struct {
B bool `short:"b"`
} `command:"sub"`
} `command:"cmd"`
}{}

assertParseSuccess(t, &opts, "cmd", "sub", "-v", "-g", "-b")

if !opts.Value {
t.Errorf("Expected Value to be true")
}

if !opts.Command.G {
t.Errorf("Expected Command.G to be true")
}

if !opts.Command.SubCommand.B {
t.Errorf("Expected Command.SubCommand.B to be true")
}
}

func TestCommandFlagOverride1(t *testing.T) {
var opts = struct {
Value bool `short:"v"`
Expand Down Expand Up @@ -146,6 +174,58 @@ func TestCommandFlagOverride2(t *testing.T) {
}
}

func TestCommandFlagOverrideSub(t *testing.T) {
var opts = struct {
Value bool `short:"v"`

Command struct {
Value bool `short:"v"`

SubCommand struct {
Value bool `short:"v"`
} `command:"sub"`
} `command:"cmd"`
}{}

assertParseSuccess(t, &opts, "cmd", "sub", "-v")

if opts.Value {
t.Errorf("Expected Value to be false")
}

if opts.Command.Value {
t.Errorf("Expected Command.Value to be false")
}

if !opts.Command.SubCommand.Value {
t.Errorf("Expected Command.Value to be true")
}
}

func TestCommandFlagOverrideSub2(t *testing.T) {
var opts = struct {
Value bool `short:"v"`

Command struct {
Value bool `short:"v"`

SubCommand struct {
G bool `short:"g"`
} `command:"sub"`
} `command:"cmd"`
}{}

assertParseSuccess(t, &opts, "cmd", "sub", "-v")

if opts.Value {
t.Errorf("Expected Value to be false")
}

if !opts.Command.Value {
t.Errorf("Expected Command.Value to be true")
}
}

func TestCommandEstimate(t *testing.T) {
var opts = struct {
Value bool `short:"v"`
Expand Down

0 comments on commit 2e3d1bd

Please sign in to comment.