Skip to content

Commit 68009a3

Browse files
authoredJun 17, 2020
feat(predict): Add argsRange (go-clix#5)
1 parent dbd6eb1 commit 68009a3

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed
 

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ Predefined options are also available:
193193

194194
- `ArgsAny()`: accepts any number args, predicts files and directories
195195
- `ArgsExact(n)`: accepts _exactly_ `n` arguments. Predicts files and directories.
196+
- `ArgsRange(n, m)`: accepts between `n` and `m` arguments (inclusive). Predicts files and directories.
196197
- `ArgsNone()`: accepts _no_ args and predicts nothing.
197198
- `ArgsSet(...string)`: Accepts _one_ argument, which MUST be included in the
198199
given set. Predicts the values from the set.

‎args.go

+21
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,27 @@ func ValidateExact(n int) ValidateFunc {
8484
}
8585
}
8686

87+
// Argument range
88+
89+
// ArgsRange checks for between n and m arguments (inclusive), predicting anything
90+
func ArgsRange(n, m int) Arguments {
91+
return Args{
92+
Validator: ValidateRange(n, m),
93+
Predictor: PredictAny(),
94+
}
95+
}
96+
97+
// ValidateRange checks that between n and m arguments (inclusive) were given
98+
func ValidateRange(n, m int) ValidateFunc {
99+
return func(args []string) error {
100+
if len(args) < n || len(args) > m {
101+
return fmt.Errorf("accepts between %v and %v args, received %v", n, m, len(args))
102+
}
103+
return nil
104+
}
105+
}
106+
107+
87108
// Any arguments
88109

89110
// ArgsAny allows any number of arguments with any value

0 commit comments

Comments
 (0)
Please sign in to comment.