-
Notifications
You must be signed in to change notification settings - Fork 136
Expand file tree
/
Copy pathargs.rs
More file actions
285 lines (255 loc) · 9.13 KB
/
Copy pathargs.rs
File metadata and controls
285 lines (255 loc) · 9.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
use std::{ffi::OsString, path::PathBuf};
use clap::{Parser, ValueHint};
// Ouch command line options (docstrings below are part of --help)
/// A command-line utility for easily compressing and decompressing files and directories.
///
/// Supported formats: tar, zip, gz, 7z, xz, lzma, lzip, bz/bz2, bz3, lz4, sz (Snappy), zst, rar and br.
///
/// Repository: https://github.com/ouch-org/ouch
#[derive(Parser, Debug, PartialEq)]
#[command(about, version)]
// Disable rustdoc::bare_urls because rustdoc parses URLs differently than Clap
#[allow(rustdoc::bare_urls)]
pub struct CliArgs {
/// Skip [Y/n] questions, default to yes
#[arg(short, long, conflicts_with = "no", global = true)]
pub yes: bool,
/// Skip [Y/n] questions, default to no
#[arg(short, long, global = true)]
pub no: bool,
/// Activate accessibility mode, reducing visual noise
#[arg(short = 'A', long, env = "ACCESSIBLE", global = true)]
pub accessible: bool,
/// Ignore hidden files
#[arg(short = 'H', long, global = true)]
pub hidden: bool,
/// Silence output
#[arg(short, long, global = true)]
pub quiet: bool,
/// Ignore files matched by git's ignore files
#[arg(short, long, global = true)]
pub gitignore: bool,
/// Specify the format of the archive
#[arg(short, long, global = true)]
pub format: Option<OsString>,
/// Decompress or list with password
#[arg(short, long = "password", aliases = ["pass", "pw"], global = true)]
pub password: Option<OsString>,
/// Limit the amount of concurrent threads available
#[arg(short, long, visible_alias = "threads", global = true)]
pub concurrency: Option<usize>,
// Ouch and claps subcommands
#[command(subcommand)]
pub cmd: Subcommand,
}
#[derive(Parser, PartialEq, Eq, Debug)]
#[allow(rustdoc::bare_urls)]
pub enum Subcommand {
/// Compress one or more files into one output file
#[command(visible_alias = "c")]
Compress {
/// Files to be compressed
#[arg(required = true, value_hint = ValueHint::FilePath)]
files: Vec<PathBuf>,
/// The resulting file. Its extensions can be used to specify the compression formats
#[arg(required = true, value_hint = ValueHint::FilePath)]
output: PathBuf,
/// Compression level, applied to all formats
#[arg(short, long, group = "compression-level")]
level: Option<i16>,
/// Fastest compression level possible,
/// conflicts with --level and --slow
#[arg(long, group = "compression-level")]
fast: bool,
/// Slowest (and best) compression level possible,
/// conflicts with --level and --fast
#[arg(long, group = "compression-level")]
slow: bool,
/// Archive target files instead of storing symlinks (supported by `tar` and `zip`)
#[arg(long, short = 'S')]
follow_symlinks: bool,
},
/// Decompresses one or more files, optionally into another folder
#[command(visible_alias = "d")]
Decompress {
/// Files to be decompressed, or "-" for stdin
#[arg(required = true, num_args = 1.., value_hint = ValueHint::FilePath)]
files: Vec<PathBuf>,
/// Place results in a directory other than the current one
#[arg(short = 'd', long = "dir", value_hint = ValueHint::FilePath)]
output_dir: Option<PathBuf>,
/// Remove the source file after successful decompression
#[arg(short = 'r', long)]
remove: bool,
/// Disable Smart Unpack
#[arg(long)]
no_smart_unpack: bool,
},
/// List contents of an archive
#[command(visible_aliases = ["l", "ls"])]
List {
/// Archives whose contents should be listed
#[arg(required = true, num_args = 1.., value_hint = ValueHint::FilePath)]
archives: Vec<PathBuf>,
/// Show archive contents as a tree
#[arg(short, long)]
tree: bool,
},
}
#[cfg(test)]
mod tests {
use super::*;
fn args_splitter(input: &str) -> impl Iterator<Item = &str> {
input.split_whitespace()
}
fn to_paths(iter: impl IntoIterator<Item = &'static str>) -> Vec<PathBuf> {
iter.into_iter().map(PathBuf::from).collect()
}
macro_rules! test {
($args:expr, $expected:expr) => {
let result = match CliArgs::try_parse_from(args_splitter($args)) {
Ok(result) => result,
Err(err) => panic!(
"CLI result is Err, expected Ok, input: '{}'.\nResult: '{err}'",
$args
),
};
assert_eq!(result, $expected, "CLI result mismatched, input: '{}'.", $args);
};
}
fn mock_cli_args() -> CliArgs {
CliArgs {
yes: false,
no: false,
accessible: false,
hidden: false,
quiet: false,
gitignore: false,
format: None,
// This is usually replaced in assertion tests
password: None,
concurrency: None,
cmd: Subcommand::Decompress {
// Put a crazy value here so no test can assert it unintentionally
files: vec!["\x00\x11\x22".into()],
output_dir: None,
remove: false,
no_smart_unpack: false,
},
}
}
#[test]
fn test_clap_cli_ok() {
test!(
"ouch decompress file.tar.gz",
CliArgs {
cmd: Subcommand::Decompress {
files: to_paths(["file.tar.gz"]),
output_dir: None,
remove: false,
no_smart_unpack: false,
},
..mock_cli_args()
}
);
test!(
"ouch d file.tar.gz",
CliArgs {
cmd: Subcommand::Decompress {
files: to_paths(["file.tar.gz"]),
output_dir: None,
remove: false,
no_smart_unpack: false,
},
..mock_cli_args()
}
);
test!(
"ouch d a b c",
CliArgs {
cmd: Subcommand::Decompress {
files: to_paths(["a", "b", "c"]),
output_dir: None,
remove: false,
no_smart_unpack: false,
},
..mock_cli_args()
}
);
test!(
"ouch compress file file.tar.gz",
CliArgs {
cmd: Subcommand::Compress {
files: to_paths(["file"]),
output: PathBuf::from("file.tar.gz"),
level: None,
fast: false,
slow: false,
follow_symlinks: false,
},
..mock_cli_args()
}
);
test!(
"ouch compress a b c archive.tar.gz",
CliArgs {
cmd: Subcommand::Compress {
files: to_paths(["a", "b", "c"]),
output: PathBuf::from("archive.tar.gz"),
level: None,
fast: false,
slow: false,
follow_symlinks: false,
},
..mock_cli_args()
}
);
test!(
"ouch compress a b c archive.tar.gz",
CliArgs {
cmd: Subcommand::Compress {
files: to_paths(["a", "b", "c"]),
output: PathBuf::from("archive.tar.gz"),
level: None,
fast: false,
slow: false,
follow_symlinks: false,
},
..mock_cli_args()
}
);
let inputs = [
"ouch compress a b c output --format tar.gz",
// https://github.com/clap-rs/clap/issues/5115
// "ouch compress a b c --format tar.gz output",
// "ouch compress a b --format tar.gz c output",
// "ouch compress a --format tar.gz b c output",
"ouch compress --format tar.gz a b c output",
"ouch --format tar.gz compress a b c output",
];
for input in inputs {
test!(
input,
CliArgs {
cmd: Subcommand::Compress {
files: to_paths(["a", "b", "c"]),
output: PathBuf::from("output"),
level: None,
fast: false,
slow: false,
follow_symlinks: false,
},
format: Some("tar.gz".into()),
..mock_cli_args()
}
);
}
}
#[test]
fn test_clap_cli_err() {
assert!(CliArgs::try_parse_from(args_splitter("ouch c")).is_err());
assert!(CliArgs::try_parse_from(args_splitter("ouch c input")).is_err());
assert!(CliArgs::try_parse_from(args_splitter("ouch d")).is_err());
assert!(CliArgs::try_parse_from(args_splitter("ouch l")).is_err());
}
}