gcmd.go 2.7 KB

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