cli

package
v0.0.0-...-adf7d95 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 12, 2026 License: Apache-2.0 Imports: 12 Imported by: 1

Documentation

Index

Constants

View Source
const (
	CommandGroupIDAnnotationName       = "group-id"
	CommandGroupTitleAnnotationName    = "group-title"
	CommandGroupPriorityAnnotationName = "group-priority"
)
View Source
const (
	FlagGroupIDAnnotationName       = "group-id"
	FlagGroupTitleAnnotationName    = "group-title"
	FlagGroupPriorityAnnotationName = "group-priority"
)
View Source
const CommandPriorityAnnotationName = "command-priority"

Variables

View Source
var (
	FlagEnvVarsPrefix string
)

Functions

func AddFlag

func AddFlag[T any](cmd *cobra.Command, dest *T, name string, defaultValue T, help string, opts AddFlagOptions) error

Create and bind a flag to the Cobra command. Corresponding environment variables (if enabled) parsed and the value is assigned to the flag immediately. Flag value type inferred from destination arg.

func FindUndefinedFlagEnvVarsInEnviron

func FindUndefinedFlagEnvVarsInEnviron() []string

Get a full list of environment variables that have FlagEnvVarsPrefix as a prefix but were not defined with AddFlag function.

func GetDefinedFlagEnvVarRegexes

func GetDefinedFlagEnvVarRegexes() map[FlagRegexExpr]*regexp.Regexp

func NewGroupCommand

func NewGroupCommand(ctx context.Context, use, short, long string, group *CommandGroup, options GroupCommandOptions) *cobra.Command

func NewRootCommand

func NewRootCommand(ctx context.Context, use, long string) *cobra.Command

func NewSubCommand

func NewSubCommand(ctx context.Context, use, short, long string, priority int, group *CommandGroup, options SubCommandOptions, runE func(cmd *cobra.Command, args []string) error) *cobra.Command

func SetSubCommandAnnotations

func SetSubCommandAnnotations(cmd *cobra.Command, priority int, group *CommandGroup)

Types

type AddFlagOptions

type AddFlagOptions struct {
	// This function must return a slice of regexps to be matched agains all environment variables.
	// Values of matched environment variables will become the flag value. Values priority (from
	// lowest to highest): flag default value -> environment variable value (from first to last
	// regexp; if regexp matches multiple env vars then the last has higher priority) -> cli flag
	// value. For slice and map-type flags: all env vars values and all cli flags values are joined.
	GetEnvVarRegexesFunc GetFlagEnvVarRegexesInterface

	// Group info is saved in Flag annotations, which can be used later, e.g. for grouping flags in
	// the --help output.
	Group *FlagGroup

	Type            FlagType
	ShortName       string
	Deprecated      bool
	Hidden          bool
	Required        bool
	NoSplitOnCommas bool
}

type CommandGroup

type CommandGroup struct {
	// Unique group identifier.
	ID string
	// Human-readable group title, expected to be used in the usage output.
	Title string
	// CommandGroup priority, expected to be used to sort groups in the usage output.
	Priority int
}

func NewCommandGroup

func NewCommandGroup(id, title string, priority int) *CommandGroup

type FlagGroup

type FlagGroup struct {
	// Unique group identifier.
	ID string
	// Human-readable group title, expected to be used in the usage output.
	Title string
	// FlagGroup priority, expected to be used to sort groups in the usage output.
	Priority int
}

func NewFlagGroup

func NewFlagGroup(id, title string, priority int) *FlagGroup

type FlagRegexExpr

type FlagRegexExpr struct {
	Expr  string
	Human string
}

func GetFlagGlobalAndLocalEnvVarRegexes

func GetFlagGlobalAndLocalEnvVarRegexes(cmd *cobra.Command, flagName string) ([]*FlagRegexExpr, error)

Return env var regexps in the form of "^NELM_AUTO_ROLLBACK$" and "^NELM_RELEASE_DEPLOY_AUTO_ROLLBACK$". The latter has higher priority. The format is "^<prefix><flag_name>$" and "^<prefix><command_path>_<flag_name>$".

func GetFlagGlobalAndLocalMultiEnvVarRegexes

func GetFlagGlobalAndLocalMultiEnvVarRegexes(cmd *cobra.Command, flagName string) ([]*FlagRegexExpr, error)

Return env var regexps in the form of "^NELM_LABELS_.+" and "^NELM_RELEASE_DEPLOY_LABELS_.+". // The format is "^<prefix><flag_name>_.+" and "^<prefix><command_path>_<flag_name>_.+".

func GetFlagGlobalEnvVarRegexes

func GetFlagGlobalEnvVarRegexes(cmd *cobra.Command, flagName string) ([]*FlagRegexExpr, error)

Return env var regexp in the form of "^NELM_AUTO_ROLLBACK$". The format is "^<prefix><flag_name>$".

func GetFlagGlobalMultiEnvVarRegexes

func GetFlagGlobalMultiEnvVarRegexes(cmd *cobra.Command, flagName string) ([]*FlagRegexExpr, error)

Return env var regexp in the form of "^NELM_LABELS_.+". The format is "^<prefix><flag_name>_.+".

func GetFlagLocalEnvVarRegexes

func GetFlagLocalEnvVarRegexes(cmd *cobra.Command, flagName string) ([]*FlagRegexExpr, error)

Return env var regexp in the form of "^NELM_RELEASE_DEPLOY_AUTO_ROLLBACK$". The format is "^<prefix><command_path>_<flag_name>$".

func GetFlagLocalMultiEnvVarRegexes

func GetFlagLocalMultiEnvVarRegexes(cmd *cobra.Command, flagName string) ([]*FlagRegexExpr, error)

Return env var regexp in the form of "^NELM_RELEASE_DEPLOY_LABELS_.+". The format is "^<prefix><command_path>_<flag_name>_.+".

func NewFlagRegexExpr

func NewFlagRegexExpr(expr, human string) *FlagRegexExpr

type FlagType

type FlagType string
const (
	FlagTypeDir  FlagType = "dir"
	FlagTypeFile FlagType = "file"
)

type GetFlagEnvVarRegexesInterface

type GetFlagEnvVarRegexesInterface func(cmd *cobra.Command, flagName string) ([]*FlagRegexExpr, error)

type GroupCommandOptions

type GroupCommandOptions struct{}

type SubCommandOptions

type SubCommandOptions struct {
	Args              cobra.PositionalArgs
	ValidArgsFunction func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective)
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL