command.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. //
  7. // Package command provides console operations, like options/arguments reading.
  8. package command
  9. import (
  10. "os"
  11. "regexp"
  12. "strings"
  13. )
  14. var (
  15. defaultParsedArgs = make([]string, 0)
  16. defaultParsedOptions = make(map[string]string)
  17. argumentRegex = regexp.MustCompile(`^\-{1,2}([\w\?\.\-]+)(=){0,1}(.*)$`)
  18. )
  19. // Init does custom initialization.
  20. func Init(args ...string) {
  21. if len(args) == 0 {
  22. if len(defaultParsedArgs) == 0 && len(defaultParsedOptions) == 0 {
  23. args = os.Args
  24. } else {
  25. return
  26. }
  27. } else {
  28. defaultParsedArgs = make([]string, 0)
  29. defaultParsedOptions = make(map[string]string)
  30. }
  31. // Parsing os.Args with default algorithm.
  32. for i := 0; i < len(args); {
  33. array := argumentRegex.FindStringSubmatch(args[i])
  34. if len(array) > 2 {
  35. if array[2] == "=" {
  36. defaultParsedOptions[array[1]] = array[3]
  37. } else if i < len(args)-1 {
  38. if len(args[i+1]) > 0 && args[i+1][0] == '-' {
  39. // Eg: gf gen -d -n 1
  40. defaultParsedOptions[array[1]] = array[3]
  41. } else {
  42. // Eg: gf gen -n 2
  43. defaultParsedOptions[array[1]] = args[i+1]
  44. i += 2
  45. continue
  46. }
  47. } else {
  48. // Eg: gf gen -h
  49. defaultParsedOptions[array[1]] = array[3]
  50. }
  51. } else {
  52. defaultParsedArgs = append(defaultParsedArgs, args[i])
  53. }
  54. i++
  55. }
  56. }
  57. // GetOpt returns the option value named `name`.
  58. func GetOpt(name string, def ...string) string {
  59. Init()
  60. if v, ok := defaultParsedOptions[name]; ok {
  61. return v
  62. }
  63. if len(def) > 0 {
  64. return def[0]
  65. }
  66. return ""
  67. }
  68. // GetOptAll returns all parsed options.
  69. func GetOptAll() map[string]string {
  70. Init()
  71. return defaultParsedOptions
  72. }
  73. // ContainsOpt checks whether option named `name` exist in the arguments.
  74. func ContainsOpt(name string) bool {
  75. Init()
  76. _, ok := defaultParsedOptions[name]
  77. return ok
  78. }
  79. // GetArg returns the argument at `index`.
  80. func GetArg(index int, def ...string) string {
  81. Init()
  82. if index < len(defaultParsedArgs) {
  83. return defaultParsedArgs[index]
  84. }
  85. if len(def) > 0 {
  86. return def[0]
  87. }
  88. return ""
  89. }
  90. // GetArgAll returns all parsed arguments.
  91. func GetArgAll() []string {
  92. Init()
  93. return defaultParsedArgs
  94. }
  95. // GetOptWithEnv returns the command line argument of the specified `key`.
  96. // If the argument does not exist, then it returns the environment variable with specified `key`.
  97. // It returns the default value `def` if none of them exists.
  98. //
  99. // Fetching Rules:
  100. // 1. Command line arguments are in lowercase format, eg: gf.<package name>.<variable name>;
  101. // 2. Environment arguments are in uppercase format, eg: GF_<package name>_<variable name>;
  102. func GetOptWithEnv(key string, def ...string) string {
  103. cmdKey := strings.ToLower(strings.Replace(key, "_", ".", -1))
  104. if ContainsOpt(cmdKey) {
  105. return GetOpt(cmdKey)
  106. } else {
  107. envKey := strings.ToUpper(strings.Replace(key, ".", "_", -1))
  108. if r, ok := os.LookupEnv(envKey); ok {
  109. return r
  110. } else {
  111. if len(def) > 0 {
  112. return def[0]
  113. }
  114. }
  115. }
  116. return ""
  117. }