- Home
- Skills
- Vanman2024
- Cli Builder
- Clap Patterns
clap-patterns_skill
- Python
0
GitHub Stars
1
Bundled Files
2 months ago
Catalog Refreshed
4 months ago
First Indexed
Readme & install
Copy the install command, review bundled files from the catalogue, and read any extended description pulled from the listing source.
Installation
Preview and clipboard use veilstrat where the catalogue uses aiagentskills.
npx veilstrat add skill vanman2024/cli-builder --skill clap-patterns- SKILL.md5.9 KB
Overview
This skill provides modern, type-safe Rust CLI patterns using Clap 4.x. It packages ready-to-use templates, validation helpers, subcommand patterns, and environment-variable integration to accelerate building robust command-line applications. Use it to apply best practices for parsing, validation, and structured CLI design in Rust.
How this skill works
The skill supplies example templates and snippets that leverage Clap derive macros, the Parser trait, Subcommand enums, ValueEnum, and custom value parsers. It shows how to wire environment variables, provide defaults, implement validation functions for arguments, and generate shell completions and test cases. Templates are meant to be copied into your Rust project and adapted to your commands and types.
When to use it
- Creating a new Rust CLI and wanting type-safe argument parsing
- Organizing a complex tool with nested subcommands
- Adding validation for ports, paths, or constrained numeric ranges
- Supporting environment variables for secrets or configuration
- Providing user-friendly completions and automated CLI tests
Best practices
- Prefer derive macros (Parser, Subcommand) to reduce boilerplate
- Use ValueEnum for fixed choice options to get compile-time safety
- Validate inputs early with value_parser functions and clear error messages
- Expose sensible defaults with default_value_t and document via doc comments
- Treat PathBuf for file/directory args and keep flags global when needed
Example use cases
- A single-command binary that parses files, count, and verbose flags using Parser derive
- A git-like tool with multiple subcommands and global verbose/log flags
- A server CLI that validates port numbers with a custom value parser
- A deployment tool that reads API keys and DB URLs from environment vars with fallbacks
- A config tool that enforces output format choices via ValueEnum
FAQ
Enable derive and env features, for example: clap = { version = "4.5", features = ["derive", "env"] }.
How do I validate numeric ranges like port numbers?
Write a value_parser function that parses the string, checks the range, and returns Result<T, String> for Clap to use.