mirror of
https://github.com/spf13/cobra
synced 2025-05-05 12:57:22 +00:00
enable composing PositionalArgs
This commit is contained in:
parent
442031e4ff
commit
488a4bc560
1 changed files with 13 additions and 6 deletions
19
args.go
19
args.go
|
@ -90,12 +90,7 @@ func ExactArgs(n int) PositionalArgs {
|
||||||
// there are not exactly N positional args OR
|
// there are not exactly N positional args OR
|
||||||
// there are any positional args that are not in the `ValidArgs` field of `Command`
|
// there are any positional args that are not in the `ValidArgs` field of `Command`
|
||||||
func ExactValidArgs(n int) PositionalArgs {
|
func ExactValidArgs(n int) PositionalArgs {
|
||||||
return func(cmd *Command, args []string) error {
|
return ComposedArgs(ExactArgs(n), OnlyValidArgs)
|
||||||
if err := ExactArgs(n)(cmd, args); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return OnlyValidArgs(cmd, args)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// RangeArgs returns an error if the number of args is not within the expected range.
|
// RangeArgs returns an error if the number of args is not within the expected range.
|
||||||
|
@ -107,3 +102,15 @@ func RangeArgs(min int, max int) PositionalArgs {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ComposedArgs allows combining several PositionalArgs to work in concert.
|
||||||
|
func ComposedArgs(pargs ...PositionalArgs) PositionalArgs {
|
||||||
|
return func(cmd *Command, args []string) error {
|
||||||
|
for _, parg := range pargs {
|
||||||
|
if err := parg(cmd, args); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue