typestar

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

  1. #[derive(Parser)] generates the whole command line.
  2. Doc comments become the help text for each field.
  3. Parser::parse exits with a usage message on bad input.

Keywords and builtins used here

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.

Type this snippet

Step 2 of 5 in Arguments, step 2 of 12 in CLI tools & HTTP clients.

← Previous Next →