intermediateCLI
CLI with Clap
Build CLI applications with clap derive API.
CLI with Clap
Build CLI applications with clap derive API.
Difficulty
Intermediate
Code
rust
use clap::Parser;
#[derive(Parser, Debug)]
#[command(name = "greet")]
struct Args {
#[arg(short, long)]
name: String,
#[arg(short, long, default_value_t = 1)]
count: u32,
}
fn main() {
let args = Args::parse();
for _ in 0..args.count {
println!("Hello, {}!", args.name);
}
}Explanation
Clap derive API generates CLI parsing from structs.
Key Concepts
- Read the code carefully and understand the data flow
- Try modifying the example to see how it changes behavior
- Run this code in the Rust Playground
Related Topics
Browse more examples in the cli category to build a complete understanding of this topic.