agentleFS
gurinderugurinderu/craft/skills/rust-cli/SKILL.md

rust-cli

Skill1 starsChanged 39 days agoMITView on GitHub

Command-line and terminal apps in Rust — argument parsing with clap, subcommands, config, output/exit-code conventions, and full-screen TUIs with ratatui. Use when building a CLI, parsing args/subcommands, designing output, or writing a terminal UI. Triggers: clap, subcommand, exit code, TUI, ratatui, crossterm, indicatif.

8 other projects use a version of this.

Adopted by

Most recently taken up by

Used with

Projects with it also use rust-observability and rust-database.

Other files people use alongside this one.

Files found in the same public projects as a version of this one, more often than they turn up anywhere else. Each project counts once, large collections count for less, and this file's own repository is left out.

The file

Files
SKILL.md

Rust CLI & TUI

Terminal programs come in two shapes with different models — pick the right one first. Both put real logic in a library and keep the entrypoint thin (→ rust-architecture, rust-ecosystem).

When to Use

  • Building a command-line tool (args, subcommands, config, output)
  • Designing exit codes and stdout/stderr behavior
  • Writing an interactive full-screen terminal UI

Decision: CLI or TUI?

You're buildingModelCrateWhere
A tool: parse args → do work → print → exitone-shot, non-interactiveclapclap.md
A full-screen interactive app (dashboard, editor)render loop + events + stateratatuitui.md

Most "CLIs" are the first kind — don't reach for a TUI unless the app is genuinely interactive and stateful. They can coexist (a clap subcommand that launches a TUI).

CLI anatomy

use anyhow::Result;
use clap::Parser;

#[derive(Parser)]
#[command(version, about)]
struct Cli { /* args — see clap.md */ }

fn main() -> std::process::ExitCode {
    if let Err(e) = run(Cli::parse()) {
        eprintln!("error: {e:#}");          // errors to stderr, with the full chain
        return std::process::ExitCode::FAILURE;
    }
    std::process::ExitCode::SUCCESS
}

fn run(cli: Cli) -> Result<()> { /* the actual work, returns Result */ }

main parses and maps the result to an exit code; run holds the logic and returns Result (→ rust-errors CLI pattern). Keep run thin too — call into a library.

Output conventions

A well-behaved CLI is a good Unix citizen:

  • stdout = data, stderr = diagnostics. Machine-readable results go to stdout so they pipe; logs, progress, and errors go to stderr.
  • Exit codes: 0 success, non-zero failure. Use distinct codes if scripts need to branch.
  • Respect the pipe: disable color/progress when stdout isn't a TTY (clap's color does this; std::io::IsTerminal / the anstream crate help). Don't emit ANSI into a file.
  • --quiet / --verbose to control chatter; offer --json for scriptable output where it makes sense.
  • Progress for long work: the indicatif crate (indicatif = "0.18") — to stderr.

Config layering

Precedence, highest first: CLI flags → environment → config file → defaults. clap reads flags and env (#[arg(env = "...")]); for file + merge, layer with figment (or the config crate) and let flags win. Validate the merged config once at startup — a bad config should fail fast, not mid-run (→ rust-errors: startup precondition is a defect).

figment merges typed config from layered providers, last wins:

[dependencies]
figment = { version = "0.10", features = ["toml", "env"] }
serde = { version = "1", features = ["derive"] }
use figment::{Figment, providers::{Format, Toml, Env, Serialized}};

#[derive(Default, serde::Deserialize, serde::Serialize)]
struct Config { port: u16, log_level: String }

let config: Config = Figment::new()
    .merge(Serialized::defaults(Config::default()))  // defaults
    .merge(Toml::file("app.toml"))                    // file overrides defaults
    .merge(Env::prefixed("APP_"))                     // env overrides file (APP_PORT=...)
    .extract()?;                                      // typed + validated in one step

Layer CLI flags on top last (highest precedence) by merging parsed clap values. The same figment setup serves long-running services too (→ rust-cloud-native config). Load secrets into a secrecy::SecretString rather than a plain field (→ rust-security).

Boundaries

  • Argument parsing, subcommands, derive API → clap.md.
  • Interactive full-screen UIs (render loop, widgets, events) → tui.md.
  • Error reporting style (anyhow at the top, eprintln!, exit) → rust-errors.
  • Logic-in-lib, thin binary → rust-ecosystem (project layout), rust-architecture.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.