95 字
1 分钟
Rust Clap
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);
}