Cobra Subcommand Invoke Help By Default
I think it is better to handle that on PreRunE.
var addonCmd = &cobra.Command{
Use: "addon",
Short: "Install addon packages",
Long: `Install additional packages`,
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
cmd.Help()
os.Exit(0)
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
// do actual work
},
}
It is possible to do with checking of amount of passed arguments to your program. If there is more then 0
args you will do actual work, but if it less then you will just show the "help" for the command.
var addonCmd = &cobra.Command{
Use: "addon",
Short: "Install addon packages",
Long: `Install additional packages`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
cmd.Help()
os.Exit(0)
}
// do actual work
},
}