Check for group presence after full initialization (#1839)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Fixes #1831 By moving the check for help group existence to "ExecuteC()" we no longer need groups to be added before AddCommand() is called. This provides more flexibility to developers and works better with the use of "init()" for command creation.
authored by Marc Khouzam · committed by GitHub · parent 4b9d00d · Browse files · 3 files·+100 added−8 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Signed-off-by: Marc Khouzam <marc.khouzam@gmail.com>
Diff stats
3 files+100 added−8 removed
- other:+100 added−8 removed
Provenance
- Commit
10cf7be- Tree
cf0a25d- Author
Marc Khouzam <marc.khouzam@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
4b9d00d
3 files changed+100 added−8 removed
modified:command_test.go+78 added
1860 unmodified lines1861 1862 checkStringContains(t, output, "\nTest group\n cmd")1863 }1864 1865+func TestWrongGroupFirstLevel(t *testing.T) {1866+ var rootCmd = &Command{Use: "root", Short: "test", Run: emptyRun}1867+1868+ rootCmd.AddGroup(&Group{ID: "group", Title: "Test group"})1869+ // Use the wrong group ID1870+ rootCmd.AddCommand(&Command{Use: "cmd", GroupID: "wrong", Run: emptyRun})1871+1872+ defer func() {1873+ if recover() == nil {1874+ t.Errorf("The code should have panicked due to a missing group")1875+ }1876+ }()1877+ _, err := executeCommand(rootCmd, "--help")1878+ if err != nil {1879+ t.Errorf("Unexpected error: %v", err)1880+ }1881+}1882+1883+func TestWrongGroupNestedLevel(t *testing.T) {1884+ var rootCmd = &Command{Use: "root", Short: "test", Run: emptyRun}1885+ var childCmd = &Command{Use: "child", Run: emptyRun}1886+ rootCmd.AddCommand(childCmd)1887+1888+ childCmd.AddGroup(&Group{ID: "group", Title: "Test group"})1889+ // Use the wrong group ID1890+ childCmd.AddCommand(&Command{Use: "cmd", GroupID: "wrong", Run: emptyRun})1891+1892+ defer func() {1893+ if recover() == nil {1894+ t.Errorf("The code should have panicked due to a missing group")1895+ }1896+ }()1897+ _, err := executeCommand(rootCmd, "child", "--help")1898+ if err != nil {1899+ t.Errorf("Unexpected error: %v", err)1900+ }1901+}1902+1903+func TestWrongGroupForHelp(t *testing.T) {1904+ var rootCmd = &Command{Use: "root", Short: "test", Run: emptyRun}1905+ var childCmd = &Command{Use: "child", Run: emptyRun}1906+ rootCmd.AddCommand(childCmd)1907+1908+ rootCmd.AddGroup(&Group{ID: "group", Title: "Test group"})1909+ // Use the wrong group ID1910+ rootCmd.SetHelpCommandGroupID("wrong")1911+1912+ defer func() {1913+ if recover() == nil {1914+ t.Errorf("The code should have panicked due to a missing group")1915+ }1916+ }()1917+ _, err := executeCommand(rootCmd, "--help")1918+ if err != nil {1919+ t.Errorf("Unexpected error: %v", err)1920+ }1921+}1922+1923+func TestWrongGroupForCompletion(t *testing.T) {1924+ var rootCmd = &Command{Use: "root", Short: "test", Run: emptyRun}1925+ var childCmd = &Command{Use: "child", Run: emptyRun}1926+ rootCmd.AddCommand(childCmd)1927+1928+ rootCmd.AddGroup(&Group{ID: "group", Title: "Test group"})1929+ // Use the wrong group ID1930+ rootCmd.SetCompletionCommandGroupID("wrong")1931+1932+ defer func() {1933+ if recover() == nil {1934+ t.Errorf("The code should have panicked due to a missing group")1935+ }1936+ }()1937+ _, err := executeCommand(rootCmd, "--help")1938+ if err != nil {1939+ t.Errorf("Unexpected error: %v", err)1940+ }1941+}1942+1943 func TestSetOutput(t *testing.T) {1944 c := &Command{}1945 c.SetOutput(nil)1946 if out := c.OutOrStdout(); out != os.Stdout {659 unmodified linesmodified:command.go+17 added-4 removed
996 unmodified lines997 c.InitDefaultHelpCmd()998 // initialize completion at the last point to allow for user overriding999 c.InitDefaultCompletionCmd()1000 1001+ // Now that all commands have been created, let's make sure all groups1002+ // are properly created also1003+ c.checkCommandGroups()1004+1005 args := c.args1006 1007 // Workaround FAIL with "go test -v" or "cobra.test -test.v", see #1551008 if c.args == nil && filepath.Base(os.Args[0]) != "cobra.test" {86 unmodified lines1095 }1096 return nil1097 }1098 1099+// checkCommandGroups checks if a command has been added to a group that does not exists.1100+// If so, we panic because it indicates a coding error that should be corrected.1101+func (c *Command) checkCommandGroups() {1102+ for _, sub := range c.commands {1103+ // if Group is not defined let the developer know right away1104+ if sub.GroupID != "" && !c.ContainsGroup(sub.GroupID) {1105+ panic(fmt.Sprintf("group id '%s' is not defined for subcommand '%s'", sub.GroupID, sub.CommandPath()))1106+ }1107+1108+ sub.checkCommandGroups()1109+ }1110+}1111+1112 // InitDefaultHelpFlag adds default help flag to c.1113 // It is called automatically by executing the c or by calling help and usage.1114 // If c already has help flag, it will do nothing.1115 func (c *Command) InitDefaultHelpFlag() {118 unmodified lines1234 if cmds[i] == c {1235 panic("Command can't be a child of itself")1236 }1237 cmds[i].parent = c1221- // if Group is not defined let the developer know right away1222- if x.GroupID != "" && !c.ContainsGroup(x.GroupID) {1223- panic(fmt.Sprintf("Group id '%s' is not defined for subcommand '%s'", x.GroupID, cmds[i].CommandPath()))1224- }1238 // update max lengths1239 usageLen := len(x.Use)1240 if usageLen > c.commandsMaxUseLen {1241 c.commandsMaxUseLen = usageLen569 unmodified linesmodified:user_guide.md+5 added-4 removed
490 unmodified lines491 around it. In fact, you can provide your own if you want.492 493 ### Grouping commands in help494 495-Cobra supports grouping of available commands. Groups must be explicitly defined by `AddGroup` and set by496-the `GroupId` element of a subcommand. The groups will appear in the same order as they are defined.497-If you use the generated `help` or `completion` commands, you can set the group ids by `SetHelpCommandGroupId`498-and `SetCompletionCommandGroupId`, respectively.495+Cobra supports grouping of available commands in the help output. To group commands, each group must be explicitly496+defined using `AddGroup()` on the parent command. Then a subcommand can be added to a group using the `GroupID` element497+of that subcommand. The groups will appear in the help output in the same order as they are defined using different498+calls to `AddGroup()`. If you use the generated `help` or `completion` commands, you can set their group ids using499+`SetHelpCommandGroupId()` and `SetCompletionCommandGroupId()` on the root command, respectively.500 501 ### Defining your own help502 503 You can provide your own Help command or your own template for the default command to use192 unmodified lines