uint32.go 3.0 KB

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