uint16.go 3.0 KB

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