Add subcommands to a Cobra CLI by defining a new Command struct and appending it to the parent command's Commands slice.
Add subcommands to a Cobra CLI by creating a new command struct, defining its usage and run function, and appending it to the parent command's Commands slice.
var cmdRun = &cobra.Command{
Use: "run",
Short: "Run the application",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Running...")
},
}
func init() {
rootCmd.AddCommand(cmdRun)
}
Subcommands are like specific actions you can tell your program to do, such as 'start' or 'stop'. You create a new block of code for each action and tell the main program to recognize it. It is like adding new buttons to a remote control, where each button triggers a different function.