var rootCmd = &cobra.Command{ Use: "app", Short: "A short description of your application", Long: `A longer description that spans multiple lines and contains more detailed information about your application`, Run: func(cmd *cobra.Command, args []string) { // Do something here }, } var childCmd1 = &cobra.Command{ Use: "foo", Short: "Short description of foo command", Run: func(cmd *cobra.Command, args []string) { // Do something here }, } var childCmd2 = &cobra.Command{ Use: "bar", Short: "Short description of bar command", Run: func(cmd *cobra.Command, args []string) { // Do something here }, } rootCmd.AddCommand(childCmd1) rootCmd.AddCommand(childCmd2)
var rootCmd = &cobra.Command{ Use: "app", Short: "A short description of your application", Long: `A longer description that spans multiple lines and contains more detailed information about your application`, Run: func(cmd *cobra.Command, args []string) { // Do something here }, } var childCmd1 = &cobra.Command{ Use: "foo", Short: "Short description of foo command", Run: func(cmd *cobra.Command, args []string) { if verbose { fmt.Println("Verbose mode enabled") } // Do something here }, } var verbose bool func init() { rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Enable verbose logging") } rootCmd.AddCommand(childCmd1)In this example, the `rootCmd` command sets a boolean flag value for `verbose`, and the child command `childCmd1` can access this value. When the `app foo -v` command is run, the `verbose` flag is set to `true`, and the command prints "Verbose mode enabled" to the console. This reduces the amount of code needed to handle this flag in every child command. Overall, these examples demonstrate how the parent command feature in the github.com.spf13.cobra Go library can make building CLI applications easier and more efficient.