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