int64.go 2.9 KB

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