Clap CLI Argument Parsing

Parse CLI arguments with clap derive macros for type-safe command-line tools.

RustCli Tooling

Code

snippet
use clap::Parser;

#[derive(Parser, Debug)]
#[command(name = "app", version, about)]
struct Args {
    #[arg(short, long, default_value = "8080")]
    port: u16,
    #[arg(long, default_value = "localhost")]
    host: String,
    #[arg(long)]
    verbose: bool,
    #[command(subcommand)]
    command: Option<Command>,
}

#[derive(clap::Subcommand, Debug)]
enum Command {
    Serve,
    Build { output: String },
}

fn main() {
    let args = Args::parse();
    println!("{:?}", args);
}