flag.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // Copyright 2015 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package objabi
  5. import (
  6. "flag"
  7. "fmt"
  8. "io"
  9. "io/ioutil"
  10. "log"
  11. "os"
  12. "strconv"
  13. "strings"
  14. )
  15. func Flagcount(name, usage string, val *int) {
  16. flag.Var((*count)(val), name, usage)
  17. }
  18. func Flagfn1(name, usage string, f func(string)) {
  19. flag.Var(fn1(f), name, usage)
  20. }
  21. func Flagprint(w io.Writer) {
  22. flag.CommandLine.SetOutput(w)
  23. flag.PrintDefaults()
  24. }
  25. func Flagparse(usage func()) {
  26. flag.Usage = usage
  27. os.Args = expandArgs(os.Args)
  28. flag.Parse()
  29. }
  30. // expandArgs expands "response files" arguments in the provided slice.
  31. //
  32. // A "response file" argument starts with '@' and the rest of that
  33. // argument is a filename with CR-or-CRLF-separated arguments. Each
  34. // argument in the named files can also contain response file
  35. // arguments. See Issue 18468.
  36. //
  37. // The returned slice 'out' aliases 'in' iff the input did not contain
  38. // any response file arguments.
  39. //
  40. // TODO: handle relative paths of recursive expansions in different directories?
  41. // Is there a spec for this? Are relative paths allowed?
  42. func expandArgs(in []string) (out []string) {
  43. // out is nil until we see a "@" argument.
  44. for i, s := range in {
  45. if strings.HasPrefix(s, "@") {
  46. if out == nil {
  47. out = make([]string, 0, len(in)*2)
  48. out = append(out, in[:i]...)
  49. }
  50. slurp, err := ioutil.ReadFile(s[1:])
  51. if err != nil {
  52. log.Fatal(err)
  53. }
  54. args := strings.Split(strings.TrimSpace(strings.Replace(string(slurp), "\r", "", -1)), "\n")
  55. out = append(out, expandArgs(args)...)
  56. } else if out != nil {
  57. out = append(out, s)
  58. }
  59. }
  60. if out == nil {
  61. return in
  62. }
  63. return
  64. }
  65. func AddVersionFlag() {
  66. flag.Var(versionFlag{}, "V", "print version and exit")
  67. }
  68. var buildID string // filled in by linker
  69. type versionFlag struct{}
  70. func (versionFlag) IsBoolFlag() bool { return true }
  71. func (versionFlag) Get() interface{} { return nil }
  72. func (versionFlag) String() string { return "" }
  73. func (versionFlag) Set(s string) error {
  74. name := os.Args[0]
  75. name = name[strings.LastIndex(name, `/`)+1:]
  76. name = name[strings.LastIndex(name, `\`)+1:]
  77. name = strings.TrimSuffix(name, ".exe")
  78. // If there's an active experiment, include that,
  79. // to distinguish go1.10.2 with an experiment
  80. // from go1.10.2 without an experiment.
  81. p := Expstring()
  82. if p == DefaultExpstring() {
  83. p = ""
  84. }
  85. sep := ""
  86. if p != "" {
  87. sep = " "
  88. }
  89. // The go command invokes -V=full to get a unique identifier
  90. // for this tool. It is assumed that the release version is sufficient
  91. // for releases, but during development we include the full
  92. // build ID of the binary, so that if the compiler is changed and
  93. // rebuilt, we notice and rebuild all packages.
  94. if s == "full" {
  95. if strings.HasPrefix(Version, "devel") {
  96. p += " buildID=" + buildID
  97. }
  98. }
  99. fmt.Printf("%s version %s%s%s\n", name, Version, sep, p)
  100. os.Exit(0)
  101. return nil
  102. }
  103. // count is a flag.Value that is like a flag.Bool and a flag.Int.
  104. // If used as -name, it increments the count, but -name=x sets the count.
  105. // Used for verbose flag -v.
  106. type count int
  107. func (c *count) String() string {
  108. return fmt.Sprint(int(*c))
  109. }
  110. func (c *count) Set(s string) error {
  111. switch s {
  112. case "true":
  113. *c++
  114. case "false":
  115. *c = 0
  116. default:
  117. n, err := strconv.Atoi(s)
  118. if err != nil {
  119. return fmt.Errorf("invalid count %q", s)
  120. }
  121. *c = count(n)
  122. }
  123. return nil
  124. }
  125. func (c *count) Get() interface{} {
  126. return int(*c)
  127. }
  128. func (c *count) IsBoolFlag() bool {
  129. return true
  130. }
  131. func (c *count) IsCountFlag() bool {
  132. return true
  133. }
  134. type fn1 func(string)
  135. func (f fn1) Set(s string) error {
  136. f(s)
  137. return nil
  138. }
  139. func (f fn1) String() string { return "" }