Rust Clap

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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);
}