feat: handle multiple positional file arguments

This commit is contained in:
2026-09-04 19:07:03 +02:00
parent 5fd47b445e
commit e1e56f7e27
2 changed files with 33 additions and 26 deletions
+12 -5
View File
@@ -21,7 +21,8 @@ struct Args {
#[derive(Subcommand)]
enum Commands {
Import {
file: PathBuf,
#[arg(num_args = 1..)]
files: Vec<PathBuf>,
#[arg(short, long, default_value_t = false)]
copy: bool,
},
@@ -29,7 +30,7 @@ enum Commands {
file: PathBuf,
},
Edit {
file: PathBuf,
files: PathBuf,
},
}
@@ -38,11 +39,13 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let extra_files = ["jpg", "png", "pdf", "log", "cue", "mp4", "lrc", "txt"].map(str::to_string);
match &args.command {
Commands::Import { file, copy } => {
Commands::Import { files, copy } => {
for file in files {
if file.is_dir() {
println!("Importing folder: {:?}", file);
for music_file in read_dir(file)? {
let path = music_file?.path();
if path
.extension()
.and_then(|ext| ext.to_str())
@@ -50,9 +53,11 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
{
continue;
}
let tag = get_metadata(&path);
if let Err(e) =
move_files(path.clone(), tag.clone(), extra_files.clone(), *copy, true)
move_files(path.clone(), tag, extra_files.clone(), *copy, true)
{
eprintln!("Failed to import {:?}: {}", path, e);
}
@@ -60,9 +65,11 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
} else {
println!("Importing file: {:?}", file);
let tag = get_metadata(file);
move_files(file.clone(), tag.clone(), extra_files, *copy, false)?;
move_files(file.clone(), tag.clone(), extra_files.clone(), *copy, false)?;
}
}
}
Commands::Show { file } => {
println!("Show file: {:?}", file);
let tag = get_metadata(file);
+3 -3
View File
@@ -12,7 +12,7 @@ pub fn move_files(
should_copy: bool,
has_parent: bool,
) -> Result<(), Box<dyn std::error::Error>> {
let target_dir = PathBuf::from("/home/user/Desktop/coding/keks-sort");
let target_dir = PathBuf::from("/home/user/Music/Tracks");
let artist = tag.artist();
let album = tag.album();
@@ -67,8 +67,8 @@ pub fn move_files(
}
}
let is_empty = file_path.parent().unwrap().read_dir()?.next().is_none();
if has_parent && is_empty {
let is_empty = has_parent && file_path.parent().unwrap().read_dir()?.next().is_none();
if is_empty {
remove_dir(file_path.parent().unwrap())?;
}