int8.go 2.9 KB

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