crowdsec/cmd/crowdsec-cli/args/args.go
mmetc b12ade27f4
cscli: review/update argument number checking (#3490)
* 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
2025-03-04 12:21:27 +01:00

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
}