1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-27 19:17:43 +00:00

Merge pull request #3529 from ilkecan/mv-target-dir

mv: allow a single source with --target-directory
This commit is contained in:
Sylvestre Ledru 2022-05-21 09:40:16 +02:00 committed by GitHub
commit bda9f9f889
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 9 deletions

View file

@ -13,7 +13,7 @@ mod error;
#[macro_use] #[macro_use]
extern crate uucore; extern crate uucore;
use clap::{crate_version, Arg, ArgMatches, Command}; use clap::{crate_version, Arg, ArgMatches, Command, ErrorKind};
use std::env; use std::env;
use std::ffi::OsString; use std::ffi::OsString;
use std::fs; use std::fs;
@ -70,13 +70,26 @@ static ARG_FILES: &str = "files";
#[uucore::main] #[uucore::main]
pub fn uumain(args: impl uucore::Args) -> UResult<()> { pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let matches = uu_app() let help = format!(
.after_help(&*format!( "{}\n{}",
"{}\n{}", LONG_HELP,
LONG_HELP, backup_control::BACKUP_CONTROL_LONG_HELP
backup_control::BACKUP_CONTROL_LONG_HELP );
)) let mut app = uu_app().after_help(&*help);
.get_matches_from(args); 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<OsString> = matches let files: Vec<OsString> = matches
.values_of_os(ARG_FILES) .values_of_os(ARG_FILES)
@ -181,7 +194,7 @@ pub fn uu_app<'a>() -> Command<'a> {
Arg::new(ARG_FILES) Arg::new(ARG_FILES)
.multiple_occurrences(true) .multiple_occurrences(true)
.takes_value(true) .takes_value(true)
.min_values(2) .min_values(1)
.required(true) .required(true)
.allow_invalid_utf8(true) .allow_invalid_utf8(true)
.value_hint(clap::ValueHint::AnyPath) .value_hint(clap::ValueHint::AnyPath)

View file

@ -641,6 +641,20 @@ fn test_mv_target_dir() {
assert!(at.file_exists(&format!("{}/{}", dir, file_b))); 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] #[test]
fn test_mv_overwrite_dir() { fn test_mv_overwrite_dir() {
let (at, mut ucmd) = at_and_ucmd!(); let (at, mut ucmd) = at_and_ucmd!();