Ability to hide commands from usage/help text

Added the ability to have hidden commands that cobra will still run as intended, however they won't show up in any usage/help text

adding internal field to command

private is a better name

hiding private commands in default help/usage

opting for 'hidden' over 'private'

updating all 'help command' checks to exclude hidden commands

updating how commands are displayed in usage/help text by updating/adding some methods. added tests for hidden/deprecated commands

making command hidden when testing hidden command execution

test now leverage the included suite and are much less custom. also removed deprecation tests, once I discovered them in cobra_test.go

updating hidden command test to be more reliable

removing unnecessary () when checking len(c.Deprecated)

updating command comments to be godoc friendly
This commit is contained in:
Steve Domino
2015-09-04 14:34:51 -06:00
parent 68f5a81a72
commit 6d2f1d2fa7
3 changed files with 78 additions and 19 deletions

View File

@ -19,7 +19,7 @@ var _ = os.Stderr
var tp, te, tt, t1, tr []string
var rootPersPre, echoPre, echoPersPre, timesPersPre []string
var flagb1, flagb2, flagb3, flagbr, flagbp bool
var flags1, flags2a, flags2b, flags3 string
var flags1, flags2a, flags2b, flags3, outs string
var flagi1, flagi2, flagi3, flagir int
var globalFlag1 bool
var flagEcho, rootcalled bool
@ -28,6 +28,16 @@ var versionUsed int
const strtwoParentHelp = "help message for parent flag strtwo"
const strtwoChildHelp = "help message for child flag strtwo"
var cmdHidden = &Command{
Use: "hide [secret string to print]",
Short: "Print anything to screen (if command is known)",
Long: `an absolutely utterly useless command for testing.`,
Run: func(cmd *Command, args []string) {
outs = "hidden"
},
Hidden: true,
}
var cmdPrint = &Command{
Use: "print [string to print]",
Short: "Print anything to the screen",
@ -976,15 +986,15 @@ func TestFlagOnPflagCommandLine(t *testing.T) {
func TestAddTemplateFunctions(t *testing.T) {
AddTemplateFunc("t", func() bool { return true })
AddTemplateFuncs(template.FuncMap{
"f": func() bool { return false },
"h": func() string { return "Hello," },
"f": func() bool { return false },
"h": func() string { return "Hello," },
"w": func() string { return "world." }})
const usage = "Hello, world."
c := &Command{}
c.SetUsageTemplate(`{{if t}}{{h}}{{end}}{{if f}}{{h}}{{end}} {{w}}`)
if us := c.UsageString(); us != usage {
t.Errorf("c.UsageString() != \"%s\", is \"%s\"", usage, us)
}