npx skills add https://github.com/zhanghandong/rust-skills --skill domain-cli第 3 层:领域约束
| 领域规则 | 设计约束 | Rust 实现含义 |
|---|---|---|
| 用户人体工程学 | 清晰的帮助和错误信息 | clap 派生宏 |
| 配置优先级 | CLI > 环境变量 > 文件 | 分层配置加载 |
| 退出码 | 出错时返回非零值 | 正确处理 Result |
| 标准输出/标准错误输出 | 数据 vs 错误 | 使用 eprintln! 输出错误 |
| 可中断性 | 处理 Ctrl+C | 信号处理 |
RULE: Errors to stderr, data to stdout
WHY: Pipeable output, scriptability
RUST: eprintln! for errors, println! for data
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
RULE: CLI args > env vars > config file > defaults
WHY: User expectation, override capability
RUST: Layered config with clap + figment/config
RULE: Return non-zero on any error
WHY: Script integration, automation
RUST: main() -> Result<(), Error> or explicit exit()
从约束到设计(第 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
| 用途 | 库 |
|---|---|
| 参数解析 | clap |
| 交互式提示 | dialoguer |
| 进度条 | indicatif |
| 彩色输出 | colored |
| 终端用户界面 | ratatui |
| 终端控制 | crossterm |
| 控制台工具 | console |
| 模式 | 目的 | 实现 |
|---|---|---|
| 参数结构体 | 类型安全的参数 | #[derive(Parser)] |
| 子命令 | 命令层次结构 | #[derive(Subcommand)] |
| 配置层 | 覆盖优先级 | CLI > env > file |
| 进度 | 用户反馈 | ProgressBar::new(len) |
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(())
}
| 错误 | 违反的领域规则 | 修复方法 |
|---|---|---|
| 错误输出到 stdout | 破坏管道操作 | eprintln! |
| 没有帮助文本 | 用户体验差 | #[arg(help = "...")] |
| 出错时 panic | 退出码不正确 | Result + 正确处理 |
| 长时间操作无进度显示 | 用户不确定 | indicatif |
| 约束 | 第 2 层模式 | 第 1 层实现 |
|---|---|---|
| 类型安全的参数 | 派生宏 | clap Parser |
| 错误处理 | Result 传播 | anyhow + 退出码 |
| 用户反馈 | 进度 RAII | indicatif ProgressBar |
| 配置优先级 | 构建器模式 | 分层源 |
| 何时需要 | 参见 |
|---|---|
| 错误处理 | m06-error-handling |
| 类型驱动参数 | m05-type-driven |
| 进度生命周期 | m12-lifecycle |
| 异步 CLI | m07-concurrency |
每周安装量
534
代码仓库
GitHub 星标数
920
首次出现
Jan 20, 2026
安全审计
安装于
opencode487
codex473
gemini-cli466
github-copilot451
amp409
kimi-cli403
Layer 3: Domain Constraints
| 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 |
RULE: Errors to stderr, data to stdout
WHY: Pipeable output, scriptability
RUST: eprintln! for errors, println! for data
RULE: CLI args > env vars > config file > defaults
WHY: User expectation, override capability
RUST: Layered config with clap + figment/config
RULE: Return non-zero on any error
WHY: Script integration, automation
RUST: main() -> Result<(), Error> or explicit exit()
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
| Purpose | Crate |
|---|---|
| Argument parsing | clap |
| Interactive prompts | dialoguer |
| Progress bars | indicatif |
| Colored output | colored |
| Terminal UI | ratatui |
| Terminal control | crossterm |
| Console utilities | console |
| Pattern | Purpose | Implementation |
|---|---|---|
| Args struct | Type-safe args | #[derive(Parser)] |
| Subcommands | Command hierarchy | #[derive(Subcommand)] |
| Config layers | Override precedence | CLI > env > file |
| Progress | User feedback | ProgressBar::new(len) |
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(())
}
| 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 |
| 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 |
| When | See |
|---|---|
| Error handling | m06-error-handling |
| Type-driven args | m05-type-driven |
| Progress lifecycle | m12-lifecycle |
| Async CLI | m07-concurrency |
Weekly Installs
534
Repository
GitHub Stars
920
First Seen
Jan 20, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
opencode487
codex473
gemini-cli466
github-copilot451
amp409
kimi-cli403
agent-browser 浏览器自动化工具 - Vercel Labs 命令行网页操作与测试
136,300 周安装
FlowStudio MCP 构建部署 Power Automate 云流指南 | 自动化流程开发
530 周安装
mcp2cli:无需代码,将MCP/OpenAPI/GraphQL实时转换为命令行工具
530 周安装
交互式作品集设计指南:30秒吸引招聘者,提升作品集转化率与个人品牌
530 周安装
每日销售简报AI工具 - 自动生成优先级行动计划,整合日历、CRM和邮件数据
531 周安装
生产排程实战指南:离散制造工厂的有限产能排程、换线优化与瓶颈管理
531 周安装
Angular 21 最佳实践指南:TypeScript、Signals、组件与性能优化
531 周安装