Skip to content
Myr

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

3 files+100 added−8 removed

modified:command_test.go+78 added
1860 unmodified lines
18611862 checkStringContains(t, output, "\nTest group\n cmd")1863}18641865func TestWrongGroupFirstLevel(t *testing.T) {1866 var rootCmd = &Command{Use: "root", Short: "test", Run: emptyRun}18671868 rootCmd.AddGroup(&Group{ID: "group", Title: "Test group"})1869 // Use the wrong group ID1870 rootCmd.AddCommand(&Command{Use: "cmd", GroupID: "wrong", Run: emptyRun})18711872 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}18821883func 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)18871888 childCmd.AddGroup(&Group{ID: "group", Title: "Test group"})1889 // Use the wrong group ID1890 childCmd.AddCommand(&Command{Use: "cmd", GroupID: "wrong", Run: emptyRun})18911892 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}19021903func 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)19071908 rootCmd.AddGroup(&Group{ID: "group", Title: "Test group"})1909 // Use the wrong group ID1910 rootCmd.SetHelpCommandGroupID("wrong")19111912 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}19221923func 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)19271928 rootCmd.AddGroup(&Group{ID: "group", Title: "Test group"})1929 // Use the wrong group ID1930 rootCmd.SetCompletionCommandGroupID("wrong")19311932 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}19421943func TestSetOutput(t *testing.T) {1944 c := &Command{}1945 c.SetOutput(nil)1946 if out := c.OutOrStdout(); out != os.Stdout {
659 unmodified lines
modified:command.go+17 added-4 removed
996 unmodified lines
997 c.InitDefaultHelpCmd()998 // initialize completion at the last point to allow for user overriding999 c.InitDefaultCompletionCmd()10001001 // Now that all commands have been created, let's make sure all groups1002 // are properly created also1003 c.checkCommandGroups()10041005 args := c.args10061007 // 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 lines
1095 }1096 return nil1097}10981099// 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.1101func (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 }11071108 sub.checkCommandGroups()1109 }1110}11111112// 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.1115func (c *Command) InitDefaultHelpFlag() {
118 unmodified lines
1234 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 = usageLen
569 unmodified lines
modified:user_guide.md+5 added-4 removed
490 unmodified lines
491around it. In fact, you can provide your own if you want.492493### Grouping commands in help494495Cobra supports grouping of available commands. Groups must be explicitly defined by `AddGroup` and set by496the `GroupId` element of a subcommand. The groups will appear in the same order as they are defined.497If you use the generated `help` or `completion` commands, you can set the group ids by `SetHelpCommandGroupId`498and `SetCompletionCommandGroupId`, respectively.495Cobra supports grouping of available commands in the help output. To group commands, each group must be explicitly496defined using `AddGroup()` on the parent command. Then a subcommand can be added to a group using the `GroupID` element497of that subcommand. The groups will appear in the help output in the same order as they are defined using different498calls 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.500501### Defining your own help502503You can provide your own Help command or your own template for the default command to use
192 unmodified lines