args.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright 2013-2023 The Cobra Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package cobra
  15. import (
  16. "fmt"
  17. "strings"
  18. )
  19. type PositionalArgs func(cmd *Command, args []string) error
  20. // legacyArgs validation has the following behaviour:
  21. // - root commands with no subcommands can take arbitrary arguments
  22. // - root commands with subcommands will do subcommand validity checking
  23. // - subcommands will always accept arbitrary arguments
  24. func legacyArgs(cmd *Command, args []string) error {
  25. // no subcommand, always take args
  26. if !cmd.HasSubCommands() {
  27. return nil
  28. }
  29. // root command with subcommands, do subcommand checking.
  30. if !cmd.HasParent() && len(args) > 0 {
  31. return fmt.Errorf("unknown command %q for %q%s", args[0], cmd.CommandPath(), cmd.findSuggestions(args[0]))
  32. }
  33. return nil
  34. }
  35. // NoArgs returns an error if any args are included.
  36. func NoArgs(cmd *Command, args []string) error {
  37. if len(args) > 0 {
  38. return fmt.Errorf("unknown command %q for %q", args[0], cmd.CommandPath())
  39. }
  40. return nil
  41. }
  42. // OnlyValidArgs returns an error if there are any positional args that are not in
  43. // the `ValidArgs` field of `Command`
  44. func OnlyValidArgs(cmd *Command, args []string) error {
  45. if len(cmd.ValidArgs) > 0 {
  46. // Remove any description that may be included in ValidArgs.
  47. // A description is following a tab character.
  48. var validArgs []string
  49. for _, v := range cmd.ValidArgs {
  50. validArgs = append(validArgs, strings.Split(v, "\t")[0])
  51. }
  52. for _, v := range args {
  53. if !stringInSlice(v, validArgs) {
  54. return fmt.Errorf("invalid argument %q for %q%s", v, cmd.CommandPath(), cmd.findSuggestions(args[0]))
  55. }
  56. }
  57. }
  58. return nil
  59. }
  60. // ArbitraryArgs never returns an error.
  61. func ArbitraryArgs(cmd *Command, args []string) error {
  62. return nil
  63. }
  64. // MinimumNArgs returns an error if there is not at least N args.
  65. func MinimumNArgs(n int) PositionalArgs {
  66. return func(cmd *Command, args []string) error {
  67. if len(args) < n {
  68. return fmt.Errorf("requires at least %d arg(s), only received %d", n, len(args))
  69. }
  70. return nil
  71. }
  72. }
  73. // MaximumNArgs returns an error if there are more than N args.
  74. func MaximumNArgs(n int) PositionalArgs {
  75. return func(cmd *Command, args []string) error {
  76. if len(args) > n {
  77. return fmt.Errorf("accepts at most %d arg(s), received %d", n, len(args))
  78. }
  79. return nil
  80. }
  81. }
  82. // ExactArgs returns an error if there are not exactly n args.
  83. func ExactArgs(n int) PositionalArgs {
  84. return func(cmd *Command, args []string) error {
  85. if len(args) != n {
  86. return fmt.Errorf("accepts %d arg(s), received %d", n, len(args))
  87. }
  88. return nil
  89. }
  90. }
  91. // RangeArgs returns an error if the number of args is not within the expected range.
  92. func RangeArgs(min int, max int) PositionalArgs {
  93. return func(cmd *Command, args []string) error {
  94. if len(args) < min || len(args) > max {
  95. return fmt.Errorf("accepts between %d and %d arg(s), received %d", min, max, len(args))
  96. }
  97. return nil
  98. }
  99. }
  100. // MatchAll allows combining several PositionalArgs to work in concert.
  101. func MatchAll(pargs ...PositionalArgs) PositionalArgs {
  102. return func(cmd *Command, args []string) error {
  103. for _, parg := range pargs {
  104. if err := parg(cmd, args); err != nil {
  105. return err
  106. }
  107. }
  108. return nil
  109. }
  110. }
  111. // ExactValidArgs returns an error if there are not exactly N positional args OR
  112. // there are any positional args that are not in the `ValidArgs` field of `Command`
  113. //
  114. // Deprecated: use MatchAll(ExactArgs(n), OnlyValidArgs) instead
  115. func ExactValidArgs(n int) PositionalArgs {
  116. return MatchAll(ExactArgs(n), OnlyValidArgs)
  117. }