Documentation
¶
Index ¶
- type Command
- type CommandHandler
- type Dispatcher
- func (d *Dispatcher[T]) ExecuteCommand(registry T, args []string) error
- func (d *Dispatcher[T]) GetCommand(name string) *Command[T]
- func (d *Dispatcher[T]) HasCommand(name string) bool
- func (d *Dispatcher[T]) ListCommands() []Command[T]
- func (d *Dispatcher[T]) PrintUsage()
- func (d *Dispatcher[T]) RegisterCommand(name, description string, handler CommandHandler[T]) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Command ¶
type Command[T any] struct { Name string Description string Handler CommandHandler[T] }
Command represents a CLI command with its handler and description.
type CommandHandler ¶
CommandHandler defines the function signature for command handlers. It accepts a generic registry interface and command arguments.
type Dispatcher ¶
type Dispatcher[T any] struct { // contains filtered or unexported fields }
Dispatcher manages CLI command registration and execution.
func NewDispatcher ¶
func NewDispatcher[T any]() *Dispatcher[T]
NewDispatcher creates a new CLI command dispatcher.
func (*Dispatcher[T]) ExecuteCommand ¶
func (d *Dispatcher[T]) ExecuteCommand(registry T, args []string) error
ExecuteCommand executes a CLI command based on the provided arguments.
Business logic: 1. Logs the command being executed. 2. Validates that at least one argument (the command) is provided. 3. Looks up the command in the registry. 4. If a handler is found, executes it with the remaining arguments. 5. If no handler is found, returns an "unrecognized command" error.
Parameters: - registry: The registry instance to be passed to command handlers - args: The command line arguments (excluding the program name)
Returns: - error: An error if the command execution fails or is invalid, otherwise nil
func (*Dispatcher[T]) GetCommand ¶
func (d *Dispatcher[T]) GetCommand(name string) *Command[T]
GetCommand returns a command by name, or nil if not found.
func (*Dispatcher[T]) HasCommand ¶
func (d *Dispatcher[T]) HasCommand(name string) bool
HasCommand checks if a command is registered.
func (*Dispatcher[T]) ListCommands ¶
func (d *Dispatcher[T]) ListCommands() []Command[T]
ListCommands returns a list of all registered commands with their descriptions.
func (*Dispatcher[T]) PrintUsage ¶
func (d *Dispatcher[T]) PrintUsage()
PrintUsage prints usage information for all registered commands.
func (*Dispatcher[T]) RegisterCommand ¶
func (d *Dispatcher[T]) RegisterCommand(name, description string, handler CommandHandler[T]) error
RegisterCommand registers a new command with the dispatcher.
Parameters: - name: The command name - description: Description of what the command does - handler: Function to handle the command
Returns: - error: If command name is empty or already registered