From 4474cf6b4376f7eeb684f8280e6cee4fcde9fcbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Mon, 19 Jun 2023 23:24:23 +0300 Subject: [PATCH] wip --- completions.go | 13 +++++++++---- site/content/completions/_index.md | 5 ++++- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/completions.go b/completions.go index 531a67cea3..e2d35902e9 100644 --- a/completions.go +++ b/completions.go @@ -217,7 +217,9 @@ func (c *Command) initCompleteCmd(args []string) { // 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 + out := finalCmd.OutOrStdout() for _, comp := range completions { if GetActiveHelpConfig(finalCmd) == activeHelpGlobalDisable { // Remove all activeHelp entries in this case @@ -825,7 +827,6 @@ to your powershell profile. return cmd.Root().GenPowerShellCompletion(out) } return cmd.Root().GenPowerShellCompletionWithDesc(out) - }, } if haveNoDescFlag { @@ -897,8 +898,12 @@ func CompErrorln(msg string) { CompError(fmt.Sprintf("%s\n", msg)) } -// configEnvVarGlobalPrefix should not be changed: users will be using it explicitly. -const configEnvVarGlobalPrefix = "COBRA" +// These values should not be changed: users will be using them explicitly. +const ( + configEnvVarGlobalPrefix = "COBRA" + configEnvVarSuffixDescriptions = "COMPLETION_DESCRIPTIONS" + configEnvVarDescriptionsOff = "off" +) var configEnvVarPrefixSubstRegexp = regexp.MustCompile(`[^A-Z0-9_]`) diff --git a/site/content/completions/_index.md b/site/content/completions/_index.md index 4efad2907b..6d6f4facf4 100644 --- a/site/content/completions/_index.md +++ b/site/content/completions/_index.md @@ -104,7 +104,7 @@ To tell Cobra to mark the default `completion` command as *hidden*: rootCmd.CompletionOptions.HiddenDefaultCmd = true ``` -To tell Cobra *not* to provide the user with the `--no-descriptions` flag to the completion sub-commands: +To tell Cobra *not* to provide the user with the `--no-descriptions` flag or the same functionality using `*_COMPLETION_DESCRIPTIONS` environment variables to the completion sub-commands: ``` rootCmd.CompletionOptions.DisableNoDescFlag = true ``` @@ -393,6 +393,9 @@ $ source <(helm completion bash --no-descriptions) $ helm completion [tab][tab] bash fish powershell zsh ``` + +Setting the `_COMPLETION_DESCRIPTIONS` environment variable (falling back to `COBRA_COMPLETION_DESCRIPTIONS` if empty or not set) to `off` achieves the same. `` is the name of your program with all non-ASCII-alphanumeric characters replaced by `_`. + ## Bash completions ### Dependencies