Skip to content

Commit 22953d8

Browse files
authored
Replace all non-alphanumerics in active help env var program prefix (#1940)
* Replace all non-alphanumerics in active help env var program prefix There are other characters besides the dash that are fine in program names, but are problematic in environment variable names. These include (but are not limited to) period, space, and non-ASCII letters. * Another change in docs to mention non-ASCII-alphanumeric instead of just dash
1 parent 00b68a1 commit 22953d8

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

active_help.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package cobra
1717
import (
1818
"fmt"
1919
"os"
20+
"regexp"
2021
"strings"
2122
)
2223

@@ -29,6 +30,8 @@ const (
2930
activeHelpGlobalDisable = "0"
3031
)
3132

33+
var activeHelpEnvVarPrefixSubstRegexp = regexp.MustCompile(`[^A-Z0-9_]`)
34+
3235
// AppendActiveHelp adds the specified string to the specified array to be used as ActiveHelp.
3336
// Such strings will be processed by the completion script and will be shown as ActiveHelp
3437
// to the user.
@@ -42,7 +45,7 @@ func AppendActiveHelp(compArray []string, activeHelpStr string) []string {
4245

4346
// GetActiveHelpConfig returns the value of the ActiveHelp environment variable
4447
// <PROGRAM>_ACTIVE_HELP where <PROGRAM> is the name of the root command in upper
45-
// case, with all - replaced by _.
48+
// case, with all non-ASCII-alphanumeric characters replaced by `_`.
4649
// It will always return "0" if the global environment variable COBRA_ACTIVE_HELP
4750
// is set to "0".
4851
func GetActiveHelpConfig(cmd *Command) string {
@@ -55,9 +58,10 @@ func GetActiveHelpConfig(cmd *Command) string {
5558

5659
// activeHelpEnvVar returns the name of the program-specific ActiveHelp environment
5760
// variable. It has the format <PROGRAM>_ACTIVE_HELP where <PROGRAM> is the name of the
58-
// root command in upper case, with all - replaced by _.
61+
// root command in upper case, with all non-ASCII-alphanumeric characters replaced by `_`.
5962
func activeHelpEnvVar(name string) string {
6063
// This format should not be changed: users will be using it explicitly.
6164
activeHelpEnvVar := strings.ToUpper(fmt.Sprintf("%s%s", name, activeHelpEnvVarSuffix))
62-
return strings.ReplaceAll(activeHelpEnvVar, "-", "_")
65+
activeHelpEnvVar = activeHelpEnvVarPrefixSubstRegexp.ReplaceAllString(activeHelpEnvVar, "_")
66+
return activeHelpEnvVar
6367
}

site/content/active_help.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ Allowing to configure Active Help is entirely optional; you can use Active Help
9292

9393
The way to configure Active Help is to use the program's Active Help environment
9494
variable. That variable is named `<PROGRAM>_ACTIVE_HELP` where `<PROGRAM>` is the name of your
95-
program in uppercase with any `-` replaced by an `_`. The variable should be set by the user to whatever
95+
program in uppercase with any non-ASCII-alphanumeric characters replaced by an `_`. The variable should be set by the user to whatever
9696
Active Help configuration values are supported by the program.
9797

9898
For example, say `helm` has chosen to support three levels for Active Help: `on`, `off`, `local`. Then a user
@@ -140,7 +140,7 @@ details for your users.
140140

141141
Debugging your Active Help code is done in the same way as debugging your dynamic completion code, which is with Cobra's hidden `__complete` command. Please refer to [debugging shell completion](shell_completions.md#debugging) for details.
142142

143-
When debugging with the `__complete` command, if you want to specify different Active Help configurations, you should use the active help environment variable. That variable is named `<PROGRAM>_ACTIVE_HELP` where any `-` is replaced by an `_`. For example, we can test deactivating some Active Help as shown below:
143+
When debugging with the `__complete` command, if you want to specify different Active Help configurations, you should use the active help environment variable. That variable is named `<PROGRAM>_ACTIVE_HELP` where any non-ASCII-alphanumeric characters are replaced by an `_`. For example, we can test deactivating some Active Help as shown below:
144144
```
145145
$ HELM_ACTIVE_HELP=1 bin/helm __complete install wordpress bitnami/h<ENTER>
146146
bitnami/haproxy

0 commit comments

Comments
 (0)