Skip to content

Commit e3c797e

Browse files
committed
ln: Adapt to modified backup mode determination
1 parent 88d823a commit e3c797e

File tree

1 file changed

+28
-42
lines changed

1 file changed

+28
-42
lines changed

src/uu/ln/src/ln.rs

Lines changed: 28 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use std::os::unix::fs::symlink;
2222
#[cfg(windows)]
2323
use std::os::windows::fs::{symlink_dir, symlink_file};
2424
use std::path::{Path, PathBuf};
25+
use uucore::backup_control::{self, BackupMode};
2526
use uucore::fs::{canonicalize, CanonicalizeMode};
2627

2728
pub struct Settings {
@@ -43,14 +44,6 @@ pub enum OverwriteMode {
4344
Force,
4445
}
4546

46-
#[derive(Clone, Debug, Eq, PartialEq)]
47-
pub enum BackupMode {
48-
NoBackup,
49-
SimpleBackup,
50-
NumberedBackup,
51-
ExistingBackup,
52-
}
53-
5447
fn get_usage() -> String {
5548
format!(
5649
"{0} [OPTION]... [-T] TARGET LINK_NAME (1st form)
@@ -78,7 +71,7 @@ fn get_long_usage() -> String {
7871
static ABOUT: &str = "change file owner and group";
7972

8073
mod options {
81-
pub const B: &str = "b";
74+
pub const BACKUP_NO_ARG: &str = "b";
8275
pub const BACKUP: &str = "backup";
8376
pub const FORCE: &str = "force";
8477
pub const INTERACTIVE: &str = "interactive";
@@ -99,7 +92,11 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
9992

10093
let matches = uu_app()
10194
.usage(&usage[..])
102-
.after_help(&long_usage[..])
95+
.after_help(&*format!(
96+
"{}\n{}",
97+
long_usage,
98+
backup_control::BACKUP_CONTROL_LONG_HELP
99+
))
103100
.get_matches_from(args);
104101

105102
/* the list of files */
@@ -118,33 +115,25 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
118115
OverwriteMode::NoClobber
119116
};
120117

121-
let backup_mode = if matches.is_present(options::B) {
122-
BackupMode::ExistingBackup
123-
} else if matches.is_present(options::BACKUP) {
124-
match matches.value_of(options::BACKUP) {
125-
None => BackupMode::ExistingBackup,
126-
Some(mode) => match mode {
127-
"simple" | "never" => BackupMode::SimpleBackup,
128-
"numbered" | "t" => BackupMode::NumberedBackup,
129-
"existing" | "nil" => BackupMode::ExistingBackup,
130-
"none" | "off" => BackupMode::NoBackup,
131-
_ => panic!(), // cannot happen as it is managed by clap
132-
},
118+
let backup_mode = backup_control::determine_backup_mode(
119+
matches.is_present(options::BACKUP_NO_ARG),
120+
matches.is_present(options::BACKUP),
121+
matches.value_of(options::BACKUP),
122+
);
123+
let backup_mode = match backup_mode {
124+
Err(err) => {
125+
show_usage_error!("{}", err);
126+
return 1;
133127
}
134-
} else {
135-
BackupMode::NoBackup
128+
Ok(mode) => mode,
136129
};
137130

138-
let backup_suffix = if matches.is_present(options::SUFFIX) {
139-
matches.value_of(options::SUFFIX).unwrap()
140-
} else {
141-
"~"
142-
};
131+
let backup_suffix = backup_control::determine_backup_suffix(matches.value_of(options::SUFFIX));
143132

144133
let settings = Settings {
145134
overwrite: overwrite_mode,
146135
backup: backup_mode,
147-
suffix: backup_suffix.to_string(),
136+
suffix: backup_suffix,
148137
symbolic: matches.is_present(options::SYMBOLIC),
149138
relative: matches.is_present(options::RELATIVE),
150139
target_dir: matches
@@ -162,22 +151,19 @@ pub fn uu_app() -> App<'static, 'static> {
162151
App::new(executable!())
163152
.version(crate_version!())
164153
.about(ABOUT)
165-
.arg(Arg::with_name(options::B).short(options::B).help(
166-
"make a backup of each file that would otherwise be overwritten or \
167-
removed",
168-
))
169154
.arg(
170155
Arg::with_name(options::BACKUP)
171156
.long(options::BACKUP)
172-
.help(
173-
"make a backup of each file that would otherwise be overwritten \
174-
or removed",
175-
)
157+
.help("make a backup of each existing destination file")
176158
.takes_value(true)
177-
.possible_values(&[
178-
"simple", "never", "numbered", "t", "existing", "nil", "none", "off",
179-
])
180-
.value_name("METHOD"),
159+
.require_equals(true)
160+
.min_values(0)
161+
.value_name("CONTROL"),
162+
)
163+
.arg(
164+
Arg::with_name(options::BACKUP_NO_ARG)
165+
.short(options::BACKUP_NO_ARG)
166+
.help("like --backup but does not accept an argument"),
181167
)
182168
// TODO: opts.arg(
183169
// Arg::with_name(("d", "directory", "allow users with appropriate privileges to attempt \

0 commit comments

Comments
 (0)