forked from spf13/cobra
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor TakesArgs to use an interface for arg validation.
Fix some typos in README and comments. Move arg validation to after flag validation so that the help flag is run first. Pass the same args to ValidateArgs as the Run methods receive. Update README. Signed-off-by: Daniel Nephin <[email protected]>
- Loading branch information
Showing
4 changed files
with
160 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
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 | ||
} | ||
|
||
// root command with subcommands, do subcommand checking | ||
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 | ||
} | ||
|
||
// NoArgs returns an error if any args are included | ||
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 | ||
} | ||
|
||
// OnlyValidArgs returns an error if any args are not in the list of ValidArgs | ||
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 | ||
} | ||
|
||
func stringInSlice(a string, list []string) bool { | ||
for _, b := range list { | ||
if b == a { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
// ArbitraryArgs never returns an error | ||
func ArbitraryArgs(cmd *Command, args []string) error { | ||
return nil | ||
} | ||
|
||
// MinimumNArgs returns an error if there is not at least N args | ||
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 | ||
} | ||
} | ||
|
||
// MaximumNArgs returns an error if there are more than N args | ||
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 | ||
} | ||
} | ||
|
||
// ExactArgs returns an error if there are not exactly n args | ||
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 | ||
} | ||
} | ||
|
||
// RangeArgs returns an error if the number of args is not within the expected range | ||
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters