cli

package
v0.73.0-alpha2025020401 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2025 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

Package cli provides functionality for the Terragrunt CLI.

Index

Constants

View Source
const (
	BuiltinCmdSep = "--"
)

Variables

View Source
var (
	// AppVersionTemplate is the text template for the Default version topic.
	AppVersionTemplate = ""

	// AppHelpTemplate is the text template for the Default help topic.
	AppHelpTemplate = ""

	// CommandHelpTemplate is the text template for the command help topic.
	CommandHelpTemplate = ""
)
View Source
var (
	MapFlagEnvVarSep = ","
	MapFlagKeyValSep = "="
)
View Source
var DefaultComplete = defaultComplete
View Source
var (
	// FlagSplitter uses to separate arguments and env vars with multiple values.
	FlagSplitter = strings.Split
)
View Source
var FlagStringer = cli.FlagStringer //nolint:gochecknoglobals

FlagStringer converts a flag definition to a string. This is used by help to display a flag.

View Source
var (
	SliceFlagEnvVarSep = ","
)

Functions

func ApplyFlag

func ApplyFlag(flag Flag, set *libflag.FlagSet) error

func LexicographicLess

func LexicographicLess(i, j string) bool

LexicographicLess compares strings alphabetically considering case.

func ShowAppHelp

func ShowAppHelp(ctx *Context) error

ShowAppHelp prints App help.

func ShowCommandHelp

func ShowCommandHelp(ctx *Context) error

ShowCommandHelp prints command help for the given `ctx`.

func ShowCompletions

func ShowCompletions(ctx *Context) error

ShowCompletions prints the lists of commands within a given context

func ShowVersion

func ShowVersion(ctx *Context) error

Types

type ActionFunc

type ActionFunc func(ctx *Context) error

ActionFunc is the action to execute when no commands/subcommands are specified.

type App

type App struct {
	*cli.App
	// Examples is list of examples of using the App in the help.
	Examples []string
	// List of commands to execute
	Commands Commands
	// List of flags to parse
	Flags Flags
	// CustomAppVersionTemplate text template for app version topic.
	CustomAppVersionTemplate string
	// Contributor
	Author string
	// The function to call when checking for command completions
	Complete CompleteFunc
	// An action to execute before any subcommands are run, but after the context is ready
	// If a non-nil error is returned, no subcommands are run
	Before ActionFunc
	// An action to execute after any subcommands are run, but after the subcommand has finished
	After ActionFunc
	// The action to execute when no subcommands are specified
	Action ActionFunc
	// OsExiter is the function used when the app exits. If not set defaults to os.Exit.
	OsExiter func(code int)

	// ExitErrHandler processes any error encountered while running an App before
	// it is returned to the caller. If no function is provided, HandleExitCoder
	// is used as the default behavior.
	ExitErrHandler ExitErrHandlerFunc

	// Autocomplete enables or disables subcommand auto-completion support.
	// This is enabled by default when NewApp is called. Otherwise, this
	// must enabled explicitly.
	//
	// Autocomplete requires the "Name" option to be set on CLI. This name
	// should be set exactly to the binary name that is autocompleted.
	Autocomplete bool

	// AutocompleteInstallFlag and AutocompleteUninstallFlag are the global flag
	// names for installing and uninstalling the autocompletion handlers
	// for the user's shell. The flag should omit the hyphen(s) in front of
	// the value. Both single and double hyphens will automatically be supported
	// for the flag name. These default to `autocomplete-install` and
	// `autocomplete-uninstall` respectively.
	AutocompleteInstallFlag   string
	AutocompleteUninstallFlag string

	// Autocompletion is supported via the github.com/posener/complete
	// library. This library supports bash, zsh and fish. To add support
	// for other shells, please see that library.
	AutocompleteInstaller AutocompleteInstaller
}

App is a wrapper for `urfave`'s `cli.App` struct. It should be created with the cli.NewApp() function. The main purpose of this wrapper is to parse commands and flags in the way we need, namely, if during parsing we find undefined commands or flags, instead of returning an error, we consider them as arguments, regardless of their position among the others registered commands and flags.

For example, CLI command: `terragrunt run-all apply --terragrunt-log-level trace --auto-approve --terragrunt-non-interactive` The `App` will runs the registered command `run-all`, define the registered flags `--terragrunt-log-level`, `--terragrunt-non-interactive`, and define args `apply --auto-approve` which can be obtained from the App context, ctx.Args().Slice()

func NewApp

func NewApp() *App

NewApp returns app new App instance.

func (*App) AddCommands

func (app *App) AddCommands(cmds ...*Command)

AddCommands adds new commands.

func (*App) AddFlags

func (app *App) AddFlags(flags ...Flag)

AddFlags adds new flags.

func (*App) Run

func (app *App) Run(arguments []string) error

Run is the entry point to the cli app. Parses the arguments slice and routes to the proper flag/args combination.

func (*App) RunContext

func (app *App) RunContext(ctx context.Context, arguments []string) (err error)

RunContext is like Run except it takes a Context that will be passed to its commands and sub-commands. Through this, you can propagate timeouts and cancellation requests

func (*App) VisibleCommands

func (app *App) VisibleCommands() []*cli.Command

VisibleCommands returns a slice of the Commands used for help.

func (*App) VisibleFlags

func (app *App) VisibleFlags() Flags

VisibleFlags returns a slice of the Flags used for help.

type Args

type Args []string

Args provides convenient access to CLI arguments.

func (Args) CommandName

func (args Args) CommandName() string

CommandName returns the first arg that starts without a dash `-`, otherwise that means the args do not consist any command and an empty string is returned.

func (Args) CommandNameN

func (args Args) CommandNameN(n int) string

CommandNameN returns the nth argument from `args` that starts without a dash `-`.

func (Args) Contains

func (args Args) Contains(target string) bool

Contains returns true if args contains the given `target` arg.

func (Args) First

func (args Args) First() string

First returns the first argument or a blank string

func (Args) Get

func (args Args) Get(n int) string

Get returns the nth argument, or else a blank string

func (Args) Last

func (args Args) Last() string

Last returns the last argument or a blank string.

func (Args) Len

func (args Args) Len() int

Len returns the length of the wrapped slice

func (Args) Normalize

func (args Args) Normalize(acts ...NormalizeActsType) Args

Normalize formats the arguments according to the given actions. if the given act is:

`SingleDashFlag` - converts all arguments containing double dashes to single dashes
`DoubleDashFlag` - converts all arguments containing single dashes to double dashes

func (Args) Present

func (args Args) Present() bool

Present checks if there are any arguments present

func (Args) Remove

func (args Args) Remove(name string) Args

Remove returns `args` with the `name` element removed.

func (Args) Second

func (args Args) Second() string

Second returns the first argument or a blank string.

func (Args) Slice

func (args Args) Slice() []string

Slice returns a copy of the internal slice

func (Args) Split

func (args Args) Split(sep string) (Args, Args)

Split splits `args` into two slices separated by `sep`.

func (Args) SubCommandName

func (args Args) SubCommandName() string

SubCommandName returns the second arg that starts without a dash `-`, otherwise that means the args do not consist a subcommand and an empty string is returned.

func (Args) Tail

func (args Args) Tail() Args

Tail returns the rest of the arguments (not the first one) or else an empty string slice.

func (Args) WithoutBuiltinCmdSep

func (args Args) WithoutBuiltinCmdSep() Args

type AutocompleteInstaller

type AutocompleteInstaller interface {
	Install(string) error
	Uninstall(string) error
}

AutocompleteInstaller is an interface to be implemented to perform the autocomplete installation and uninstallation with a CLI.

This interface is not exported because it only exists for unit tests to be able to test that the installation is called properly.

type BoolFlag

