Modern type-safe Rust CLI patterns with Clap derive macros, Parser trait, Subcommand enums, validation, and value parsers. Use when building CLI applications, creating Clap commands, implementing type-safe Rust CLIs, or when user mentions Clap, CLI patterns, Rust command-line, derive macros, Parser trait, Subcommands, or command-line interfaces.
/plugin marketplace add vanman2024/cli-builder/plugin install cli-builder@cli-builderThis skill is limited to using the following tools:
examples/quick-start.mdexamples/real-world-cli.mdexamples/validation-examples.mdscripts/generate-completions.shscripts/test-cli.shscripts/validate-cargo.shtemplates/basic-parser.rstemplates/builder-pattern.rstemplates/env-variables.rstemplates/full-featured-cli.rstemplates/subcommands.rstemplates/value-enum.rstemplates/value-parser.rsProvides modern type-safe Rust CLI patterns using Clap 4.x with derive macros, Parser trait, Subcommand enums, custom validation, value parsers, and environment variable integration for building maintainable command-line applications.
Use derive macros for automatic CLI parsing with type safety:
use clap::Parser;
#[derive(Parser)]
#[command(name = "myapp")]
#[command(author, version, about, long_about = None)]
struct Cli {
/// Input file path
#[arg(short, long, value_name = "FILE")]
input: std::path::PathBuf,
/// Optional output file
#[arg(short, long)]
output: Option<std::path::PathBuf>,
/// Verbose mode
#[arg(short, long)]
verbose: bool,
/// Number of items to process
#[arg(short, long, default_value_t = 10)]
count: usize,
}
fn main() {
let cli = Cli::parse();
if cli.verbose {
println!("Processing: {:?}", cli.input);
}
}
Organize complex CLIs with nested subcommands:
use clap::{Parser, Subcommand};
#[derive(Parser)]
#[command(name = "git")]
struct Cli {
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand)]
enum Commands {
/// Add files to staging
Add {
/// Files to add
#[arg(value_name = "FILE")]
files: Vec<String>,
},
/// Commit changes
Commit {
/// Commit message
#[arg(short, long)]
message: String,
},
}
Implement custom parsing and validation:
use clap::Parser;
use std::ops::RangeInclusive;
const PORT_RANGE: RangeInclusive<usize> = 1..=65535;
fn port_in_range(s: &str) -> Result<u16, String> {
let port: usize = s
.parse()
.map_err(|_| format!("`{s}` isn't a valid port number"))?;
if PORT_RANGE.contains(&port) {
Ok(port as u16)
} else {
Err(format!("port not in range {}-{}", PORT_RANGE.start(), PORT_RANGE.end()))
}
}
#[derive(Parser)]
struct Cli {
/// Port to listen on
#[arg(short, long, value_parser = port_in_range)]
port: u16,
}
Support environment variables with fallback:
use clap::Parser;
#[derive(Parser)]
struct Cli {
/// API key (or set API_KEY env var)
#[arg(long, env = "API_KEY")]
api_key: String,
/// Database URL
#[arg(long, env = "DATABASE_URL")]
database_url: String,
/// Optional log level
#[arg(long, env = "LOG_LEVEL", default_value = "info")]
log_level: String,
}
Use ValueEnum for type-safe option selection:
use clap::{Parser, ValueEnum};
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
enum Format {
Json,
Yaml,
Toml,
}
#[derive(Parser)]
struct Cli {
/// Output format
#[arg(value_enum, short, long, default_value_t = Format::Json)]
format: Format,
}
The following Rust templates demonstrate Clap patterns:
Helper scripts for Clap development:
Choose the appropriate template based on your CLI complexity:
basic-parser.rssubcommands.rsvalue-parser.rsenv-variables.rsAdd Clap to Cargo.toml:
[dependencies]
clap = { version = "4.5", features = ["derive", "env"] }
Implement your CLI using the selected template as a starting point
Generate completions using the provided script for better UX
--help)default_value_t#[arg(short, long, num_args = 1..)]
files: Vec<PathBuf>,
#[arg(long, required_unless_present = "config")]
database_url: Option<String>,
#[arg(long, conflicts_with = "json")]
yaml: bool,
#[arg(global = true, short, long)]
verbose: bool,
Run the test script to validate your CLI:
bash scripts/test-cli.sh your-binary
This tests:
--help)--version)skills/clap-patterns/templates/skills/clap-patterns/scripts/skills/clap-patterns/examples/This skill should be used when the user asks to "create an agent", "add an agent", "write a subagent", "agent frontmatter", "when to use description", "agent examples", "agent tools", "agent colors", "autonomous agent", or needs guidance on agent structure, system prompts, triggering conditions, or agent development best practices for Claude Code plugins.
This skill should be used when the user asks to "create a slash command", "add a command", "write a custom command", "define command arguments", "use command frontmatter", "organize commands", "create command with file references", "interactive command", "use AskUserQuestion in command", or needs guidance on slash command structure, YAML frontmatter fields, dynamic arguments, bash execution in commands, user interaction patterns, or command development best practices for Claude Code.
This skill should be used when the user asks to "create a hook", "add a PreToolUse/PostToolUse/Stop hook", "validate tool use", "implement prompt-based hooks", "use ${CLAUDE_PLUGIN_ROOT}", "set up event-driven automation", "block dangerous commands", or mentions hook events (PreToolUse, PostToolUse, Stop, SubagentStop, SessionStart, SessionEnd, UserPromptSubmit, PreCompact, Notification). Provides comprehensive guidance for creating and implementing Claude Code plugin hooks with focus on advanced prompt-based hooks API.