Skip to content

Commit

Permalink
Merge pull request #43 from ikrivosheev/feat/upgrade_clap
Browse files Browse the repository at this point in the history
feat: upgraded clap in examples
  • Loading branch information
mdsteele authored Sep 12, 2023
2 parents 5c5279d + 0a74af1 commit 95cefc0
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 50 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
91 changes: 43 additions & 48 deletions examples/cfbtool.rs
Original file line number Diff line number Diff line change
@@ -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<String> },

/// Changes storage CLSIDs
Chcls { clsid: Uuid, path: Vec<String> },

/// Lists storage contents
Ls {
#[clap(short, long)]
/// Lists in long format
long: bool,

#[clap(short, long)]
/// Includes . in output
all: bool,

path: Vec<String>,
},
}

fn split(path: &str) -> (PathBuf, PathBuf) {
let mut pieces = path.splitn(2, ':');
if let Some(piece1) = pieces.next() {
Expand Down Expand Up @@ -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 <mdsteele@alum.mit.edu>")
.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() {
Expand Down

0 comments on commit 95cefc0

Please sign in to comment.