rust - clap命令行
顶层命令定义:Cli 结构体
rust
// 让这个结构体支持"命令解析"(clap库的功能)
#[derive(Parser)]
// 命令行工具的名字:输入 blog-tool 就会触发
#[command(name = "blog-tool")]
// 工具的简介(--help 时显示)
#[command(about = "Blog management tool for image and markdown handling", long_about = None)]
struct Cli {
// 子命令:scan / cleanup / report
#[command(subcommand)]
command: Commands,
}子命令枚举:Commands
// 让这个枚举支持"子命令"解析
#[derive(Subcommand)]
enum Commands {
// 子命令 1:scan
Scan {
/// 注释:--help 时显示的说明
#[command(about = "扫描文档,迁移图片")]
// 可选参数:-r 或 --root,指定项目根目录,默认是当前文件夹 .
#[arg(short, long, default_value = ".")]
root: PathBuf,
},
// 子命令 2:cleanup
Cleanup {
#[command(about = "删除未使用的图片")]
#[arg(short, long, default_value = ".")]
root: PathBuf,
},
// 子命令 3:report
Report {
#[command(about = "显示错误文件")]
#[arg(short, long, default_value = ".")]
root: PathBuf,
}
}- 三个斜线
///文档注释,--help时会显示给用户看。
https://github.com/clap-rs/clap
https://juejin.cn/post/7539004695760093222
https://youerning.top/post/rust/rust-clap-tutorial/