Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewCobraCommand ¶
NewCobraCommand creates a cobra.Command from a Command implementation.
This function automatically handles the full command lifecycle:
- Sets the command name from c.Name()
- Binds flags if the command implements Options interface
- Calls Complete() before Run() if command implements ComplexOptions
- Calls Validate() before Run() if command implements ComplexOptions
- Calls Run() to execute the command
Note: The type assertions work on the concrete type, not the Command interface. This means if you return a concrete type that embeds *QueryOptions pointer, the promoted methods will be found through method promotion.
Types ¶
type Command ¶
type Command interface {
// Name returns the command's name used for CLI invocation.
Name() string
// Run executes the command logic with the parsed cobra.Command and arguments.
// Called after flags are bound and validated (if implemented).
Run(cmd *cobra.Command, args []string) error
}
Command represents a CLI command that can be executed.
A command implements at least Name() and Run() methods. Commands that also implement Options or ComplexOptions interfaces get additional functionality like flag binding and validation.
Example:
var _ cli.Command = &MyCommand{}
type MyCommand struct{}
func (c *MyCommand) Name() string { return "mycommand" }
func (c *MyCommand) Run(cmd *cobra.Command, args []string) error { ... }
type CommonOptions ¶
type CommonOptions struct {
// Workspace is the working directory for the command.
// Defaults to current working directory if not set.
Workspace string
// Logger is a structured logger for the command.
// Initialized in Complete() with the command name.
Logger log.Logger
}
CommonOptions provides common functionality for CLI commands.
It implements the ComplexOptions interface and provides:
- Workspace: The working directory for the command
- Logger: A structured logger with the command name
Commands can embed this struct to reuse these common features.
Example:
type MyCommand struct {
cli.CommonOptions
MyField string
}
func (c *MyCommand) Complete(cmd *cobra.Command, args []string) error {
// Always call parent first to initialize Logger and Workspace
return c.CommonOptions.Complete(cmd, args)
}
func (*CommonOptions) BindFlags ¶
func (c *CommonOptions) BindFlags(fs *pflag.FlagSet)
BindFlags implements ComplexOptions interface.
This is a no-op for CommonOptions, allowing derived types to add their own flags.
func (*CommonOptions) Complete ¶
func (c *CommonOptions) Complete(cmd *cobra.Command, args []string) error
Complete implements ComplexOptions interface.
Initializes the Logger with the command name and sets Workspace to the current working directory if not already set.
IMPORTANT: Derived types MUST call this method first in their own Complete() implementation to ensure proper initialization.
func (*CommonOptions) Validate ¶
func (c *CommonOptions) Validate() error
Validate implements ComplexOptions interface.
Performs base validation. Derived types should call this method first in their own Validate() implementation before adding custom validation logic.
type ComplexOptions ¶
type ComplexOptions interface {
Options
// Complete initializes the command's options after flags have been parsed.
// This is the right place to:
// - Initialize resources (loggers, clients, etc.)
// - Load configuration from files
// - Parse and validate arguments
// - Set up derived state
Complete(cmd *cobra.Command, args []string) error
// Validate validates the command's options after completion.
// Return an error if the configuration is invalid.
// Called automatically by NewCobraCommand before Run().
Validate() error
}
ComplexOptions extends Options with lifecycle methods for initialization and validation.
This interface is for commands that require additional setup after flag parsing and validation before execution. The lifecycle is:
- BindFlags() - from Options (if implemented)
- Complete() - initialize resources, parse args, set up logger, etc.
- Validate() - validate the configuration
type Options ¶
type Options interface {
// BindFlags binds the command's flags to the pflag.FlagSet.
// This allows each subcommand to define its own command line flags.
BindFlags(fs *pflag.FlagSet)
}
Options provides flag binding capability for commands.
Implementing this interface allows NewCobraCommand to automatically call BindFlags during command construction.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package example provides a root command with simple and complex subcommands.
|
Package example provides a root command with simple and complex subcommands. |
|
complex
Package complex demonstrates advanced usage with CommonOptions.
|
Package complex demonstrates advanced usage with CommonOptions. |
|
simple
Package simple demonstrates basic usage of the cli package.
|
Package simple demonstrates basic usage of the cli package. |