mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-01-05 23:47:50 +00:00
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:
@@ -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, " "))))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user