arrow_back Return to Posts

Parsing CLI Arguments in Rust
June 2022 ~ Rust

TLDR: Use Clap.

While rust natively supports accessing cli arguments, the rust docs says: “a much nicer way is to use one of the many available libraries."

Using clap

use clap::Parser;

#[derive(Parser)]
struct Cli {
    pattern: String,
    
    #[clap(parse(from_os_str))]
    path: std::path::PathBuf,
}

fn main() {
    let args = Cli::parse();
   
    println!("Pattern: {}", args.pattern);
    println!("Path: {}", args.path.display());
}

Comments