mirror of
https://github.com/spf13/cobra
synced 2025-04-27 17:17:20 +00:00
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.
This commit is contained in:
parent
4cafa37bc4
commit
a502bd1711
2 changed files with 8 additions and 4 deletions
|
@ -17,6 +17,7 @@ package cobra
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -29,6 +30,8 @@ const (
|
||||||
activeHelpGlobalDisable = "0"
|
activeHelpGlobalDisable = "0"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var activeHelpEnvVarPrefixSubstRegexp = regexp.MustCompile(`[^A-Z0-9_]`)
|
||||||
|
|
||||||
// AppendActiveHelp adds the specified string to the specified array to be used as ActiveHelp.
|
// AppendActiveHelp adds the specified string to the specified array to be used as ActiveHelp.
|
||||||
// Such strings will be processed by the completion script and will be shown as ActiveHelp
|
// Such strings will be processed by the completion script and will be shown as ActiveHelp
|
||||||
// to the user.
|
// to the user.
|
||||||
|
@ -42,7 +45,7 @@ func AppendActiveHelp(compArray []string, activeHelpStr string) []string {
|
||||||
|
|
||||||
// GetActiveHelpConfig returns the value of the ActiveHelp environment variable
|
// GetActiveHelpConfig returns the value of the ActiveHelp environment variable
|
||||||
// <PROGRAM>_ACTIVE_HELP where <PROGRAM> is the name of the root command in upper
|
// <PROGRAM>_ACTIVE_HELP where <PROGRAM> is the name of the root command in upper
|
||||||
// case, with all - replaced by _.
|
// case, with all non-ASCII-alphanumeric characters replaced by `_`.
|
||||||
// It will always return "0" if the global environment variable COBRA_ACTIVE_HELP
|
// It will always return "0" if the global environment variable COBRA_ACTIVE_HELP
|
||||||
// is set to "0".
|
// is set to "0".
|
||||||
func GetActiveHelpConfig(cmd *Command) string {
|
func GetActiveHelpConfig(cmd *Command) string {
|
||||||
|
@ -55,9 +58,10 @@ func GetActiveHelpConfig(cmd *Command) string {
|
||||||
|
|
||||||
// activeHelpEnvVar returns the name of the program-specific ActiveHelp environment
|
// activeHelpEnvVar returns the name of the program-specific ActiveHelp environment
|
||||||
// variable. It has the format <PROGRAM>_ACTIVE_HELP where <PROGRAM> is the name of the
|
// variable. It has the format <PROGRAM>_ACTIVE_HELP where <PROGRAM> is the name of the
|
||||||
// root command in upper case, with all - replaced by _.
|
// root command in upper case, with all non-ASCII-alphanumeric characters replaced by `_`.
|
||||||
func activeHelpEnvVar(name string) string {
|
func activeHelpEnvVar(name string) string {
|
||||||
// This format should not be changed: users will be using it explicitly.
|
// This format should not be changed: users will be using it explicitly.
|
||||||
activeHelpEnvVar := strings.ToUpper(fmt.Sprintf("%s%s", name, activeHelpEnvVarSuffix))
|
activeHelpEnvVar := strings.ToUpper(fmt.Sprintf("%s%s", name, activeHelpEnvVarSuffix))
|
||||||
return strings.ReplaceAll(activeHelpEnvVar, "-", "_")
|
activeHelpEnvVar = activeHelpEnvVarPrefixSubstRegexp.ReplaceAllString(activeHelpEnvVar, "_")
|
||||||
|
return activeHelpEnvVar
|
||||||
}
|
}
|
||||||
|
|
|
@ -92,7 +92,7 @@ Allowing to configure Active Help is entirely optional; you can use Active Help
|
||||||
|
|
||||||
The way to configure Active Help is to use the program's Active Help environment
|
The way to configure Active Help is to use the program's Active Help environment
|
||||||
variable. That variable is named `<PROGRAM>_ACTIVE_HELP` where `<PROGRAM>` is the name of your
|
variable. That variable is named `<PROGRAM>_ACTIVE_HELP` where `<PROGRAM>` is the name of your
|
||||||
program in uppercase with any `-` replaced by an `_`. The variable should be set by the user to whatever
|
program in uppercase with any non-ASCII-alphanumeric characters replaced by an `_`. The variable should be set by the user to whatever
|
||||||
Active Help configuration values are supported by the program.
|
Active Help configuration values are supported by the program.
|
||||||
|
|
||||||
For example, say `helm` has chosen to support three levels for Active Help: `on`, `off`, `local`. Then a user
|
For example, say `helm` has chosen to support three levels for Active Help: `on`, `off`, `local`. Then a user
|
||||||
|
|
Loading…
Add table
Reference in a new issue