CLI entrypoints with clap + Effect

Rust’s standard answer for parsing is clap. For id_effect, treat the binary as a thin shell:

  1. Parse argv into a struct (#[derive(clap::Parser)]).
  2. Assemble the environment your program needs — its capability list R (HNil, a single capability, or an HCons list such as id_effect_config::ConfigEnv) — from flags and process environment.
  3. Run your program Effect<A, E, R> at the edge with id_effect_cli::run_main, which provides the environment and calls block_on on the default runtime.
  4. Exit with the std::process::ExitCode it returns (see the next sections).

This mirrors Effect.ts @effect/cli: declarative argument models composed into a single program — here the “program” is an Effect value rather than a Future chain.

Returning ExitCode keeps main testable and avoids calling std::process::exit deep inside library code. This is examples/cli-minimal/src/main.rs:

use clap::Parser;
use id_effect_cli::{RunMainConfig, run_main};

#[derive(Parser, Debug)]
struct Cli {
    /// API token (loaded into config as `API_TOKEN`, then wrapped as a `Secret`).
    #[arg(long)]
    token: String,
}

fn main() -> std::process::ExitCode {
    let cli = Cli::parse();
    let (eff, env) = cli_minimal::app_effect(cli.token); // Effect<(), ConfigError, ConfigEnv> + its ConfigEnv
    run_main(eff, env, RunMainConfig::with_tracing())
}

run_main(effect, env, config) accepts a Send Effect or a !Send LocalEffect over the capability list R; pass HNil when the effect needs nothing. A missing capability is a compile error, not a runtime failure. A complete version of this shape, with configuration from the environment and tests, is the cookbook example 153_cookbook_cli_edge (cargo run -p cli_minimal --example 153_cookbook_cli_edge -- --name Ada).

Call run_main from main only: it parks the calling thread, and inside an async context (a tokio task) it runs nothing and exits 101.

Workspace helpers

The id_effect_cli crate provides:

  • An optional clap dependency (feature clap, on by default).
  • run_main — provides env, optionally installs the in-process tracing buffer (RunMainConfig::with_tracing() runs the program under tracing_hook(), so its with_spans show up in snapshot_tracing()), runs the effect, reports a failure on stderr, and maps the whole Exit to an ExitCode. RunMainConfig::minimal() skips tracing.
  • exit_code_for_exit / exit_code_for_cause / exit_code_for_result when you already have an Exit, a Cause or a Result (from block_on in a custom driver, a test, or a supervisor).
#![allow(unused)]
fn main() {
use std::process::ExitCode;
use id_effect::{Effect, HNil, fail, succeed};
use id_effect_cli::{RunMainConfig, run_main};

fn greet(name: &str) -> Effect<(), String> {
    if name.is_empty() {
        fail("name must not be empty".to_string())
    } else {
        let line = format!("hello, {name}");
        succeed(()).map(move |()| println!("{line}"))
    }
}

assert_eq!(run_main(greet("ada"), HNil, RunMainConfig::minimal()), ExitCode::SUCCESS);
assert_eq!(run_main(greet(""), HNil, RunMainConfig::minimal()), ExitCode::from(1));
}

Template

See the checked-in example examples/cli-minimal (--token …, Secret via id_effect_config).

Further reading