glimr/console/command
Glimr Console Command
Provides a fluent API for defining console commands. Commands capture their context at construction time via closures, allowing framework and user commands to coexist.
For database commands, use driver-specific command modules:
- glimr_sqlite/command.handler() for SQLite
- glimr_postgres/command.handler() for PostgreSQL
Types
Represents a console command. Regular commands have a simple handler function. Database commands use CommandWithDb which includes a closure that manages pool lifecycle.
pub type Command {
Command(
name: String,
description: String,
args: List(CommandArg),
handler: fn(ParsedArgs) -> Nil,
)
CommandWithDb(
name: String,
description: String,
args: List(CommandArg),
driver_type: driver.DriverType,
run_with_pool: fn(ParsedArgs, driver.Connection) -> Nil,
)
}
Constructors
-
Command( name: String, description: String, args: List(CommandArg), handler: fn(ParsedArgs) -> Nil, ) -
CommandWithDb( name: String, description: String, args: List(CommandArg), driver_type: driver.DriverType, run_with_pool: fn(ParsedArgs, driver.Connection) -> Nil, )
Defines an argument, flag, or option that a command accepts.
Use Argument for required positional arguments,
Flag for boolean flags like –verbose or -v, and
Option for options with values and defaults like –format=json.
pub type CommandArg {
Argument(name: String, description: String)
Flag(name: String, short: String, description: String)
Option(name: String, description: String, default: String)
}
Constructors
-
Argument(name: String, description: String) -
Flag(name: String, short: String, description: String) -
Option(name: String, description: String, default: String)
Parsed arguments passed to command handlers. Contains positional arguments as a Dict, flags as a List, and options with values as a Dict. Use get_arg(), has_flag(), and get_option() to access values.
pub type ParsedArgs {
ParsedArgs(
arguments: dict.Dict(String, String),
flags: List(String),
options: dict.Dict(String, String),
)
}
Constructors
Values
pub fn args(cmd: Command, arguments: List(CommandArg)) -> Command
Sets the arguments, flags, and options for a command. Use Argument for required positional args, Flag for boolean flags, and Option for options that take values.
Example
command.new()
|> command.name("make:controller")
|> command.args([
Argument("name", "The name of the controller"),
Flag("resource", "r", "Generate a resource controller"),
Option("template", "Template to use for generation", "default"),
])
|> command.handler(fn(args) { ... })
pub fn db_option() -> CommandArg
Returns the standard –database option for commands that need database access. Add this to your command args when using driver-specific handlers. Uses “_default” as the default value to find the connection with is_default: True.
pub fn description(cmd: Command, description: String) -> Command
Sets the description shown in the help output. Description should be a one-liner, nice and simple. This text appears next to the command name when users run the help command.
pub fn get_arg(parsed: ParsedArgs, name: String) -> String
Gets a positional argument value from ParsedArgs by name. Arguments are required and validated before the handler runs, so this will crash if the argument is missing.
pub fn get_option(parsed: ParsedArgs, name: String) -> String
Gets an option value from ParsedArgs by name. Returns the provided value or the option’s default.
pub fn handler(
cmd: Command,
handler: fn(ParsedArgs) -> Nil,
) -> Command
Sets the handler function for the command. The handler receives ParsedArgs only. For database commands, use driver-specific handlers (glimr_sqlite/command.handler or glimr_postgres/command.handler) instead.
pub fn has_flag(parsed: ParsedArgs, name: String) -> Bool
Checks if a flag was provided by the user. Returns True if –name or -short was passed on the CLI. The name parameter should match the Flag’s name field.