clap's derive API in Rust
A struct with attributes becomes a parser, a help text and a version flag.
use clap::Parser;
/// Practice typing one language at a time.
#[derive(Parser, Debug)]
#[command(name = "typestar", version)]
struct Cli {
/// Language to practice
lang: String,
/// Seconds per sprint
#[arg(short, long, default_value_t = 60)]
seconds: u32,
}
fn main() {
let cli = Cli::parse();
println!("{} for {}s", cli.lang, cli.seconds);
}
How it works
#[derive(Parser)]generates the whole command line.- Doc comments become the help text for each field.
Parser::parseexits with a usage message on bad input.
Keywords and builtins used here
CliStringfnletmainstructu32use
The run, in numbers
- Lines
- 18
- Characters to type
- 346
- Tokens
- 63
- Three-star pace
- 100 tpm
At the three-star pace of 100 tokens a minute, this run takes about 38 seconds.
Step 2 of 5 in Arguments, step 2 of 12 in CLI tools & HTTP clients.