Skip to content

Commit

Permalink
cmdx: Add arg helpers
Browse files Browse the repository at this point in the history
Signed-off-by: arekkas <aeneas@ory.am>
  • Loading branch information
arekkas committed Oct 9, 2018
1 parent 2dfc052 commit 2f9e0a8
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions cmdx/args.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,35 @@
package cmdx

import "github.com/spf13/cobra"
import (
"github.com/spf13/cobra"
"strings"
)

func MinArgs(cmd *cobra.Command, args []string, min int) {
if len(args) < min {
Fatalf(`%s
Expected %d command line arguments but got %d.`, cmd.UsageString(), min, len(args))
Expected at least %d command line arguments but only got %d.`, cmd.UsageString(), min, len(args))
}
}

func ExactArgs(cmd *cobra.Command, args []string, min int) {
if len(args) < min {
Fatalf(`%s
Expected exactly %d command line arguments but got %d.`, cmd.UsageString(), min, len(args))
}
}

func RangeArgs(cmd *cobra.Command, args []string, allowed []int) {
for _, a := range allowed {
if len(args) == a {
return
}
}
if len(args) < min {
Fatalf(`%s
Expected exact %s command line arguments but got %d.`, cmd.UsageString(), strings.Join(allowed, ", "), len(args))
}
}

0 comments on commit 2f9e0a8

Please sign in to comment.