TIL#2024-07-09: Collecting trailing clap args into a Vec
I wrote a quick CLI tool today in Rust to generate some large CSV files to test S3 file streaming into Databricks.
So I span up a new cargo project named random_csv
cargo new random_csv
clap
is my go-to for CLI argument parsing in Rust, so I added it to my Cargo.toml
cargo add clap --features derive
I wanted usage of the cli to look like this:
random_csv -n <number-of-rows> -f <output-file> <column1:type> <column2:type> ...
Where the trailing arguments are the column names of the CSV file.
To collect the trailing arguments into a Vec<String>
the clap Parser just needed to look like this:
#[derive(Parser)]
struct Cli {
#[clap(short, long)]
number: i32,
#[clap(short, long)]
file: String,
#[clap()]
header: Vec<String>,
}
By not providing short or long flags for the header
field, clap will collect all trailing arguments into a Vec<String>