Skip to content

Commit

Permalink
fix: show help on failed noargs commands (#1285)
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Dagelic <[email protected]>
  • Loading branch information
idagelic authored Oct 23, 2024
1 parent 8964d52 commit e5b360e
Showing 1 changed file with 6 additions and 12 deletions.
18 changes: 6 additions & 12 deletions pkg/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func validateCommands(rootCmd *cobra.Command, args []string) (cmd *cobra.Command
currentCmd := rootCmd

// Filter flags from args
sanitzedArgs := []string{}
sanitizedArgs := []string{}
for i := 0; i < len(args); i++ {
if strings.HasPrefix(args[i], "-") {
flags = append(flags, args[i])
Expand All @@ -123,11 +123,11 @@ func validateCommands(rootCmd *cobra.Command, args []string) (cmd *cobra.Command
}
continue
}
sanitzedArgs = append(sanitzedArgs, args[i])
sanitizedArgs = append(sanitizedArgs, args[i])
}

for len(sanitzedArgs) > 0 {
subCmd, subArgs, err := currentCmd.Find(sanitzedArgs)
for len(sanitizedArgs) > 0 {
subCmd, subArgs, err := currentCmd.Find(sanitizedArgs)
if err != nil {
return currentCmd, flags, false, err
}
Expand All @@ -137,16 +137,10 @@ func validateCommands(rootCmd *cobra.Command, args []string) (cmd *cobra.Command
}

currentCmd = subCmd
sanitzedArgs = subArgs
sanitizedArgs = subArgs
}

if len(sanitzedArgs) > 0 {
if err := currentCmd.ValidateArgs(sanitzedArgs); err != nil {
return currentCmd, flags, false, err
}
}

return currentCmd, flags, false, nil
return currentCmd, flags, false, currentCmd.ValidateArgs(sanitizedArgs)
}

func SetupRootCommand(cmd *cobra.Command) {
Expand Down

0 comments on commit e5b360e

Please sign in to comment.