Skip to content

Commit 719ff59

Browse files
committed
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.
1 parent 4dd4b25 commit 719ff59

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

active_help.go

Lines changed: 7 additions & 3 deletions
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
}

active_help.md

Lines changed: 1 addition & 1 deletion
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

0 commit comments

Comments
 (0)