Skip to content
Myr

Merge pull request #143 from sdomino/feature/hidden-commands

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Feature/hidden commands

authored by Eric Paris · committed by Eric Paris · parents 68f5a81, 6d2f1d2 · Browse files · 3 files+78 added−19 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+78 added−19 removed

  • other:+78 added−19 removed

3 files+78 added−19 removed

modified:cobra_test.go+15 added-5 removed
17 unmodified lines
1819var tp, te, tt, t1, tr []string20var rootPersPre, echoPre, echoPersPre, timesPersPre []string21var flagb1, flagb2, flagb3, flagbr, flagbp bool22var flags1, flags2a, flags2b, flags3 string22var flags1, flags2a, flags2b, flags3, outs string23var flagi1, flagi2, flagi3, flagir int24var globalFlag1 bool25var flagEcho, rootcalled bool26var versionUsed int2728const strtwoParentHelp = "help message for parent flag strtwo"29const strtwoChildHelp = "help message for child flag strtwo"3031var cmdHidden = &Command{32 Use: "hide [secret string to print]",33 Short: "Print anything to screen (if command is known)",34 Long: `an absolutely utterly useless command for testing.`,35 Run: func(cmd *Command, args []string) {36 outs = "hidden"37 },38 Hidden: true,39}4041var cmdPrint = &Command{42 Use: "print [string to print]",43 Short: "Print anything to the screen",44 Long: `an absolutely utterly useless command for testing.`,
940 unmodified lines
985986func TestAddTemplateFunctions(t *testing.T) {987 AddTemplateFunc("t", func() bool { return true })988 AddTemplateFuncs(template.FuncMap{979 "f": func() bool { return false }, 980 "h": func() string { return "Hello," }, 989 "f": func() bool { return false },990 "h": func() string { return "Hello," },991 "w": func() string { return "world." }})992993 const usage = "Hello, world."984 994995 c := &Command{}996 c.SetUsageTemplate(`{{if t}}{{h}}{{end}}{{if f}}{{h}}{{end}} {{w}}`)987 997998 if us := c.UsageString(); us != usage {999 t.Errorf("c.UsageString() != \"%s\", is \"%s\"", usage, us)1000 }1001}
modified:command_test.go+24 added
3 unmodified lines
4 "reflect"5 "testing"6)78// test to ensure hidden commands run as intended9func TestHiddenCommandExecutes(t *testing.T) {1011 // ensure that outs does not already equal what the command will be setting it12 // to, if it did this test would not actually be testing anything...13 if outs == "hidden" {14 t.Errorf("outs should NOT EQUAL hidden")15 }1617 cmdHidden.Execute()1819 // upon running the command, the value of outs should now be 'hidden'20 if outs != "hidden" {21 t.Errorf("Hidden command failed to run!")22 }23}2425// test to ensure hidden commands do not show up in usage/help text26func TestHiddenCommandIsHidden(t *testing.T) {27 if cmdHidden.IsAvailableCommand() {28 t.Errorf("Hidden command found!")29 }30}3132func TestStripFlags(t *testing.T) {33 tests := []struct {34 input []string35 output []string
79 unmodified lines
modified:command.go+39 added-14 removed
49 unmodified lines
50 // Custom functions used by the bash autocompletion generator51 BashCompletionFunction string52 // Is this command deprecated and should print this string when used?53 Deprecated string54 // Is this command hidden and should NOT show up in the list of available commands?55 Hidden bool56 // Full set of flags57 flags *flag.FlagSet58 // Set of flags childrens of this command will inherit59 pflags *flag.FlagSet
197 unmodified lines
257 {{.NameAndAliases}}258{{end}}{{if .HasExample}}259260Examples:259{{ .Example }}{{end}}{{ if .HasNonHelpSubCommands}}261{{ .Example }}{{end}}{{ if .HasAvailableSubCommands}}262261Available Commands: {{range .Commands}}{{if (not .IsHelpCommand)}}263Available Commands: {{range .Commands}}{{if .IsAvailableCommand}}264 {{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{ if .HasLocalFlags}}265266Flags:267{{.LocalFlags.FlagUsages}}{{end}}{{ if .HasInheritedFlags}}
583 unmodified lines
851func (c *Command) HasSubCommands() bool {852 return len(c.commands) > 0853}854855// IsAvailableCommand determines if a command is available as a non-help command856// (this includes all non deprecated/hidden commands)857func (c *Command) IsAvailableCommand() bool {858859 // a command is 'available' if it is runnable and is not deprecated/hidden860 return c.Runnable() && len(c.Deprecated) == 0 && !c.Hidden861}862863// IsHelpCommand determines if a command is a 'help' command; a help command is864// determined by the fact that it is NOT runnable/hidden/deprecated, and has no865// sub commands that are runnable/hidden/deprecated866func (c *Command) IsHelpCommand() bool {854 if c.Runnable() {867868 // if a command is runnable, deprecated, or hidden it is not a 'help' command869 if c.Runnable() || len(c.Deprecated) != 0 || c.Hidden {870 return false871 }872873 // if any non-help sub commands are found, the command is not a 'help' command874 for _, sub := range c.commands {858 if len(sub.Deprecated) != 0 {859 continue860 }875 if !sub.IsHelpCommand() {876 return false877 }878 }879880 // the command either has no sub commands, or no non-help sub commands881 return true882}883884// HasHelpSubCommands determines if a command has any avilable 'help' sub commands885// that need to be shown in the usage/help default template under 'additional help886// topics'887func (c *Command) HasHelpSubCommands() bool {888889 // return true on the first found available 'help' sub command890 for _, sub := range c.commands {870 if len(sub.Deprecated) != 0 {871 continue872 }891 if sub.IsHelpCommand() {892 return true893 }894 }895896 // the command either has no sub commands, or no available 'help' sub commands897 return false898}899900// HasAvailableSubCommands determines if a command has available sub commands that901// need to be shown in the usage/help default template under 'available commands'880func (c *Command) HasNonHelpSubCommands() bool {902func (c *Command) HasAvailableSubCommands() bool {903904 // return true on the first found available (non deprecated/help/hidden)905 // sub command906 for _, sub := range c.commands {882 if len(sub.Deprecated) != 0 {883 continue884 }885 if !sub.IsHelpCommand() {907 if sub.IsAvailableCommand() {908 return true909 }910 }911912 // the command either has no sub comamnds, or no available (non deprecated/help/hidden)913 // sub commands914 return false915}916917// Determine if the command is a child command
186 unmodified lines