1 use std::{ffi::OsString, path::PathBuf};
3 use clap::{Parser, ValueHint};
5 // Ouch command line options (docstrings below are part of --help)
6 /// A command-line utility for easily compressing and decompressing files and directories.
8 /// Supported formats: tar, zip, gz, 7z, xz/lzma, bz/bz2, lz4, sz (Snappy), zst and rar.
10 /// Repository: https://github.com/ouch-org/ouch
11 #[derive(Parser, Debug, PartialEq)]
12 #[command(about, version)]
13 // Disable rustdoc::bare_urls because rustdoc parses URLs differently than Clap
14 #[allow(rustdoc::bare_urls)]
16 /// Skip [Y/n] questions positively
17 #[arg(short, long, conflicts_with = "no", global = true)]
20 /// Skip [Y/n] questions negatively
21 #[arg(short, long, global = true)]
24 /// Activate accessibility mode, reducing visual noise
25 #[arg(short = 'A', long, env = "ACCESSIBLE", global = true)]
28 /// Ignores hidden files
29 #[arg(short = 'H', long, global = true)]
33 #[arg(short = 'q', long, global = true)]
36 /// Ignores files matched by git's ignore files
37 #[arg(short = 'g', long, global = true)]
40 /// Specify the format of the archive
41 #[arg(short, long, global = true)]
42 pub format: Option<OsString>,
44 // Ouch and claps subcommands
45 #[command(subcommand)]
49 #[derive(Parser, PartialEq, Eq, Debug)]
50 #[allow(rustdoc::bare_urls)]
52 /// Compress one or more files into one output file
53 #[command(visible_alias = "c")]
55 /// Files to be compressed
56 #[arg(required = true, value_hint = ValueHint::FilePath)]
59 /// The resulting file. Its extensions can be used to specify the compression formats
60 #[arg(required = true, value_hint = ValueHint::FilePath)]
63 /// Compression level, applied to all formats
64 #[arg(short, long, group = "compression-level")]
67 /// Fastest compression level possible,
68 /// conflicts with --level and --slow
69 #[arg(long, group = "compression-level")]
72 /// Slowest (and best) compression level possible,
73 /// conflicts with --level and --fast
74 #[arg(long, group = "compression-level")]
77 /// Decompresses one or more files, optionally into another folder
78 #[command(visible_alias = "d")]
80 /// Files to be decompressed
81 #[arg(required = true, num_args = 1.., value_hint = ValueHint::FilePath)]
84 /// Place results in a directory other than the current one
85 #[arg(short = 'd', long = "dir", value_hint = ValueHint::FilePath)]
86 output_dir: Option<PathBuf>,
88 /// List contents of an archive
89 #[command(visible_aliases = ["l", "ls"])]
91 /// Archives whose contents should be listed
92 #[arg(required = true, num_args = 1.., value_hint = ValueHint::FilePath)]
93 archives: Vec<PathBuf>,
95 /// Show archive contents as a tree
105 fn args_splitter(input: &str) -> impl Iterator<Item = &str> {
106 input.split_whitespace()
109 fn to_paths(iter: impl IntoIterator<Item = &'static str>) -> Vec<PathBuf> {
110 iter.into_iter().map(PathBuf::from).collect()
114 ($args:expr, $expected:expr) => {
115 let result = match CliArgs::try_parse_from(args_splitter($args)) {
116 Ok(result) => result,
118 "CLI result is Err, expected Ok, input: '{}'.\nResult: '{err}'",
122 assert_eq!(result, $expected, "CLI result mismatched, input: '{}'.", $args);
126 fn mock_cli_args() -> CliArgs {
135 // This is usually replaced in assertion tests
136 cmd: Subcommand::Decompress {
137 // Put a crazy value here so no test can assert it unintentionally
138 files: vec!["\x00\x11\x22".into()],
145 fn test_clap_cli_ok() {
147 "ouch decompress file.tar.gz",
149 cmd: Subcommand::Decompress {
150 files: to_paths(["file.tar.gz"]),
157 "ouch d file.tar.gz",
159 cmd: Subcommand::Decompress {
160 files: to_paths(["file.tar.gz"]),
169 cmd: Subcommand::Decompress {
170 files: to_paths(["a", "b", "c"]),
178 "ouch compress file file.tar.gz",
180 cmd: Subcommand::Compress {
181 files: to_paths(["file"]),
182 output: PathBuf::from("file.tar.gz"),
191 "ouch compress a b c archive.tar.gz",
193 cmd: Subcommand::Compress {
194 files: to_paths(["a", "b", "c"]),
195 output: PathBuf::from("archive.tar.gz"),
204 "ouch compress a b c archive.tar.gz",
206 cmd: Subcommand::Compress {
207 files: to_paths(["a", "b", "c"]),
208 output: PathBuf::from("archive.tar.gz"),
218 "ouch compress a b c output --format tar.gz",
219 // https://github.com/clap-rs/clap/issues/5115
220 // "ouch compress a b c --format tar.gz output",
221 // "ouch compress a b --format tar.gz c output",
222 // "ouch compress a --format tar.gz b c output",
223 "ouch compress --format tar.gz a b c output",
224 "ouch --format tar.gz compress a b c output",
226 for input in inputs {
230 cmd: Subcommand::Compress {
231 files: to_paths(["a", "b", "c"]),
232 output: PathBuf::from("output"),
237 format: Some("tar.gz".into()),
245 fn test_clap_cli_err() {
246 assert!(CliArgs::try_parse_from(args_splitter("ouch c")).is_err());
247 assert!(CliArgs::try_parse_from(args_splitter("ouch c input")).is_err());
248 assert!(CliArgs::try_parse_from(args_splitter("ouch d")).is_err());
249 assert!(CliArgs::try_parse_from(args_splitter("ouch l")).is_err());