Documentation
¶
Index ¶
- Constants
- Variables
- func AppFromContext(ctx CommandContext) (forge.App, bool)
- func Colorize(colorFunc func(...any) string, s string) string
- func ConfigureColors(config ColorConfig)
- func FormatError(err error, colors bool) string
- func GenerateBashCompletion(cli CLI, w io.Writer) error
- func GenerateFishCompletion(cli CLI, w io.Writer) error
- func GenerateZshCompletion(cli CLI, w io.Writer) error
- func GetExitCode(err error) int
- func GetService[T any](ctx CommandContext, name string) (T, error)
- func MultiSelectAsync(ctx context.Context, question string, loader OptionsLoader) ([]string, error)
- func MultiSelectWithProgressFeedback(ctx context.Context, question string, loader ProgressLoader) ([]string, error)
- func MultiSelectWithRetry(ctx context.Context, question string, loader OptionsLoader, maxRetries int) ([]string, error)
- func MustGetApp(ctx CommandContext) forge.App
- func MustGetService[T any](ctx CommandContext, name string) T
- func PromptPassword(question string) (string, error)
- func PromptWithDefault(question, defaultValue string) (string, error)
- func SelectAsync(ctx context.Context, question string, loader OptionsLoader) (string, error)
- func SelectWithProgressFeedback(ctx context.Context, question string, loader ProgressLoader) (string, error)
- func SelectWithRetry(ctx context.Context, question string, loader OptionsLoader, maxRetries int) (string, error)
- func WithApp(app forge.App) func(*Config)
- type BasePlugin
- func (p *BasePlugin) AddCommand(cmd Command)
- func (p *BasePlugin) AddDependency(dep string)
- func (p *BasePlugin) Commands() []Command
- func (p *BasePlugin) Dependencies() []string
- func (p *BasePlugin) Description() string
- func (p *BasePlugin) Initialize() error
- func (p *BasePlugin) Name() string
- func (p *BasePlugin) SetCommands(commands []Command)
- func (p *BasePlugin) SetDependencies(dependencies []string)
- func (p *BasePlugin) Version() string
- type CLI
- type CLIError
- type CLILogger
- func (l *CLILogger) Debug(msg string, args ...any)
- func (l *CLILogger) Error(msg string, args ...any)
- func (l *CLILogger) Info(msg string, args ...any)
- func (l *CLILogger) NewLine()
- func (l *CLILogger) Print(msg string, args ...any)
- func (l *CLILogger) Printf(format string, args ...any)
- func (l *CLILogger) Println(args ...any)
- func (l *CLILogger) Separator()
- func (l *CLILogger) SetColors(enabled bool)
- func (l *CLILogger) SetLevel(level LogLevel)
- func (l *CLILogger) SetOutput(w io.Writer)
- func (l *CLILogger) Success(msg string, args ...any)
- func (l *CLILogger) Warning(msg string, args ...any)
- type ColorConfig
- type Command
- type CommandContext
- type CommandHandler
- type CommandOption
- func WithAfter(fn MiddlewareFunc) CommandOption
- func WithAliases(aliases ...string) CommandOption
- func WithBefore(fn MiddlewareFunc) CommandOption
- func WithFlag(flag Flag) CommandOption
- func WithFlags(flags ...Flag) CommandOption
- func WithSubcommand(sub Command) CommandOption
- func WithUsage(usage string) CommandOption
- type Config
- type Flag
- func NewBoolFlag(name, shortName, description string, defaultValue bool, opts ...FlagOption) Flag
- func NewDurationFlag(name, shortName, description string, defaultValue time.Duration, ...) Flag
- func NewFlag(name string, flagType FlagType, opts ...FlagOption) Flag
- func NewIntFlag(name, shortName, description string, defaultValue int, opts ...FlagOption) Flag
- func NewStringFlag(name, shortName, description, defaultValue string, opts ...FlagOption) Flag
- func NewStringSliceFlag(name, shortName, description string, defaultValue []string, opts ...FlagOption) Flag
- type FlagOption
- func Required() FlagOption
- func ValidateEnum(allowed ...string) FlagOption
- func ValidateRange(min, max int) FlagOption
- func WithAlias(alias string) FlagOption
- func WithBoolFlag(name, shortName, description string, defaultValue bool, opts ...FlagOption) FlagOption
- func WithDefault(value any) FlagOption
- func WithDescription(desc string) FlagOption
- func WithDurationFlag(name, shortName, description string, defaultValue time.Duration, ...) FlagOption
- func WithIntFlag(name, shortName, description string, defaultValue int, opts ...FlagOption) FlagOption
- func WithStringFlag(name, shortName, description, defaultValue string, opts ...FlagOption) FlagOption
- func WithStringSliceFlag(name, shortName, description string, defaultValue []string, opts ...FlagOption) FlagOption
- func WithValidator(validator func(any) error) FlagOption
- type FlagType
- type FlagValue
- type LogLevel
- type LoggerOption
- type MiddlewareFunc
- type OptionsLoader
- type Plugin
- type PluginMetadata
- type PluginRegistry
- func (r *PluginRegistry) All() []Plugin
- func (r *PluginRegistry) Count() int
- func (r *PluginRegistry) Get(name string) (Plugin, error)
- func (r *PluginRegistry) GetMetadata(name string) (*PluginMetadata, error)
- func (r *PluginRegistry) Has(name string) bool
- func (r *PluginRegistry) Metadata() []*PluginMetadata
- func (r *PluginRegistry) Names() []string
- func (r *PluginRegistry) Register(plugin Plugin) error
- func (r *PluginRegistry) RegisterAll(plugins []Plugin) error
- type ProgressBar
- type ProgressLoader
- type Spinner
- type TableAlignment
- type TableStyle
- type TableWriter
Constants ¶
const ( ExitSuccess = 0 ExitError = 1 ExitUsageError = 2 ExitNotFound = 127 ExitInternalError = 3 )
Common exit codes.
Variables ¶
var ( // Color functions for output. Green = color.New(color.FgGreen).SprintFunc() Red = color.New(color.FgRed).SprintFunc() Yellow = color.New(color.FgYellow).SprintFunc() Blue = color.New(color.FgBlue).SprintFunc() Cyan = color.New(color.FgCyan).SprintFunc() Magenta = color.New(color.FgMagenta).SprintFunc() White = color.New(color.FgWhite).SprintFunc() Gray = color.New(color.FgHiBlack).SprintFunc() // Style functions. Bold = color.New(color.Bold).SprintFunc() Underline = color.New(color.Underline).SprintFunc() Italic = color.New(color.Italic).SprintFunc() // Combined styles. BoldGreen = color.New(color.FgGreen, color.Bold).SprintFunc() BoldRed = color.New(color.FgRed, color.Bold).SprintFunc() BoldYellow = color.New(color.FgYellow, color.Bold).SprintFunc() BoldBlue = color.New(color.FgBlue, color.Bold).SprintFunc() )
var ( // ErrCommandNotFound is returned when a command is not found. ErrCommandNotFound = errors.New("command not found") // ErrFlagRequired is returned when a required flag is missing. ErrFlagRequired = errors.New("required flag missing") // ErrFlagInvalid is returned when a flag value is invalid. ErrFlagInvalid = errors.New("invalid flag value") // ErrInvalidArguments is returned when arguments are invalid. ErrInvalidArguments = errors.New("invalid arguments") // ErrPluginNotFound is returned when a plugin is not found. ErrPluginNotFound = errors.New("plugin not found") // ErrPluginAlreadyRegistered is returned when trying to register a duplicate plugin. ErrPluginAlreadyRegistered = errors.New("plugin already registered") // ErrCircularDependency is returned when plugins have circular dependencies. ErrCircularDependency = errors.New("circular plugin dependency detected") )
Functions ¶
func AppFromContext ¶
func AppFromContext(ctx CommandContext) (forge.App, bool)
AppFromContext retrieves the Forge app from a command context.
func ConfigureColors ¶
func ConfigureColors(config ColorConfig)
ConfigureColors configures the global color settings.
func FormatError ¶
FormatError formats an error for display.
func GenerateBashCompletion ¶
GenerateBashCompletion generates bash completion script.
func GenerateFishCompletion ¶
GenerateFishCompletion generates fish completion script.
func GenerateZshCompletion ¶
GenerateZshCompletion generates zsh completion script.
func GetExitCode ¶
GetExitCode extracts the exit code from an error.
func GetService ¶
func GetService[T any](ctx CommandContext, name string) (T, error)
GetService retrieves a service from the Forge app via DI.
func MultiSelectAsync ¶
MultiSelectAsync prompts for multiple selections with async-loaded options Shows a spinner while loading options.
func MultiSelectWithProgressFeedback ¶
func MultiSelectWithProgressFeedback(ctx context.Context, question string, loader ProgressLoader) ([]string, error)
MultiSelectWithProgressFeedback prompts for multiple selections with progress feedback.
func MultiSelectWithRetry ¶
func MultiSelectWithRetry(ctx context.Context, question string, loader OptionsLoader, maxRetries int) ([]string, error)
MultiSelectWithRetry prompts for multiple selections with retry on failure.
func MustGetApp ¶
func MustGetApp(ctx CommandContext) forge.App
MustGetApp retrieves the Forge app from context or panics.
func MustGetService ¶
func MustGetService[T any](ctx CommandContext, name string) T
MustGetService retrieves a service from the Forge app or panics.
func PromptPassword ¶
PromptPassword prompts for a password (hidden input).
func PromptWithDefault ¶
PromptWithDefault prompts with a default value.
func SelectAsync ¶
SelectAsync prompts for selection with async-loaded options Shows a spinner while loading options.
func SelectWithProgressFeedback ¶
func SelectWithProgressFeedback(ctx context.Context, question string, loader ProgressLoader) (string, error)
SelectWithProgressFeedback prompts with detailed progress feedback.
func SelectWithRetry ¶
func SelectWithRetry(ctx context.Context, question string, loader OptionsLoader, maxRetries int) (string, error)
SelectWithRetry prompts for selection with retry on failure Useful for loading from flaky sources.
Types ¶
type BasePlugin ¶
type BasePlugin struct {
// contains filtered or unexported fields
}
BasePlugin provides a base implementation for plugins.
func NewBasePlugin ¶
func NewBasePlugin(name, version, description string) *BasePlugin
NewBasePlugin creates a new base plugin.
func (*BasePlugin) AddCommand ¶
func (p *BasePlugin) AddCommand(cmd Command)
AddCommand adds a command to the plugin.
func (*BasePlugin) AddDependency ¶
func (p *BasePlugin) AddDependency(dep string)
AddDependency adds a dependency to the plugin.
func (*BasePlugin) Commands ¶
func (p *BasePlugin) Commands() []Command
Commands returns the commands provided by this plugin.
func (*BasePlugin) Dependencies ¶
func (p *BasePlugin) Dependencies() []string
Dependencies returns the plugin dependencies.
func (*BasePlugin) Description ¶
func (p *BasePlugin) Description() string
Description returns the plugin description.
func (*BasePlugin) Initialize ¶
func (p *BasePlugin) Initialize() error
Initialize is called when the plugin is registered.
func (*BasePlugin) SetCommands ¶
func (p *BasePlugin) SetCommands(commands []Command)
SetCommands sets the commands for the plugin.
func (*BasePlugin) SetDependencies ¶
func (p *BasePlugin) SetDependencies(dependencies []string)
SetDependencies sets the dependencies for the plugin.
func (*BasePlugin) Version ¶
func (p *BasePlugin) Version() string
Version returns the plugin version.
type CLI ¶
type CLI interface {
// Identity
Name() string
Version() string
Description() string
// Commands
AddCommand(cmd Command) error
Commands() []Command
Run(args []string) error
// Global flags
Flag(name string, opts ...FlagOption) Flag
// Configuration
SetOutput(w io.Writer)
SetErrorHandler(handler func(error))
// Plugin support
RegisterPlugin(plugin Plugin) error
Plugins() []Plugin
}
CLI represents a command-line application.
type CLIError ¶
CLIError represents a CLI-specific error with an exit code.
type CLILogger ¶
type CLILogger struct {
// contains filtered or unexported fields
}
CLILogger is a CLI-friendly logger with color support.
func NewCLILogger ¶
func NewCLILogger(opts ...LoggerOption) *CLILogger
NewCLILogger creates a new CLI logger.
type ColorConfig ¶
ColorConfig controls color output behavior.
func DefaultColorConfig ¶
func DefaultColorConfig() ColorConfig
DefaultColorConfig returns the default color configuration.
type Command ¶
type Command interface {
// Identity
Name() string
Description() string
Usage() string
Aliases() []string
// Execution
Run(ctx CommandContext) error
// Subcommands
AddSubcommand(cmd Command) error
Subcommands() []Command
FindSubcommand(name string) (Command, bool)
// Flags
Flags() []Flag
AddFlag(flag Flag)
// Middleware
Before(fn MiddlewareFunc) Command
After(fn MiddlewareFunc) Command
// Parent (for navigation)
SetParent(parent Command)
Parent() Command
}
Command represents a CLI command.
func NewCommand ¶
func NewCommand(name, description string, handler CommandHandler, opts ...CommandOption) Command
NewCommand creates a new command.
type CommandContext ¶
type CommandContext interface {
// Arguments
Args() []string
Arg(index int) string
NArgs() int
// Flags
Flag(name string) FlagValue
String(name string) string
Int(name string) int
Bool(name string) bool
StringSlice(name string) []string
Duration(name string) int64
// Output
Println(a ...any)
Printf(format string, a ...any)
Error(err error)
Success(msg string)
Warning(msg string)
Info(msg string)
// Input
Prompt(question string) (string, error)
Confirm(question string) (bool, error)
Select(question string, options []string) (string, error)
MultiSelect(question string, options []string) ([]string, error)
// Async Input (with spinner feedback)
SelectAsync(question string, loader OptionsLoader) (string, error)
MultiSelectAsync(question string, loader OptionsLoader) ([]string, error)
SelectWithRetry(question string, loader OptionsLoader, maxRetries int) (string, error)
MultiSelectWithRetry(question string, loader OptionsLoader, maxRetries int) ([]string, error)
// Progress
ProgressBar(total int) ProgressBar
Spinner(message string) Spinner
// Tables
Table() TableWriter
// Context
Context() context.Context
// App integration (optional, returns nil if not running with Forge app)
App() forge.App
// Command reference
Command() Command
// Logger
Logger() *CLILogger
}
CommandContext provides context to command execution.
type CommandHandler ¶
type CommandHandler func(ctx CommandContext) error
CommandHandler is a function that handles command execution.
type CommandOption ¶
type CommandOption func(*command)
CommandOption is a functional option for configuring commands.
func WithAfter ¶
func WithAfter(fn MiddlewareFunc) CommandOption
WithAfter adds an after middleware.
func WithAliases ¶
func WithAliases(aliases ...string) CommandOption
WithAliases sets command aliases.
func WithBefore ¶
func WithBefore(fn MiddlewareFunc) CommandOption
WithBefore adds a before middleware.
func WithFlags ¶
func WithFlags(flags ...Flag) CommandOption
WithFlags adds multiple flags to the command.
func WithSubcommand ¶
func WithSubcommand(sub Command) CommandOption
WithSubcommand adds a subcommand.
type Config ¶
type Config struct {
Name string
Version string
Description string
Output io.Writer
ErrorHandler func(error)
Logger *CLILogger
App forge.App // Optional Forge app integration
}
Config configures a CLI application.
type Flag ¶
type Flag interface {
Name() string
ShortName() string
Description() string
DefaultValue() any
Required() bool
Type() FlagType
Validate(value any) error
}
Flag represents a command-line flag.
func NewBoolFlag ¶
func NewBoolFlag(name, shortName, description string, defaultValue bool, opts ...FlagOption) Flag
NewBoolFlag creates a bool flag.
func NewDurationFlag ¶
func NewDurationFlag(name, shortName, description string, defaultValue time.Duration, opts ...FlagOption) Flag
NewDurationFlag creates a duration flag.
func NewFlag ¶
func NewFlag(name string, flagType FlagType, opts ...FlagOption) Flag
NewFlag creates a new flag.
func NewIntFlag ¶
func NewIntFlag(name, shortName, description string, defaultValue int, opts ...FlagOption) Flag
NewIntFlag creates an int flag.
func NewStringFlag ¶
func NewStringFlag(name, shortName, description, defaultValue string, opts ...FlagOption) Flag
NewStringFlag creates a string flag.
func NewStringSliceFlag ¶
func NewStringSliceFlag(name, shortName, description string, defaultValue []string, opts ...FlagOption) Flag
NewStringSliceFlag creates a string slice flag.
type FlagOption ¶
type FlagOption func(*flagConfig)
FlagOption is a functional option for configuring flags.
func ValidateEnum ¶
func ValidateEnum(allowed ...string) FlagOption
ValidateEnum validates that a string flag is one of the allowed values.
func ValidateRange ¶
func ValidateRange(min, max int) FlagOption
ValidateRange validates that an int flag is within a range.
func WithAlias ¶
func WithAlias(alias string) FlagOption
WithAlias sets a short name alias for the flag.
func WithBoolFlag ¶
func WithBoolFlag(name, shortName, description string, defaultValue bool, opts ...FlagOption) FlagOption
WithBoolFlag creates a bool flag.
func WithDefault ¶
func WithDefault(value any) FlagOption
WithDefault sets a default value for the flag.
func WithDescription ¶
func WithDescription(desc string) FlagOption
WithDescription sets the description for the flag.
func WithDurationFlag ¶
func WithDurationFlag(name, shortName, description string, defaultValue time.Duration, opts ...FlagOption) FlagOption
WithDurationFlag creates a duration flag.
func WithIntFlag ¶
func WithIntFlag(name, shortName, description string, defaultValue int, opts ...FlagOption) FlagOption
WithIntFlag creates an int flag.
func WithStringFlag ¶
func WithStringFlag(name, shortName, description, defaultValue string, opts ...FlagOption) FlagOption
WithStringFlag creates a string flag.
func WithStringSliceFlag ¶
func WithStringSliceFlag(name, shortName, description string, defaultValue []string, opts ...FlagOption) FlagOption
WithStringSliceFlag creates a string slice flag.
func WithValidator ¶
func WithValidator(validator func(any) error) FlagOption
WithValidator sets a custom validator for the flag.
type FlagValue ¶
type FlagValue interface {
String() string
Int() int
Bool() bool
StringSlice() []string
Duration() time.Duration
IsSet() bool
Raw() any
}
FlagValue represents a parsed flag value.
type LoggerOption ¶
type LoggerOption func(*loggerConfig)
LoggerOption is a functional option for configuring the logger.
func WithColors ¶
func WithColors(enabled bool) LoggerOption
WithColors enables or disables colored output.
func WithPrefix ¶
func WithPrefix(prefix string) LoggerOption
WithPrefix sets a prefix for all log messages.
type MiddlewareFunc ¶
type MiddlewareFunc func(next CommandHandler) CommandHandler
MiddlewareFunc is a function that wraps a CommandHandler.
type OptionsLoader ¶
OptionsLoader is a function that loads options asynchronously.
type Plugin ¶
type Plugin interface {
// Name returns the unique name of the plugin
Name() string
// Version returns the plugin version
Version() string
// Description returns a human-readable description
Description() string
// Commands returns the commands provided by this plugin
Commands() []Command
// Dependencies returns the names of plugins this plugin depends on
Dependencies() []string
// Initialize is called when the plugin is registered
// This allows the plugin to perform setup before commands are added
Initialize() error
}
Plugin represents a CLI plugin that can provide commands.
type PluginMetadata ¶
type PluginMetadata struct {
Name string
Version string
Description string
Dependencies []string
Commands []string // Command names provided by this plugin
}
PluginMetadata contains information about a registered plugin.
type PluginRegistry ¶
type PluginRegistry struct {
// contains filtered or unexported fields
}
PluginRegistry manages CLI plugins.
func NewPluginRegistry ¶
func NewPluginRegistry() *PluginRegistry
NewPluginRegistry creates a new plugin registry.
func (*PluginRegistry) All ¶
func (r *PluginRegistry) All() []Plugin
All returns all registered plugins.
func (*PluginRegistry) Count ¶
func (r *PluginRegistry) Count() int
Count returns the number of registered plugins.
func (*PluginRegistry) Get ¶
func (r *PluginRegistry) Get(name string) (Plugin, error)
Get retrieves a plugin by name.
func (*PluginRegistry) GetMetadata ¶
func (r *PluginRegistry) GetMetadata(name string) (*PluginMetadata, error)
GetMetadata returns metadata for a specific plugin.
func (*PluginRegistry) Has ¶
func (r *PluginRegistry) Has(name string) bool
Has checks if a plugin exists.
func (*PluginRegistry) Metadata ¶
func (r *PluginRegistry) Metadata() []*PluginMetadata
Metadata returns metadata for all plugins.
func (*PluginRegistry) Names ¶
func (r *PluginRegistry) Names() []string
Names returns the names of all registered plugins sorted alphabetically.
func (*PluginRegistry) Register ¶
func (r *PluginRegistry) Register(plugin Plugin) error
Register registers a plugin.
func (*PluginRegistry) RegisterAll ¶
func (r *PluginRegistry) RegisterAll(plugins []Plugin) error
RegisterAll registers multiple plugins with dependency resolution.
type ProgressBar ¶
ProgressBar displays a progress bar.
type ProgressLoader ¶
type ProgressLoader func(ctx context.Context, progress func(current, total int, message string)) ([]string, error)
SelectWithProgress prompts for selection with progress feedback Useful when loading takes multiple steps.
type TableAlignment ¶
type TableAlignment int
TableAlignment defines column alignment.
const ( AlignLeft TableAlignment = iota AlignCenter AlignRight )
type TableStyle ¶
type TableStyle int
TableStyle defines the table border style.
const ( StyleDefault TableStyle = iota StyleRounded StyleSimple StyleCompact StyleMarkdown )
type TableWriter ¶
type TableWriter interface {
SetHeader(headers []string)
AppendRow(row []string)
SetAlignment(alignment TableAlignment)
SetColumnAlignment(column int, alignment TableAlignment)
SetStyle(style TableStyle)
SetMaxColumnWidth(width int)
SetMinColumnWidth(width int)
Render()
}
TableWriter provides table formatting.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
async_select
command
|
|
|
enhanced_features
command
|
|
|
forge_integration
command
|
|
|
interactive
command
|
|
|
interactive_arrows
command
|
|
|
plugin
command
|
|
|
simple
command
|
|
|
subcommands
command
|