type BoolFlag struct {

	// The name of the flag.
	Name string
	// The default value of the flag to display in the help, if it is empty, the value is taken from `Destination`.
	DefaultText string
	// A short usage description to display in help.
	Usage string
	// Aliases are usually used for the short flag name, like `-h`.
	Aliases []string
	// The names of the env variables that are parsed and assigned to `Destination` before the flag value.
	EnvVars []string
	// Action is a function that is called when the flag is specified. It is executed only after all command flags have been parsed.
	Action FlagActionFunc[bool]
	// FlagSetterFunc represents function type that is called when the flag is specified.
	// Executed during value parsing, in case of an error the returned error is wrapped with the flag or environment variable name.
	Setter FlagSetterFunc[bool]
	// Destination ia a pointer to which the value of the flag or env var is assigned.
	// It also uses as the default value displayed in the help.
	Destination *bool
	// If set to true, then the assigned flag value will be inverted
	Negative bool
	// Hidden hides the flag from the help, if set to true.
	Hidden bool
	// contains filtered or unexported fields
}

func (*BoolFlag) Apply

func (flag *BoolFlag) Apply(set *libflag.FlagSet) error

Apply applies Flag settings to the given flag set.

func (*BoolFlag) GetCategory

func (flag *BoolFlag) GetCategory() string

GetCategory returns the category for the flag. Implements `cli.DocGenerationFlag.GetCategory` required to generate help.

func (*BoolFlag) GetDefaultText

func (flag *BoolFlag) GetDefaultText() string

GetDefaultText returns the flags value as string representation and an empty string if the flag takes no value at all.

func (*BoolFlag) GetEnvVars

func (flag *BoolFlag) GetEnvVars() []string

GetEnvVars implements `cli.Flag` interface.

func (*BoolFlag) GetHidden

func (flag *BoolFlag) GetHidden() bool

GetHidden returns true if the flag should be hidden from the help.

func (*BoolFlag) GetUsage

func (flag *BoolFlag) GetUsage() string

GetUsage returns the usage string for the flag.

func (*BoolFlag) GetValue

func (flag *BoolFlag) GetValue() string

GetValue returns the flags value as string representation and an empty string if the flag takes no value at all. Implements `cli.DocGenerationFlag.GetValue` required to generate help.

func (*BoolFlag) LookupEnv

func (flag *BoolFlag) LookupEnv(envVar string) []string

func (*BoolFlag) Names

func (flag *BoolFlag) Names() []string

Names returns the names of the flag.

func (*BoolFlag) RunAction

func (flag *BoolFlag) RunAction(ctx *Context) error

RunAction implements ActionableFlag.RunAction

func (*BoolFlag) SplitValue

func (flag *BoolFlag) SplitValue(val string) []string

func (*BoolFlag) String

func (flag *BoolFlag) String() string

String returns a readable representation of this value (for usage defaults).

func (*BoolFlag) TakesValue

func (flag *BoolFlag) TakesValue() bool

TakesValue returns true of the flag takes a value, otherwise false. Implements `cli.DocGenerationFlag.TakesValue` required to generate help.

func (*BoolFlag) Value

func (flag *BoolFlag) Value() FlagValue

type Command

type Command struct {
	// Name is the command name.
	Name string
	// Aliases is a list of aliases for the command.
	Aliases []string
	// Usage is a short description of the usage of the command.
	Usage string
	// UsageText is custom text to show on the `Usage` section of help.
	UsageText string
	// Description is a longer explanation of how the command works.
	Description string
	// Examples is list of examples of using the command in the help.
	Examples []string
	// Flags is list of flags to parse.
	Flags Flags
	// ErrorOnUndefinedFlag causes the application to exit and return an error on any undefined flag.
	ErrorOnUndefinedFlag bool
	// Full name of cmd for help, defaults to full cmd name, including parent commands.
	HelpName string
	// if this is a root "special" cmd
	IsRoot bool
	// Boolean to hide this cmd from help
	Hidden bool
	// CustomHelpTemplate the text template for the cmd help topic.
	// cli.go uses text/template to render templates. You can
	// render custom help text by setting this variable.
	CustomHelpTemplate string
	// CustomHelp is a func that is executed when help for a command needs to be displayed.
	CustomHelp HelpFunc
	// List of child commands
	Subcommands Commands
	// Treat all flags as normal arguments if true
	SkipFlagParsing bool
	// Boolean to disable the parsing command, but it will still be shown in the help.
	SkipRunning bool
	// The function to call when checking for command completions
	Complete CompleteFunc
	// An action to execute before any subcommands are run, but after the context is ready
	// If a non-nil error is returned, no subcommands are run
	Before ActionFunc
	// An action to execute after any subcommands are run, but after the subcommand has finished
	After ActionFunc
	// The action to execute when no subcommands are specified
	Action ActionFunc
}

