From aa98218fcfbc603f20d28b6b8e14bea24bd72deb Mon Sep 17 00:00:00 2001
From: Jessica Yuen <im.jessicayuen@gmail.com>
Date: Mon, 27 Nov 2017 14:31:17 -0800
Subject: [PATCH] Clarify errors for commands requiring subcommands

Currently, commands such as `ks env non-existent-subcommand` will output
an error `Command 'env' requires a subcommand`.

This is not helpful since the user may assume there is the subcommand
'non-existent-subcommand'. This commit will clarify the error messages
in this scenario.

Signed-off-by: Jessica Yuen <im.jessicayuen@gmail.com>
---
 cmd/env.go       | 4 ++++
 cmd/param.go     | 4 ++++
 cmd/pkg.go       | 3 +++
 cmd/prototype.go | 3 +++
 cmd/registry.go  | 3 +++
 5 files changed, 17 insertions(+)

diff --git a/cmd/env.go b/cmd/env.go
index 7e881818..72fb9940 100644
--- a/cmd/env.go
+++ b/cmd/env.go
@@ -18,6 +18,7 @@ package cmd
 import (
 	"fmt"
 	"os"
+	"strings"
 
 	"github.com/spf13/cobra"
 	"github.com/spf13/pflag"
@@ -90,6 +91,9 @@ uniquely identifies the cluster.
 		  staging.jsonnet
           params.libsonnet`,
 	RunE: func(cmd *cobra.Command, args []string) error {
+		if len(args) != 0 {
+			return fmt.Errorf("%s is not a valid subcommand\n\n%s", strings.Join(args, " "), cmd.UsageString())
+		}
 		return fmt.Errorf("Command 'env' requires a subcommand\n\n%s", cmd.UsageString())
 	},
 }
diff --git a/cmd/param.go b/cmd/param.go
index e5a8c34a..7b9b9120 100644
--- a/cmd/param.go
+++ b/cmd/param.go
@@ -17,6 +17,7 @@ package cmd
 
 import (
 	"fmt"
+	"strings"
 
 	"github.com/spf13/cobra"
 
@@ -58,6 +59,9 @@ run:
     ks env --help
 `,
 	RunE: func(cmd *cobra.Command, args []string) error {
+		if len(args) != 0 {
+			return fmt.Errorf("%s is not a valid subcommand\n\n%s", strings.Join(args, " "), cmd.UsageString())
+		}
 		return fmt.Errorf("Command 'param' requires a subcommand\n\n%s", cmd.UsageString())
 	},
 }
diff --git a/cmd/pkg.go b/cmd/pkg.go
index cbaa40b0..4e1c65e0 100644
--- a/cmd/pkg.go
+++ b/cmd/pkg.go
@@ -44,6 +44,9 @@ var pkgCmd = &cobra.Command{
 	Use:   "pkg",
 	Short: `Manage packages and dependencies for the current ksonnet project`,
 	RunE: func(cmd *cobra.Command, args []string) error {
+		if len(args) != 0 {
+			return fmt.Errorf("%s is not a valid subcommand\n\n%s", strings.Join(args, " "), cmd.UsageString())
+		}
 		return fmt.Errorf("Command 'pkg' requires a subcommand\n\n%s", cmd.UsageString())
 	},
 }
diff --git a/cmd/prototype.go b/cmd/prototype.go
index d87f137d..11cd1a52 100644
--- a/cmd/prototype.go
+++ b/cmd/prototype.go
@@ -44,6 +44,9 @@ var prototypeCmd = &cobra.Command{
 	Use:   "prototype",
 	Short: `Instantiate, inspect, and get examples for ksonnet prototypes`,
 	RunE: func(cmd *cobra.Command, args []string) error {
+		if len(args) != 0 {
+			return fmt.Errorf("%s is not a valid subcommand\n\n%s", strings.Join(args, " "), cmd.UsageString())
+		}
 		return fmt.Errorf("Command 'prototype' requires a subcommand\n\n%s", cmd.UsageString())
 	},
 	Long: `Manage, inspect, instantiate, and get examples for ksonnet prototypes.
diff --git a/cmd/registry.go b/cmd/registry.go
index bf467396..87339173 100644
--- a/cmd/registry.go
+++ b/cmd/registry.go
@@ -20,6 +20,9 @@ var registryCmd = &cobra.Command{
 	Use:   "registry",
 	Short: `Manage registries for current project`,
 	RunE: func(cmd *cobra.Command, args []string) error {
+		if len(args) != 0 {
+			return fmt.Errorf("%s is not a valid subcommand\n\n%s", strings.Join(args, " "), cmd.UsageString())
+		}
 		return fmt.Errorf("Command 'registry' requires a subcommand\n\n%s", cmd.UsageString())
 	},
 	Long: `Manage and inspect ksonnet registries.
-- 
GitLab