Documentation
¶
Index ¶
- Constants
- func Exit(code int, args ...interface{}) error
- type ActionFunc
- type App
- type Args
- type Ask
- type BeforeFunc
- type BoolFlag
- type Choice
- type Command
- type CommandNotFoundFunc
- type Confirmation
- type DurationFlag
- type ExitError
- type Flag
- type Float64Flag
- type Float64SliceFlag
- type Formatter
- func (f *Formatter) Escape(text string) string
- func (f *Formatter) Format(message string) string
- func (f *Formatter) GetStyle(name string) (string, error)
- func (f *Formatter) HasStyle(name string) bool
- func (f *Formatter) IsDecorated() bool
- func (f *Formatter) SetDecorated(decorated bool)
- func (f *Formatter) SetStyle(name string, code string)
- type GenericFlag
- type HookFunc
- type Input
- func (in *Input) Args() Args
- func (in *Input) Bool(name string) bool
- func (in *Input) Command() *Command
- func (in *Input) Duration(name string) time.Duration
- func (in *Input) FindCommand(name string) *Command
- func (in *Input) Float64(name string) float64
- func (in *Input) Float64Slice(name string) []float64
- func (in *Input) Generic(name string) interface{}
- func (in *Input) Int(name string) int
- func (in *Input) Int64(name string) int64
- func (in *Input) IntSlice(name string) []int
- func (in *Input) IsInteractive() bool
- func (in *Input) IsSet(name string) bool
- func (in *Input) NArg() int
- func (in *Input) Root() *Command
- func (in *Input) String(name string) string
- func (in *Input) StringSlice(name string) []string
- func (in *Input) Uint(name string) uint
- type Int64Flag
- type IntFlag
- type IntSliceFlag
- type NotFoundError
- type Output
- func (o *Output) Ask(question string, defaultVal interface{}) (interface{}, error)
- func (o *Output) AskHidden(question string) (string, error)
- func (o *Output) AskString(question, defaultVal string) (string, error)
- func (o *Output) Choice(question string, choices map[string]string, defaultVal interface{}) (interface{}, error)
- func (o *Output) Confirm(question string, defaultVal bool) (bool, error)
- func (o *Output) Debug(args ...interface{})
- func (o *Output) Debugf(format string, args ...interface{})
- func (o *Output) Error(args ...interface{})
- func (o *Output) Errorf(format string, args ...interface{})
- func (o *Output) GetVerbosity() int
- func (o *Output) Info(args ...interface{})
- func (o *Output) Infof(format string, args ...interface{})
- func (o *Output) RenderTable(t *Table) string
- func (o *Output) SetVerbosity(level int)
- func (o *Output) Success(args ...interface{})
- func (o *Output) Successf(format string, args ...interface{})
- func (o *Output) Table(header []string, rows [][]string) string
- func (o *Output) TableAny(header []string, rows [][]interface{}) string
- func (o *Output) Verbose(args ...interface{})
- func (o *Output) Verbosef(format string, args ...interface{})
- func (o *Output) Warn(args ...interface{})
- func (o *Output) Warnf(format string, args ...interface{})
- type Question
- func (q *Question) GetAutocompleterValues() []string
- func (q *Question) GetDefault() interface{}
- func (q *Question) GetMaxAttempts() *int
- func (q *Question) GetNormalizer() func(string) string
- func (q *Question) GetQuestion() string
- func (q *Question) GetValidator() func(string) (interface{}, error)
- func (q *Question) IsHidden() bool
- func (q *Question) SetAutocompleterValues(values []string) *Question
- func (q *Question) SetHidden(hidden bool) *Question
- func (q *Question) SetMaxAttempts(attempts int) *Question
- func (q *Question) SetNormalizer(normalizer func(string) string) *Question
- func (q *Question) SetValidator(validator func(string) (interface{}, error)) *Question
- type StringFlag
- type StringSliceFlag
- type Table
- func (t *Table) AddRow(row []string, first bool) *Table
- func (t *Table) AddRowAny(row []interface{}, first bool) *Table
- func (t *Table) AddSection(title string) *Table
- func (t *Table) AddSeparator() *Table
- func (t *Table) Render() string
- func (t *Table) SetCellAlign(align int) *Table
- func (t *Table) SetHeader(header []string, align int) *Table
- func (t *Table) SetRows(rows [][]string, align int) *Table
- func (t *Table) SetStyle(style string) *Table
- type UintFlag
- type UsageError
- type ValueSource
- type VersionPrinterFunc
Constants ¶
const ( VerbosityQuiet = 0 // Keep only warnings and errors VerbosityNormal = 1 VerbosityDebug = 4 // --verbose )
Output verbosity levels.
const ( AlignLeft = 1 AlignRight = 0 AlignCenter = 2 )
Table alignment modes.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type ActionFunc ¶
ActionFunc is the command execution function: context, input (args+flags), output (logging/table/ask).
type App ¶
type App = Command
App is a type alias for the root command. Usage: `app := &cli.App{...}`.
type Args ¶
type Args []string
Args holds the positional arguments of a command.
type Ask ¶
type Ask struct {
// contains filtered or unexported fields
}
Ask handles interactive questions.
type BeforeFunc ¶
BeforeFunc is a hook function run before Action. It may derive a new context and return it; the updated context is passed to Action/After and subsequent hooks.
type BoolFlag ¶
type BoolFlag struct {
Name string
Aliases []string
Usage string
Required bool
Hidden bool
DefaultText string
Sources ValueSource
Value bool
}
BoolFlag is a boolean switch flag.
func (BoolFlag) IsRequired ¶
func (BoolFlag) TakesValue ¶
type Choice ¶
type Choice struct {
*Question
// contains filtered or unexported fields
}
Choice is a multiple-choice question.
func (*Choice) GetChoices ¶
GetChoices returns the choices.
func (*Choice) IsMultiselect ¶
IsMultiselect reports whether multi-select is enabled.
func (*Choice) SetErrorMessage ¶
SetErrorMessage sets the validation error message.
func (*Choice) SetMultiselect ¶
SetMultiselect toggles multi-select.
type Command ¶
type Command struct {
// Metadata
Name string // Command name; usually empty for the root command (App)
Aliases []string // Command aliases; the command can be invoked by any alias
Usage string // One-line purpose description, shown in the help listing
UsageText string // Custom usage line; generated by the library by default
Description string // Detailed command description, shown in help
ArgsUsage string // Usage description for positional arguments (e.g. "<name> [flags]")
Hidden bool // When true, hidden from help/listing but still directly invocable
Version string // Version of the root command (App); enables --version/-v
// Behavior
Flags []Flag // Flags defined by this command
Subcommands []*Command // List of subcommands
Before BeforeFunc // Hook run before execution; may return an updated context
After HookFunc // Hook run after execution (invoked after a subcommand runs)
Action ActionFunc // Command execution function
CommandNotFound CommandNotFoundFunc // Handler for when a command is not found
VersionPrinter VersionPrinterFunc // Custom version printing function
Stdin io.Reader // Injectable standard input (defaults to os.Stdin)
// contains filtered or unexported fields
}
Command is the command struct. The root command is the App; both are the same type.
type CommandNotFoundFunc ¶
CommandNotFoundFunc handles a command that was not found.
type Confirmation ¶
type Confirmation struct {
*Question
// contains filtered or unexported fields
}
Confirmation is a yes/no question.
func NewConfirmation ¶
func NewConfirmation(question string, defaultVal bool, trueAnswerRegex string) *Confirmation
NewConfirmation creates a confirmation question.
type DurationFlag ¶
type DurationFlag struct {
Name string
Aliases []string
Usage string
Required bool
Hidden bool
DefaultText string
Sources ValueSource
Value time.Duration
}
DurationFlag is a time duration flag.
func (DurationFlag) IsRequired ¶
func (f DurationFlag) IsRequired() bool
func (DurationFlag) Names ¶
func (f DurationFlag) Names() []string
func (DurationFlag) TakesValue ¶
func (f DurationFlag) TakesValue() bool
type ExitError ¶
ExitError carries an exit code and an error, for ending a command early with an exit code from an Action.
type Flag ¶
type Flag interface {
// Names returns the primary name plus aliases.
Names() []string
// TakesValue reports whether the flag takes a value (bool flags return false).
TakesValue() bool
// IsRequired reports whether the flag is required.
IsRequired() bool
// contains filtered or unexported methods
}
Flag is the interface for all flag types.
type Float64Flag ¶
type Float64Flag struct {
Name string
Aliases []string
Usage string
Required bool
Hidden bool
DefaultText string
Sources ValueSource
Value float64
}
Float64Flag is a floating-point flag.
func (Float64Flag) IsRequired ¶
func (f Float64Flag) IsRequired() bool
func (Float64Flag) Names ¶
func (f Float64Flag) Names() []string
func (Float64Flag) TakesValue ¶
func (f Float64Flag) TakesValue() bool
type Float64SliceFlag ¶
type Float64SliceFlag struct {
Name string
Aliases []string
Usage string
Required bool
Hidden bool
DefaultText string
Sources ValueSource
Value []float64
}
Float64SliceFlag is a floating-point slice flag.
func (Float64SliceFlag) IsRequired ¶
func (f Float64SliceFlag) IsRequired() bool
func (Float64SliceFlag) Names ¶
func (f Float64SliceFlag) Names() []string
func (Float64SliceFlag) TakesValue ¶
func (f Float64SliceFlag) TakesValue() bool
type Formatter ¶
type Formatter struct {
// contains filtered or unexported fields
}
Formatter renders colors via <style>...</style> tags, delegating the underlying ANSI rendering to fatih/color (Symfony-style tag rendering; colors are not self-implemented).
func NewFormatter ¶
func NewFormatter() *Formatter
NewFormatter creates a formatter with the default styles.
func (*Formatter) Format ¶
Format processes a message, replacing style tags with ANSI codes (rendered via fatih/color).
func (*Formatter) IsDecorated ¶
IsDecorated reports whether styling is enabled.
func (*Formatter) SetDecorated ¶
SetDecorated enables/disables styling. When disabled, plain text is output (color codes stripped).
type GenericFlag ¶
type GenericFlag struct {
Name string
Aliases []string
Usage string
Required bool
Hidden bool
DefaultText string
Sources ValueSource
Value flag.Value
}
GenericFlag is a generic flag that allows a custom flag.Value implementation.
func (GenericFlag) IsRequired ¶
func (f GenericFlag) IsRequired() bool
func (GenericFlag) Names ¶
func (f GenericFlag) Names() []string
func (GenericFlag) TakesValue ¶
func (f GenericFlag) TakesValue() bool
type Input ¶
type Input struct {
// contains filtered or unexported fields
}
Input carries the input side during command execution: the current command, positional arguments, and flag values. It is separated from Output to avoid merging input and output into a single object.
func (*Input) FindCommand ¶
FindCommand recursively finds a subcommand under the root command (App) by name or alias; returns nil when not found.
Often used to invoke command B from inside the Action of command A, for example:
if b := in.FindCommand("build"); b != nil {
return b.Run(ctx, []string{"build", "--env", "prod"})
}
func (*Input) Float64Slice ¶
Float64Slice returns the value of a floating-point slice flag.
func (*Input) IsInteractive ¶
IsInteractive reports whether interactive Q&A is allowed; it is false with --no-interaction or when stdin is not a terminal.
func (*Input) IsSet ¶
IsSet reports whether the flag was explicitly set (command line or environment variable).
func (*Input) StringSlice ¶
StringSlice returns the value of a string slice flag.
type Int64Flag ¶
type Int64Flag struct {
Name string
Aliases []string
Usage string
Required bool
Hidden bool
DefaultText string
Sources ValueSource
Value int64
}
Int64Flag is a 64-bit integer flag.
func (Int64Flag) IsRequired ¶
func (Int64Flag) TakesValue ¶
type IntFlag ¶
type IntFlag struct {
Name string
Aliases []string
Usage string
Required bool
Hidden bool
DefaultText string
Sources ValueSource
Value int
}
IntFlag is an integer flag.
func (IntFlag) IsRequired ¶
func (IntFlag) TakesValue ¶
type IntSliceFlag ¶
type IntSliceFlag struct {
Name string
Aliases []string
Usage string
Required bool
Hidden bool
DefaultText string
Sources ValueSource
Value []int
}
IntSliceFlag is an integer slice flag.
func (IntSliceFlag) IsRequired ¶
func (f IntSliceFlag) IsRequired() bool
func (IntSliceFlag) Names ¶
func (f IntSliceFlag) Names() []string
func (IntSliceFlag) TakesValue ¶
func (f IntSliceFlag) TakesValue() bool
type NotFoundError ¶
type NotFoundError struct {
Command string
}
NotFoundError represents a command/help topic that does not exist.
func (*NotFoundError) Error ¶
func (e *NotFoundError) Error() string
type Output ¶
type Output struct {
// contains filtered or unexported fields
}
Output handles common log output: Info / Success / Warn / Error / Debug / Verbose, plus table rendering and interactive Q&A. Color is enabled automatically on terminals.
func NewOutput ¶
func NewOutput() *Output
NewOutput creates an output object writing to stdout/stderr. Color detection is delegated to fatih/color.
func (*Output) Choice ¶
func (o *Output) Choice(question string, choices map[string]string, defaultVal interface{}) (interface{}, error)
Choice asks a multiple-choice question; choices is a key->label mapping.
func (*Output) Debug ¶
func (o *Output) Debug(args ...interface{})
Debug prints a debug message (requires --verbose).
func (*Output) Error ¶
func (o *Output) Error(args ...interface{})
Error prints an error message (written to stderr, not suppressed by --quiet).
func (*Output) GetVerbosity ¶
GetVerbosity returns the output level.
func (*Output) Info ¶
func (o *Output) Info(args ...interface{})
Info prints an informational message.
func (*Output) RenderTable ¶
RenderTable renders and prints a custom Table (style/sections/separators supported) and returns the text.
func (*Output) SetVerbosity ¶
SetVerbosity sets the output level.
func (*Output) Success ¶
func (o *Output) Success(args ...interface{})
Success prints a success message.
func (*Output) TableAny ¶
TableAny renders and prints a table with arbitrary cell types (cells are stringified automatically).
func (*Output) Verbose ¶
func (o *Output) Verbose(args ...interface{})
Verbose prints a verbose message (requires --verbose).
type Question ¶
type Question struct {
// contains filtered or unexported fields
}
Question represents an interactive question.
func NewQuestion ¶
NewQuestion creates a normal question.
func (*Question) GetAutocompleterValues ¶
GetAutocompleterValues returns the autocompletion candidates.
func (*Question) GetDefault ¶
func (q *Question) GetDefault() interface{}
GetDefault returns the default answer.
func (*Question) GetMaxAttempts ¶
GetMaxAttempts returns the maximum number of attempts.
func (*Question) GetNormalizer ¶
GetNormalizer returns the normalizer function.
func (*Question) GetQuestion ¶
GetQuestion returns the question text.
func (*Question) GetValidator ¶
GetValidator returns the validator.
func (*Question) SetAutocompleterValues ¶
SetAutocompleterValues sets the autocompletion candidates.
func (*Question) SetMaxAttempts ¶
SetMaxAttempts sets the maximum number of attempts.
func (*Question) SetNormalizer ¶
SetNormalizer sets the normalizer function.
type StringFlag ¶
type StringFlag struct {
Name string
Aliases []string
Usage string
Required bool
Hidden bool
DefaultText string
Sources ValueSource
Value string
}
StringFlag is a string flag.
func (StringFlag) IsRequired ¶
func (f StringFlag) IsRequired() bool
func (StringFlag) Names ¶
func (f StringFlag) Names() []string
func (StringFlag) TakesValue ¶
func (f StringFlag) TakesValue() bool
type StringSliceFlag ¶
type StringSliceFlag struct {
Name string
Aliases []string
Usage string
Required bool
Hidden bool
DefaultText string
Sources ValueSource
Value []string
}
StringSliceFlag is a string slice flag (repeatable).
func (StringSliceFlag) IsRequired ¶
func (f StringSliceFlag) IsRequired() bool
func (StringSliceFlag) Names ¶
func (f StringSliceFlag) Names() []string
func (StringSliceFlag) TakesValue ¶
func (f StringSliceFlag) TakesValue() bool
type Table ¶
type Table struct {
// contains filtered or unexported fields
}
Table renders a table to the console.
func (*Table) AddSection ¶
AddSection appends a full-width section title row.
func (*Table) AddSeparator ¶
AddSeparator appends a horizontal separator line.
func (*Table) SetCellAlign ¶
SetCellAlign sets the cell alignment.
type UintFlag ¶
type UintFlag struct {
Name string
Aliases []string
Usage string
Required bool
Hidden bool
DefaultText string
Sources ValueSource
Value uint
}
UintFlag is an unsigned integer flag.
func (UintFlag) IsRequired ¶
func (UintFlag) TakesValue ¶
type UsageError ¶
type UsageError struct {
Msg string
}
UsageError represents a command-line usage error (unknown option, missing required flag, etc.).
func (*UsageError) Error ¶
func (e *UsageError) Error() string
type ValueSource ¶
type ValueSource interface {
// Lookup returns the value provided by this source; returns empty string and false when not found.
Lookup() (string, bool)
// String returns a human-readable description of the source, used in help text.
String() string
}
ValueSource is the interface for flag value sources.
A flag may declare a value source (Sources); at runtime the first hit wins by priority: command line > Sources > default value (Value). The builtin implementation is EnvVars; custom implementations are also supported.
func EnvVars ¶
func EnvVars(envs ...string) ValueSource
EnvVars builds a ValueSource that reads from environment variables; it is one implementation of ValueSource.
Usage: Sources: cli.EnvVars("APP_LANG", "LANG")
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
example
|
|
|
ask
command
ask —— demos interactive Q&A: free input, hidden input, confirmation, and choice.
|
ask —— demos interactive Q&A: free input, hidden input, confirmation, and choice. |
|
commands
command
commands —— demos command organization & the help system: grouping, hidden commands, custom usage, and not-found handling.
|
commands —— demos command organization & the help system: grouping, hidden commands, custom usage, and not-found handling. |
|
context
command
context —— demos passing values through context.Context across the command tree.
|
context —— demos passing values through context.Context across the command tree. |
|
flags
command
flags —— demos every flag type: string/int/float/bool/duration/slice/required/env.
|
flags —— demos every flag type: string/int/float/bool/duration/slice/required/env. |
|
hello
command
hello —— minimal intro: one App, one subcommand, one positional argument, one flag.
|
hello —— minimal intro: one App, one subcommand, one positional argument, one flag. |
|
invoke
command
invoke —— demos calling command B from inside command A.
|
invoke —— demos calling command B from inside command A. |
|
logging
command
logging —— demos log output and level control.
|
logging —— demos log output and level control. |
|
table
command
table —— demos table rendering: multiple styles, section titles, and separators.
|
table —— demos table rendering: multiple styles, section titles, and separators. |