Merge pull request #35206 from juanvallejo/jvallejo/exit-w-errorcode-on-non-existent-cmds

Automatic merge from submit-queue

update default run function for sub-commands

**Release note**:

``` release-note
release-note-none
```

This patch updates parent commands of sub-commands to exit with a usage
error and exit code 1 on an invalid (non-sub-command) argument.

cc @kargakis
This commit is contained in:
Kubernetes Submit Queue
2016-11-05 07:33:46 -07:00
committed by GitHub
11 changed files with 40 additions and 40 deletions

View File

@@ -708,3 +708,20 @@ func IsSiblingCommandExists(cmd *cobra.Command, targetCmdName string) bool {
return false
}
// DefaultSubCommandRun prints a command's help string to the specified output if no
// arguments (sub-commands) are provided, or a usage error otherwise.
func DefaultSubCommandRun(out io.Writer) func(c *cobra.Command, args []string) {
return func(c *cobra.Command, args []string) {
c.SetOutput(out)
RequireNoArguments(c, args)
c.Help()
}
}
// RequireNoArguments exits with a usage error if extra arguments are provided.
func RequireNoArguments(c *cobra.Command, args []string) {
if len(args) > 0 {
CheckErr(UsageError(c, fmt.Sprintf(`unknown command %q`, strings.Join(args, " "))))
}
}