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
Provenance
- Commit
b625a4d- Tree
6452698- Author
Eric Paris <eparis@redhat.com>- Committer
Eric Paris <eparis@redhat.com>- Parent 1
68f5a81- Parent 2
6d2f1d2
3 files changed+78 added−19 removed
modified:cobra_test.go+15 added-5 removed
17 unmodified lines18 19 var tp, te, tt, t1, tr []string20 var rootPersPre, echoPre, echoPersPre, timesPersPre []string21 var flagb1, flagb2, flagb3, flagbr, flagbp bool22-var flags1, flags2a, flags2b, flags3 string22+var flags1, flags2a, flags2b, flags3, outs string23 var flagi1, flagi2, flagi3, flagir int24 var globalFlag1 bool25 var flagEcho, rootcalled bool26 var versionUsed int27 28 const strtwoParentHelp = "help message for parent flag strtwo"29 const strtwoChildHelp = "help message for child flag strtwo"30 31+var 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+}40+41 var 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 lines985 986 func 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." }})992 993 const usage = "Hello, world."984- 994+995 c := &Command{}996 c.SetUsageTemplate(`{{if t}}{{h}}{{end}}{{if f}}{{h}}{{end}} {{w}}`)987- 997+998 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 lines4 "reflect"5 "testing"6 )7 8+// test to ensure hidden commands run as intended9+func TestHiddenCommandExecutes(t *testing.T) {10+11+ // 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+ }16+17+ cmdHidden.Execute()18+19+ // 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+}24+25+// test to ensure hidden commands do not show up in usage/help text26+func TestHiddenCommandIsHidden(t *testing.T) {27+ if cmdHidden.IsAvailableCommand() {28+ t.Errorf("Hidden command found!")29+ }30+}31+32 func TestStripFlags(t *testing.T) {33 tests := []struct {34 input []string35 output []string79 unmodified linesmodified:command.go+39 added-14 removed
49 unmodified lines50 // 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.FlagSet197 unmodified lines257 {{.NameAndAliases}}258 {{end}}{{if .HasExample}}259 260 Examples:259-{{ .Example }}{{end}}{{ if .HasNonHelpSubCommands}}261+{{ .Example }}{{end}}{{ if .HasAvailableSubCommands}}262 261-Available Commands: {{range .Commands}}{{if (not .IsHelpCommand)}}263+Available Commands: {{range .Commands}}{{if .IsAvailableCommand}}264 {{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{ if .HasLocalFlags}}265 266 Flags:267 {{.LocalFlags.FlagUsages}}{{end}}{{ if .HasInheritedFlags}}583 unmodified lines851 func (c *Command) HasSubCommands() bool {852 return len(c.commands) > 0853 }854 855+// IsAvailableCommand determines if a command is available as a non-help command856+// (this includes all non deprecated/hidden commands)857+func (c *Command) IsAvailableCommand() bool {858+859+ // a command is 'available' if it is runnable and is not deprecated/hidden860+ return c.Runnable() && len(c.Deprecated) == 0 && !c.Hidden861+}862+863+// 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/deprecated866 func (c *Command) IsHelpCommand() bool {854- if c.Runnable() {867+868+ // 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 }872+873+ // 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 }879+880+ // the command either has no sub commands, or no non-help sub commands881 return true882 }883 884+// 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'887 func (c *Command) HasHelpSubCommands() bool {888+889+ // 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 }895+896+ // the command either has no sub commands, or no available 'help' sub commands897 return false898 }899 900+// HasAvailableSubCommands determines if a command has available sub commands that901+// need to be shown in the usage/help default template under 'available commands'880-func (c *Command) HasNonHelpSubCommands() bool {902+func (c *Command) HasAvailableSubCommands() bool {903+904+ // 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 }911+912+ // the command either has no sub comamnds, or no available (non deprecated/help/hidden)913+ // sub commands914 return false915 }916 917 // Determine if the command is a child command186 unmodified lines