func (*Command) HasName

func (cmd *Command) HasName(name string) bool

HasName returns true if Command.Name matches given name

func (*Command) Names

func (cmd *Command) Names() []string

Names returns the names including short names and aliases.

func (*Command) Run

func (cmd *Command) Run(ctx *Context, args Args) (err error)

Run parses the given args for the presence of flags as well as subcommands. If this is the final command, starts its execution.

func (*Command) Subcommand

func (cmd *Command) Subcommand(name string) *Command

Subcommand returns a subcommand that matches the given name.

func (*Command) VisibleFlags

func (cmd *Command) VisibleFlags() Flags

VisibleFlags returns a slice of the Flags, used by `urfave/cli` package to generate help.

func (*Command) VisibleSubcommands

func (cmd *Command) VisibleSubcommands() []*cli.Command

VisibleSubcommands returns a slice of the Commands with Hidden=false. Used by `urfave/cli` package to generate help.

func (*Command) WrapAction

func (cmd *Command) WrapAction(fn func(ctx *Context, action ActionFunc) error) *Command

type Commands

type Commands []*Command

func (*Commands) Add

func (commands *Commands) Add(cmd *Command)

Add adds a new cmd to the list.

func (Commands) Filter

func (commands Commands) Filter(names []string) Commands

Filter returns a list of commands filtered by the given names.

func (Commands) Get

func (commands Commands) Get(name string) *Command

Get returns a Command by the given name.

func (Commands) Len

func (commands Commands) Len() int

func (Commands) Less

func (commands Commands) Less(i, j int) bool

func (Commands) SkipRunning

func (commands Commands) SkipRunning() Commands

SkipRunning prevents running commands as the final commands, but keep showing them in help.

func (Commands) Swap

func (commands Commands) Swap(i, j int)

func (Commands) VisibleCommands

func (commands Commands) VisibleCommands() []*cli.Command

VisibleCommands returns a slice of the Commands with Hidden=false. Used by `urfave/cli` package to generate help.

func (Commands) WrapAction

func (commands Commands) WrapAction(fn func(ctx *Context, action ActionFunc) error) Commands

type CompleteFunc

type CompleteFunc func(ctx *Context) error

CompleteFunc is an action to execute when the shell completion flag is set

type Context

type Context struct {
	context.Context
	*App
	Command *Command
	// contains filtered or unexported fields
}

Context can be used to retrieve context-specific args and parsed command-line options.

func NewAppContext

func NewAppContext(ctx context.Context, app *App, args Args) *Context

func (*Context) Args

func (ctx *Context) Args() Args

Args returns the command line arguments associated with the context.

func (*Context) Flag

func (ctx *Context) Flag(name string) Flag

func (*Context) NewCommandContext

func (ctx *Context) NewCommandContext(command *Command, args Args) *Context

func (*Context) Parent

func (ctx *Context) Parent() *Context

func (*Context) Value

func (ctx *Context) Value(key any) any

func (*Context) WithValue

func (ctx *Context) WithValue(key, val any) *Context

type ExitCode

type ExitCode byte

ExitCode is a number between 0 and 255, which is returned by any Unix command when it returns control to its parent process.

const (
	ExitCodeSuccess ExitCode = iota
	ExitCodeGeneralError
)

Constants for exit codes.

