Domain Cli

by actionbook5c40d3ad7851No license1.5K starsListed Oct 8, 2026Updated Oct 8, 2026Repository updated 6 weeks ago

Use when building CLI tools. Keywords: CLI, command line, terminal, clap, structopt, argument parsing, subcommand, interactive, TUI, ratatui, crossterm, indicatif, progress bar, colored output, shell completion, config file, environment variable, 命令行, 终端应用, 参数解析

Instructions onlySoftware Development
AI-generated overview

Guidance for building Rust command-line tools with clap, config layering, progress bars and terminal UI.

What it does
This skill supplies domain constraints and design guidance for building command-line applications in Rust. It covers argument parsing with clap derive macros, subcommands, configuration precedence (CLI over environment over file), exit codes, stdout versus stderr usage, and user feedback such as progress bars. It also lists relevant crates and a sample CLI structure, and points to related skills for error handling, type-driven design, lifecycle and concurrency.
When to use it
Use it when designing or implementing a Rust CLI tool, especially when deciding how to parse arguments, layer configuration, handle errors and exit codes, or add interactive prompts, progress display and terminal UI. It is also useful as a checklist of common CLI mistakes to avoid.
Requirements
No scripts or assets ship with the skill; it is instructions only. Applying it assumes a Rust project with crates such as clap, dialoguer, indicatif, colored, ratatui, crossterm or console available.

CLI Domain

Layer 3: Domain Constraints

Domain Constraints → Design Implications

Domain RuleDesign ConstraintRust Implication
User ergonomicsClear help, errorsclap derive macros
Config precedenceCLI > env > fileLayered config loading
Exit codesNon-zero on errorProper Result handling
Stdout/stderrData vs errorseprintln! for errors
InterruptibleHandle Ctrl+CSignal handling

Critical Constraints

User Communication

RULE: Errors to stderr, data to stdoutWHY: Pipeable output, scriptabilityRUST: eprintln! for errors, println! for data

Configuration Priority

RULE: CLI args > env vars > config file > defaultsWHY: User expectation, override capabilityRUST: Layered config with clap + figment/config

Exit Codes

RULE: Return non-zero on any errorWHY: Script integration, automationRUST: main() -> Result<(), Error> or explicit exit()

Trace Down ↓

From constraints to design (Layer 2):

"Need argument parsing"    ↓ m05-type-driven: Derive structs for args    ↓ clap: #[derive(Parser)]
"Need config layering"    ↓ m09-domain: Config as domain object    ↓ figment/config: Layer sources
"Need progress display"    ↓ m12-lifecycle: Progress bar as RAII    ↓ indicatif: ProgressBar

Key Crates

PurposeCrate
Argument parsingclap
Interactive promptsdialoguer
Progress barsindicatif
Colored outputcolored
Terminal UIratatui
Terminal controlcrossterm
Console utilitiesconsole

Design Patterns

PatternPurposeImplementation
Args structType-safe args#[derive(Parser)]
SubcommandsCommand hierarchy#[derive(Subcommand)]
Config layersOverride precedenceCLI > env > file
ProgressUser feedbackProgressBar::new(len)

Code Pattern: CLI Structure

rust
use clap::{Parser, Subcommand};
#[derive(Parser)]#[command(name = "myapp", about = "My CLI tool")]struct Cli {    /// Enable verbose output    #[arg(short, long)]    verbose: bool,
    #[command(subcommand)]    command: Commands,}
#[derive(Subcommand)]enum Commands {    /// Initialize a new project    Init { name: String },    /// Run the application    Run {        #[arg(short, long)]        port: Option<u16>,    },}
fn main() -> anyhow::Result<()> {    let cli = Cli::parse();    match cli.command {        Commands::Init { name } => init_project(&name)?,        Commands::Run { port } => run_server(port.unwrap_or(8080))?,    }    Ok(())}

Common Mistakes

MistakeDomain ViolationFix
Errors to stdoutBreaks pipingeprintln!
No help textPoor UX#[arg(help = "...")]
Panic on errorBad exit codeResult + proper handling
No progress for long opsUser uncertaintyindicatif

Trace to Layer 1

ConstraintLayer 2 PatternLayer 1 Implementation
Type-safe argsDerive macrosclap Parser
Error handlingResult propagationanyhow + exit codes
User feedbackProgress RAIIindicatif ProgressBar
Config precedenceBuilder patternLayered sources

Related Skills

WhenSee
Error handlingm06-error-handling
Type-driven argsm05-type-driven
Progress lifecyclem12-lifecycle
Async CLIm07-concurrency

Source and attribution

Source:actionbook/rust-skillsinskills/domain-cliat commit5c40d3a

License: No license

Content belongs to its original authors. SourceWeft indexes it from a public repository.

Report or request removal