diff --git a/src/uu/mv/src/mv.rs b/src/uu/mv/src/mv.rs index fa8284061..76b54264e 100644 --- a/src/uu/mv/src/mv.rs +++ b/src/uu/mv/src/mv.rs @@ -13,7 +13,7 @@ mod error; #[macro_use] extern crate uucore; -use clap::{crate_version, Arg, ArgMatches, Command}; +use clap::{crate_version, Arg, ArgMatches, Command, ErrorKind}; use std::env; use std::ffi::OsString; use std::fs; @@ -70,13 +70,26 @@ static ARG_FILES: &str = "files"; #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let matches = uu_app() - .after_help(&*format!( - "{}\n{}", - LONG_HELP, - backup_control::BACKUP_CONTROL_LONG_HELP - )) - .get_matches_from(args); + let help = format!( + "{}\n{}", + LONG_HELP, + backup_control::BACKUP_CONTROL_LONG_HELP + ); + let mut app = uu_app().after_help(&*help); + let matches = app + .try_get_matches_from_mut(args) + .unwrap_or_else(|e| e.exit()); + + if !matches.is_present(OPT_TARGET_DIRECTORY) && matches.occurrences_of(ARG_FILES) == 1 { + app.error( + ErrorKind::TooFewValues, + format!( + "The argument '<{}>...' requires at least 2 values, but only 1 was provided", + ARG_FILES + ), + ) + .exit(); + } let files: Vec = matches .values_of_os(ARG_FILES) @@ -181,7 +194,7 @@ pub fn uu_app<'a>() -> Command<'a> { Arg::new(ARG_FILES) .multiple_occurrences(true) .takes_value(true) - .min_values(2) + .min_values(1) .required(true) .allow_invalid_utf8(true) .value_hint(clap::ValueHint::AnyPath) diff --git a/tests/by-util/test_mv.rs b/tests/by-util/test_mv.rs index 06f4d5259..96bd96856 100644 --- a/tests/by-util/test_mv.rs +++ b/tests/by-util/test_mv.rs @@ -641,6 +641,20 @@ fn test_mv_target_dir() { assert!(at.file_exists(&format!("{}/{}", dir, file_b))); } +#[test] +fn test_mv_target_dir_single_source() { + let (at, mut ucmd) = at_and_ucmd!(); + let dir = "test_mv_target_dir_single_source_dir"; + let file = "test_mv_target_dir_single_source_file"; + + at.touch(file); + at.mkdir(dir); + ucmd.arg("-t").arg(dir).arg(file).succeeds().no_stderr(); + + assert!(!at.file_exists(file)); + assert!(at.file_exists(&format!("{}/{}", dir, file))); +} + #[test] fn test_mv_overwrite_dir() { let (at, mut ucmd) = at_and_ucmd!();