From 371a77a994aeb0beae53f24a0edbf99d70133c33 Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Wed, 14 Aug 2024 23:51:43 +0200 Subject: add basic arguments --- Cargo.lock | 21 +++++++++++++++++++++ Cargo.toml | 4 +++- src/cli.rs | 18 ++++++++++++++++++ src/main.rs | 39 +++++++++++++++++++++++++++------------ 4 files changed, 69 insertions(+), 13 deletions(-) create mode 100644 src/cli.rs diff --git a/Cargo.lock b/Cargo.lock index e55b4c3..5e9d079 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -384,6 +384,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "11d8838454fda655dafd3accb2b6e2bea645b9e4078abe84a22ceb947235c5cc" dependencies = [ "clap_builder", + "clap_derive", ] [[package]] @@ -399,6 +400,18 @@ dependencies = [ "terminal_size", ] +[[package]] +name = "clap_derive" +version = "4.5.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "501d359d5f3dcaf6ecdeee48833ae73ec6e42723a1e52419c79abf9507eec0a0" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "clap_lex" version = "0.7.2" @@ -1661,6 +1674,12 @@ dependencies = [ "hashbrown", ] +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + [[package]] name = "hex" version = "0.4.3" @@ -2380,6 +2399,8 @@ name = "rsrclean" version = "0.1.0" dependencies = [ "cargo", + "clap", + "clap_derive", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 8d159ff..40dbdab 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,4 +4,6 @@ version = "0.1.0" edition = "2021" [dependencies] -cargo = "0.81.0" +cargo = "0.81" +clap = { version = "4.5", features = ["derive"] } +clap_derive = { version = "4.5" } diff --git a/src/cli.rs b/src/cli.rs new file mode 100644 index 0000000..6b1a043 --- /dev/null +++ b/src/cli.rs @@ -0,0 +1,18 @@ +use std::path::PathBuf; + +use clap::Parser; + +#[derive(Parser, Clone)] +#[command(author, version, about, long_about = None)] +pub struct Args { + pub dir: Option, + + #[arg(short, long, default_value_t = 2)] + pub level: u8, + + #[arg(short, long)] + pub doc: bool, + + #[arg(long, default_value_t = true)] + pub dry_run: bool, +} diff --git a/src/main.rs b/src/main.rs index cdc7d03..beac641 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,24 @@ -use std::path::Path; +use std::path::{Path, PathBuf}; -use cargo::{core::Workspace, ops::{clean, CleanOptions}, util::{context::GlobalContext, interning::InternedString}, CargoResult}; +use cargo::{ + core::Workspace, + ops::{clean, CleanOptions}, + util::{context::GlobalContext, interning::InternedString}, + CargoResult, +}; +use clap::Parser; + +use cli::Args; + +mod cli; fn main() { - let paths = std::fs::read_dir("./").unwrap(); + let cli = Args::parse(); + + let paths = std::fs::read_dir(cli.clone().dir.unwrap_or(PathBuf::from("./"))).unwrap(); for path in paths { let p = path.unwrap(); - handle_path(&p.path(), 0, 2); + handle_path(&p.path(), 0, cli.clone()); } } @@ -14,24 +26,26 @@ fn is_cargo_toml(path: &Path) -> bool { path.is_file() && (path.file_name().unwrap() == "Cargo.toml") } -fn handle_path(path: &Path, iter: u8, max_iter: u8) { +fn handle_path(path: &Path, iter: u8, cli: Args) { if is_cargo_toml(path) { let abs_path = std::fs::canonicalize(path).unwrap(); println!("Clean: {}", abs_path.as_path().to_str().unwrap()); - clean_project(abs_path.as_path()).unwrap(); + clean_project(abs_path.as_path(), cli).unwrap(); return; }; if path.is_dir() { - if iter > max_iter { return; }; + if iter >= cli.level { + return; + }; let paths = std::fs::read_dir(path).unwrap(); for path in paths { let p = path.unwrap(); - handle_path(&p.path(), iter + 1, 1); - }; + handle_path(&p.path(), iter + 1, cli.clone()); + } } } -fn clean_project(path: &Path) -> CargoResult<()> { +fn clean_project(path: &Path, cli: Args) -> CargoResult<()> { let gctx = GlobalContext::default()?; let workspace = Workspace::new(path, &gctx)?; @@ -42,9 +56,10 @@ fn clean_project(path: &Path) -> CargoResult<()> { targets: vec![], profile_specified: false, requested_profile: InternedString::new("dev"), - doc: false, - dry_run: true, + doc: cli.doc, + dry_run: cli.dry_run, }; + // return Ok(()); clean(&workspace, &opts) } -- cgit v1.2.3