Skip to content

Commit

Permalink
Hide hidden cmds from completion
Browse files Browse the repository at this point in the history
  • Loading branch information
heewa committed Mar 16, 2016
1 parent 72577f3 commit 445c48d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
4 changes: 3 additions & 1 deletion cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ func (c *cmdMixin) CmdCompletion(context *ParseContext) []string {
} else {
// If all args are satisfied, then go back to completing commands
for _, cmd := range c.cmdGroup.commandOrder {
options = append(options, cmd.name)
if !cmd.hidden {
options = append(options, cmd.name)
}
}
}

Expand Down
27 changes: 27 additions & 0 deletions cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,33 @@ func TestCmdCompletion(t *testing.T) {
assert.Equal(t, []string{"sub1", "sub2"}, complete(t, app, "two"))
}

func TestHiddenCmdCompletion(t *testing.T) {
app := newTestApp()

// top level visible & hidden cmds, with no sub-cmds
app.Command("visible1", "")
app.Command("hidden1", "").Hidden()

// visible cmd with visible & hidden sub-cmds
visible2 := app.Command("visible2", "")
visible2.Command("visible2-visible", "")
visible2.Command("visible2-hidden", "").Hidden()

// hidden cmd with visible & hidden sub-cmds
hidden2 := app.Command("hidden2", "").Hidden()
hidden2.Command("hidden2-visible", "")
hidden2.Command("hidden2-hidden", "").Hidden()

// Only top level visible cmds should show
assert.Equal(t, []string{"help", "visible1", "visible2"}, complete(t, app))

// Only visible sub-cmds should show
assert.Equal(t, []string{"visible2-visible"}, complete(t, app, "visible2"))

// Hidden commands should still complete visible sub-cmds
assert.Equal(t, []string{"hidden2-visible"}, complete(t, app, "hidden2"))
}

func TestDefaultCmdCompletion(t *testing.T) {
app := newTestApp()

Expand Down

0 comments on commit 445c48d

Please sign in to comment.