From 445c48d66dbd55e88227665d021d9820cb681f95 Mon Sep 17 00:00:00 2001 From: Heewa Barfchin Date: Wed, 16 Mar 2016 13:59:41 -0400 Subject: [PATCH] Hide hidden cmds from completion --- cmd.go | 4 +++- cmd_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/cmd.go b/cmd.go index e30831b..0473b87 100644 --- a/cmd.go +++ b/cmd.go @@ -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) + } } } diff --git a/cmd_test.go b/cmd_test.go index e1b69e3..86877da 100644 --- a/cmd_test.go +++ b/cmd_test.go @@ -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()