feat: add command line options

This commit is contained in:
Vladimir Rubin 2024-12-30 17:18:49 +02:00
parent 1e811d61ed
commit 140a7cc71f
Signed by: vavakado
GPG key ID: CAB744727F36B524

View file

@ -11,9 +11,40 @@ use filesorters::Config;
static CONFIG: OnceLock<Config> = OnceLock::new();
const VERSION: &str = env!("CARGO_PKG_VERSION");
fn main() {
let config_path = get_config_path();
let args: Vec<String> = env::args().collect();
if args.len() > 1
&& (args.contains(&"-v".to_string()) || args.contains(&"--version".to_string()))
{
println!("Filesorters version {}", VERSION);
return;
}
if args.len() > 1
&& (args.contains(&"-c".to_string()) || args.contains(&"--config".to_string()))
{
println!("Config location: {}", config_path.display());
return;
}
if args.len() > 1 && (args.contains(&"-h".to_string()) || args.contains(&"--help".to_string()))
{
print!(
"Options:
-h, --help - show this help message
-v, --version - show the version
-c, --config-path - config file location
"
);
return;
}
if !config_path.exists() {
println!("Config file does not exist, creating...");
filesorters::Config::create(&config_path).unwrap();
@ -195,8 +226,8 @@ fn move_file_to_directory(path: &Path, dir: &Path) {
if !dir.exists() {
match fs::create_dir(dir) {
Ok(_) => {},
Err(err) => eprintln!("Error creating destination directory: {}", err)
Ok(_) => {}
Err(err) => eprintln!("Error creating destination directory: {}", err),
}
return;
}
@ -204,11 +235,12 @@ fn move_file_to_directory(path: &Path, dir: &Path) {
let destination = dir.join(path.file_name().unwrap_or_default());
match fs::rename(path, &destination) {
Ok(()) => {},
Ok(()) => {}
Err(err) => eprintln!("Error moving file: {}", err),
}
}
#[inline]
fn get_config_path() -> PathBuf {
if cfg!(target_os = "windows") {
let username = env::var("USERNAME").unwrap_or_else(|_| "Default".to_string());