int32.go 2.9 KB

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