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

Added -f option + Changed help information.

This commit is contained in:
Meir Klemfner 2020-10-04 17:56:58 +03:00
parent 745df542b7
commit 56f37bfe38
2 changed files with 98 additions and 11 deletions

View file

@ -1,11 +1,10 @@
use czkawka_core::duplicate::{CheckingMethod, DeleteMethod};
use std::path::PathBuf;
use structopt::StructOpt;
#[derive(Debug, StructOpt)]
#[structopt(name = "czkawka")]
#[structopt(name = "czkawka", help_message = HELP_MESSAGE, template = HELP_TEMPLATE)]
pub enum Commands {
#[structopt(name = "dup", about = "Finds duplicate files")]
#[structopt(name = "dup", about = "Finds duplicate files", help_message = HELP_MESSAGE, after_help = "EXAMPLE:\n czkawka dup -d /home/rafal -e /home/rafal/Obrazy -m 25 -x 7z rar IMAGE -s hashmb -f results.txt -D aeo")]
Duplicates {
#[structopt(flatten)]
directories: Directories,
@ -22,16 +21,20 @@ pub enum Commands {
#[structopt(short = "D", long, default_value = "AEO", parse(try_from_str = parse_delete_method), help = "Delete method (AEN, AEO, ON, OO)", long_help = "Methods to delete the files.\nAEN - All files except the newest,\nAEO - All files except the oldest,\nON - Only 1 file, the newest,\nOO - Only 1 file, the oldest")]
delete_method: DeleteMethod,
#[structopt(flatten)]
file_to_save: FileToSave,
#[structopt(flatten)]
not_recursive: NotRecursive,
},
#[structopt(name = "empty-folders", about = "Finds emtpty folders")]
#[structopt(name = "empty-folders", about = "Finds emtpty folders", help_message = HELP_MESSAGE, after_help = "EXAMPLE:\n czkawka empty-folders -d /home/rafal/rr /home/gateway -f results.txt")]
EmptyFolders {
#[structopt(flatten)]
directories: Directories,
#[structopt(short = "D", long, help = "Delete found folders")]
delete_folders: bool,
#[structopt(flatten)]
file_to_save: FileToSave,
},
#[structopt(name = "big", about = "Finds big files")]
#[structopt(name = "big", about = "Finds big files", help_message = HELP_MESSAGE, after_help = "EXAMPLE:\n czkawka big -d /home/rafal/ /home/piszczal -e /home/rafal/Roman -n 25 -x VIDEO -f results.txt")]
BiggestFiles {
#[structopt(flatten)]
directories: Directories,
@ -44,9 +47,11 @@ pub enum Commands {
#[structopt(short, long, default_value = "50", help = "Number of files to be shown")]
number_of_files: usize,
#[structopt(flatten)]
file_to_save: FileToSave,
#[structopt(flatten)]
not_recursive: NotRecursive,
},
#[structopt(name = "empty-files", about = "Finds emtpy files")]
#[structopt(name = "empty-files", about = "Finds emtpy files", help_message = HELP_MESSAGE, after_help = "EXAMPLE:\n czkawka empty-files -d /home/rafal /home/szczekacz -e /home/rafal/Pulpit -R -f results.txt")]
EmptyFiles {
#[structopt(flatten)]
directories: Directories,
@ -59,9 +64,11 @@ pub enum Commands {
#[structopt(short = "D", long, help = "Delete found files")]
delete_files: bool,
#[structopt(flatten)]
file_to_save: FileToSave,
#[structopt(flatten)]
not_recursive: NotRecursive,
},
#[structopt(name = "temp", about = "Finds temporary files")]
#[structopt(name = "temp", about = "Finds temporary files", help_message = HELP_MESSAGE, after_help = "EXAMPLE:\n czkawka temp -d /home/rafal/ -E */.git */tmp* *Pulpit -f results.txt -D")]
Temporary {
#[structopt(flatten)]
directories: Directories,
@ -72,13 +79,15 @@ pub enum Commands {
#[structopt(short = "D", long, help = "Delete found files")]
delete_files: bool,
#[structopt(flatten)]
file_to_save: FileToSave,
#[structopt(flatten)]
not_recursive: NotRecursive,
},
}
#[derive(Debug, StructOpt)]
pub struct Directories {
#[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)")]
#[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>,
}
@ -111,6 +120,22 @@ pub struct NotRecursive {
pub not_recursive: bool,
}
#[derive(Debug, StructOpt)]
pub struct FileToSave {
#[structopt(short, long, value_name = "file-name", help = "Saves the results into the file")]
pub file_to_save: Option<PathBuf>,
}
impl FileToSave {
pub fn file_name(&self) -> Option<&str> {
if let Some(file_name) = &self.file_to_save {
return file_name.to_str();
}
None
}
}
fn parse_checking_method(src: &str) -> Result<CheckingMethod, &'static str> {
match src.to_ascii_lowercase().as_str() {
"size" => Ok(CheckingMethod::Size),
@ -142,3 +167,26 @@ fn parse_min_size(src: &str) -> Result<u64, String> {
Err(e) => Err(e.to_string()),
}
}
static HELP_MESSAGE: &str = "Prints help information (--help will give more information)";
const HELP_TEMPLATE: &str = r#"
{bin} {version}
USAGE:
{usage} [SCFLAGS] [SCOPTIONS]
FLAGS:
{flags}
SUBCOMMANDS:
{subcommands}
use {usage} -h to get more info about a specific tool
EXAMPLES:
{bin} dup -d /home/rafal -e /home/rafal/Obrazy -m 25 -x 7z rar IMAGE -s hashmb -f results.txt -D aeo
{bin} empty-folders -d /home/rafal/rr /home/gateway -f results.txt
{bin} big -d /home/rafal/ /home/piszczal -e /home/rafal/Roman -n 25 -x VIDEO -f results.txt
{bin} empty-files -d /home/rafal /home/szczekacz -e /home/rafal/Pulpit -R -f results.txt
{bin} temp -d /home/rafal/ -E */.git */tmp* *Pulpit -f results.txt -D"#;

View file

@ -2,7 +2,7 @@ mod commands;
use commands::Commands;
#[allow(unused_imports)] // It is used in release.
#[allow(unused_imports)] // It is used in release for print_results().
use czkawka_core::common_traits::*;
use czkawka_core::{
@ -12,7 +12,7 @@ use czkawka_core::{
empty_folder::EmptyFolder,
temporary::{self, Temporary},
};
use std::path::PathBuf;
use std::{path::PathBuf, process};
use structopt::StructOpt;
fn path_list_to_str(path_list: Vec<PathBuf>) -> String {
@ -35,6 +35,7 @@ fn main() {
allowed_extensions,
search_method,
delete_method,
file_to_save,
not_recursive,
} => {
let mut df = DuplicateFinder::new();
@ -50,11 +51,18 @@ fn main() {
df.find_duplicates();
if let Some(file_name) = file_to_save.file_name() {
if !df.save_results_to_file(file_name) {
df.get_text_messages().print_messages();
process::exit(1);
}
}
#[cfg(not(debug_assertions))] // This will show too much probably unnecessary data to debug, comment line only if needed
df.print_results();
df.get_text_messages().print_messages();
}
Commands::EmptyFolders { directories, delete_folders } => {
Commands::EmptyFolders { directories, delete_folders, file_to_save } => {
let mut ef = EmptyFolder::new();
ef.set_included_directory(path_list_to_str(directories.directories));
@ -62,6 +70,13 @@ fn main() {
ef.find_empty_folders();
if let Some(file_name) = file_to_save.file_name() {
if !ef.save_results_to_file(file_name) {
ef.get_text_messages().print_messages();
process::exit(1);
}
}
#[cfg(not(debug_assertions))] // This will show too much probably unnecessary data to debug, comment line only if needed
ef.print_results();
ef.get_text_messages().print_messages();
@ -72,6 +87,7 @@ fn main() {
excluded_items,
allowed_extensions,
number_of_files,
file_to_save,
not_recursive,
} => {
let mut bf = BigFile::new();
@ -85,6 +101,13 @@ fn main() {
bf.find_big_files();
if let Some(file_name) = file_to_save.file_name() {
if !bf.save_results_to_file(file_name) {
bf.get_text_messages().print_messages();
process::exit(1);
}
}
#[cfg(not(debug_assertions))] // This will show too much probably unnecessary data to debug, comment line only if needed
bf.print_results();
bf.get_text_messages().print_messages();
@ -95,6 +118,7 @@ fn main() {
excluded_items,
allowed_extensions,
delete_files,
file_to_save,
not_recursive,
} => {
let mut ef = EmptyFiles::new();
@ -111,6 +135,13 @@ fn main() {
ef.find_empty_files();
if let Some(file_name) = file_to_save.file_name() {
if !ef.save_results_to_file(file_name) {
ef.get_text_messages().print_messages();
process::exit(1);
}
}
#[cfg(not(debug_assertions))] // This will show too much probably unnecessary data to debug, comment line only if needed
ef.print_results();
ef.get_text_messages().print_messages();
@ -120,6 +151,7 @@ fn main() {
excluded_directories,
excluded_items,
delete_files,
file_to_save,
not_recursive,
} => {
let mut tf = Temporary::new();
@ -135,6 +167,13 @@ fn main() {
tf.find_temporary_files();
if let Some(file_name) = file_to_save.file_name() {
if !tf.save_results_to_file(file_name) {
tf.get_text_messages().print_messages();
process::exit(1);
}
}
#[cfg(not(debug_assertions))] // This will show too much probably unnecessary data to debug, comment line only if needed
tf.print_results();
tf.get_text_messages().print_messages();