clap-rust

Guides building and reviewing Rust command-line interfaces with the clap derive and builder APIs.

2|Updated May 16, 2026
One-click install
npx skills add https://github.com/avbel/ai-skills --skill clap-rust-avbel
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: clap-rust
Source: https://github.com/avbel/ai-skills/tree/main/skills/clap-rust
Command: npx skills add https://github.com/avbel/ai-skills --skill clap-rust-avbel

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes scripts (resource) components.

What problem does it solve? Rust developers repeatedly re-explain clap conventions to AI agents, leading to inconsistent CLI code with manual string parsing, wrong flag actions, and missing tests. This Skill encodes idiomatic clap 4.x patterns so generated CLIs follow derive-first structure, typed value parsers, and proper test coverage. ## Core Features & Use Cases - Derive-first CLI patterns: Conventions for Parser, Args, Subcommand, and ValueEnum with doc-comment help text, flatten for shared flags, and typed defaults. - Typed parsing and validation: Guidance on value_parser ranges, NonEmptyStringValueParser, PathBuf/SocketAddr field types, and when to validate at parse time versus after. - Flags, env, and subcommands: Rules for ArgAction SetTrue/SetFalse/Count/Append, env variable integration with the env feature, and required versus optional subcommands. - Testing and bootstrap script: debug_assert and try_parse_from test patterns plus a helper script that prints starter snippets for derive, builder, subcommand, value-enum, and test scenarios. - Use Case: Ask your agent to scaffold a new Rust CLI with subcommands and env-based configuration, and it produces clap 4.6 derive code with typed parsers and shape-validation tests. ## Quick Start Ask your AI agent to create a Rust CLI with clap derive that has a serve subcommand, a port flag validated to range 1-65535, and a debug_assert test.

Frequently Asked Questions about clap-rust

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I build a CLI in Rust with clap derive?▼

Derive Parser on a top-level struct, use Args for reusable argument groups, Subcommand for command enums, and ValueEnum for finite value sets. Doc comments become help text, Option<T> marks optional values, and default_value_t provides typed defaults.

When should I use clap builder API instead of derive?▼

Use the builder API when arguments are generated dynamically, when augmenting another parser, or when the CLI shape cannot be represented cleanly with derive. For static application CLIs, derive is the preferred style.

How do I read environment variables in clap arguments?▼

Enable the env feature in Cargo.toml, then add env = "VAR_NAME" to the arg attribute. Combine with default_value_t for typed fallbacks, and document the variable name explicitly so it appears clearly in help output.

How do I test a clap CLI without exiting the process?▼

Call Cli::command().debug_assert() via CommandFactory to validate the command shape, and use try_parse_from or try_get_matches_from in tests. Avoid parse() or get_matches() in tests because they exit the process on error.

How do I validate a port number range in clap?▼

Use value_parser with clap::value_parser!(u16).range(1..=65535) on the field. Typed parsers validate at parse time, so you avoid manual string parsing after Clap has run.

How do I implement -v -vv -vvv verbosity flags in clap?▼

Use a u8 field with action = clap::ArgAction::Count on a short and long flag. Each occurrence increments the count, giving you 0-3 verbosity levels without extra parsing logic.