CLI entrypoints with clap + Effect
Rust’s standard answer for parsing is clap. For id_effect, treat the binary as a thin shell:
- Parse argv into a struct (
#[derive(clap::Parser)]). - Assemble the environment your program needs — its capability list
R(HNil, a single capability, or anHConslist such asid_effect_config::ConfigEnv) — from flags and process environment. - Run your program
Effect<A, E, R>at the edge withid_effect_cli::run_main, which provides the environment and callsblock_onon the default runtime. - Exit with the
std::process::ExitCodeit 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.
Recommended main shape
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
clapdependency (featureclap, on by default). run_main— providesenv, optionally installs the in-process tracing buffer (RunMainConfig::with_tracing()runs the program undertracing_hook(), so itswith_spans show up insnapshot_tracing()), runs the effect, reports a failure on stderr, and maps the wholeExitto anExitCode.RunMainConfig::minimal()skips tracing.exit_code_for_exit/exit_code_for_cause/exit_code_for_resultwhen you already have anExit, aCauseor aResult(fromblock_onin 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
- Exit codes for
main—Exit/Cause→ExitCodetable - Config +
Secretfrom flags — wiringid_effect_configat the edge - Error handling —
Causevs plainE - Configuration (
id_effect_config)