type ExitCoder

type ExitCoder interface {
	error
	ExitCode() int
	Unwrap() error
}

ExitCoder is the interface checked by `App` and `Command` for a custom exit code.

func NewExitError

func NewExitError(message interface{}, exitCode ExitCode) ExitCoder

NewExitError calls Exit to create a new ExitCoder.

type ExitErrHandlerFunc

type ExitErrHandlerFunc func(ctx *Context, err error) error

ExitErrHandlerFunc is executed if provided in order to handle exitError values returned by Actions and Before/After functions.

type Flag

type Flag interface {
	// `urfave/cli/v2` uses to generate help
	cli.DocGenerationFlag

	Value() FlagValue

	GetHidden() bool
	RunAction(*Context) error

	LookupEnv(envVar string) []string
}

type FlagActionFunc

type FlagActionFunc[T any] func(ctx *Context, value T) error

FlagActionFunc represents function type that is called when the flag is specified. Executed after flag have been parsed and assigned to the `Destination` field.

type FlagSetterFunc

type FlagSetterFunc[T any] func(value T) error

FlagSetterFunc represents function type that is called when the flag is specified. Unlike `FlagActionFunc` where the function is called after the value has been parsed and assigned to the `Destination` field, `FlagSetterFunc` is called earlier, during the variable parsing. if `FlagSetterFunc` returns the error, it will be wrapped with the flag or environment variable name. Example: `fmt.Errorf("invalid value \"invalid-value\" for env var TG_ENV_VAR: %w", err)` Therefore, using `FlagSetterFunc` is preferable to `FlagActionFunc` when you need to indicate in the error from where the value came from. If the flag has multiple values, `FlagSetterFunc` will be called for each value.

type FlagValue

type FlagValue interface {
	fmt.Stringer

	Get() any

	Set(str string) error

	Getter(name string) FlagValueGetter

	GetName() string

	GetInitialTextValue() string

	// IsSet returns true if the flag was set either by env var or CLI arg.
	IsSet() bool

	// IsSet returns true if the flag was by CLI arg.
	IsArgSet() bool

	// IsEnvSet returns true if the flag was set by env var.
	IsEnvSet() bool

	// optional interface to indicate boolean flags that can be
	// supplied without "=value" text
	IsBoolFlag() bool
}

type FlagValueGetter

type FlagValueGetter interface {
	libflag.Getter

	EnvSet(str string) error
}

type FlagVariable

type FlagVariable[T any] interface {
	libflag.Getter
	Clone(dest *T) FlagVariable[T]
}

type Flags

type Flags []Flag

func (Flags) Add

func (flags Flags) Add(newFlags ...Flag) Flags

Add adds a new flag to the list.

func (Flags) Apply

func (flags Flags) Apply(flagSet *libflag.FlagSet) error

func (Flags) Filter

func (flags Flags) Filter(names ...string) Flags

Filter returns a list of flags filtered by the given names.

func (Flags) Get

func (flags Flags) Get(name string) Flag

Get returns a Flag by the given name.

func (Flags) Len

func (flags Flags) Len() int

func (Flags) Less

func (flags Flags) Less(i, j int) bool

func (Flags) RunActions

func (flags Flags) RunActions(ctx *Context) error

func (Flags) Sort

func (flags Flags) Sort() Flags

func (Flags) Swap

func (flags Flags) Swap(i, j int)

func (Flags) VisibleFlags

func (flags Flags) VisibleFlags() Flags

VisibleFlags returns a slice of the Flags. Used by `urfave/cli` package to generate help.

type GenericFlag

