domain-cli
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCLI Domain
CLI 领域
Layer 3: Domain Constraints
第3层:领域约束
Domain Constraints → Design Implications
领域约束 → 设计影响
| Domain Rule | Design Constraint | Rust Implication |
|---|---|---|
| User ergonomics | Clear help, errors | clap derive macros |
| Config precedence | CLI > env > file | Layered config loading |
| Exit codes | Non-zero on error | Proper Result handling |
| Stdout/stderr | Data vs errors | eprintln! for errors |
| Interruptible | Handle Ctrl+C | Signal handling |
| 领域规则 | 设计约束 | Rust 实现建议 |
|---|---|---|
| 用户易用性 | 清晰的帮助信息与错误提示 | clap derive macros |
| 配置优先级 | CLI 参数 > 环境变量 > 配置文件 | 分层配置加载 |
| 退出码 | 出错时返回非零值 | 正确的 Result 处理 |
| 标准输出/标准错误 | 数据输出到stdout,错误输出到stderr | 使用eprintln!输出错误 |
| 可中断性 | 处理Ctrl+C | 信号处理 |
Critical Constraints
关键约束
User Communication
用户通信
RULE: Errors to stderr, data to stdout
WHY: Pipeable output, scriptability
RUST: eprintln! for errors, println! for dataRULE: Errors to stderr, data to stdout
WHY: Pipeable output, scriptability
RUST: eprintln! for errors, println! for dataConfiguration Priority
配置优先级
RULE: CLI args > env vars > config file > defaults
WHY: User expectation, override capability
RUST: Layered config with clap + figment/configRULE: CLI args > env vars > config file > defaults
WHY: User expectation, override capability
RUST: Layered config with clap + figment/configExit Codes
退出码
RULE: Return non-zero on any error
WHY: Script integration, automation
RUST: main() -> Result<(), Error> or explicit exit()RULE: Return non-zero on any error
WHY: Script integration, automation
RUST: 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从约束到设计(第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: ProgressBarKey Crates
关键依赖库(Crates)
| Purpose | Crate |
|---|---|
| Argument parsing | clap |
| Interactive prompts | dialoguer |
| Progress bars | indicatif |
| Colored output | colored |
| Terminal UI | ratatui |
| Terminal control | crossterm |
| Console utilities | console |
| 用途 | Crate |
|---|---|
| 参数解析 | clap |
| 交互式提示 | dialoguer |
| 进度条 | indicatif |
| 彩色输出 | colored |
| 终端UI | ratatui |
| 终端控制 | crossterm |
| 控制台工具 | console |
Design Patterns
设计模式
| Pattern | Purpose | Implementation |
|---|---|---|
| Args struct | Type-safe args | |
| Subcommands | Command hierarchy | |
| Config layers | Override precedence | CLI > env > file |
| Progress | User feedback | |
| 模式 | 用途 | 实现方式 |
|---|---|---|
| 参数结构体 | 类型安全的参数处理 | |
| 子命令 | 命令层级结构 | |
| 配置分层 | 覆盖优先级 | CLI > 环境变量 > 配置文件 |
| 进度显示 | 用户反馈 | |
Code Pattern: CLI Structure
代码模式:CLI 结构
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(())
}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
常见错误
| Mistake | Domain Violation | Fix |
|---|---|---|
| Errors to stdout | Breaks piping | eprintln! |
| No help text | Poor UX | #[arg(help = "...")] |
| Panic on error | Bad exit code | Result + proper handling |
| No progress for long ops | User uncertainty | indicatif |
| 错误 | 违反的领域规则 | 修复方案 |
|---|---|---|
| 错误输出到stdout | 破坏管道功能 | 使用eprintln! |
| 无帮助文本 | 糟糕的用户体验 | 添加#[arg(help = "...")] |
| 出错时触发Panic | 错误的退出码 | 使用Result+正确的错误处理 |
| 长时间操作无进度提示 | 用户不确定状态 | 使用indicatif |
Trace to Layer 1
追溯到第1层
| Constraint | Layer 2 Pattern | Layer 1 Implementation |
|---|---|---|
| Type-safe args | Derive macros | clap Parser |
| Error handling | Result propagation | anyhow + exit codes |
| User feedback | Progress RAII | indicatif ProgressBar |
| Config precedence | Builder pattern | Layered sources |
| 约束 | 第2层模式 | 第1层实现 |
|---|---|---|
| 类型安全的参数 | 派生宏 | clap Parser |
| 错误处理 | Result传播 | anyhow + 退出码 |
| 用户反馈 | 进度条RAII模式 | indicatif ProgressBar |
| 配置优先级 | 构建器模式 | 分层数据源 |
Related Skills
相关技能
| When | See |
|---|---|
| Error handling | m06-error-handling |
| Type-driven args | m05-type-driven |
| Progress lifecycle | m12-lifecycle |
| Async CLI | m07-concurrency |
| 场景 | 参考内容 |
|---|---|
| 错误处理 | m06-error-handling |
| 类型驱动的参数 | m05-type-driven |
| 进度条生命周期 | m12-lifecycle |
| 异步CLI | m07-concurrency |