gcmd.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 gcmd provides console operations, like options/arguments reading and command running.
  8. package gcmd
  9. import (
  10. "github.com/gogf/gf/container/gvar"
  11. "github.com/gogf/gf/internal/command"
  12. "os"
  13. "strings"
  14. )
  15. var (
  16. defaultCommandFuncMap = make(map[string]func())
  17. )
  18. // Custom initialization.
  19. func Init(args ...string) {
  20. command.Init(args...)
  21. }
  22. // GetOpt returns the option value named <name>.
  23. func GetOpt(name string, def ...string) string {
  24. Init()
  25. return command.GetOpt(name, def...)
  26. }
  27. // GetOptVar returns the option value named <name> as gvar.Var.
  28. func GetOptVar(name string, def ...string) *gvar.Var {
  29. Init()
  30. return gvar.New(GetOpt(name, def...))
  31. }
  32. // GetOptAll returns all parsed options.
  33. func GetOptAll() map[string]string {
  34. Init()
  35. return command.GetOptAll()
  36. }
  37. // ContainsOpt checks whether option named <name> exist in the arguments.
  38. func ContainsOpt(name string) bool {
  39. Init()
  40. return command.ContainsOpt(name)
  41. }
  42. // GetArg returns the argument at <index>.
  43. func GetArg(index int, def ...string) string {
  44. Init()
  45. return command.GetArg(index, def...)
  46. }
  47. // GetArgVar returns the argument at <index> as gvar.Var.
  48. func GetArgVar(index int, def ...string) *gvar.Var {
  49. Init()
  50. return gvar.New(GetArg(index, def...))
  51. }
  52. // GetArgAll returns all parsed arguments.
  53. func GetArgAll() []string {
  54. Init()
  55. return command.GetArgAll()
  56. }
  57. // GetWithEnv is alias of GetOptWithEnv.
  58. // Deprecated, use GetOptWithEnv instead.
  59. func GetWithEnv(key string, def ...interface{}) *gvar.Var {
  60. return GetOptWithEnv(key, def...)
  61. }
  62. // GetOptWithEnv returns the command line argument of the specified <key>.
  63. // If the argument does not exist, then it returns the environment variable with specified <key>.
  64. // It returns the default value <def> if none of them exists.
  65. //
  66. // Fetching Rules:
  67. // 1. Command line arguments are in lowercase format, eg: gf.<package name>.<variable name>;
  68. // 2. Environment arguments are in uppercase format, eg: GF_<package name>_<variable name>;
  69. func GetOptWithEnv(key string, def ...interface{}) *gvar.Var {
  70. cmdKey := strings.ToLower(strings.Replace(key, "_", ".", -1))
  71. if ContainsOpt(cmdKey) {
  72. return gvar.New(GetOpt(cmdKey))
  73. } else {
  74. envKey := strings.ToUpper(strings.Replace(key, ".", "_", -1))
  75. if r, ok := os.LookupEnv(envKey); ok {
  76. return gvar.New(r)
  77. } else {
  78. if len(def) > 0 {
  79. return gvar.New(def[0])
  80. }
  81. }
  82. }
  83. return gvar.New(nil)
  84. }
  85. // BuildOptions builds the options as string.
  86. func BuildOptions(m map[string]string, prefix ...string) string {
  87. options := ""
  88. leadStr := "-"
  89. if len(prefix) > 0 {
  90. leadStr = prefix[0]
  91. }
  92. for k, v := range m {
  93. if len(options) > 0 {
  94. options += " "
  95. }
  96. options += leadStr + k
  97. if v != "" {
  98. options += "=" + v
  99. }
  100. }
  101. return options
  102. }