Make use of the general purpose config env var getter in active help

This commit is contained in:
Ville Skyttä 2023-10-24 23:36:49 +03:00
parent 957f4eb69c
commit 97b70019e9
3 changed files with 13 additions and 13 deletions

View file

@ -17,21 +17,17 @@ package cobra
import ( import (
"fmt" "fmt"
"os" "os"
"regexp"
"strings"
) )
const ( const (
activeHelpMarker = "_activeHelp_ " activeHelpMarker = "_activeHelp_ "
// The below values should not be changed: programs will be using them explicitly // The below values should not be changed: programs will be using them explicitly
// in their user documentation, and users will be using them explicitly. // in their user documentation, and users will be using them explicitly.
activeHelpEnvVarSuffix = "_ACTIVE_HELP" activeHelpEnvVarSuffix = "ACTIVE_HELP"
activeHelpGlobalEnvVar = "COBRA_ACTIVE_HELP" activeHelpGlobalEnvVar = configEnvVarGlobalPrefix + "_" + activeHelpEnvVarSuffix
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.
@ -60,8 +56,5 @@ func GetActiveHelpConfig(cmd *Command) string {
// 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 non-ASCII-alphanumeric characters 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. return configEnvVar(name, activeHelpEnvVarSuffix)
activeHelpEnvVar := strings.ToUpper(fmt.Sprintf("%s%s", name, activeHelpEnvVarSuffix))
activeHelpEnvVar = activeHelpEnvVarPrefixSubstRegexp.ReplaceAllString(activeHelpEnvVar, "_")
return activeHelpEnvVar
} }

View file

@ -212,7 +212,7 @@ func (c *Command) initCompleteCmd(args []string) {
// 2- Even without completions, we need to print the directive // 2- Even without completions, we need to print the directive
} }
noDescriptions := (cmd.CalledAs() == ShellCompNoDescRequestCmd) noDescriptions := cmd.CalledAs() == ShellCompNoDescRequestCmd || GetEnvConfig(cmd, configEnvVarSuffixDescriptions) == configEnvVarDescriptionsOff
noActiveHelp := GetActiveHelpConfig(finalCmd) == activeHelpGlobalDisable noActiveHelp := GetActiveHelpConfig(finalCmd) == activeHelpGlobalDisable
out := finalCmd.OutOrStdout() out := finalCmd.OutOrStdout()
for _, comp := range completions { for _, comp := range completions {
@ -901,8 +901,12 @@ func CompErrorln(msg string) {
CompError(fmt.Sprintf("%s\n", msg)) CompError(fmt.Sprintf("%s\n", msg))
} }
// configEnvVarGlobalPrefix should not be changed: users will be using it explicitly. // These values should not be changed: users will be using them explicitly.
const configEnvVarGlobalPrefix = "COBRA" const (
configEnvVarGlobalPrefix = "COBRA"
configEnvVarSuffixDescriptions = "COMPLETION_DESCRIPTIONS"
configEnvVarDescriptionsOff = "off"
)
var configEnvVarPrefixSubstRegexp = regexp.MustCompile(`[^A-Z0-9_]`) var configEnvVarPrefixSubstRegexp = regexp.MustCompile(`[^A-Z0-9_]`)

View file

@ -393,6 +393,9 @@ $ source <(helm completion bash --no-descriptions)
$ helm completion [tab][tab] $ helm completion [tab][tab]
bash fish powershell zsh bash fish powershell zsh
``` ```
Setting the `<PROGRAM>_COMPLETION_DESCRIPTIONS` environment variable (falling back to `COBRA_COMPLETION_DESCRIPTIONS` if empty or not set) to `off` achieves the same. `<PROGRAM>` is the name of your program with all non-ASCII-alphanumeric characters replaced by `_`.
## Bash completions ## Bash completions
### Dependencies ### Dependencies