github.com/kcmvp/gob@v1.0.17/cmd/gbc/command/validator.go (about)

     1  package command
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/fatih/color"
     7  	"github.com/samber/lo"
     8  	"github.com/spf13/cobra"
     9  )
    10  
    11  func MinimumNArgs(n int) cobra.PositionalArgs {
    12  	return func(cmd *cobra.Command, args []string) error {
    13  		if len(args) < n {
    14  			return errors.New(color.RedString("requires at least %d arg(s), only received %d", n, len(args)))
    15  		}
    16  		return nil
    17  	}
    18  }
    19  
    20  func OnlyValidArgs(cmd *cobra.Command, args []string) error {
    21  	for _, arg := range args {
    22  		if !lo.Contains(cmd.ValidArgs, arg) {
    23  			return errors.New(color.RedString("invalid argument %q for %s", arg, cmd.CommandPath()))
    24  		}
    25  	}
    26  	return nil
    27  }