uint.go 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package pflag
  2. import "strconv"
  3. // -- uint Value
  4. type uintValue uint
  5. func newUintValue(val uint, p *uint) *uintValue {
  6. *p = val
  7. return (*uintValue)(p)
  8. }
  9. func (i *uintValue) Set(s string) error {
  10. v, err := strconv.ParseUint(s, 0, 64)
  11. *i = uintValue(v)
  12. return err
  13. }
  14. func (i *uintValue) Type() string {
  15. return "uint"
  16. }
  17. func (i *uintValue) String() string { return strconv.FormatUint(uint64(*i), 10) }
  18. func uintConv(sval string) (interface{}, error) {
  19. v, err := strconv.ParseUint(sval, 0, 0)
  20. if err != nil {
  21. return 0, err
  22. }
  23. return uint(v), nil
  24. }
  25. // GetUint return the uint value of a flag with the given name
  26. func (f *FlagSet) GetUint(name string) (uint, error) {
  27. val, err := f.getFlagType(name, "uint", uintConv)
  28. if err != nil {
  29. return 0, err
  30. }
  31. return val.(uint), nil
  32. }
  33. // UintVar defines a uint flag with specified name, default value, and usage string.
  34. // The argument p points to a uint variable in which to store the value of the flag.
  35. func (f *FlagSet) UintVar(p *uint, name string, value uint, usage string) {
  36. f.VarP(newUintValue(value, p), name, "", usage)
  37. }
  38. // UintVarP is like UintVar, but accepts a shorthand letter that can be used after a single dash.
  39. func (f *FlagSet) UintVarP(p *uint, name, shorthand string, value uint, usage string) {
  40. f.VarP(newUintValue(value, p), name, shorthand, usage)
  41. }
  42. // UintVar defines a uint flag with specified name, default value, and usage string.
  43. // The argument p points to a uint variable in which to store the value of the flag.
  44. func UintVar(p *uint, name string, value uint, usage string) {
  45. CommandLine.VarP(newUintValue(value, p), name, "", usage)
  46. }
  47. // UintVarP is like UintVar, but accepts a shorthand letter that can be used after a single dash.
  48. func UintVarP(p *uint, name, shorthand string, value uint, usage string) {
  49. CommandLine.VarP(newUintValue(value, p), name, shorthand, usage)
  50. }
  51. // Uint defines a uint flag with specified name, default value, and usage string.
  52. // The return value is the address of a uint variable that stores the value of the flag.
  53. func (f *FlagSet) Uint(name string, value uint, usage string) *uint {
  54. p := new(uint)
  55. f.UintVarP(p, name, "", value, usage)
  56. return p
  57. }
  58. // UintP is like Uint, but accepts a shorthand letter that can be used after a single dash.
  59. func (f *FlagSet) UintP(name, shorthand string, value uint, usage string) *uint {
  60. p := new(uint)
  61. f.UintVarP(p, name, shorthand, value, usage)
  62. return p
  63. }
  64. // Uint defines a uint flag with specified name, default value, and usage string.
  65. // The return value is the address of a uint variable that stores the value of the flag.
  66. func Uint(name string, value uint, usage string) *uint {
  67. return CommandLine.UintP(name, "", value, usage)
  68. }
  69. // UintP is like Uint, but accepts a shorthand letter that can be used after a single dash.
  70. func UintP(name, shorthand string, value uint, usage string) *uint {
  71. return CommandLine.UintP(name, shorthand, value, usage)
  72. }