active_help.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright 2013-2023 The Cobra Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package cobra
  15. import (
  16. "fmt"
  17. "os"
  18. "regexp"
  19. "strings"
  20. )
  21. const (
  22. activeHelpMarker = "_activeHelp_ "
  23. // The below values should not be changed: programs will be using them explicitly
  24. // in their user documentation, and users will be using them explicitly.
  25. activeHelpEnvVarSuffix = "_ACTIVE_HELP"
  26. activeHelpGlobalEnvVar = "COBRA_ACTIVE_HELP"
  27. activeHelpGlobalDisable = "0"
  28. )
  29. var activeHelpEnvVarPrefixSubstRegexp = regexp.MustCompile(`[^A-Z0-9_]`)
  30. // AppendActiveHelp adds the specified string to the specified array to be used as ActiveHelp.
  31. // Such strings will be processed by the completion script and will be shown as ActiveHelp
  32. // to the user.
  33. // The array parameter should be the array that will contain the completions.
  34. // This function can be called multiple times before and/or after completions are added to
  35. // the array. Each time this function is called with the same array, the new
  36. // ActiveHelp line will be shown below the previous ones when completion is triggered.
  37. func AppendActiveHelp(compArray []string, activeHelpStr string) []string {
  38. return append(compArray, fmt.Sprintf("%s%s", activeHelpMarker, activeHelpStr))
  39. }
  40. // GetActiveHelpConfig returns the value of the ActiveHelp environment variable
  41. // <PROGRAM>_ACTIVE_HELP where <PROGRAM> is the name of the root command in upper
  42. // case, with all non-ASCII-alphanumeric characters replaced by `_`.
  43. // It will always return "0" if the global environment variable COBRA_ACTIVE_HELP
  44. // is set to "0".
  45. func GetActiveHelpConfig(cmd *Command) string {
  46. activeHelpCfg := os.Getenv(activeHelpGlobalEnvVar)
  47. if activeHelpCfg != activeHelpGlobalDisable {
  48. activeHelpCfg = os.Getenv(activeHelpEnvVar(cmd.Root().Name()))
  49. }
  50. return activeHelpCfg
  51. }
  52. // activeHelpEnvVar returns the name of the program-specific ActiveHelp environment
  53. // variable. It has the format <PROGRAM>_ACTIVE_HELP where <PROGRAM> is the name of the
  54. // root command in upper case, with all non-ASCII-alphanumeric characters replaced by `_`.
  55. func activeHelpEnvVar(name string) string {
  56. // This format should not be changed: users will be using it explicitly.
  57. activeHelpEnvVar := strings.ToUpper(fmt.Sprintf("%s%s", name, activeHelpEnvVarSuffix))
  58. activeHelpEnvVar = activeHelpEnvVarPrefixSubstRegexp.ReplaceAllString(activeHelpEnvVar, "_")
  59. return activeHelpEnvVar
  60. }