-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
args.go
35 lines (28 loc) · 834 Bytes
/
args.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package cmdx
import (
"github.com/spf13/cobra"
)
// MinArgs fatals if args does not satisfy min.
func MinArgs(cmd *cobra.Command, args []string, min int) {
if len(args) < min {
Fatalf(`%s
Expected at least %d command line arguments but only got %d.`, cmd.UsageString(), min, len(args))
}
}
// ExactArgs fatals if args does not equal l.
func ExactArgs(cmd *cobra.Command, args []string, l int) {
if len(args) < l {
Fatalf(`%s
Expected exactly %d command line arguments but got %d.`, cmd.UsageString(), l, len(args))
}
}
// RangeArgs fatals if args does not satisfy any of the lengths set in r.
func RangeArgs(cmd *cobra.Command, args []string, r []int) {
for _, a := range r {
if len(args) == a {
return
}
}
Fatalf(`%s
Expected exact %v command line arguments but got %d.`, cmd.UsageString(), r, len(args))
}