Skip to content

Subcommand Group | panic: Group id 'Group1' is not defined for subcommand 'app1 sub1' #1831

@KINGSABRI

Description

@KINGSABRI

Hi everyone
I'm trying to implement the subcommand group feature introduced in v[v1.6.0]. I followed the test code

/app1/cmd/root.go

func init() {
	cobra.EnableCaseInsensitive = true
	cobra.EnableCommandSorting = false

	rootCmd.CompletionOptions.DisableDefaultCmd = true 
	rootCmd.Flags().SortFlags = false
	rootCmd.PersistentFlags().SortFlags = false

	rootCmd.AddGroup(&cobra.Group{ID: "Group1", Title: "Group1 Subcommands:"})
}

/app1/cmd/sub1.go

var sub1Cmd = &cobra.Command{
	Use:     "sub1",
	GroupID: "Group1",
	Aliases: []string{"1"},
	Run: func(cmd *cobra.Command, args []string) {
		fmt.Println("Hiii")
	},
}

func init() {
	rootCmd.AddCommand(sub1Cmd)
	sub1Cmd.Flags().SortFlags = false
	sub1Cmd.Flags().StringP("ipaddress", "i", "0.0.0.0", "IP address to listen to")
}

When I run the app I get

panic: Group id 'Group1' is not defined for subcommand 'app1 sub1'
goroutine 1 [running]:
github.com/spf13/cobra.(*Command).AddCommand(0xd024e0, {0xc00024fd08, 0x1, 0x1?})
        /home/USER/go/pkg/mod/github.com/spf13/cobra@v1.6.0/command.go:1223 +0x2d4
app1/cmd.init.3()
        /home/USER/Code/app1/cmd/sub1Cmd.go:47 +0x54

Q\ is there any complex example that wrapup all cobra's features?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions