Documentation
¶
Overview ¶
Package cli provides a simple, struct-driven CLI framework. It follows a Configure-Validate-Run lifecycle and integrates with github.com/runreveal/lib/loader for config file loading.
Index ¶
- func DumpConfig(handler Runnable) map[string]any
- func GlobalsFromContext[T any](ctx context.Context) *T
- func IsSet(ctx context.Context, flagName string) bool
- func NoArgs(args []string) error
- func Register(nodes ...Node)
- type App
- type AppOption
- func WithConfigFlag(flagName string) AppOption
- func WithDefaultConfig(data []byte) AppOption
- func WithDefaultConfigCommand(name string) AppOption
- func WithGlobals(ptr any) AppOption
- func WithMiddleware(m Middleware) AppOption
- func WithOutput(w io.Writer) AppOption
- func WithVersion(v string) AppOption
- type ArgsFunc
- type CmdOption
- type CommandInfo
- type Completer
- type Completion
- type Configurer
- type ExitError
- type FlagSet
- type HelpExtra
- type Middleware
- type Node
- type Runnable
- type Validator
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DumpConfig ¶
DumpConfig returns the resolved configuration of handler as a map of flag name → current field value. It reflects directly over the handler struct, so it captures values set by both CLI flags and config files.
func GlobalsFromContext ¶
GlobalsFromContext retrieves the globals pointer from context, cast to *T. Returns nil if no globals were registered or the type doesn't match.
func IsSet ¶
IsSet reports whether a flag was explicitly set on the command line. Must be called from within Run to return meaningful results.
Types ¶
type App ¶
type App struct {
// contains filtered or unexported fields
}
App is the top-level CLI application.
func (*App) AddCommand ¶
AddCommand adds top-level command nodes to the app.
type AppOption ¶
type AppOption func(*App)
AppOption configures an App.
func WithConfigFlag ¶
WithConfigFlag sets which flag name holds the config file path.
func WithDefaultConfig ¶
WithDefaultConfig registers a default configuration that can be printed with "myapp defcon". Typically used with go:embed to ship a reference config alongside the binary. The command name defaults to "defcon" but can be overridden with WithDefaultConfigCommand.
func WithDefaultConfigCommand ¶
WithDefaultConfigCommand overrides the command name used to print the default config (default: "defcon").
func WithGlobals ¶
WithGlobals registers a struct pointer whose cli-tagged fields become flags available on every command. The pointer is stored in context and can be retrieved with GlobalsFromContext.
func WithMiddleware ¶
func WithMiddleware(m Middleware) AppOption
WithMiddleware adds a middleware to the app.
func WithOutput ¶
WithOutput sets the writer for all output: help/errors (normally stderr) and completion/defcon (normally stdout). Useful for testing.
func WithVersion ¶
WithVersion sets the application version (enables --version flag).
type ArgsFunc ¶
ArgsFunc validates positional arguments.
type CmdOption ¶
type CmdOption func(*cmdOptions)
CmdOption configures a Command or Group node.
type CommandInfo ¶
type CommandInfo struct {
Name string // full command path, e.g. "admin migrate"
Args []string // positional args after flag parsing
}
CommandInfo is passed to middleware.
type Completer ¶
type Completer interface {
Complete(ctx context.Context, args []string) []Completion
}
Completer is optionally implemented by command handlers to provide custom completions for positional arguments.
type Completion ¶
Completion represents a single shell completion suggestion.
type Configurer ¶
type Configurer interface {
Configure() error
}
Configurer is optionally implemented by globals or handler structs. Called after config file loading to initialize resources (e.g. open database connections, create clients).
type FlagSet ¶
type FlagSet struct {
// contains filtered or unexported fields
}
FlagSet is a parsed set of flag definitions.
func FlagSetFromContext ¶
FlagSetFromContext returns the *FlagSet stored in ctx during command execution, or nil if called outside of a command handler.
type HelpExtra ¶
type HelpExtra interface {
ExtraHelp() string
}
HelpExtra is optionally implemented by command handlers to append additional information to help output. Useful for showing available loader types, config file schemas, or other context that the cli framework can't derive from struct tags alone.
type Middleware ¶
Middleware wraps command execution.
type Node ¶
type Node interface {
// contains filtered or unexported methods
}
Node is a node in the command tree.
func Command ¶
Command creates a command node. Each element of opts may be a Node (child subcommand) or a CmdOption (behavioural option); they are distinguished by type at runtime.
func Group ¶
Group creates a group node that only prints help when invoked directly. Each element of opts may be a Node (child subcommand) or a CmdOption (e.g. WithLong); they are distinguished by type at runtime.
func Registered ¶
func Registered() []Node
Registered returns a copy of all nodes added via Register. Call this when building the App to include registered commands.