int64.go 2.9 KB

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