Back to list

Clap CLI Argument Parser

Lv.5804@mukitaro21 playsDec 28, 2025

Clap command-line argument parser. Most popular Rust CLI library.

preview.rust
Rust
1use clap::{Parser, Subcommand};
2
3#[derive(Parser)]
4#[command(name = "myapp")]
5#[command(about = "A sample CLI application")]
6struct Cli {
7 #[arg(short, long, action = clap::ArgAction::Count)]
8 verbose: u8,
9
10 #[command(subcommand)]
11 command: Commands,
12}
13
14#[derive(Subcommand)]
15enum Commands {
16 Add {
17 #[arg(short, long)]
18 name: String,
19 },
20 Remove {
21 #[arg(short, long)]
22 id: u32,
23 },
24 List {
25 #[arg(short, long, default_value_t = 10)]
26 limit: usize,
27 },
28}
29
30fn main() {
31 let cli = Cli::parse();
32
33 match &cli.command {
34 Commands::Add { name } => println!("Adding: {}", name),
35 Commands::Remove { id } => println!("Removing: {}", id),
36 Commands::List { limit } => println!("Listing {} items", limit),
37 }
38}

Custom problems are not included in rankings