1
0
Fork 0
mirror of synced 2024-06-01 18:19:46 +12:00

Removed parsing from main.

Made --duplicates required and Created parser for min_size.
This commit is contained in:
Meir Klemfner 2020-10-04 14:56:21 +03:00
parent b83d5a6f3f
commit 5996dde418
2 changed files with 19 additions and 28 deletions

View file

@ -1,5 +1,5 @@
use czkawka_core::duplicate::{CheckingMethod, DeleteMethod};
use std::{path::PathBuf, process};
use std::path::PathBuf;
use structopt::StructOpt;
#[derive(Debug, StructOpt)]
@ -13,7 +13,7 @@ pub enum Commands {
excluded_directories: ExludedDirectories,
#[structopt(flatten)]
excluded_items: ExludedItems,
#[structopt(short, long, default_value = "1024", help = "Minimum size in bytes", long_help = "Minimum size of checked files in bytes, assigning bigger value may speed up searching")]
#[structopt(short, long, parse(try_from_str = parse_min_size), default_value = "1024", help = "Minimum size in bytes", long_help = "Minimum size of checked files in bytes, assigning bigger value may speed up searching")]
min_size: u64,
#[structopt(flatten)]
allowed_extensions: AllowedExtensions,
@ -78,19 +78,10 @@ pub enum Commands {
#[derive(Debug, StructOpt)]
pub struct Directories {
#[structopt(short, long, parse(from_os_str), help = "Directorie(s) to search", long_help = "List of directorie(s) which will be searched(absolute path)")]
#[structopt(short, long, parse(from_os_str), required(true), help = "Directorie(s) to search", long_help = "List of directorie(s) which will be searched(absolute path)")]
pub directories: Vec<PathBuf>,
}
impl Directories {
pub fn not_empty(&self) {
if self.directories.is_empty() {
eprintln!("error: At least one directory should be provided.");
process::exit(1);
}
}
}
#[derive(Debug, StructOpt)]
pub struct ExludedDirectories {
#[structopt(short, long, parse(from_os_str), help = "Exluded directorie(s)", long_help = "List of directorie(s) which will be excluded from search(absolute path)")]
@ -138,3 +129,18 @@ fn parse_delete_method(src: &str) -> Result<DeleteMethod, &'static str> {
_ => Err("Couldn't parse the delete method (allowed: AEN, AEO, ON, OO)"),
}
}
fn parse_min_size(src: &str) -> Result<u64, String> {
match src.parse::<u64>() {
Ok(min_size) => {
if min_size > 0 {
Ok(min_size)
} else {
Err("Minimum file size must be at least 1 byte".to_string())
}
}
Err(e) => {
Err(e.to_string())
}
}
}

View file

@ -12,7 +12,7 @@ use czkawka_core::{
empty_folder::EmptyFolder,
temporary::{self, Temporary},
};
use std::{path::PathBuf, process};
use std::path::PathBuf;
use structopt::StructOpt;
fn path_list_to_str(path_list: Vec<PathBuf>) -> String {
@ -37,13 +37,6 @@ fn main() {
delete_method,
not_recursive,
} => {
directories.not_empty();
if min_size == 0 {
eprintln!("error: Minimum file size must be at least 1 byte.");
process::exit(1);
}
let mut df = DuplicateFinder::new();
df.set_included_directory(path_list_to_str(directories.directories));
@ -62,8 +55,6 @@ fn main() {
df.get_text_messages().print_messages();
}
Commands::EmptyFolders { directories, delete_folders } => {
directories.not_empty();
let mut ef = EmptyFolder::new();
ef.set_included_directory(path_list_to_str(directories.directories));
@ -83,8 +74,6 @@ fn main() {
number_of_files,
not_recursive,
} => {
directories.not_empty();
let mut bf = BigFile::new();
bf.set_included_directory(path_list_to_str(directories.directories));
@ -108,8 +97,6 @@ fn main() {
delete_files,
not_recursive,
} => {
directories.not_empty();
let mut ef = EmptyFiles::new();
ef.set_included_directory(path_list_to_str(directories.directories));
@ -135,8 +122,6 @@ fn main() {
delete_files,
not_recursive,
} => {
directories.not_empty();
let mut tf = Temporary::new();
tf.set_included_directory(path_list_to_str(directories.directories));