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

Implement Default for Options of mv and cp (#7506)

This commit is contained in:
jmjoy 2025-03-20 23:08:44 +08:00 committed by GitHub
parent 187d3e58b5
commit 7bd90bb663
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 91 additions and 22 deletions

View file

@ -110,15 +110,16 @@ impl UError for Error {
pub type CopyResult<T> = Result<T, Error>; pub type CopyResult<T> = Result<T, Error>;
/// Specifies how to overwrite files. /// Specifies how to overwrite files.
#[derive(Clone, Copy, Eq, PartialEq)] #[derive(Debug, Clone, Copy, Eq, PartialEq, Default)]
pub enum ClobberMode { pub enum ClobberMode {
Force, Force,
RemoveDestination, RemoveDestination,
#[default]
Standard, Standard,
} }
/// Specifies whether files should be overwritten. /// Specifies whether files should be overwritten.
#[derive(Clone, Copy, Eq, PartialEq)] #[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum OverwriteMode { pub enum OverwriteMode {
/// [Default] Always overwrite existing files /// [Default] Always overwrite existing files
Clobber(ClobberMode), Clobber(ClobberMode),
@ -128,18 +129,39 @@ pub enum OverwriteMode {
NoClobber, NoClobber,
} }
impl Default for OverwriteMode {
fn default() -> Self {
Self::Clobber(ClobberMode::default())
}
}
/// Possible arguments for `--reflink`. /// Possible arguments for `--reflink`.
#[derive(Copy, Clone, Eq, PartialEq)] #[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum ReflinkMode { pub enum ReflinkMode {
Always, Always,
Auto, Auto,
Never, Never,
} }
impl Default for ReflinkMode {
#[allow(clippy::derivable_impls)]
fn default() -> Self {
#[cfg(any(target_os = "linux", target_os = "android", target_os = "macos"))]
{
ReflinkMode::Auto
}
#[cfg(not(any(target_os = "linux", target_os = "android", target_os = "macos")))]
{
ReflinkMode::Never
}
}
}
/// Possible arguments for `--sparse`. /// Possible arguments for `--sparse`.
#[derive(Copy, Clone, Eq, PartialEq)] #[derive(Debug, Copy, Clone, Eq, PartialEq, Default)]
pub enum SparseMode { pub enum SparseMode {
Always, Always,
#[default]
Auto, Auto,
Never, Never,
} }
@ -152,10 +174,11 @@ pub enum TargetType {
} }
/// Copy action to perform /// Copy action to perform
#[derive(PartialEq)] #[derive(Debug, Clone, Eq, PartialEq, Default)]
pub enum CopyMode { pub enum CopyMode {
Link, Link,
SymLink, SymLink,
#[default]
Copy, Copy,
Update, Update,
AttrOnly, AttrOnly,
@ -174,7 +197,7 @@ pub enum CopyMode {
/// For full compatibility with GNU, these options should also combine. We /// For full compatibility with GNU, these options should also combine. We
/// currently only do a best effort imitation of that behavior, because it is /// currently only do a best effort imitation of that behavior, because it is
/// difficult to achieve in clap, especially with `--no-preserve`. /// difficult to achieve in clap, especially with `--no-preserve`.
#[derive(Debug, PartialEq, Clone, Copy)] #[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub struct Attributes { pub struct Attributes {
#[cfg(unix)] #[cfg(unix)]
pub ownership: Preserve, pub ownership: Preserve,
@ -185,6 +208,12 @@ pub struct Attributes {
pub xattr: Preserve, pub xattr: Preserve,
} }
impl Default for Attributes {
fn default() -> Self {
Self::NONE
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)] #[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Preserve { pub enum Preserve {
// explicit means whether the --no-preserve flag is used or not to distinguish out the default value. // explicit means whether the --no-preserve flag is used or not to distinguish out the default value.
@ -224,6 +253,7 @@ impl Ord for Preserve {
/// ///
/// The fields are documented with the arguments that determine their value. /// The fields are documented with the arguments that determine their value.
#[allow(dead_code)] #[allow(dead_code)]
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Options { pub struct Options {
/// `--attributes-only` /// `--attributes-only`
pub attributes_only: bool, pub attributes_only: bool,
@ -287,6 +317,34 @@ pub struct Options {
pub progress_bar: bool, pub progress_bar: bool,
} }
impl Default for Options {
fn default() -> Self {
Self {
attributes_only: false,
backup: BackupMode::default(),
copy_contents: false,
cli_dereference: false,
copy_mode: CopyMode::default(),
dereference: false,
no_target_dir: false,
one_file_system: false,
overwrite: OverwriteMode::default(),
parents: false,
sparse_mode: SparseMode::default(),
strip_trailing_slashes: false,
reflink_mode: ReflinkMode::default(),
attributes: Attributes::default(),
recursive: false,
backup_suffix: backup_control::DEFAULT_BACKUP_SUFFIX.to_owned(),
target_dir: None,
update: UpdateMode::default(),
debug: false,
verbose: false,
progress_bar: false,
}
}
}
/// Enum representing if a file has been skipped. /// Enum representing if a file has been skipped.
#[derive(Clone, Copy, Debug, PartialEq, Eq)] #[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum PerformedAction { enum PerformedAction {
@ -1091,18 +1149,7 @@ impl Options {
} }
} }
} else { } else {
#[cfg(any(target_os = "linux", target_os = "android", target_os = "macos"))] ReflinkMode::default()
{
ReflinkMode::Auto
}
#[cfg(not(any(
target_os = "linux",
target_os = "android",
target_os = "macos"
)))]
{
ReflinkMode::Never
}
} }
}, },
sparse_mode: { sparse_mode: {

View file

@ -88,14 +88,32 @@ pub struct Options {
pub debug: bool, pub debug: bool,
} }
impl Default for Options {
fn default() -> Self {
Self {
overwrite: OverwriteMode::default(),
backup: BackupMode::default(),
suffix: backup_control::DEFAULT_BACKUP_SUFFIX.to_owned(),
update: UpdateMode::default(),
target_dir: None,
no_target_dir: false,
verbose: false,
strip_slashes: false,
progress_bar: false,
debug: false,
}
}
}
/// specifies behavior of the overwrite flag /// specifies behavior of the overwrite flag
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(Clone, Debug, Eq, PartialEq, Default)]
pub enum OverwriteMode { pub enum OverwriteMode {
/// '-n' '--no-clobber' do not overwrite /// '-n' '--no-clobber' do not overwrite
NoClobber, NoClobber,
/// '-i' '--interactive' prompt before overwrite /// '-i' '--interactive' prompt before overwrite
Interactive, Interactive,
///'-f' '--force' overwrite without prompt ///'-f' '--force' overwrite without prompt
#[default]
Force, Force,
} }

