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

mv: allow a single source with --target-directory

This commit is contained in:
ilkecan 2022-05-14 03:00:29 +00:00
parent 8891571e9a
commit 530d5f6dbf
2 changed files with 36 additions and 9 deletions

View file

@ -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<OsString> = matches
.values_of_os(ARG_FILES)
@ -180,7 +193,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)
)

View file

@ -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!();