update default run function for sub-commands

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.
This commit is contained in:
juanvallejo
2016-10-20 10:41:46 -04:00
parent f67ecd73f7
commit 988e747649
11 changed files with 40 additions and 40 deletions

View File

@@ -752,3 +752,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, " "))))
}
}