Rust CLI Development
Overview
Rust provides excellent tools for building high-performance command-line applications. This skill should be invoked when creating CLI tools that require performance, safety, or native binaries.
Core Principles
- Clap: Use clap for argument parsing
- Error Handling: Use Result and the ? operator
- Structopt: Derive CLI from structs
- Binary Focus: Focus on single-purpose tools
Preparation Checklist
- Initialize:
cargo new --bin <name> - Add clap:
cargo add clap - Plan CLI structure
- Design error handling
Step-by-Step Process
- Initialize: Create Rust binary project
- Setup: Add clap for argument parsing
- Define: Use structopt for CLI structure
- Implement: Write command handlers
- Handle Errors: Implement proper error handling
- Build: Compile and test
Do's and Don'ts
- ✅ Do use clap derive macros
- ✅ Do implement proper error messages
- ✅ Do use Result for error handling
- ❌ Don't skip error handling
- ❌ Don't use panics for errors
- ❌ Don't make monolithic commands
Anti-Patterns
- Manual Parsing: Not using clap
- Panic Instead of Errors: Using unwrap/panic
- No Help: Missing --help documentation
- Monolithic Commands: Not separating subcommands