type GenericFlag[T GenericType] struct {

	// The name of the flag.
	Name string
	// The default value of the flag to display in the help, if it is empty, the value is taken from `Destination`.
	DefaultText string
	// A short usage description to display in help.
	Usage string
	// Aliases are usually used for the short flag name, like `-h`.
	Aliases []string
	// The names of the env variables that are parsed and assigned to `Destination` before the flag value.
	EnvVars []string
	// Action is a function that is called when the flag is specified. It is executed only after all command flags have been parsed.
	Action FlagActionFunc[T]
	// Setter allows to set a value to any type by calling its `func(bool) error` function.
	Setter FlagSetterFunc[T]
	// Destination is a pointer to which the value of the flag or env var is assigned.
	// It also uses as the default value displayed in the help.
	Destination *T
	// Hidden hides the flag from the help, if set to true.
	Hidden bool
	// contains filtered or unexported fields
}

func (*GenericFlag[T]) Apply

func (flag *GenericFlag[T]) Apply(set *libflag.FlagSet) error

Apply applies Flag settings to the given flag set.

func (*GenericFlag) GetCategory

func (flag *GenericFlag) GetCategory() string

GetCategory returns the category for the flag. Implements `cli.DocGenerationFlag.GetCategory` required to generate help.

func (*GenericFlag[T]) GetDefaultText

func (flag *GenericFlag[T]) GetDefaultText() string

GetDefaultText returns the flags value as string representation and an empty string if the flag takes no value at all.

func (*GenericFlag[T]) GetEnvVars

func (flag *GenericFlag[T]) GetEnvVars() []string

GetEnvVars implements `cli.Flag` interface.

func (*GenericFlag[T]) GetHidden

func (flag *GenericFlag[T]) GetHidden() bool

GetHidden returns true if the flag should be hidden from the help.

func (*GenericFlag[T]) GetUsage

func (flag *GenericFlag[T]) GetUsage() string

GetUsage returns the usage string for the flag.

func (*GenericFlag) GetValue

func (flag *GenericFlag) GetValue() string

GetValue returns the flags value as string representation and an empty string if the flag takes no value at all. Implements `cli.DocGenerationFlag.GetValue` required to generate help.

func (*GenericFlag) LookupEnv

func (flag *GenericFlag) LookupEnv(envVar string) []string

func (*GenericFlag[T]) Names

func (flag *GenericFlag[T]) Names() []string

Names returns the names of the flag.

func (*GenericFlag[T]) RunAction

func (flag *GenericFlag[T]) RunAction(ctx *Context) error

RunAction implements ActionableFlag.RunAction

func (*GenericFlag) SplitValue

func (flag *GenericFlag) SplitValue(val string) []string

func (*GenericFlag[T]) String

func (flag *GenericFlag[T]) String() string

String returns a readable representation of this value (for usage defaults).

func (*GenericFlag) TakesValue

func (flag *GenericFlag) TakesValue() bool

TakesValue returns true of the flag takes a value, otherwise false. Implements `cli.DocGenerationFlag.TakesValue` required to generate help.

func (*GenericFlag) Value

func (flag *GenericFlag) Value() FlagValue

type GenericType

type GenericType interface {
	string | int | int64 | uint
}

type HelpFunc

type HelpFunc func(ctx *Context) error

HelpFunc is the action to execute when help needs to be displayed.

type InvalidCommandNameError

type InvalidCommandNameError string

func (InvalidCommandNameError) Error

func (cmdName InvalidCommandNameError) Error() string

type InvalidKeyValueError

type InvalidKeyValueError struct {
	// contains filtered or unexported fields
}

func NewInvalidKeyValueError

func NewInvalidKeyValueError(sep, value string) *InvalidKeyValueError

func (InvalidKeyValueError) Error

func (err InvalidKeyValueError) Error() string

type InvalidValueError

type InvalidValueError struct {
	// contains filtered or unexported fields
}

InvalidValueError is used to wrap errors from `strconv` to make the error message more user friendly.

func (InvalidValueError) Error

func (err InvalidValueError) Error() string

func (InvalidValueError) Unwrap

func (err InvalidValueError) Unwrap() error

type LookupEnvFuncType

type LookupEnvFuncType func(key string) []string

type MapFlag

