args.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package cobra
  2. import (
  3. "fmt"
  4. )
  5. type PositionalArgs func(cmd *Command, args []string) error
  6. // Legacy arg validation has the following behaviour:
  7. // - root commands with no subcommands can take arbitrary arguments
  8. // - root commands with subcommands will do subcommand validity checking
  9. // - subcommands will always accept arbitrary arguments
  10. func legacyArgs(cmd *Command, args []string) error {
  11. // no subcommand, always take args
  12. if !cmd.HasSubCommands() {
  13. return nil
  14. }
  15. // root command with subcommands, do subcommand checking.
  16. if !cmd.HasParent() && len(args) > 0 {
  17. return fmt.Errorf("unknown command %q for %q%s", args[0], cmd.CommandPath(), cmd.findSuggestions(args[0]))
  18. }
  19. return nil
  20. }
  21. // NoArgs returns an error if any args are included.
  22. func NoArgs(cmd *Command, args []string) error {
  23. if len(args) > 0 {
  24. return fmt.Errorf("unknown command %q for %q", args[0], cmd.CommandPath())
  25. }
  26. return nil
  27. }
  28. // OnlyValidArgs returns an error if any args are not in the list of ValidArgs.
  29. func OnlyValidArgs(cmd *Command, args []string) error {
  30. if len(cmd.ValidArgs) > 0 {
  31. for _, v := range args {
  32. if !stringInSlice(v, cmd.ValidArgs) {
  33. return fmt.Errorf("invalid argument %q for %q%s", v, cmd.CommandPath(), cmd.findSuggestions(args[0]))
  34. }
  35. }
  36. }
  37. return nil
  38. }
  39. // ArbitraryArgs never returns an error.
  40. func ArbitraryArgs(cmd *Command, args []string) error {
  41. return nil
  42. }
  43. // MinimumNArgs returns an error if there is not at least N args.
  44. func MinimumNArgs(n int) PositionalArgs {
  45. return func(cmd *Command, args []string) error {
  46. if len(args) < n {
  47. return fmt.Errorf("requires at least %d arg(s), only received %d", n, len(args))
  48. }
  49. return nil
  50. }
  51. }
  52. // MaximumNArgs returns an error if there are more than N args.
  53. func MaximumNArgs(n int) PositionalArgs {
  54. return func(cmd *Command, args []string) error {
  55. if len(args) > n {
  56. return fmt.Errorf("accepts at most %d arg(s), received %d", n, len(args))
  57. }
  58. return nil
  59. }
  60. }
  61. // ExactArgs returns an error if there are not exactly n args.
  62. func ExactArgs(n int) PositionalArgs {
  63. return func(cmd *Command, args []string) error {
  64. if len(args) != n {
  65. return fmt.Errorf("accepts %d arg(s), received %d", n, len(args))
  66. }
  67. return nil
  68. }
  69. }
  70. // RangeArgs returns an error if the number of args is not within the expected range.
  71. func RangeArgs(min int, max int) PositionalArgs {
  72. return func(cmd *Command, args []string) error {
  73. if len(args) < min || len(args) > max {
  74. return fmt.Errorf("accepts between %d and %d arg(s), received %d", min, max, len(args))
  75. }
  76. return nil
  77. }
  78. }