CLI framework for Rust: Clap — a powerful and convenient tool for creating console applications

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

What is Clap and why is it needed?



In the world of systems programming, Rust has established itself as a language combining the performance of C/C++ with memory safety. However, when creating console utilities, developers often face the routine task of parsing command-line arguments. The Clap library (Command Line Argument Parser) is the most popular and functional CLI framework for Rust, which handles all the work of processing flags, options, and subcommands.



Clap allows you to:

  • Automatically generate help (--help) and version (--version);
  • Process positional arguments, named options, and flags;
  • Create nested subcommands (like in git or cargo);
  • Validate data types and check argument requirements;
  • Work with autocompletion for Bash, Zsh, Fish, and PowerShell shells.


Thanks to the powerful derive macro and declarative API, Clap reduces code volume by 5–10 times compared to manual parsing. The library supports both simple utilities and complex interfaces with dozens of options.



Installing Clap in a Rust project



Add the dependency to Cargo.toml:



[dependencies]clap = { version = "4.5", features = ["derive"] }


The features = ["derive"] flag enables macro support — this is the recommended approach for most projects. For minimalistic utilities, you can use only the builder API (without derive).



Example 1: Simple parser with the derive macro



Let's create a utility that accepts a username and an optional greeting flag:



use clap::Parser;

#[derive(Parser)]#[command(name = "greet")]#[command(about = "Simple greeting", long_about = None)]struct Cli { /// User name name: String,

/// Print greeting in uppercase #[arg(short, long)] uppercase: bool,}

fn main() { let cli = Cli::parse();

let greeting = format!("Hello, {}!", cli.name); if cli.uppercase { println!("{}", greeting.to_uppercase()); } else { println!("{}", greeting); }}


Run the program:



$ cargo run -- WorldHello, World!

$ cargo run -- World --uppercaseHELLO, WORLD!

$ cargo run -- --help# Automatically generated help


Example 2: Options with types and default values



Clap automatically converts strings to the required types (numbers, enums, dates). Let's add an option to specify the number of repetitions:



use clap::Parser;

#[derive(Parser)]#[command(name = "repeat")]struct Cli { /// Text to repeat text: String,

/// Number of repetitions (default 1) #[arg(short, long, default_value_t = 1)] count: u32,}

fn main() { let cli = Cli::parse(); for _ in 0..cli.count { println!("{}", cli.text); }}


Now you can run:



$ cargo run -- "Rust" -c 3RustRustRust


Example 3: Subcommands — like in Git



Let's create a mini-utility for task management (todo list) with add, list, and done commands:



use clap::{Parser, Subcommand};

#[derive(Parser)]#[command(name = "todo")]#[command(about = "Task manager")]struct Cli { #[command(subcommand)] command: Commands,}

#[derive(Subcommand)]enum Commands { /// Add a new task Add { /// Task text task: String, }, /// Show all tasks List, /// Mark a task as completed Done { /// Task ID id: u32, },}

fn main() { let cli = Cli::parse();

match &cli.comman

Recommendations