本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
3.0KB

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