95 字
1 分钟
Rust Clap
2024-04-19
use clap::{arg, command, Parser};

#[derive(Debug, Parser)]
#[command(version, about, long_about = None)]
pub struct Ctx {
    // 位置参数
    #[arg(short, long, help = "是否删除", default_value_t = true)]
    pub rm: bool,

    // 会被clap忽略
    #[clap(skip)]
    pub dir: String,

    // 子命令
    #[clap(subcommand)]
    pub sub: Sub,

    // 跟随参数,在 "-arg value" 后面
    pub targets: Vec<String>,
}

pub struct Sub {
    #[arg(short, long, help = "数量", default_value = 0)]
    pub val: u8,
}

fn main() {
    let ctx = Ctx::parse();
    println!("{:?}", ctx);
}
Rust Clap
https://blog.lpkt.cn/posts/rust-clap/
作者
lollipopkit
发布于
2024-04-19
许可协议
CC BY-NC-SA 4.0