Comprehensive best practices guide for building CLI applications in Rust using clap. Contains 42 rules across 8 categories, prioritized by impact to guide CLI design, argument parsing, and testing.
When to Apply
Reference these guidelines when:
Designing new Rust CLI applications
Adding arguments or subcommands to existing CLIs
Validating and parsing command-line input
Writing integration tests for CLI tools
Improving help text and user experience
Rule Categories by Priority
Priority
Category
Impact
Prefix
1
Type-Driven Design
CRITICAL
type-
2
Derive API Patterns
CRITICAL
derive-
3
Argument Configuration
HIGH
arg-
4
Validation & Parsing
HIGH
valid-
5
Subcommand Architecture
MEDIUM-HIGH
subcmd-
6
Help & Documentation
MEDIUM
help-
7
Error Handling
MEDIUM
error-
8
Testing Patterns
LOW-MEDIUM
test-
Quick Reference
1. Type-Driven Design (CRITICAL)
type-valueenum-enums - Use ValueEnum for enumerated arguments
type-option-optional - Use Option for truly optional arguments
type-pathbuf-files - Use PathBuf for file system arguments
type-vec-multiple - Use Vec for multiple value arguments
type-newtype-semantic - Use newtypes for semantic distinction
type-bool-flags - Use bool for simple flags
2. Derive API Patterns (CRITICAL)
derive-parser-entry - Derive Parser for CLI entry point
derive-command-metadata - Use Command attribute for metadata
derive-subcommand-enum - Use Subcommand derive for command hierarchies
derive-args-reusable - Derive Args for reusable argument groups
derive-doc-comments - Use doc comments for help text
derive-global-options - Use Global for cross-subcommand options
derive-propagate-version - Propagate version to subcommands
3. Argument Configuration (HIGH)
arg-default-value - Use default_value for sensible defaults
arg-env-fallback - Use env for environment variable fallback
arg-short-long - Provide both short and long option names
arg-conflicts-with - Use conflicts_with for mutually exclusive options
arg-requires - Use requires for dependent arguments
arg-value-name - Use value_name for descriptive placeholders
4. Validation & Parsing (HIGH)
valid-value-parser - Use value_parser for custom validation
valid-possible-values - Use PossibleValuesParser for string constraints
valid-fromstr-types - Implement FromStr for domain types
valid-try-parse - Use try_parse for graceful error handling
valid-num-args - Use num_args for value count constraints
5. Subcommand Architecture (MEDIUM-HIGH)
subcmd-nested-hierarchy - Use nested subcommands for complex CLIs
subcmd-args-struct - Use struct for subcommand arguments
subcmd-required-help - Require subcommand or show help
subcmd-arg-groups - Use ArgGroup for one-of-many requirements
subcmd-external - Use external subcommands for plugin systems
6. Help & Documentation (MEDIUM)
help-shell-completions - Generate shell completions with clap_complete
help-next-heading - Use next_help_heading for organized help
help-after-help - Use after_help for examples and context
help-hide-options - Hide advanced options from default help
7. Error Handling (MEDIUM)
error-exit-codes - Use appropriate exit codes
error-context - Add context to error messages
error-suggestions - Enable suggestions for typos
error-color-styles - Use colored output for error visibility
8. Testing Patterns (LOW-MEDIUM)
test-assert-cmd - Use assert_cmd for integration testing
test-predicates - Use predicates for flexible assertions
test-temp-files - Use assert_fs for temporary test files
test-parse-from - Use parse_from for unit testing parsers
test-trycmd-snapshots - Use trycmd for snapshot testing
How to Use
Read individual reference files for detailed explanations and code examples:
Section definitions - Category structure and impact levels
Rule template - Template for adding new rules
Reference Files
File
Description
references/_sections.md
Category definitions and ordering
assets/templates/_template.md
Template for new rules
metadata.json
Version and reference information
1---2name: rust-clap3description: Rust Clap Best Practices4---5# Rust Clap Best Practices67Comprehensive best practices guide for building CLI applications in Rust using clap. Contains 42 rules across 8 categories, prioritized by impact to guide CLI design, argument parsing, and testing.89## When to Apply1011Reference these guidelines when:12- Designing new Rust CLI applications13- Adding arguments or subcommands to existing CLIs14- Validating and parsing command-line input15- Writing integration tests for CLI tools16- Improving help text and user experience1718## Rule Categories by Priority1920| Priority | Category | Impact | Prefix |21|----------|----------|--------|--------|22| 1 | Type-Driven Design | CRITICAL | `type-` |23| 2 | Derive API Patterns | CRITICAL | `derive-` |24| 3 | Argument Configuration | HIGH | `arg-` |25| 4 | Validation & Parsing | HIGH | `valid-` |26| 5 | Subcommand Architecture | MEDIUM-HIGH | `subcmd-` |27| 6 | Help & Documentation | MEDIUM | `help-` |28| 7 | Error Handling | MEDIUM | `error-` |29| 8 | Testing Patterns | LOW-MEDIUM | `test-` |3031## Quick Reference3233### 1. Type-Driven Design (CRITICAL)3435- [`type-valueenum-enums`](references/type-valueenum-enums.md) - Use ValueEnum for enumerated arguments36- [`type-option-optional`](references/type-option-optional.md) - Use Option for truly optional arguments37- [`type-pathbuf-files`](references/type-pathbuf-files.md) - Use PathBuf for file system arguments38- [`type-vec-multiple`](references/type-vec-multiple.md) - Use Vec for multiple value arguments39- [`type-newtype-semantic`](references/type-newtype-semantic.md) - Use newtypes for semantic distinction40- [`type-bool-flags`](references/type-bool-flags.md) - Use bool for simple flags4142### 2. Derive API Patterns (CRITICAL)4344- [`derive-parser-entry`](references/derive-parser-entry.md) - Derive Parser for CLI entry point45- [`derive-command-metadata`](references/derive-command-metadata.md) - Use Command attribute for metadata46- [`derive-subcommand-enum`](references/derive-subcommand-enum.md) - Use Subcommand derive for command hierarchies47- [`derive-args-reusable`](references/derive-args-reusable.md) - Derive Args for reusable argument groups48- [`derive-doc-comments`](references/derive-doc-comments.md) - Use doc comments for help text49- [`derive-global-options`](references/derive-global-options.md) - Use Global for cross-subcommand options50- [`derive-propagate-version`](references/derive-propagate-version.md) - Propagate version to subcommands5152### 3. Argument Configuration (HIGH)5354- [`arg-default-value`](references/arg-default-value.md) - Use default_value for sensible defaults55- [`arg-env-fallback`](references/arg-env-fallback.md) - Use env for environment variable fallback56- [`arg-short-long`](references/arg-short-long.md) - Provide both short and long option names57- [`arg-conflicts-with`](references/arg-conflicts-with.md) - Use conflicts_with for mutually exclusive options58- [`arg-requires`](references/arg-requires.md) - Use requires for dependent arguments59- [`arg-value-name`](references/arg-value-name.md) - Use value_name for descriptive placeholders6061### 4. Validation & Parsing (HIGH)6263- [`valid-value-parser`](references/valid-value-parser.md) - Use value_parser for custom validation64- [`valid-possible-values`](references/valid-possible-values.md) - Use PossibleValuesParser for string constraints65- [`valid-fromstr-types`](references/valid-fromstr-types.md) - Implement FromStr for domain types66- [`valid-try-parse`](references/valid-try-parse.md) - Use try_parse for graceful error handling67- [`valid-num-args`](references/valid-num-args.md) - Use num_args for value count constraints6869### 5. Subcommand Architecture (MEDIUM-HIGH)7071- [`subcmd-nested-hierarchy`](references/subcmd-nested-hierarchy.md) - Use nested subcommands for complex CLIs72- [`subcmd-args-struct`](references/subcmd-args-struct.md) - Use struct for subcommand arguments73- [`subcmd-required-help`](references/subcmd-required-help.md) - Require subcommand or show help74- [`subcmd-arg-groups`](references/subcmd-arg-groups.md) - Use ArgGroup for one-of-many requirements75- [`subcmd-external`](references/subcmd-external.md) - Use external subcommands for plugin systems7677### 6. Help & Documentation (MEDIUM)7879- [`help-shell-completions`](references/help-shell-completions.md) - Generate shell completions with clap_complete80- [`help-next-heading`](references/help-next-heading.md) - Use next_help_heading for organized help81- [`help-after-help`](references/help-after-help.md) - Use after_help for examples and context82- [`help-hide-options`](references/help-hide-options.md) - Hide advanced options from default help8384### 7. Error Handling (MEDIUM)8586- [`error-exit-codes`](references/error-exit-codes.md) - Use appropriate exit codes87- [`error-context`](references/error-context.md) - Add context to error messages88- [`error-suggestions`](references/error-suggestions.md) - Enable suggestions for typos89- [`error-color-styles`](references/error-color-styles.md) - Use colored output for error visibility9091### 8. Testing Patterns (LOW-MEDIUM)9293- [`test-assert-cmd`](references/test-assert-cmd.md) - Use assert_cmd for integration testing94- [`test-predicates`](references/test-predicates.md) - Use predicates for flexible assertions95- [`test-temp-files`](references/test-temp-files.md) - Use assert_fs for temporary test files96- [`test-parse-from`](references/test-parse-from.md) - Use parse_from for unit testing parsers97- [`test-trycmd-snapshots`](references/test-trycmd-snapshots.md) - Use trycmd for snapshot testing9899## How to Use100101Read individual reference files for detailed explanations and code examples:102103- [Section definitions](references/_sections.md) - Category structure and impact levels104- [Rule template](assets/templates/_template.md) - Template for adding new rules105106## Reference Files107108| File | Description |109|------|-------------|110| [references/_sections.md](references/_sections.md) | Category definitions and ordering |111| [assets/templates/_template.md](assets/templates/_template.md) | Template for new rules |112| [metadata.json](metadata.json) | Version and reference information |
Run npx skillmds@latest add comeonoliver/rust-clap in your terminal (requires Node.js), paste this page's agent-chat prompt into Claude, Cursor, or any MCP-connected agent, or download the SKILL.md file and copy it into your agent's skills directory.
Rust Clap Best Practices It is listed under Coding & Dev Tools on SkillMD.
This skill has not completed SkillMD's automated safety review yet. Independent scanners report: SkillSpector: PASS, Skill Scanner: PASS. SkillMD never runs a skill's scripts for you; review the SKILL.md before installing.
This skill is tagged as working with Claude Code, Claude.ai, OpenAI Codex. SKILL.md is an open format, so most agents that read a skills directory can load it too.
Yes. Installing skills from SkillMD is free, and the skill stays under its author's original license.
ComeOnOliver (@comeonoliver) published this skill. Their other Agent Skills are listed on their SkillMD profile.