View file

@ -114,13 +114,16 @@ static VALID_ARGS_HELP: &str = "Valid arguments are:
- 'existing', 'nil' - 'existing', 'nil'
- 'numbered', 't'"; - 'numbered', 't'";
pub const DEFAULT_BACKUP_SUFFIX: &str = "~";
/// Available backup modes. /// Available backup modes.
/// ///
/// The mapping of the backup modes to the CLI arguments is annotated on the /// The mapping of the backup modes to the CLI arguments is annotated on the
/// enum variants. /// enum variants.
#[derive(Debug, Clone, Copy, Eq, PartialEq)] #[derive(Debug, Clone, Copy, Eq, PartialEq, Default)]
pub enum BackupMode { pub enum BackupMode {
/// Argument 'none', 'off' /// Argument 'none', 'off'
#[default]
NoBackup, NoBackup,
/// Argument 'simple', 'never' /// Argument 'simple', 'never'
SimpleBackup, SimpleBackup,
@ -254,7 +257,7 @@ pub fn determine_backup_suffix(matches: &ArgMatches) -> String {
if let Some(suffix) = supplied_suffix { if let Some(suffix) = supplied_suffix {
String::from(suffix) String::from(suffix)
} else { } else {
env::var("SIMPLE_BACKUP_SUFFIX").unwrap_or_else(|_| "~".to_owned()) env::var("SIMPLE_BACKUP_SUFFIX").unwrap_or_else(|_| DEFAULT_BACKUP_SUFFIX.to_owned())
} }
} }

View file

@ -49,9 +49,10 @@
use clap::ArgMatches; use clap::ArgMatches;
/// Available update mode /// Available update mode
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(Clone, Debug, Eq, PartialEq, Default)]
pub enum UpdateMode { pub enum UpdateMode {
/// --update=`all`, `` /// --update=`all`, ``
#[default]
ReplaceAll, ReplaceAll,
/// --update=`none` /// --update=`none`
ReplaceNone, ReplaceNone,