2017-08-31 15:20:55 +00:00
|
|
|
package cobra
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
|
|
|
type PositionalArgs func(cmd *Command, args []string) error
|
|
|
|
|
|
|
|
// Legacy arg validation has the following behaviour:
|
|
|
|
// - root commands with no subcommands can take arbitrary arguments
|
|
|
|
// - root commands with subcommands will do subcommand validity checking
|
|
|
|
// - subcommands will always accept arbitrary arguments
|
|
|
|
func legacyArgs(cmd *Command, args []string) error {
|
|
|
|
// no subcommand, always take args
|
|
|
|
if !cmd.HasSubCommands() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-01-16 13:20:59 +00:00
|
|
|
// root command with subcommands, do subcommand checking.
|
2017-08-31 15:20:55 +00:00
|
|
|
if !cmd.HasParent() && len(args) > 0 {
|
|
|
|
return fmt.Errorf("unknown command %q for %q%s", args[0], cmd.CommandPath(), cmd.findSuggestions(args[0]))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-01-16 13:20:59 +00:00
|
|
|
// NoArgs returns an error if any args are included.
|
2017-08-31 15:20:55 +00:00
|
|
|
func NoArgs(cmd *Command, args []string) error {
|
|
|
|
if len(args) > 0 {
|
|
|
|
return fmt.Errorf("unknown command %q for %q", args[0], cmd.CommandPath())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-01-16 13:20:59 +00:00
|
|
|
// OnlyValidArgs returns an error if any args are not in the list of ValidArgs.
|
2017-08-31 15:20:55 +00:00
|
|
|
func OnlyValidArgs(cmd *Command, args []string) error {
|
|
|
|
if len(cmd.ValidArgs) > 0 {
|
|
|
|
for _, v := range args {
|
|
|
|
if !stringInSlice(v, cmd.ValidArgs) {
|
|
|
|
return fmt.Errorf("invalid argument %q for %q%s", v, cmd.CommandPath(), cmd.findSuggestions(args[0]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-01-16 13:20:59 +00:00
|
|
|
// ArbitraryArgs never returns an error.
|
2017-08-31 15:20:55 +00:00
|
|
|
func ArbitraryArgs(cmd *Command, args []string) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-01-16 13:20:59 +00:00
|
|
|
// MinimumNArgs returns an error if there is not at least N args.
|
2017-08-31 15:20:55 +00:00
|
|
|
func MinimumNArgs(n int) PositionalArgs {
|
|
|
|
return func(cmd *Command, args []string) error {
|
|
|
|
if len(args) < n {
|
|
|
|
return fmt.Errorf("requires at least %d arg(s), only received %d", n, len(args))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-16 13:20:59 +00:00
|
|
|
// MaximumNArgs returns an error if there are more than N args.
|
2017-08-31 15:20:55 +00:00
|
|
|
func MaximumNArgs(n int) PositionalArgs {
|
|
|
|
return func(cmd *Command, args []string) error {
|
|
|
|
if len(args) > n {
|
|
|
|
return fmt.Errorf("accepts at most %d arg(s), received %d", n, len(args))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-16 13:20:59 +00:00
|
|
|
// ExactArgs returns an error if there are not exactly n args.
|
2017-08-31 15:20:55 +00:00
|
|
|
func ExactArgs(n int) PositionalArgs {
|
|
|
|
return func(cmd *Command, args []string) error {
|
|
|
|
if len(args) != n {
|
|
|
|
return fmt.Errorf("accepts %d arg(s), received %d", n, len(args))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-16 13:20:59 +00:00
|
|
|
// RangeArgs returns an error if the number of args is not within the expected range.
|
2017-08-31 15:20:55 +00:00
|
|
|
func RangeArgs(min int, max int) PositionalArgs {
|
|
|
|
return func(cmd *Command, args []string) error {
|
|
|
|
if len(args) < min || len(args) > max {
|
|
|
|
return fmt.Errorf("accepts between %d and %d arg(s), received %d", min, max, len(args))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|