uint64.go 3.0 KB

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