diff --git a/active_help.go b/active_help.go new file mode 100644 index 000000000..b3efa4875 --- /dev/null +++ b/active_help.go @@ -0,0 +1,52 @@ +package cobra + +import ( + "fmt" + "os" + "strings" +) + +const ( + activeHelpMarker = "_activeHelp_ " + // The below values should not be changed: programs will be using them explicitly + // in their user documentation, and users will be using them explicitly. + activeHelpEnvVarSuffix = "_ACTIVE_HELP" + activeHelpGlobalEnvVar = "COBRA_ACTIVE_HELP" + activeHelpGlobalDisable = "0" +) + +// 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 +// to the user. +// The array parameter should be the array that will contain the completions. +// This function can be called multiple times before and/or after completions are added to +// the array. Each time this function is called with the same array, the new +// ActiveHelp line will be shown below the previous ones when completion is triggered. +func AppendActiveHelp(compArray []string, activeHelpStr string) []string { + return append(compArray, fmt.Sprintf("%s%s", activeHelpMarker, activeHelpStr)) +} + +// activeHelpEnvVar returns the name of the program-specific ActiveHelp environment +// variable. It has the format _ACTIVE_HELP where is the name of the +// root command in upper case, with all - replaced by _. +// This format should not be changed: users will be using it explicitly. +func activeHelpEnvVar(name string) string { + activeHelpEnvVar := strings.ToUpper(fmt.Sprintf("%s%s", name, activeHelpEnvVarSuffix)) + return strings.ReplaceAll(activeHelpEnvVar, "-", "_") +} + +// setActiveHelpConfig first checks the global environment variable +// of ActiveHelp to see if it is disabling active help, and if it is not, +// it then looks to the program-specific variable. +// It then sets the ActiveHelpConfig value to make it available when +// calling the completion function. We also set it on the root, +// just in case users try to access it from there. +func setActiveHelpConfig(cmd *Command) { + activeHelpCfg := os.Getenv(activeHelpGlobalEnvVar) + if activeHelpCfg != activeHelpGlobalDisable { + activeHelpCfg = os.Getenv(activeHelpEnvVar(cmd.Root().Name())) + } + + cmd.ActiveHelpConfig = activeHelpCfg + cmd.Root().ActiveHelpConfig = cmd.ActiveHelpConfig +} diff --git a/active_help.md b/active_help.md index 4c68a0d00..67c2df42e 100644 --- a/active_help.md +++ b/active_help.md @@ -173,4 +173,4 @@ Completion ended with directive: ShellCompDirectiveDefault If a user wants to disable Active Help for every single program based on Cobra, the global environment variable `COBRA_ACTIVE_HELP` can be used as follows: ``` export COBRA_ACTIVE_HELP=0 -``` \ No newline at end of file +``` diff --git a/completions.go b/completions.go index ddc049d4b..6997e724e 100644 --- a/completions.go +++ b/completions.go @@ -81,11 +81,6 @@ const ( compCmdNoDescFlagName = "no-descriptions" compCmdNoDescFlagDesc = "disable completion descriptions" compCmdNoDescFlagDefault = false - - activeHelpMarker = "_activeHelp_ " - activeHelpEnvVarSuffix = "_ACTIVE_HELP" - activeHelpGlobalEnvVar = "COBRA_ACTIVE_HELP" - activeHelpGlobalDisable = "0" ) // CompletionOptions are the options to control shell completion @@ -100,11 +95,6 @@ type CompletionOptions struct { DisableDescriptions bool } -func activeHelpEnvVar(name string) string { - activeHelpEnvVar := strings.ToUpper(fmt.Sprintf("%s%s", name, activeHelpEnvVarSuffix)) - return strings.ReplaceAll(activeHelpEnvVar, "-", "_") -} - // NoFileCompletions can be used to disable file completion for commands that should // not trigger file completions. func NoFileCompletions(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) { @@ -127,17 +117,6 @@ func (c *Command) RegisterFlagCompletionFunc(flagName string, f func(cmd *Comman return nil } -// 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 -// to the user. -// The array parameter should be the array that will contain the completions. -// This function can be called multiple times before and/or after completions are added to -// the array. Each time this function is called with the same array, the new -// ActiveHelp line will be shown below the previous ones when completion is triggered. -func AppendActiveHelp(compArray []string, activeHelpStr string) []string { - return append(compArray, fmt.Sprintf("%s%s", activeHelpMarker, activeHelpStr)) -} - // Returns a string listing the different directive enabled in the specified parameter func (d ShellCompDirective) string() string { var directives []string @@ -445,18 +424,7 @@ func (c *Command) getCompletions(args []string) (*Command, []string, ShellCompDi // Call the registered completion function to get the completions. var comps []string - // First set the ActiveHelpConfig value to make it available when - // calling the completion function. We also set it on the root, - // just in case users try to access it from there. - // First check the global environment variable to see if it is - // disabling active help, and if it is not, use the program-specific var. - activeHelpVar := os.Getenv(activeHelpGlobalEnvVar) - if activeHelpVar != activeHelpGlobalDisable { - activeHelpVar = os.Getenv(activeHelpEnvVar(c.Root().Name())) - } - - finalCmd.ActiveHelpConfig = activeHelpVar - finalCmd.Root().ActiveHelpConfig = finalCmd.ActiveHelpConfig + setActiveHelpConfig(finalCmd) comps, directive = completionFn(finalCmd, finalArgs, toComplete) completions = append(completions, comps...)