active_help.go
This file
Path
active_help.go
60 lines · 2.6 KiB · Go
Last changed by
feat: add CompletionWithDesc helper (#2231)ccoVeillebefore adoption8cb30f9
History of this fileTake it
update copyright year (#1927)Unai Martinez-Corralbefore adoption9e6b58a
line 1why, one step back1// Copyright 2013-2023 The Cobra Authorsadd missing license headers (#1809)Unai Martinez-Corralbefore adoption6d978a9
lines 2–14why, one step back2//3// Licensed under the Apache License, Version 2.0 (the "License");4// you may not use this file except in compliance with the License.5// You may obtain a copy of the License at6//7// http://www.apache.org/licenses/LICENSE-2.08//9// Unless required by applicable law or agreed to in writing, software10// distributed under the License is distributed on an "AS IS" BASIS,11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12// See the License for the specific language governing permissions and13// limitations under the License.14Add Active Help support (#1482)Marc Khouzambefore adoptionf464d6c
lines 15–25why, one step back15package cobra1617import (18 "fmt"19 "os"20)2122const (23 activeHelpMarker = "_activeHelp_ "24 // The below values should not be changed: programs will be using them explicitly25 // in their user documentation, and users will be using them explicitly.Add env variable to suppress completion descriptions on create (#1938)Ville Skyttäbefore adoptione63925d
lines 26–27why, one step back26 activeHelpEnvVarSuffix = "ACTIVE_HELP"27 activeHelpGlobalEnvVar = configEnvVarGlobalPrefix + "_" + activeHelpEnvVarSuffixAdd Active Help support (#1482)Marc Khouzambefore adoptionf464d6c
lines 28–37why, one step back28 activeHelpGlobalDisable = "0"29)3031// AppendActiveHelp adds the specified string to the specified array to be used as ActiveHelp.32// Such strings will be processed by the completion script and will be shown as ActiveHelp33// to the user.34// The array parameter should be the array that will contain the completions.35// This function can be called multiple times before and/or after completions are added to36// the array. Each time this function is called with the same array, the new37// ActiveHelp line will be shown below the previous ones when completion is triggered.feat: add CompletionWithDesc helper (#2231)ccoVeillebefore adoption8cb30f9
line 38why, one step back38func AppendActiveHelp(compArray []Completion, activeHelpStr string) []Completion {Add Active Help support (#1482)Marc Khouzambefore adoptionf464d6c
lines 39–43why, one step back39 return append(compArray, fmt.Sprintf("%s%s", activeHelpMarker, activeHelpStr))40}4142// GetActiveHelpConfig returns the value of the ActiveHelp environment variable43// <PROGRAM>_ACTIVE_HELP where <PROGRAM> is the name of the root command in upperReplace all non-alphanumerics in active help env var program prefix (#1940)Ville Skyttäbefore adoption22953d8
line 44why, one step back44// case, with all non-ASCII-alphanumeric characters replaced by `_`.Add Active Help support (#1482)Marc Khouzambefore adoptionf464d6c
lines 45–56why, one step back45// It will always return "0" if the global environment variable COBRA_ACTIVE_HELP46// is set to "0".47func GetActiveHelpConfig(cmd *Command) string {48 activeHelpCfg := os.Getenv(activeHelpGlobalEnvVar)49 if activeHelpCfg != activeHelpGlobalDisable {50 activeHelpCfg = os.Getenv(activeHelpEnvVar(cmd.Root().Name()))51 }52 return activeHelpCfg53}5455// activeHelpEnvVar returns the name of the program-specific ActiveHelp environment56// variable. It has the format <PROGRAM>_ACTIVE_HELP where <PROGRAM> is the name of theReplace all non-alphanumerics in active help env var program prefix (#1940)Ville Skyttäbefore adoption22953d8
line 57why, one step back57// root command in upper case, with all non-ASCII-alphanumeric characters replaced by `_`.Add Active Help support (#1482)Marc Khouzambefore adoptionf464d6c
line 58why, one step back58func activeHelpEnvVar(name string) string {Add env variable to suppress completion descriptions on create (#1938)Ville Skyttäbefore adoptione63925d
line 59why, one step back59 return configEnvVar(name, activeHelpEnvVarSuffix)Add Active Help support (#1482)Marc Khouzambefore adoptionf464d6c
line 60why, one step back60}