rust-clap
Rust Clap Best Practices
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 argumentstype-option-optional- Use Option for truly optional argumentstype-pathbuf-files- Use PathBuf for file system argumentstype-vec-multiple- Use Vec for multiple value argumentstype-newtype-semantic- Use newtypes for semantic distinctiontype-bool-flags- Use bool for simple flags
2. Derive API Patterns (CRITICAL)
derive-parser-entry- Derive Parser for CLI entry pointderive-command-metadata- Use Command attribute for metadataderive-subcommand-enum- Use Subcommand derive for command hierarchiesderive-args-reusable- Derive Args for reusable argument groupsderive-doc-comments- Use doc comments for help textderive-global-options- Use Global for cross-subcommand optionsderive-propagate-version- Propagate version to subcommands
3. Argument Configuration (HIGH)
arg-default-value- Use default_value for sensible defaultsarg-env-fallback- Use env for environment variable fallbackarg-short-long- Provide both short and long option namesarg-conflicts-with- Use conflicts_with for mutually exclusive optionsarg-requires- Use requires for dependent argumentsarg-value-name- Use value_name for descriptive placeholders
4. Validation & Parsing (HIGH)
valid-value-parser- Use value_parser for custom validationvalid-possible-values- Use PossibleValuesParser for string constraintsvalid-fromstr-types- Implement FromStr for domain typesvalid-try-parse- Use try_parse for graceful error handlingvalid-num-args- Use num_args for value count constraints
5. Subcommand Architecture (MEDIUM-HIGH)
subcmd-nested-hierarchy- Use nested subcommands for complex CLIssubcmd-args-struct- Use struct for subcommand argumentssubcmd-required-help- Require subcommand or show helpsubcmd-arg-groups- Use ArgGroup for one-of-many requirementssubcmd-external- Use external subcommands for plugin systems
6. Help & Documentation (MEDIUM)
help-shell-completions- Generate shell completions with clap_completehelp-next-heading- Use next_help_heading for organized helphelp-after-help- Use after_help for examples and contexthelp-hide-options- Hide advanced options from default help
7. Error Handling (MEDIUM)
error-exit-codes- Use appropriate exit codeserror-context- Add context to error messageserror-suggestions- Enable suggestions for typoserror-color-styles- Use colored output for error visibility
8. Testing Patterns (LOW-MEDIUM)
test-assert-cmd- Use assert_cmd for integration testingtest-predicates- Use predicates for flexible assertionstest-temp-files- Use assert_fs for temporary test filestest-parse-from- Use parse_from for unit testing parserstest-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 |
More from pproenca/dot-skills
zod
Zod schema validation best practices for type safety, parsing, and error handling. This skill should be used when defining z.object schemas, using z.string validations, safeParse, or z.infer. This skill does NOT cover React Hook Form integration patterns (use react-hook-form skill) or OpenAPI client generation (use orval skill).
2.0Kclean-architecture
Clean Architecture principles and best practices from Robert C. Martin's book. This skill should be used when designing software systems, reviewing code structure, or refactoring applications to achieve better separation of concerns. Triggers on tasks involving layers, boundaries, dependency direction, entities, use cases, or system architecture.
1.4Kemilkowal-animations
Emil Kowalski's animation best practices for web interfaces. Use when writing, reviewing, or implementing animations in React, CSS, or Framer Motion. Triggers on tasks involving transitions, easing, gestures, toasts, drawers, or motion.
918vitest
Vitest testing framework patterns for test setup, async testing, mocking with vi.*, snapshots, and test performance (formerly test-vitest). This skill should be used when writing or debugging Vitest tests. This skill does NOT cover TDD methodology (use test-tdd skill), API mocking with MSW (use test-msw skill), or Jest-specific APIs.
907typescript
This skill should be used when the user asks to "optimize TypeScript performance", "speed up tsc compilation", "configure tsconfig.json", "fix type errors", "improve async patterns", or encounters TS errors (TS2322, TS2339, "is not assignable to"). Also triggers on .ts, .tsx, .d.ts file work involving type definitions, module organization, or memory management. Does NOT cover TypeScript basics, framework-specific patterns, or testing.
821nuqs
nuqs (type-safe URL query state) best practices for Next.js applications. This skill should be used when writing, reviewing, or refactoring code that uses nuqs for URL state management. Triggers on tasks involving useQueryState, useQueryStates, search params, URL state, query parameters, nuqs parsers, or Next.js routing with state.
735