@@ -22,6 +22,7 @@ use std::os::unix::fs::symlink;
22
22
#[ cfg( windows) ]
23
23
use std:: os:: windows:: fs:: { symlink_dir, symlink_file} ;
24
24
use std:: path:: { Path , PathBuf } ;
25
+ use uucore:: backup_control:: { self , BackupMode } ;
25
26
use uucore:: fs:: { canonicalize, CanonicalizeMode } ;
26
27
27
28
pub struct Settings {
@@ -43,14 +44,6 @@ pub enum OverwriteMode {
43
44
Force ,
44
45
}
45
46
46
- #[ derive( Clone , Debug , Eq , PartialEq ) ]
47
- pub enum BackupMode {
48
- NoBackup ,
49
- SimpleBackup ,
50
- NumberedBackup ,
51
- ExistingBackup ,
52
- }
53
-
54
47
fn get_usage ( ) -> String {
55
48
format ! (
56
49
"{0} [OPTION]... [-T] TARGET LINK_NAME (1st form)
@@ -78,7 +71,7 @@ fn get_long_usage() -> String {
78
71
static ABOUT : & str = "change file owner and group" ;
79
72
80
73
mod options {
81
- pub const B : & str = "b" ;
74
+ pub const BACKUP_NO_ARG : & str = "b" ;
82
75
pub const BACKUP : & str = "backup" ;
83
76
pub const FORCE : & str = "force" ;
84
77
pub const INTERACTIVE : & str = "interactive" ;
@@ -99,7 +92,11 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
99
92
100
93
let matches = uu_app ( )
101
94
. usage ( & usage[ ..] )
102
- . after_help ( & long_usage[ ..] )
95
+ . after_help ( & * format ! (
96
+ "{}\n {}" ,
97
+ long_usage,
98
+ backup_control:: BACKUP_CONTROL_LONG_HELP
99
+ ) )
103
100
. get_matches_from ( args) ;
104
101
105
102
/* the list of files */
@@ -118,33 +115,25 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
118
115
OverwriteMode :: NoClobber
119
116
} ;
120
117
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 ;
133
127
}
134
- } else {
135
- BackupMode :: NoBackup
128
+ Ok ( mode) => mode,
136
129
} ;
137
130
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 ) ) ;
143
132
144
133
let settings = Settings {
145
134
overwrite : overwrite_mode,
146
135
backup : backup_mode,
147
- suffix : backup_suffix. to_string ( ) ,
136
+ suffix : backup_suffix,
148
137
symbolic : matches. is_present ( options:: SYMBOLIC ) ,
149
138
relative : matches. is_present ( options:: RELATIVE ) ,
150
139
target_dir : matches
@@ -162,22 +151,19 @@ pub fn uu_app() -> App<'static, 'static> {
162
151
App :: new ( executable ! ( ) )
163
152
. version ( crate_version ! ( ) )
164
153
. 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
- ) )
169
154
. arg (
170
155
Arg :: with_name ( options:: BACKUP )
171
156
. 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" )
176
158
. 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" ) ,
181
167
)
182
168
// TODO: opts.arg(
183
169
// Arg::with_name(("d", "directory", "allow users with appropriate privileges to attempt \
0 commit comments