From 0a74af1fcbd5a0983e8ba280cc402df711ffa3a3 Mon Sep 17 00:00:00 2001 From: Ivan Krivosheev Date: Mon, 11 Sep 2023 20:24:46 +0300 Subject: [PATCH] feat: upgraded clap in examples --- Cargo.toml | 4 +- examples/cfbtool.rs | 91 +++++++++++++++++++++------------------------ 2 files changed, 45 insertions(+), 50 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f4da1c0..025f161 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,11 +11,11 @@ edition = "2018" [dependencies] byteorder = "1" -fnv = "1.0.7" +fnv = "1.0" uuid = "1" [dev-dependencies] -clap = "2.27" +clap = { version = "4.4", features = ["derive"] } rand = "0.8" rand_pcg = "0.3" time = "0.3" diff --git a/examples/cfbtool.rs b/examples/cfbtool.rs index 051fc99..3f0b832 100644 --- a/examples/cfbtool.rs +++ b/examples/cfbtool.rs @@ -1,9 +1,39 @@ -use clap::{App, Arg, SubCommand}; use std::io; use std::path::PathBuf; + +use clap::{Parser, Subcommand}; use time::OffsetDateTime; use uuid::Uuid; +#[derive(Parser, Debug)] +#[clap(author, about, long_about = None)] +struct Cli { + #[clap(subcommand)] + command: Command, +} + +#[derive(Subcommand, Debug)] +enum Command { + /// Concatenates and prints streams + Cat { path: Vec }, + + /// Changes storage CLSIDs + Chcls { clsid: Uuid, path: Vec }, + + /// Lists storage contents + Ls { + #[clap(short, long)] + /// Lists in long format + long: bool, + + #[clap(short, long)] + /// Includes . in output + all: bool, + + path: Vec, + }, +} + fn split(path: &str) -> (PathBuf, PathBuf) { let mut pieces = path.splitn(2, ':'); if let Some(piece1) = pieces.next() { @@ -51,68 +81,33 @@ fn list_entry(name: &str, entry: &cfb::Entry, long: bool) { } fn main() { - let matches = App::new("cfbtool") - .version("0.1") - .author("Matthew D. Steele ") - .about("Inspects and modifies CFB files") - .subcommand( - SubCommand::with_name("cat") - .about("Concatenates and prints streams") - .arg(Arg::with_name("path").multiple(true)), - ) - .subcommand( - SubCommand::with_name("chcls") - .about("Changes storage CLSIDs") - .arg(Arg::with_name("clsid").required(true)) - .arg(Arg::with_name("path").multiple(true)), - ) - .subcommand( - SubCommand::with_name("ls") - .about("Lists storage contents") - .arg( - Arg::with_name("all") - .short("a") - .help("Includes . in output"), - ) - .arg( - Arg::with_name("long") - .short("l") - .help("Lists in long format"), - ) - .arg(Arg::with_name("path").multiple(true)), - ) - .get_matches(); - if let Some(submatches) = matches.subcommand_matches("cat") { - if let Some(paths) = submatches.values_of("path") { - for path in paths { - let (comp_path, inner_path) = split(path); + let cli = Cli::parse(); + match cli.command { + Command::Cat { path } => { + for path in path { + let (comp_path, inner_path) = split(&path); let mut comp = cfb::open(&comp_path).unwrap(); let mut stream = comp.open_stream(inner_path).unwrap(); io::copy(&mut stream, &mut io::stdout()).unwrap(); } } - } else if let Some(submatches) = matches.subcommand_matches("chcls") { - let clsid = - Uuid::parse_str(submatches.value_of("clsid").unwrap()).unwrap(); - if let Some(paths) = submatches.values_of("path") { - for path in paths { - let (comp_path, inner_path) = split(path); + Command::Chcls { clsid, path } => { + for path in path { + let (comp_path, inner_path) = split(&path); let mut comp = cfb::open(&comp_path).unwrap(); comp.set_storage_clsid(inner_path, clsid).unwrap(); comp.flush().unwrap(); } } - } else if let Some(submatches) = matches.subcommand_matches("ls") { - if let Some(paths) = submatches.values_of("path") { - let long = submatches.is_present("long"); - for path in paths { - let (comp_path, inner_path) = split(path); + Command::Ls { long, all, path } => { + for path in path { + let (comp_path, inner_path) = split(&path); let comp = cfb::open(&comp_path).unwrap(); let entry = comp.entry(&inner_path).unwrap(); if entry.is_stream() { list_entry(entry.name(), &entry, long); } else { - if submatches.is_present("all") { + if all { list_entry(".", &entry, long); } for subentry in comp.read_storage(&inner_path).unwrap() {