int.go 2.7 KB

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