Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Console Commands

A module adds a command to the application. lat <name> runs it from the project, after the application has booted.

Register a command

use std::sync::Arc;
use laterite_core::command::{Command, CommandCx, CommandError, CommandReg};
use laterite_core::t;

struct Greet;

#[async_trait::async_trait]
impl Command for Greet {
    async fn run(&self, cx: &CommandCx<'_>, args: &[String]) -> Result<(), CommandError> {
        let name = args.first().ok_or("usage: greet <name>")?;
        println!("Hello {name}, from {}", cx.storage_dir());
        Ok(())
    }
}

impl Module for AcmeBlog {
    fn register(&self, registry: &mut Registry) {
        registry.add(CommandReg::new("greet", t!("Greet someone"), Arc::new(Greet)));
    }
}
lat greet Ada
lat help-app
CommandReg::newDescription
nameThe word that runs it: lowercase letters, digits and hyphens.
aboutOne line in lat help-app.
commandWhat runs.
CommandCxGives
db()The application’s database, migrated.
registry()Every module’s registrations.
storage_dir()backend.storage.

Parse arguments

use clap::Parser;

#[derive(Parser)]
struct Args {
    name: String,
    #[arg(long)]
    loud: bool,
}

async fn run(&self, cx: &CommandCx<'_>, args: &[String]) -> Result<(), CommandError> {
    let args = Args::try_parse_from(std::iter::once("greet".to_string()).chain(args.iter().cloned()))?;
    println!("{}", if args.loud { args.name.to_uppercase() } else { args.name });
    Ok(())
}
CaseResult
run returns an errorThe message is printed and the binary exits non-zero.
A name the binary answers itself (serve, admin, routes, seed, build, publish, queue, schedule, help)The start aborts.
Two modules register one nameThe start aborts.
An unknown nameThe error lists every command.

The application’s main must call Bootstrap::run, as lat new writes it.