active_help.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Copyright 2013-2022 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. "strings"
  19. )
  20. const (
  21. activeHelpMarker = "_activeHelp_ "
  22. // The below values should not be changed: programs will be using them explicitly
  23. // in their user documentation, and users will be using them explicitly.
  24. activeHelpEnvVarSuffix = "_ACTIVE_HELP"
  25. activeHelpGlobalEnvVar = "COBRA_ACTIVE_HELP"
  26. activeHelpGlobalDisable = "0"
  27. )
  28. // AppendActiveHelp adds the specified string to the specified array to be used as ActiveHelp.
  29. // Such strings will be processed by the completion script and will be shown as ActiveHelp
  30. // to the user.
  31. // The array parameter should be the array that will contain the completions.
  32. // This function can be called multiple times before and/or after completions are added to
  33. // the array. Each time this function is called with the same array, the new
  34. // ActiveHelp line will be shown below the previous ones when completion is triggered.
  35. func AppendActiveHelp(compArray []string, activeHelpStr string) []string {
  36. return append(compArray, fmt.Sprintf("%s%s", activeHelpMarker, activeHelpStr))
  37. }
  38. // GetActiveHelpConfig returns the value of the ActiveHelp environment variable
  39. // <PROGRAM>_ACTIVE_HELP where <PROGRAM> is the name of the root command in upper
  40. // case, with all - replaced by _.
  41. // It will always return "0" if the global environment variable COBRA_ACTIVE_HELP
  42. // is set to "0".
  43. func GetActiveHelpConfig(cmd *Command) string {
  44. activeHelpCfg := os.Getenv(activeHelpGlobalEnvVar)
  45. if activeHelpCfg != activeHelpGlobalDisable {
  46. activeHelpCfg = os.Getenv(activeHelpEnvVar(cmd.Root().Name()))
  47. }
  48. return activeHelpCfg
  49. }
  50. // activeHelpEnvVar returns the name of the program-specific ActiveHelp environment
  51. // variable. It has the format <PROGRAM>_ACTIVE_HELP where <PROGRAM> is the name of the
  52. // root command in upper case, with all - replaced by _.
  53. func activeHelpEnvVar(name string) string {
  54. // This format should not be changed: users will be using it explicitly.
  55. activeHelpEnvVar := strings.ToUpper(fmt.Sprintf("%s%s", name, activeHelpEnvVarSuffix))
  56. return strings.ReplaceAll(activeHelpEnvVar, "-", "_")
  57. }