Command line argument parsing.
clap is a powerful and feature-rich command-line argument parser
that provides a polished CLI experience out-of-the-box.
It supports two primary API approaches: the derive API for simplicity
and the builder API for maximum flexibility.
Examples
Simple command-line tool using the derive API:
use clap::Parser;
#[derive(Parser)]
#[command(version, about = "A simple greeting tool")]
struct Args {
#[arg(short, long)]
name: String,
#[arg(short, long, default_value = "1")]
count: u8,
}
fn main() {
let args = Args::parse_from(&["prog", "--name", "World"]);
for _ in 0..args.count {
println!("Hello, {}!", args.name);
}
}
Application with subcommands:
use clap::{Parser, Subcommand};
#[derive(Parser)]
#[command(version, about = "File management tool")]
struct Cli {
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand)]
enum Commands {
Create {
name: String,
},
List {
#[arg(short, long)]
all: bool,
},
}
fn main() {
let cli = Cli::parse_from(&["prog", "create", "test.txt"]);
match cli.command {
Commands::Create { name } => {
println!("Creating file: {}", name);
}
Commands::List { all } => {
if all {
println!("Listing all files including hidden");
} else {
println!("Listing visible files");
}
}
}
}