uint32.go 3.0 KB

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