mirror of
https://github.com/crowdsecurity/crowdsec.git
synced 2025-05-12 12:55:53 +02:00
* cscsli: remove unused Command.Args setting * cscli: review/update argument number checking cscli will consistently print the help text if the number of arguments is wrong for the command, but not for other types of errors. * fix func tests * lint
54 lines
1.7 KiB
Go
54 lines
1.7 KiB
Go
package args
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
// MinimumNArgs is a drop-in replacement for cobra.MinimumNArgs that prints the usage in case of wrong number of arguments, but not other errors.
|
|
func MinimumNArgs(n int) cobra.PositionalArgs {
|
|
return func(cmd *cobra.Command, args []string) error {
|
|
if len(args) < n {
|
|
_ = cmd.Help()
|
|
fmt.Fprintln(cmd.OutOrStdout(), "")
|
|
return fmt.Errorf("requires at least %d arg(s), only received %d", n, len(args))
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// MaximumNArgs is a drop-in replacement for cobra.MaximumNArgs that prints the usage in case of wrong number of arguments, but not other errors.
|
|
func MaximumNArgs(n int) cobra.PositionalArgs {
|
|
return func(cmd *cobra.Command, args []string) error {
|
|
if len(args) > n {
|
|
_ = cmd.Help()
|
|
fmt.Fprintln(cmd.OutOrStdout(), "")
|
|
return fmt.Errorf("accepts at most %d arg(s), received %d", n, len(args))
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// ExactArgs is a drop-in replacement for cobra.ExactArgs that prints the usage in case of wrong number of arguments, but not other errors.
|
|
func ExactArgs(n int) cobra.PositionalArgs {
|
|
return func(cmd *cobra.Command, args []string) error {
|
|
if len(args) != n {
|
|
_ = cmd.Help()
|
|
fmt.Fprintln(cmd.OutOrStdout(), "")
|
|
return fmt.Errorf("accepts %d arg(s), received %d", n, len(args))
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// NoArgs is a drop-in replacement for cobra.NoArgs that prints the usage in case of wrong number of arguments, but not other errors.
|
|
func NoArgs(cmd *cobra.Command, args []string) error {
|
|
if len(args) > 0 {
|
|
_ = cmd.Help()
|
|
fmt.Fprintln(cmd.OutOrStdout(), "")
|
|
return fmt.Errorf("unknown command %q for %q", args[0], cmd.CommandPath())
|
|
}
|
|
return nil
|
|
}
|
|
|