flags.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package flags
  17. import (
  18. "encoding"
  19. "errors"
  20. "flag"
  21. "math/big"
  22. "os"
  23. "os/user"
  24. "path"
  25. "strings"
  26. "github.com/ethereum/go-ethereum/common/math"
  27. "github.com/urfave/cli/v2"
  28. )
  29. // DirectoryString is custom type which is registered in the flags library which cli uses for
  30. // argument parsing. This allows us to expand Value to an absolute path when
  31. // the argument is parsed
  32. type DirectoryString string
  33. func (s *DirectoryString) String() string {
  34. return string(*s)
  35. }
  36. func (s *DirectoryString) Set(value string) error {
  37. *s = DirectoryString(expandPath(value))
  38. return nil
  39. }
  40. var (
  41. _ cli.Flag = (*DirectoryFlag)(nil)
  42. _ cli.RequiredFlag = (*DirectoryFlag)(nil)
  43. _ cli.VisibleFlag = (*DirectoryFlag)(nil)
  44. _ cli.DocGenerationFlag = (*DirectoryFlag)(nil)
  45. _ cli.CategorizableFlag = (*DirectoryFlag)(nil)
  46. )
  47. // DirectoryFlag is custom cli.Flag type which expand the received string to an absolute path.
  48. // e.g. ~/.ethereum -> /home/username/.ethereum
  49. type DirectoryFlag struct {
  50. Name string
  51. Category string
  52. DefaultText string
  53. Usage string
  54. Required bool
  55. Hidden bool
  56. HasBeenSet bool
  57. Value DirectoryString
  58. Aliases []string
  59. }
  60. // For cli.Flag:
  61. func (f *DirectoryFlag) Names() []string { return append([]string{f.Name}, f.Aliases...) }
  62. func (f *DirectoryFlag) IsSet() bool { return f.HasBeenSet }
  63. func (f *DirectoryFlag) String() string { return cli.FlagStringer(f) }
  64. // Apply called by cli library, grabs variable from environment (if in env)
  65. // and adds variable to flag set for parsing.
  66. func (f *DirectoryFlag) Apply(set *flag.FlagSet) error {
  67. eachName(f, func(name string) {
  68. set.Var(&f.Value, f.Name, f.Usage)
  69. })
  70. return nil
  71. }
  72. // For cli.RequiredFlag:
  73. func (f *DirectoryFlag) IsRequired() bool { return f.Required }
  74. // For cli.VisibleFlag:
  75. func (f *DirectoryFlag) IsVisible() bool { return !f.Hidden }
  76. // For cli.CategorizableFlag:
  77. func (f *DirectoryFlag) GetCategory() string { return f.Category }
  78. // For cli.DocGenerationFlag:
  79. func (f *DirectoryFlag) TakesValue() bool { return true }
  80. func (f *DirectoryFlag) GetUsage() string { return f.Usage }
  81. func (f *DirectoryFlag) GetValue() string { return f.Value.String() }
  82. func (f *DirectoryFlag) GetEnvVars() []string { return nil } // env not supported
  83. func (f *DirectoryFlag) GetDefaultText() string {
  84. if f.DefaultText != "" {
  85. return f.DefaultText
  86. }
  87. return f.GetValue()
  88. }
  89. type TextMarshaler interface {
  90. encoding.TextMarshaler
  91. encoding.TextUnmarshaler
  92. }
  93. // textMarshalerVal turns a TextMarshaler into a flag.Value
  94. type textMarshalerVal struct {
  95. v TextMarshaler
  96. }
  97. func (v textMarshalerVal) String() string {
  98. if v.v == nil {
  99. return ""
  100. }
  101. text, _ := v.v.MarshalText()
  102. return string(text)
  103. }
  104. func (v textMarshalerVal) Set(s string) error {
  105. return v.v.UnmarshalText([]byte(s))
  106. }
  107. var (
  108. _ cli.Flag = (*TextMarshalerFlag)(nil)
  109. _ cli.RequiredFlag = (*TextMarshalerFlag)(nil)
  110. _ cli.VisibleFlag = (*TextMarshalerFlag)(nil)
  111. _ cli.DocGenerationFlag = (*TextMarshalerFlag)(nil)
  112. _ cli.CategorizableFlag = (*TextMarshalerFlag)(nil)
  113. )
  114. // TextMarshalerFlag wraps a TextMarshaler value.
  115. type TextMarshalerFlag struct {
  116. Name string
  117. Category string
  118. DefaultText string
  119. Usage string
  120. Required bool
  121. Hidden bool
  122. HasBeenSet bool
  123. Value TextMarshaler
  124. Aliases []string
  125. }
  126. // For cli.Flag:
  127. func (f *TextMarshalerFlag) Names() []string { return append([]string{f.Name}, f.Aliases...) }
  128. func (f *TextMarshalerFlag) IsSet() bool { return f.HasBeenSet }
  129. func (f *TextMarshalerFlag) String() string { return cli.FlagStringer(f) }
  130. func (f *TextMarshalerFlag) Apply(set *flag.FlagSet) error {
  131. eachName(f, func(name string) {
  132. set.Var(textMarshalerVal{f.Value}, f.Name, f.Usage)
  133. })
  134. return nil
  135. }
  136. // For cli.RequiredFlag:
  137. func (f *TextMarshalerFlag) IsRequired() bool { return f.Required }
  138. // For cli.VisibleFlag:
  139. func (f *TextMarshalerFlag) IsVisible() bool { return !f.Hidden }
  140. // For cli.CategorizableFlag:
  141. func (f *TextMarshalerFlag) GetCategory() string { return f.Category }
  142. // For cli.DocGenerationFlag:
  143. func (f *TextMarshalerFlag) TakesValue() bool { return true }
  144. func (f *TextMarshalerFlag) GetUsage() string { return f.Usage }
  145. func (f *TextMarshalerFlag) GetEnvVars() []string { return nil } // env not supported
  146. func (f *TextMarshalerFlag) GetValue() string {
  147. t, err := f.Value.MarshalText()
  148. if err != nil {
  149. return "(ERR: " + err.Error() + ")"
  150. }
  151. return string(t)
  152. }
  153. func (f *TextMarshalerFlag) GetDefaultText() string {
  154. if f.DefaultText != "" {
  155. return f.DefaultText
  156. }
  157. return f.GetValue()
  158. }
  159. // GlobalTextMarshaler returns the value of a TextMarshalerFlag from the global flag set.
  160. func GlobalTextMarshaler(ctx *cli.Context, name string) TextMarshaler {
  161. val := ctx.Generic(name)
  162. if val == nil {
  163. return nil
  164. }
  165. return val.(textMarshalerVal).v
  166. }
  167. var (
  168. _ cli.Flag = (*BigFlag)(nil)
  169. _ cli.RequiredFlag = (*BigFlag)(nil)
  170. _ cli.VisibleFlag = (*BigFlag)(nil)
  171. _ cli.DocGenerationFlag = (*BigFlag)(nil)
  172. _ cli.CategorizableFlag = (*BigFlag)(nil)
  173. )
  174. // BigFlag is a command line flag that accepts 256 bit big integers in decimal or
  175. // hexadecimal syntax.
  176. type BigFlag struct {
  177. Name string
  178. Category string
  179. DefaultText string
  180. Usage string
  181. Required bool
  182. Hidden bool
  183. HasBeenSet bool
  184. Value *big.Int
  185. Aliases []string
  186. }
  187. // For cli.Flag:
  188. func (f *BigFlag) Names() []string { return append([]string{f.Name}, f.Aliases...) }
  189. func (f *BigFlag) IsSet() bool { return f.HasBeenSet }
  190. func (f *BigFlag) String() string { return cli.FlagStringer(f) }
  191. func (f *BigFlag) Apply(set *flag.FlagSet) error {
  192. eachName(f, func(name string) {
  193. f.Value = new(big.Int)
  194. set.Var((*bigValue)(f.Value), f.Name, f.Usage)
  195. })
  196. return nil
  197. }
  198. // For cli.RequiredFlag:
  199. func (f *BigFlag) IsRequired() bool { return f.Required }
  200. // For cli.VisibleFlag:
  201. func (f *BigFlag) IsVisible() bool { return !f.Hidden }
  202. // For cli.CategorizableFlag:
  203. func (f *BigFlag) GetCategory() string { return f.Category }
  204. // For cli.DocGenerationFlag:
  205. func (f *BigFlag) TakesValue() bool { return true }
  206. func (f *BigFlag) GetUsage() string { return f.Usage }
  207. func (f *BigFlag) GetValue() string { return f.Value.String() }
  208. func (f *BigFlag) GetEnvVars() []string { return nil } // env not supported
  209. func (f *BigFlag) GetDefaultText() string {
  210. if f.DefaultText != "" {
  211. return f.DefaultText
  212. }
  213. return f.GetValue()
  214. }
  215. // bigValue turns *big.Int into a flag.Value
  216. type bigValue big.Int
  217. func (b *bigValue) String() string {
  218. if b == nil {
  219. return ""
  220. }
  221. return (*big.Int)(b).String()
  222. }
  223. func (b *bigValue) Set(s string) error {
  224. intVal, ok := math.ParseBig256(s)
  225. if !ok {
  226. return errors.New("invalid integer syntax")
  227. }
  228. *b = (bigValue)(*intVal)
  229. return nil
  230. }
  231. // GlobalBig returns the value of a BigFlag from the global flag set.
  232. func GlobalBig(ctx *cli.Context, name string) *big.Int {
  233. val := ctx.Generic(name)
  234. if val == nil {
  235. return nil
  236. }
  237. return (*big.Int)(val.(*bigValue))
  238. }
  239. // Expands a file path
  240. // 1. replace tilde with users home dir
  241. // 2. expands embedded environment variables
  242. // 3. cleans the path, e.g. /a/b/../c -> /a/c
  243. // Note, it has limitations, e.g. ~someuser/tmp will not be expanded
  244. func expandPath(p string) string {
  245. if strings.HasPrefix(p, "~/") || strings.HasPrefix(p, "~\\") {
  246. if home := HomeDir(); home != "" {
  247. p = home + p[1:]
  248. }
  249. }
  250. return path.Clean(os.ExpandEnv(p))
  251. }
  252. func HomeDir() string {
  253. if home := os.Getenv("HOME"); home != "" {
  254. return home
  255. }
  256. if usr, err := user.Current(); err == nil {
  257. return usr.HomeDir
  258. }
  259. return ""
  260. }
  261. func eachName(f cli.Flag, fn func(string)) {
  262. for _, name := range f.Names() {
  263. name = strings.Trim(name, " ")
  264. fn(name)
  265. }
  266. }