diff --git a/docs/cmd/kn_revision.md b/docs/cmd/kn_revision.md index 01f393e23d..452fdf3125 100644 --- a/docs/cmd/kn_revision.md +++ b/docs/cmd/kn_revision.md @@ -6,6 +6,10 @@ Revision command group Revision command group +``` +kn revision [flags] +``` + ### Options ``` diff --git a/docs/cmd/kn_service.md b/docs/cmd/kn_service.md index 85cd324ac9..5b4a36f95c 100644 --- a/docs/cmd/kn_service.md +++ b/docs/cmd/kn_service.md @@ -6,6 +6,10 @@ Service command group Service command group +``` +kn service [flags] +``` + ### Options ``` diff --git a/pkg/kn/core/root.go b/pkg/kn/core/root.go index 7522af0255..cf5b812743 100644 --- a/pkg/kn/core/root.go +++ b/pkg/kn/core/root.go @@ -15,6 +15,7 @@ package core import ( + "errors" "flag" "fmt" "os" @@ -71,11 +72,36 @@ Eventing: Manage event subscriptions and channels. Connect up event sources.`, rootCmd.AddCommand(commands.NewCompletionCommand(p)) rootCmd.AddCommand(commands.NewVersionCommand(p)) + // Deal with empty and unknown sub command groups + emptyAndUnknownSubcommands(rootCmd) + // For glog parse error. flag.CommandLine.Parse([]string{}) return rootCmd } +// emptyAndUnknownSubcommands adds a RunE to all commands that are groups to +// deal with errors when called with empty or unknown sub command +func emptyAndUnknownSubcommands(cmd *cobra.Command) { + for _, childCmd := range cmd.Commands() { + if childCmd.HasSubCommands() && childCmd.RunE == nil { + childCmd.RunE = func(aCmd *cobra.Command, args []string) error { + aCmd.Help() + fmt.Println() + + if len(args) == 0 { + return errors.New(fmt.Sprintf("please provide a valid sub-command for \"kn %s\"", aCmd.Name())) + } else { + return errors.New(fmt.Sprintf("unknown sub-command \"%s\" for \"kn %s\"", args[0], aCmd.Name())) + } + } + + // recurse to deal with child commands that are themselves command groups + emptyAndUnknownSubcommands(childCmd) + } + } +} + // initConfig reads in config file and ENV variables if set. func initConfig() { if commands.CfgFile != "" {