Clap example for CLI



examples/clap/clap-example/Cargo.toml
[package]
name = "clap-example"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
clap = { version = "4.3", features = ["derive"] }

examples/clap/clap-example/src/main.rs
use clap::Parser;

#[derive(Parser, Debug)]
#[command(version)]
struct Cli {
    #[arg(long, default_value = "127.0.0.1")]
    host: String,

    #[arg(long, default_value = "5000")]
    port: String,

    #[arg(long, default_value = "")]
    indexfile: String,

    #[arg(long, default_value = ".")]
    path: std::path::PathBuf,

    #[arg(long, default_value_t = 0, help = "A number mapped to i32")]
    limit: i32,

    #[arg(long, default_value_t = 0, help = "A number mapped to u8")]
    small: u8,

    #[arg(long, short, default_value_t = false, help = "Turn on debug mode")]
    debug: bool,
}

fn main() {
    let args = Cli::parse();
    dbg!(&args.host);
    dbg!(&args);
    if args.debug {
        println!("In debug mode");
    }
}