uint.go 2.9 KB

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