type MapFlag[K MapFlagKeyType, V MapFlagValueType] struct {

	// The name of the flag.
	Name string
	// The default value of the flag to display in the help, if it is empty, the value is taken from `Destination`.
	DefaultText string
	// A short usage description to display in help.
	Usage string
	// Aliases are usually used for the short flag name, like `-h`.
	Aliases []string
	// Action is a function that is called when the flag is specified. It is executed only after all command flags have been parsed.
	Action FlagActionFunc[map[K]V]
	// FlagSetterFunc represents function type that is called when the flag is specified.
	// Executed during value parsing, in case of an error the returned error is wrapped with the flag or environment variable name.
	Setter MapFlagSetterFunc[K, V]
	// The names of the env variables that are parsed and assigned to `Destination` before the flag value.
	EnvVars []string
	// DisableEnvVar disables the creation of the environment variable by default.
	DisableEnvVar bool
	// Destination is a pointer to which the value of the flag or env var is assigned.
	// It also uses as the default value displayed in the help.
	Destination *map[K]V
	// The func used to split the EvnVar, by default `strings.Split`
	Splitter SplitterFunc
	// The EnvVarSep value is passed to the Splitter function as an argument to split the args.
	EnvVarSep string
	// The KeyValSep value is passed to the Splitter function as an argument to split `key` and `val` of the arg.
	KeyValSep string
	// Hidden hides the flag from the help, if set to true.
	Hidden bool
	// contains filtered or unexported fields
}

MapFlag is a key value flag.

func (*MapFlag[K, V]) Apply

func (flag *MapFlag[K, V]) Apply(set *libflag.FlagSet) error

Apply applies Flag settings to the given flag set.

func (*MapFlag) GetCategory

func (flag *MapFlag) GetCategory() string

GetCategory returns the category for the flag. Implements `cli.DocGenerationFlag.GetCategory` required to generate help.

func (*MapFlag[K, V]) GetDefaultText

func (flag *MapFlag[K, V]) GetDefaultText() string

GetDefaultText returns the flags value as string representation and an empty string if the flag takes no value at all.

func (*MapFlag[K, V]) GetEnvVars

func (flag *MapFlag[K, V]) GetEnvVars() []string

GetEnvVars implements `cli.Flag` interface.

func (*MapFlag[K, V]) GetHidden

func (flag *MapFlag[K, V]) GetHidden() bool

GetHidden returns true if the flag should be hidden from the help.

func (*MapFlag[K, V]) GetUsage

func (flag *MapFlag[K, V]) GetUsage() string

GetUsage returns the usage string for the flag.

func (*MapFlag) GetValue

func (flag *MapFlag) GetValue() string

GetValue returns the flags value as string representation and an empty string if the flag takes no value at all. Implements `cli.DocGenerationFlag.GetValue` required to generate help.

func (*MapFlag) LookupEnv

func (flag *MapFlag) LookupEnv(envVar string) []string

func (*MapFlag[K, V]) Names

func (flag *MapFlag[K, V]) Names() []string

Names returns the names of the flag.

func (*MapFlag[K, V]) RunAction

func (flag *MapFlag[K, V]) RunAction(ctx *Context) error

RunAction implements ActionableFlag.RunAction

func (*MapFlag) SplitValue

func (flag *MapFlag) SplitValue(val string) []string

func (*MapFlag[K, V]) String

func (flag *MapFlag[K, V]) String() string

String returns a readable representation of this value (for usage defaults).

func (*MapFlag) TakesValue

func (flag *MapFlag) TakesValue() bool

TakesValue returns true of the flag takes a value, otherwise false. Implements `cli.DocGenerationFlag.TakesValue` required to generate help.

func (*MapFlag) Value

func (flag *MapFlag) Value() FlagValue

type MapFlagKeyType

type MapFlagKeyType interface {
	GenericType
}

type MapFlagSetterFunc

type MapFlagSetterFunc[K any, V any] func(key K, value V) error

type MapFlagValueType

type MapFlagValueType interface {
	GenericType | bool
}

type NormalizeActsType

type NormalizeActsType byte
const (
	SingleDashFlag NormalizeActsType = iota
	DoubleDashFlag
)

