github.com/icyphox/x@v0.0.355-0.20220311094250-029bd783e8b8/cmdx/args.go (about)

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