uint64.go 3.0 KB

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