github.com/puellanivis/breton@v0.2.16/lib/gnuflag/uint.go (about)

     1  package gnuflag
     2  
     3  import (
     4  	"strconv"
     5  )
     6  
     7  // -- uint Value
     8  type uintValue uint
     9  
    10  func (u *uintValue) Set(s string) error {
    11  	v, err := strconv.ParseUint(s, 0, strconv.IntSize)
    12  	*u = uintValue(v)
    13  	return err
    14  }
    15  
    16  func (u uintValue) Get() interface{} { return uint(u) }
    17  
    18  func (u uintValue) String() string { return strconv.FormatUint(uint64(u), 10) }
    19  
    20  // Uint defines a uint flag with specified name, and usage string.
    21  // The return value is the address of a uint variable that stores the value of the flag.
    22  func (f *FlagSet) Uint(name string, usage string, options ...Option) *uint {
    23  	p := new(uint)
    24  	if err := f.Var((*uintValue)(p), name, usage, options...); err != nil {
    25  		panic(err)
    26  	}
    27  	return p
    28  }
    29  
    30  // Uint defines a uint flag with specified name, and usage string.
    31  // The return value is the address of a uint variable that stores the value of the flag.
    32  func Uint(name string, usage string, options ...Option) *uint {
    33  	return CommandLine.Uint(name, usage, options...)
    34  }
    35  
    36  // -- uint64 Value
    37  type uint64Value uint64
    38  
    39  func (u *uint64Value) Set(s string) error {
    40  	v, err := strconv.ParseUint(s, 0, 64)
    41  	*u = uint64Value(v)
    42  	return err
    43  }
    44  
    45  func (u uint64Value) Get() interface{} { return uint64(u) }
    46  
    47  func (u uint64Value) String() string { return strconv.FormatUint(uint64(u), 10) }
    48  
    49  // Uint64 defines a uint64 flag with specified name, and usage string.
    50  // The return value is the address of a uint64 variable that stores the value of the flag.
    51  func (f *FlagSet) Uint64(name string, usage string, options ...Option) *uint64 {
    52  	p := new(uint64)
    53  	if err := f.Var((*uint64Value)(p), name, usage, options...); err != nil {
    54  		panic(err)
    55  	}
    56  	return p
    57  }
    58  
    59  // Uint64 defines a uint64 flag with specified name, and usage string.
    60  // The return value is the address of a uint64 variable that stores the value of the flag.
    61  func Uint64(name string, usage string, options ...Option) *uint64 {
    62  	return CommandLine.Uint64(name, usage, options...)
    63  }