Path: blob/main/vendor/github.com/spf13/cobra/active_help.go
2875 views
// Copyright 2013-2023 The Cobra Authors1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314package cobra1516import (17"fmt"18"os"19)2021const (22activeHelpMarker = "_activeHelp_ "23// The below values should not be changed: programs will be using them explicitly24// in their user documentation, and users will be using them explicitly.25activeHelpEnvVarSuffix = "ACTIVE_HELP"26activeHelpGlobalEnvVar = configEnvVarGlobalPrefix + "_" + activeHelpEnvVarSuffix27activeHelpGlobalDisable = "0"28)2930// AppendActiveHelp adds the specified string to the specified array to be used as ActiveHelp.31// Such strings will be processed by the completion script and will be shown as ActiveHelp32// to the user.33// The array parameter should be the array that will contain the completions.34// This function can be called multiple times before and/or after completions are added to35// the array. Each time this function is called with the same array, the new36// ActiveHelp line will be shown below the previous ones when completion is triggered.37func AppendActiveHelp(compArray []Completion, activeHelpStr string) []Completion {38return append(compArray, fmt.Sprintf("%s%s", activeHelpMarker, activeHelpStr))39}4041// GetActiveHelpConfig returns the value of the ActiveHelp environment variable42// <PROGRAM>_ACTIVE_HELP where <PROGRAM> is the name of the root command in upper43// case, with all non-ASCII-alphanumeric characters replaced by `_`.44// It will always return "0" if the global environment variable COBRA_ACTIVE_HELP45// is set to "0".46func GetActiveHelpConfig(cmd *Command) string {47activeHelpCfg := os.Getenv(activeHelpGlobalEnvVar)48if activeHelpCfg != activeHelpGlobalDisable {49activeHelpCfg = os.Getenv(activeHelpEnvVar(cmd.Root().Name()))50}51return activeHelpCfg52}5354// activeHelpEnvVar returns the name of the program-specific ActiveHelp environment55// variable. It has the format <PROGRAM>_ACTIVE_HELP where <PROGRAM> is the name of the56// root command in upper case, with all non-ASCII-alphanumeric characters replaced by `_`.57func activeHelpEnvVar(name string) string {58return configEnvVar(name, activeHelpEnvVarSuffix)59}606162