clap-patterns
Common Clap patterns and idioms for argument parsing, validation, and CLI design. Use when implementing CLI arguments with Clap v4+.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Common Clap patterns and idioms for argument parsing, validation, and CLI design. Use when implementing CLI arguments with Clap v4+.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
Configuration management patterns including file formats, precedence, environment variables, and XDG directories. Use when implementing configuration systems for CLI applications.
Distribution and packaging patterns including shell completions, man pages, cross-compilation, and release automation. Use when preparing CLI tools for distribution.
CLI user experience best practices for error messages, colors, progress indicators, and output formatting. Use when improving CLI usability and user experience.
Advanced concurrency patterns for Tokio including fan-out/fan-in, pipeline processing, rate limiting, and coordinated shutdown. Use when building high-concurrency async systems.
Network programming patterns with Hyper, Tonic, and Tower. Use when building HTTP services, gRPC applications, implementing middleware, connection pooling, or health checks.
Common Tokio patterns and idioms for async programming. Use when implementing worker pools, request-response patterns, pub/sub, timeouts, retries, or graceful shutdown.
| name | clap-patterns |
| description | Common Clap patterns and idioms for argument parsing, validation, and CLI design. Use when implementing CLI arguments with Clap v4+. |
Common patterns and idioms for using Clap v4+ effectively in Rust CLI applications.
#[derive(Parser)]
#[command(version, about)]
struct Cli {
#[arg(short, long)]
input: PathBuf,
}
fn build_cli() -> Command {
Command::new("app")
.arg(Arg::new("input").short('i'))
}
#[derive(Parser)]
struct Cli {
#[arg(short, long, global = true, action = ArgAction::Count)]
verbose: u8,
#[command(subcommand)]
command: Commands,
}
#[derive(Parser)]
#[command(group(
ArgGroup::new("format")
.required(true)
.args(&["json", "yaml", "toml"])
))]
struct Cli {
#[arg(long)]
json: bool,
#[arg(long)]
yaml: bool,
#[arg(long)]
toml: bool,
}
fn parse_port(s: &str) -> Result<u16, String> {
let port: u16 = s.parse()
.map_err(|_| format!("`{s}` isn't a valid port"))?;
if (1024..=65535).contains(&port) {
Ok(port)
} else {
Err(format!("port not in range 1024-65535"))
}
}
#[derive(Parser)]
struct Cli {
#[arg(long, value_parser = parse_port)]
port: u16,
}
#[derive(Parser)]
struct Cli {
#[arg(long, env = "API_TOKEN")]
token: String,
#[arg(long, env = "API_ENDPOINT", default_value = "https://api.example.com")]
endpoint: String,
}
#[derive(Args)]
struct CommonOpts {
#[arg(short, long)]
verbose: bool,
#[arg(short, long)]
config: Option<PathBuf>,
}
#[derive(Parser)]
struct Cli {
#[command(flatten)]
common: CommonOpts,
#[command(subcommand)]
command: Commands,
}
#[derive(Parser)]
struct Cli {
/// Tags (can be specified multiple times)
#[arg(short, long)]
tag: Vec<String>,
/// Files to process
files: Vec<PathBuf>,
}
// Usage: myapp --tag rust --tag cli file1.txt file2.txt
#[derive(Parser)]
struct Cli {
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand)]
enum Commands {
Build(BuildArgs),
Test(TestArgs),
}
#[derive(Args)]
struct BuildArgs {
#[command(flatten)]
common: CommonOpts,
#[arg(short, long)]
release: bool,
}
#[derive(Parser)]
struct Cli {
/// Verbosity (-v, -vv, -vvv)
#[arg(short, long, action = ArgAction::Count)]
verbose: u8,
}
// Usage: -v (1), -vv (2), -vvv (3)
#[derive(Parser)]
#[command(
after_help = "EXAMPLES:\n \
myapp --input file.txt\n \
myapp -i file.txt -vv\n\n\
For more info: https://example.com"
)]
struct Cli {
// ...
}
#[derive(Parser)]
struct Cli {
#[arg(short, long, value_name = "FILE", value_hint = ValueHint::FilePath)]
input: PathBuf,
#[arg(short, long, value_name = "DIR", value_hint = ValueHint::DirPath)]
output: PathBuf,
#[arg(short, long, value_name = "URL", value_hint = ValueHint::Url)]
endpoint: String,
}
fn default_config_path() -> PathBuf {
dirs::config_dir()
.unwrap()
.join("myapp")
.join("config.toml")
}
#[derive(Parser)]
struct Cli {
#[arg(long, default_value_os_t = default_config_path())]
config: PathBuf,
}
value_name for clearer help textValueEnum for fixed set of choicesafter_help