command.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. defaultParsedArgs, defaultParsedOptions = ParseUsingDefaultAlgorithm(args...)
  33. }
  34. // ParseUsingDefaultAlgorithm parses arguments using default algorithm.
  35. func ParseUsingDefaultAlgorithm(args ...string) (parsedArgs []string, parsedOptions map[string]string) {
  36. parsedArgs = make([]string, 0)
  37. parsedOptions = make(map[string]string)
  38. for i := 0; i < len(args); {
  39. array := argumentRegex.FindStringSubmatch(args[i])
  40. if len(array) > 2 {
  41. if array[2] == "=" {
  42. parsedOptions[array[1]] = array[3]
  43. } else if i < len(args)-1 {
  44. if len(args[i+1]) > 0 && args[i+1][0] == '-' {
  45. // Eg: gf gen -d -n 1
  46. parsedOptions[array[1]] = array[3]
  47. } else {
  48. // Eg: gf gen -n 2
  49. parsedOptions[array[1]] = args[i+1]
  50. i += 2
  51. continue
  52. }
  53. } else {
  54. // Eg: gf gen -h
  55. parsedOptions[array[1]] = array[3]
  56. }
  57. } else {
  58. parsedArgs = append(parsedArgs, args[i])
  59. }
  60. i++
  61. }
  62. return
  63. }
  64. // GetOpt returns the option value named `name`.
  65. func GetOpt(name string, def ...string) string {
  66. Init()
  67. if v, ok := defaultParsedOptions[name]; ok {
  68. return v
  69. }
  70. if len(def) > 0 {
  71. return def[0]
  72. }
  73. return ""
  74. }
  75. // GetOptAll returns all parsed options.
  76. func GetOptAll() map[string]string {
  77. Init()
  78. return defaultParsedOptions
  79. }
  80. // ContainsOpt checks whether option named `name` exist in the arguments.
  81. func ContainsOpt(name string) bool {
  82. Init()
  83. _, ok := defaultParsedOptions[name]
  84. return ok
  85. }
  86. // GetArg returns the argument at `index`.
  87. func GetArg(index int, def ...string) string {
  88. Init()
  89. if index < len(defaultParsedArgs) {
  90. return defaultParsedArgs[index]
  91. }
  92. if len(def) > 0 {
  93. return def[0]
  94. }
  95. return ""
  96. }
  97. // GetArgAll returns all parsed arguments.
  98. func GetArgAll() []string {
  99. Init()
  100. return defaultParsedArgs
  101. }
  102. // GetOptWithEnv returns the command line argument of the specified `key`.
  103. // If the argument does not exist, then it returns the environment variable with specified `key`.
  104. // It returns the default value `def` if none of them exists.
  105. //
  106. // Fetching Rules:
  107. // 1. Command line arguments are in lowercase format, eg: gf.package.variable;
  108. // 2. Environment arguments are in uppercase format, eg: GF_PACKAGE_VARIABLE;
  109. func GetOptWithEnv(key string, def ...string) string {
  110. cmdKey := strings.ToLower(strings.ReplaceAll(key, "_", "."))
  111. if ContainsOpt(cmdKey) {
  112. return GetOpt(cmdKey)
  113. } else {
  114. envKey := strings.ToUpper(strings.ReplaceAll(key, ".", "_"))
  115. if r, ok := os.LookupEnv(envKey); ok {
  116. return r
  117. } else {
  118. if len(def) > 0 {
  119. return def[0]
  120. }
  121. }
  122. }
  123. return ""
  124. }