type SliceFlag

type SliceFlag[T SliceFlagType] struct {

	// The name of the flag.
	Name string
	// The default value of the flag to display in the help, if it is empty, the value is taken from `Destination`.
	DefaultText string
	// A short usage description to display in help.
	Usage string
	// Aliases are usually used for the short flag name, like `-h`.
	Aliases []string
	// The names of the env variables that are parsed and assigned to `Destination` before the flag value.
	EnvVars []string
	// Action is a function that is called when the flag is specified. It is executed only after all command flags have been parsed.
	Action FlagActionFunc[[]T]
	// FlagSetterFunc represents function type that is called when the flag is specified.
	// Executed during value parsing, in case of an error the returned error is wrapped with the flag or environment variable name.
	Setter FlagSetterFunc[T]
	// Destination is a pointer to which the value of the flag or env var is assigned.
	// It also uses as the default value displayed in the help.
	Destination *[]T
	// The func used to split the EvnVar, by default `strings.Split`
	Splitter SplitterFunc
	// The EnvVarSep value is passed to the Splitter function as an argument.
	EnvVarSep string
	// Hidden hides the flag from the help, if set to true.
	Hidden bool
	// contains filtered or unexported fields
}

SliceFlag is a multiple flag.

func (*SliceFlag[T]) Apply

func (flag *SliceFlag[T]) Apply(set *libflag.FlagSet) error

Apply applies Flag settings to the given flag set.

func (*SliceFlag) GetCategory

func (flag *SliceFlag) GetCategory() string

GetCategory returns the category for the flag. Implements `cli.DocGenerationFlag.GetCategory` required to generate help.

func (*SliceFlag[T]) GetDefaultText

func (flag *SliceFlag[T]) GetDefaultText() string

GetDefaultText returns the flags value as string representation and an empty string if the flag takes no value at all.

func (*SliceFlag[T]) GetEnvVars

func (flag *SliceFlag[T]) GetEnvVars() []string

GetEnvVars implements `cli.Flag` interface.

func (*SliceFlag[T]) GetHidden

func (flag *SliceFlag[T]) GetHidden() bool

GetHidden returns true if the flag should be hidden from the help.

func (*SliceFlag[T]) GetUsage

func (flag *SliceFlag[T]) GetUsage() string

GetUsage returns the usage string for the flag.

func (*SliceFlag) GetValue

func (flag *SliceFlag) GetValue() string

GetValue returns the flags value as string representation and an empty string if the flag takes no value at all. Implements `cli.DocGenerationFlag.GetValue` required to generate help.

func (*SliceFlag) LookupEnv

func (flag *SliceFlag) LookupEnv(envVar string) []string

func (*SliceFlag[T]) Names

func (flag *SliceFlag[T]) Names() []string

Names returns the names of the flag.

func (*SliceFlag[T]) RunAction

func (flag *SliceFlag[T]) RunAction(ctx *Context) error

RunAction implements ActionableFlag.RunAction

func (*SliceFlag) SplitValue

func (flag *SliceFlag) SplitValue(val string) []string

func (*SliceFlag[T]) String

func (flag *SliceFlag[T]) String() string

String returns a readable representation of this value (for usage defaults).

func (*SliceFlag) TakesValue

func (flag *SliceFlag) TakesValue() bool

TakesValue returns true of the flag takes a value, otherwise false. Implements `cli.DocGenerationFlag.TakesValue` required to generate help.

func (*SliceFlag) Value

func (flag *SliceFlag) Value() FlagValue

type SliceFlagType

type SliceFlagType interface {
	GenericType
}

type SplitterFunc

type SplitterFunc func(s, sep string) []string

SplitterFunc is used to parse flags containing multiple values.

type UndefinedFlagError

type UndefinedFlagError string

func (UndefinedFlagError) Error

func (err UndefinedFlagError) Error() string

type Value

type Value interface {
	libflag.Getter
	Reset()
}

Jump to

Keyboard shortcuts

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