uint8.go 2.9 KB

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