Commit 639b9d1

Karn Wong <[email protected]>
2024-11-02 13:14:37
feat: first-level subcommand display help on default
1 parent 149b8d9
Changed files (3)
cmd/generate/generate.go
@@ -1,7 +1,7 @@
 package generate
 
 import (
-	"fmt"
+	"os"
 
 	"github.com/spf13/cobra"
 )
@@ -11,6 +11,9 @@ var Cmd = &cobra.Command{
 	Short: "Generate stuff",
 	Long:  `Generate stuff`,
 	Run: func(cmd *cobra.Command, args []string) {
-		fmt.Println("Please specify subcommand")
+		if len(args) == 0 {
+			cmd.Help()
+			os.Exit(0)
+		}
 	},
 }
cmd/get/get.go
@@ -1,7 +1,7 @@
 package get
 
 import (
-	"fmt"
+	"os"
 
 	"github.com/spf13/cobra"
 )
@@ -11,6 +11,9 @@ var Cmd = &cobra.Command{
 	Short: "Obtain information",
 	Long:  `Obtain information`,
 	Run: func(cmd *cobra.Command, args []string) {
-		fmt.Println("Please specify subcommand")
+		if len(args) == 0 {
+			cmd.Help()
+			os.Exit(0)
+		}
 	},
 }
cmd/root.go
@@ -27,7 +27,6 @@ func Execute() {
 }
 
 func init() {
-	rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
 	rootCmd.AddCommand(get.Cmd)
 	rootCmd.AddCommand(generate.Cmd)
 }