vendor/github.com/spf13/cobra/active_help.go
author Mikael Berthe <mikael@lilotux.net>
Tue, 23 Aug 2022 22:39:43 +0200
changeset 260 445e01aede7e
child 265 05c40b36d3b2
permissions -rw-r--r--
Update vendor directory
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
260
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     1
package cobra
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     2
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     3
import (
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     4
	"fmt"
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     5
	"os"
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     6
	"strings"
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     7
)
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     8
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     9
const (
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    10
	activeHelpMarker = "_activeHelp_ "
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    11
	// The below values should not be changed: programs will be using them explicitly
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    12
	// in their user documentation, and users will be using them explicitly.
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    13
	activeHelpEnvVarSuffix  = "_ACTIVE_HELP"
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    14
	activeHelpGlobalEnvVar  = "COBRA_ACTIVE_HELP"
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    15
	activeHelpGlobalDisable = "0"
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    16
)
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    17
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    18
// AppendActiveHelp adds the specified string to the specified array to be used as ActiveHelp.
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    19
// Such strings will be processed by the completion script and will be shown as ActiveHelp
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    20
// to the user.
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    21
// The array parameter should be the array that will contain the completions.
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    22
// This function can be called multiple times before and/or after completions are added to
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    23
// the array.  Each time this function is called with the same array, the new
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    24
// ActiveHelp line will be shown below the previous ones when completion is triggered.
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    25
func AppendActiveHelp(compArray []string, activeHelpStr string) []string {
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    26
	return append(compArray, fmt.Sprintf("%s%s", activeHelpMarker, activeHelpStr))
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    27
}
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    28
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    29
// GetActiveHelpConfig returns the value of the ActiveHelp environment variable
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    30
// <PROGRAM>_ACTIVE_HELP where <PROGRAM> is the name of the root command in upper
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    31
// case, with all - replaced by _.
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    32
// It will always return "0" if the global environment variable COBRA_ACTIVE_HELP
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    33
// is set to "0".
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    34
func GetActiveHelpConfig(cmd *Command) string {
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    35
	activeHelpCfg := os.Getenv(activeHelpGlobalEnvVar)
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    36
	if activeHelpCfg != activeHelpGlobalDisable {
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    37
		activeHelpCfg = os.Getenv(activeHelpEnvVar(cmd.Root().Name()))
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    38
	}
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    39
	return activeHelpCfg
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    40
}
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    41
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    42
// activeHelpEnvVar returns the name of the program-specific ActiveHelp environment
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    43
// variable.  It has the format <PROGRAM>_ACTIVE_HELP where <PROGRAM> is the name of the
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    44
// root command in upper case, with all - replaced by _.
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    45
func activeHelpEnvVar(name string) string {
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    46
	// This format should not be changed: users will be using it explicitly.
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    47
	activeHelpEnvVar := strings.ToUpper(fmt.Sprintf("%s%s", name, activeHelpEnvVarSuffix))
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    48
	return strings.ReplaceAll(activeHelpEnvVar, "-", "_")
445e01aede7e Update vendor directory
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    49
}