github.com/sleungcy/cli@v7.1.0+incompatible/command/flag/integer_limit.go (about)

     1  package flag
     2  
     3  import (
     4  	"strconv"
     5  
     6  	"code.cloudfoundry.org/cli/types"
     7  	flags "github.com/jessevdk/go-flags"
     8  )
     9  
    10  // When setting limits allows -1 as as an unlimited value. Unlimited value is represented by nil
    11  type IntegerLimit types.NullInt
    12  
    13  func (i *IntegerLimit) UnmarshalFlag(val string) error {
    14  	if val == "" {
    15  		return nil
    16  	}
    17  
    18  	intVal, err := strconv.Atoi(val)
    19  
    20  	if err != nil || intVal < -1 {
    21  		return &flags.Error{
    22  			Type:    flags.ErrRequired,
    23  			Message: "invalid integer limit (expected int >= -1)",
    24  		}
    25  	}
    26  
    27  	i.IsSet = true
    28  	i.Value = intVal
    29  
    30  	return nil
    31  }
    32  
    33  func (i *IntegerLimit) IsValidValue(val string) error {
    34  	return i.UnmarshalFlag(val)
    35  }