From 884e67f2052c53e07ec34ebd473ccb3f93c62177 Mon Sep 17 00:00:00 2001 From: Oleg Kovalov Date: Fri, 31 Dec 2021 11:39:41 -0800 Subject: [PATCH] Add IsHidden flag (#6) --- acmd.go | 6 ++++++ acmd_test.go | 21 +++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/acmd.go b/acmd.go index 349ecfb..a4d091e 100644 --- a/acmd.go +++ b/acmd.go @@ -46,6 +46,9 @@ type Command struct { // subcommands of the command. Subcommands []Command + + // IsHidden reports whether command should not be show in help. Default false. + IsHidden bool } // Config for the runner. @@ -297,6 +300,9 @@ func printCommands(w io.Writer, cmds []Command) { minwidth, tabwidth, padding, padchar, flags := 0, 0, 11, byte(' '), uint(0) tw := tabwriter.NewWriter(w, minwidth, tabwidth, padding, padchar, flags) for _, cmd := range cmds { + if cmd.IsHidden { + continue + } desc := cmd.Description if desc == "" { desc = "" diff --git a/acmd_test.go b/acmd_test.go index daf3783..dfb81d9 100644 --- a/acmd_test.go +++ b/acmd_test.go @@ -264,3 +264,24 @@ func TestRunner_suggestCommand(t *testing.T) { } } } + +func TestCommand_IsHidden(t *testing.T) { + buf := &bytes.Buffer{} + cmds := []Command{ + {Name: "for", Do: nopFunc}, + {Name: "foo", Do: nopFunc, IsHidden: true}, + {Name: "bar", Do: nopFunc}, + } + r := RunnerOf(cmds, Config{ + Args: []string{"help"}, + AppName: "ci", + Output: buf, + }) + if err := r.Run(); err != nil { + t.Fatal(err) + } + + if strings.Contains(buf.String(), "foo") { + t.Fatal("should not show foo") + } +}