gcmd.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // Copyright 2019 gf Author(https://github.com/gogf/gf). 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 gcmd provides console operations, like options/arguments reading and command running.
  8. package gcmd
  9. import (
  10. "os"
  11. "strings"
  12. "github.com/gogf/gf/container/gvar"
  13. "github.com/gogf/gf/text/gregex"
  14. )
  15. var (
  16. defaultParsedArgs = make([]string, 0)
  17. defaultParsedOptions = make(map[string]string)
  18. defaultCommandFuncMap = make(map[string]func())
  19. )
  20. // Custom initialization.
  21. func Init(args ...string) {
  22. if len(args) == 0 {
  23. if len(defaultParsedArgs) == 0 && len(defaultParsedOptions) == 0 {
  24. args = os.Args
  25. } else {
  26. return
  27. }
  28. } else {
  29. defaultParsedArgs = make([]string, 0)
  30. defaultParsedOptions = make(map[string]string)
  31. }
  32. // Parsing os.Args with default algorithm.
  33. for i := 0; i < len(args); {
  34. array, _ := gregex.MatchString(`^\-{1,2}([\w\?\.\-]+)(=){0,1}(.*)$`, args[i])
  35. if len(array) > 2 {
  36. if array[2] == "=" {
  37. defaultParsedOptions[array[1]] = array[3]
  38. } else if i < len(args)-1 {
  39. if len(args[i+1]) > 0 && args[i+1][0] == '-' {
  40. // Eg: gf gen -d -n 1
  41. defaultParsedOptions[array[1]] = array[3]
  42. } else {
  43. // Eg: gf gen -n 2
  44. defaultParsedOptions[array[1]] = args[i+1]
  45. i += 2
  46. continue
  47. }
  48. } else {
  49. // Eg: gf gen -h
  50. defaultParsedOptions[array[1]] = array[3]
  51. }
  52. } else {
  53. defaultParsedArgs = append(defaultParsedArgs, args[i])
  54. }
  55. i++
  56. }
  57. }
  58. // GetOpt returns the option value named <name>.
  59. func GetOpt(name string, def ...string) string {
  60. Init()
  61. if v, ok := defaultParsedOptions[name]; ok {
  62. return v
  63. }
  64. if len(def) > 0 {
  65. return def[0]
  66. }
  67. return ""
  68. }
  69. // GetOptVar returns the option value named <name> as gvar.Var.
  70. func GetOptVar(name string, def ...string) *gvar.Var {
  71. Init()
  72. return gvar.New(GetOpt(name, def...))
  73. }
  74. // GetOptAll returns all parsed options.
  75. func GetOptAll() map[string]string {
  76. Init()
  77. return defaultParsedOptions
  78. }
  79. // ContainsOpt checks whether option named <name> exist in the arguments.
  80. func ContainsOpt(name string, def ...string) bool {
  81. Init()
  82. _, ok := defaultParsedOptions[name]
  83. return ok
  84. }
  85. // GetArg returns the argument at <index>.
  86. func GetArg(index int, def ...string) string {
  87. Init()
  88. if index < len(defaultParsedArgs) {
  89. return defaultParsedArgs[index]
  90. }
  91. if len(def) > 0 {
  92. return def[0]
  93. }
  94. return ""
  95. }
  96. // GetArgVar returns the argument at <index> as gvar.Var.
  97. func GetArgVar(index int, def ...string) *gvar.Var {
  98. Init()
  99. return gvar.New(GetArg(index, def...))
  100. }
  101. // GetArgAll returns all parsed arguments.
  102. func GetArgAll() []string {
  103. Init()
  104. return defaultParsedArgs
  105. }
  106. // GetWithEnv returns the command line argument of the specified <key>.
  107. // If the argument does not exist, then it returns the environment variable with specified <key>.
  108. // It returns the default value <def> if none of them exists.
  109. //
  110. // Fetching Rules:
  111. // 1. Command line arguments are in lowercase format, eg: gf.<package name>.<variable name>;
  112. // 2. Environment arguments are in uppercase format, eg: GF_<package name>_<variable name>;
  113. func GetWithEnv(key string, def ...interface{}) *gvar.Var {
  114. value := interface{}(nil)
  115. if len(def) > 0 {
  116. value = def[0]
  117. }
  118. cmdKey := strings.ToLower(strings.Replace(key, "_", ".", -1))
  119. if v := GetOpt(cmdKey); v != "" {
  120. value = v
  121. } else {
  122. envKey := strings.ToUpper(strings.Replace(key, ".", "_", -1))
  123. if v := os.Getenv(envKey); v != "" {
  124. value = v
  125. }
  126. }
  127. return gvar.New(value)
  128. }
  129. // BuildOptions builds the options as string.
  130. func BuildOptions(m map[string]string, prefix ...string) string {
  131. options := ""
  132. leadStr := "-"
  133. if len(prefix) > 0 {
  134. leadStr = prefix[0]
  135. }
  136. for k, v := range m {
  137. if len(options) > 0 {
  138. options += " "
  139. }
  140. options += leadStr + k
  141. if v != "" {
  142. options += "=" + v
  143. }
  144. }
  145. return options
  146. }