gcmd.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. "os"
  11. "github.com/gogf/gf/v2/container/gvar"
  12. "github.com/gogf/gf/v2/internal/command"
  13. "github.com/gogf/gf/v2/internal/utils"
  14. "github.com/gogf/gf/v2/os/gctx"
  15. )
  16. const (
  17. CtxKeyParser gctx.StrKey = `CtxKeyParser`
  18. CtxKeyCommand gctx.StrKey = `CtxKeyCommand`
  19. CtxKeyArguments gctx.StrKey = `CtxKeyArguments`
  20. )
  21. const (
  22. helpOptionName = "help"
  23. helpOptionNameShort = "h"
  24. maxLineChars = 120
  25. tracingInstrumentName = "github.com/gogf/gf/v2/os/gcmd.Command"
  26. tagNameName = "name"
  27. tagNameShort = "short"
  28. )
  29. // Init does custom initialization.
  30. func Init(args ...string) {
  31. command.Init(args...)
  32. }
  33. // GetOpt returns the option value named `name` as gvar.Var.
  34. func GetOpt(name string, def ...string) *gvar.Var {
  35. if v := command.GetOpt(name, def...); v != "" {
  36. return gvar.New(v)
  37. }
  38. if command.ContainsOpt(name) {
  39. return gvar.New("")
  40. }
  41. return nil
  42. }
  43. // GetOptAll returns all parsed options.
  44. func GetOptAll() map[string]string {
  45. return command.GetOptAll()
  46. }
  47. // GetArg returns the argument at `index` as gvar.Var.
  48. func GetArg(index int, def ...string) *gvar.Var {
  49. if v := command.GetArg(index, def...); v != "" {
  50. return gvar.New(v)
  51. }
  52. return nil
  53. }
  54. // GetArgAll returns all parsed arguments.
  55. func GetArgAll() []string {
  56. return command.GetArgAll()
  57. }
  58. // GetOptWithEnv returns the command line argument of the specified `key`.
  59. // If the argument does not exist, then it returns the environment variable with specified `key`.
  60. // It returns the default value `def` if none of them exists.
  61. //
  62. // Fetching Rules:
  63. // 1. Command line arguments are in lowercase format, eg: gf.`package name`.<variable name>;
  64. // 2. Environment arguments are in uppercase format, eg: GF_`package name`_<variable name>;
  65. func GetOptWithEnv(key string, def ...interface{}) *gvar.Var {
  66. cmdKey := utils.FormatCmdKey(key)
  67. if command.ContainsOpt(cmdKey) {
  68. return gvar.New(GetOpt(cmdKey))
  69. } else {
  70. envKey := utils.FormatEnvKey(key)
  71. if r, ok := os.LookupEnv(envKey); ok {
  72. return gvar.New(r)
  73. } else {
  74. if len(def) > 0 {
  75. return gvar.New(def[0])
  76. }
  77. }
  78. }
  79. return nil
  80. }
  81. // BuildOptions builds the options as string.
  82. func BuildOptions(m map[string]string, prefix ...string) string {
  83. options := ""
  84. leadStr := "-"
  85. if len(prefix) > 0 {
  86. leadStr = prefix[0]
  87. }
  88. for k, v := range m {
  89. if len(options) > 0 {
  90. options += " "
  91. }
  92. options += leadStr + k
  93. if v != "" {
  94. options += "=" + v
  95. }
  96. }
  97. return options
  98. }