podman

Форк
0
67 строк · 2.8 Кб
1
// Copyright 2013-2023 The Cobra Authors
2
//
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 at
6
//
7
//      http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// 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 and
13
// limitations under the License.
14

15
package cobra
16

17
import (
18
	"fmt"
19
	"os"
20
	"regexp"
21
	"strings"
22
)
23

24
const (
25
	activeHelpMarker = "_activeHelp_ "
26
	// The below values should not be changed: programs will be using them explicitly
27
	// in their user documentation, and users will be using them explicitly.
28
	activeHelpEnvVarSuffix  = "_ACTIVE_HELP"
29
	activeHelpGlobalEnvVar  = "COBRA_ACTIVE_HELP"
30
	activeHelpGlobalDisable = "0"
31
)
32

33
var activeHelpEnvVarPrefixSubstRegexp = regexp.MustCompile(`[^A-Z0-9_]`)
34

35
// AppendActiveHelp adds the specified string to the specified array to be used as ActiveHelp.
36
// Such strings will be processed by the completion script and will be shown as ActiveHelp
37
// to the user.
38
// The array parameter should be the array that will contain the completions.
39
// This function can be called multiple times before and/or after completions are added to
40
// the array.  Each time this function is called with the same array, the new
41
// ActiveHelp line will be shown below the previous ones when completion is triggered.
42
func AppendActiveHelp(compArray []string, activeHelpStr string) []string {
43
	return append(compArray, fmt.Sprintf("%s%s", activeHelpMarker, activeHelpStr))
44
}
45

46
// GetActiveHelpConfig returns the value of the ActiveHelp environment variable
47
// <PROGRAM>_ACTIVE_HELP where <PROGRAM> is the name of the root command in upper
48
// case, with all non-ASCII-alphanumeric characters replaced by `_`.
49
// It will always return "0" if the global environment variable COBRA_ACTIVE_HELP
50
// is set to "0".
51
func GetActiveHelpConfig(cmd *Command) string {
52
	activeHelpCfg := os.Getenv(activeHelpGlobalEnvVar)
53
	if activeHelpCfg != activeHelpGlobalDisable {
54
		activeHelpCfg = os.Getenv(activeHelpEnvVar(cmd.Root().Name()))
55
	}
56
	return activeHelpCfg
57
}
58

59
// activeHelpEnvVar returns the name of the program-specific ActiveHelp environment
60
// variable.  It has the format <PROGRAM>_ACTIVE_HELP where <PROGRAM> is the name of the
61
// root command in upper case, with all non-ASCII-alphanumeric characters replaced by `_`.
62
func activeHelpEnvVar(name string) string {
63
	// This format should not be changed: users will be using it explicitly.
64
	activeHelpEnvVar := strings.ToUpper(fmt.Sprintf("%s%s", name, activeHelpEnvVarSuffix))
65
	activeHelpEnvVar = activeHelpEnvVarPrefixSubstRegexp.ReplaceAllString(activeHelpEnvVar, "_")
66
	return activeHelpEnvVar
67
}
68

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.