stuffr_cli/cli.rs
1//! The `stuffr` command's argument surface.
2//!
3//! Pulled out of `main.rs` and re-exported from `lib.rs` so an integration
4//! test can ask clap itself — via [`clap::CommandFactory`] — what flags and
5//! subcommands exist, rather than keeping a second, hand-maintained list that
6//! the examples page could silently drift out of sync with. `main.rs` still
7//! owns everything that isn't the argument shape: parsing, dispatch, and the
8//! examples text itself.
9
10use clap::{Parser, Subcommand};
11
12#[derive(Parser)]
13#[command(
14 name = "stuffr",
15 version,
16 about = "Universal compression and archive toolkit"
17)]
18pub struct Cli {
19 #[command(subcommand)]
20 pub command: Option<Command>,
21 /// Print detailed usage examples covering every feature, then exit.
22 #[arg(long)]
23 pub examples: bool,
24}
25
26#[derive(Subcommand)]
27pub enum Command {
28 /// Compress a file, or collect several into an archive.
29 Pack {
30 /// Input paths, or `-` for stdin.
31 ///
32 /// More than one path needs an output that names a CONTAINER (`-o
33 /// bundle.tar`, or `--format tar`): a codec compresses one stream and
34 /// has nowhere to put a second.
35 #[arg(required = true, num_args = 1.., value_name = "PATHS")]
36 paths: Vec<String>,
37 /// Output path. Defaults to INPUT plus the format's extension.
38 #[arg(short, long)]
39 output: Option<String>,
40 /// Use this format instead of inferring one from the output name.
41 #[arg(long)]
42 format: Option<String>,
43 /// Compression level. gzip accepts 0-9.
44 #[arg(long)]
45 level: Option<i32>,
46 /// Overwrite an existing output.
47 #[arg(long)]
48 force: bool,
49 /// Skip the fsync that makes the output durable before it is published.
50 #[arg(long)]
51 no_sync: bool,
52 /// Use a weak fallback encoder that this build ships in place of the
53 /// format's real one. Worse ratio, and buffers the whole input.
54 #[arg(long)]
55 allow_weak_encoder: bool,
56 /// Encode with N worker threads. 0 means auto-detect.
57 ///
58 /// OMITTING this flag is not the same as passing 0: without it, stuffr
59 /// encodes single-threaded, so the same input always produces the same
60 /// bytes on any machine. Multi-threaded xz and zstd split the input
61 /// per worker, so their output depends on the worker count.
62 #[arg(long)]
63 threads: Option<usize>,
64 /// Use the full detected CPU budget, uncapped.
65 ///
66 /// Lifts the CPU cap only — `--memory-limit` still binds. Turbo means
67 /// "use my cores", not "ignore the OOM killer".
68 #[arg(long)]
69 turbo: bool,
70 /// Cap the memory stuffr will ask for, e.g. 512M or 2G.
71 ///
72 /// On encode this bounds the worker count. Defaults to 25% of
73 /// available RAM, honouring cgroup limits.
74 #[arg(long, value_name = "SIZE")]
75 memory_limit: Option<String>,
76 /// Fail (exit 4) if anything was approximated or lost.
77 ///
78 /// On the pack side that means something the walk met and could not
79 /// store — a socket, an undecodable name, an unreadable directory —
80 /// or an entry shape this container has none of, such as a directory
81 /// or a symlink in an `ar`.
82 #[arg(long)]
83 strict_fidelity: bool,
84 },
85 /// Decompress a file, or extract an archive's entries.
86 Unpack {
87 /// Input path, or `-` for stdin.
88 input: String,
89 /// Entries to extract; every entry when none are given.
90 ///
91 /// An exact entry name, or a directory selecting everything beneath
92 /// it. Needs -C, which is what asks for entry-aware extraction.
93 #[arg(value_name = "PATTERNS")]
94 patterns: Vec<String>,
95 /// Extract the entry at this position instead of naming it. Repeatable.
96 ///
97 /// 0-BASED, counting in archive order — the same number `stuffr list`
98 /// prints in its first column. This is how you reach an entry whose
99 /// name you cannot type: `Makefile` and `makefile` from a
100 /// case-sensitive filesystem on a case-insensitive one. Needs -C,
101 /// like PATTERNS, and cannot be combined with them — they are two
102 /// ways of saying the same thing.
103 ///
104 /// It does NOT reach a record a zip's own index shadows: `unpack`
105 /// counts the entries the index enumerates, so a record a later one
106 /// hides is not at any index here — it has no number at all. `stuffr
107 /// salvage --list` finds those, and `salvage --index N` selects them
108 /// by SCAN position, which is a different numbering.
109 #[arg(long, value_name = "N")]
110 index: Vec<usize>,
111 /// Extract the archive's entries into this directory.
112 ///
113 /// Absolute entry paths, `..` traversal and symlinks escaping this
114 /// directory are refused (exit 7), never silently sanitised.
115 #[arg(short = 'C', long, value_name = "DIR")]
116 directory: Option<String>,
117 /// Fail (exit 4) if anything was approximated or lost.
118 ///
119 /// On extraction that means an entry whose mode or mtime could not be
120 /// restored, or one skipped for having no shape on disk.
121 #[arg(long)]
122 strict_fidelity: bool,
123 /// Output path. Defaults to INPUT with its extension removed.
124 #[arg(short, long)]
125 output: Option<String>,
126 /// Overwrite an existing output.
127 #[arg(long)]
128 force: bool,
129 /// Use this format instead of detecting one. Required for formats with
130 /// no magic bytes and no extension.
131 #[arg(long)]
132 format: Option<String>,
133 /// Refuse a decode expanding by more than this ratio.
134 #[arg(long)]
135 max_ratio: Option<u64>,
136 /// Skip the fsync that makes the output durable before it is published.
137 #[arg(long)]
138 no_sync: bool,
139 /// Cap the memory stuffr will ask for, e.g. 512M or 2G.
140 ///
141 /// Defaults to 25% of available RAM, honouring cgroup limits.
142 #[arg(long, value_name = "SIZE")]
143 memory_limit: Option<String>,
144 },
145 /// Change a file's compression or its archive format, without unpacking
146 /// it to disk.
147 ///
148 /// Codec mode — the container is kept (`.tar.gz` -> `.tar.zst`, `.gz` ->
149 /// `.xz`, `.tar.xz` -> `.tar`): the container, or the plain stream, is
150 /// copied byte for byte and only the codec around it changes.
151 ///
152 /// Entry mode — the container changes (`.tgz` -> `.zip`, `.zip` ->
153 /// `.tar.xz`): every entry is read from the source and written into the
154 /// target under pack's rules, and what the target cannot hold (a
155 /// directory or symlink in an ar, a device or hardlink anywhere) is
156 /// skipped with a fidelity warning.
157 ///
158 /// The input's chain is detected by content, never by its name; the
159 /// target's comes from OUT's name or --format.
160 Convert {
161 /// Input path, or `-` for stdin.
162 input: String,
163 /// Output path; the same as -o. Give one or the other, not both.
164 #[arg(value_name = "OUT")]
165 output_pos: Option<String>,
166 /// Output path, or `-` for stdout (which needs --format).
167 #[arg(short, long)]
168 output: Option<String>,
169 /// Use this target format instead of inferring one from the output
170 /// name. A codec or a container; one the name contradicts is refused.
171 #[arg(long)]
172 format: Option<String>,
173 /// Compression level for the target codec. gzip accepts 0-9.
174 ///
175 /// Also what makes converting a file to the chain it already has
176 /// (`.tar.gz` -> `.tar.gz`) a re-encode rather than "nothing to
177 /// convert".
178 #[arg(long)]
179 level: Option<i32>,
180 /// Encode with N worker threads. 0 means auto-detect.
181 ///
182 /// OMITTING this flag is not the same as passing 0: without it, stuffr
183 /// encodes single-threaded, so the same input always produces the same
184 /// bytes on any machine. Multi-threaded xz and zstd split the input
185 /// per worker, so their output depends on the worker count.
186 #[arg(long)]
187 threads: Option<usize>,
188 /// Use the full detected CPU budget, uncapped.
189 ///
190 /// Lifts the CPU cap only — `--memory-limit` still binds. Turbo means
191 /// "use my cores", not "ignore the OOM killer".
192 #[arg(long)]
193 turbo: bool,
194 /// Use a weak fallback encoder that this build ships in place of the
195 /// format's real one. Worse ratio, and buffers the whole input.
196 #[arg(long)]
197 allow_weak_encoder: bool,
198 /// Overwrite an existing output.
199 #[arg(long)]
200 force: bool,
201 /// Fail (exit 4) if anything was approximated or lost.
202 ///
203 /// In codec mode the container is copied byte for byte and nothing
204 /// is lost. In entry mode every skipped entry or missing field is a
205 /// warning, and this turns any of them into exit 4 — with the output
206 /// still written, as with pack.
207 #[arg(long)]
208 strict_fidelity: bool,
209 /// Refuse a decode expanding by more than this ratio.
210 #[arg(long)]
211 max_ratio: Option<u64>,
212 /// Cap the memory stuffr will ask for, e.g. 512M or 2G.
213 ///
214 /// Bounds every source codec layer's decoder and the encoder's worker
215 /// count. Defaults to 25% of available RAM, honouring cgroup limits.
216 #[arg(long, value_name = "SIZE")]
217 memory_limit: Option<String>,
218 /// Skip the fsync that makes the output durable before it is published.
219 #[arg(long)]
220 no_sync: bool,
221 },
222 /// Decompress a file, or stream an archive's entries, to stdout.
223 Cat {
224 /// Input path, or `-` for stdin.
225 input: String,
226 /// Entries whose bytes to stream, in archive order.
227 ///
228 /// An exact entry name, or a directory selecting everything beneath
229 /// it. Naming one is what asks for entry-aware streaming; without
230 /// any, `cat` decodes the input as a single stream.
231 #[arg(value_name = "PATTERNS")]
232 patterns: Vec<String>,
233 /// Stream the entry at this position instead of naming it. Repeatable.
234 ///
235 /// 0-BASED, counting in archive order — the same number `stuffr list`
236 /// prints in its first column. `stuffr cat a.zip --index 6 > out.sh`
237 /// is how you pull out an entry whose name is ambiguous or
238 /// untypeable. Like PATTERNS, naming one asks for entry-aware
239 /// streaming; the two cannot be combined.
240 #[arg(long, value_name = "N")]
241 index: Vec<usize>,
242 /// Use this format instead of detecting one. Required for formats with
243 /// no magic bytes and no extension.
244 #[arg(long)]
245 format: Option<String>,
246 /// Refuse a decode expanding by more than this ratio.
247 #[arg(long)]
248 max_ratio: Option<u64>,
249 /// Cap the memory stuffr will ask for, e.g. 512M or 2G.
250 ///
251 /// Defaults to 25% of available RAM, honouring cgroup limits.
252 #[arg(long, value_name = "SIZE")]
253 memory_limit: Option<String>,
254 },
255 /// Identify a stream without decoding it.
256 Info {
257 /// Input path, or `-` for stdin.
258 input: String,
259 /// Emit machine-readable JSON instead of the human-readable report.
260 #[arg(long)]
261 json: bool,
262 /// Cap the memory stuffr will ask for, e.g. 512M or 2G.
263 ///
264 /// Defaults to 25% of available RAM, honouring cgroup limits.
265 #[arg(long, value_name = "SIZE")]
266 memory_limit: Option<String>,
267 },
268 /// List the formats this build contains.
269 Formats,
270 /// List an archive's entries without extracting.
271 ///
272 /// The first column is the entry's 0-based position in archive order,
273 /// which is what `cat --index` and `unpack --index` select by.
274 #[command(alias = "ls")]
275 List {
276 /// Archive path, or `-` for stdin.
277 input: String,
278 /// Emit machine-readable JSON instead of one entry per line.
279 #[arg(long)]
280 json: bool,
281 /// Refuse a codec layer beneath the container expanding by more
282 /// than this ratio (e.g. a `.tar.gz` bomb).
283 #[arg(long)]
284 max_ratio: Option<u64>,
285 /// Cap the memory stuffr will ask for, e.g. 512M or 2G.
286 ///
287 /// Bounds the codec layer beneath the container, which `--max-ratio`
288 /// cannot see: a pure xz/lzma/lzip decoder sizes its dictionary from
289 /// a value the file declares in its own header, before producing any
290 /// output. Defaults to 25% of available RAM, honouring cgroup limits.
291 #[arg(long, value_name = "SIZE")]
292 memory_limit: Option<String>,
293 /// Fail (exit 4) if the read approximated or lost anything.
294 ///
295 /// A listing can be incomplete — a zip whose index shadows records
296 /// enumerates fewer entries than the archive declares — so `list`
297 /// gates on fidelity for the same reason `test` and `unpack -C` do.
298 #[arg(long)]
299 strict_fidelity: bool,
300 },
301 /// Verify every entry's integrity without extracting.
302 Test {
303 /// Archive path, or `-` for stdin.
304 input: String,
305 /// Refuse a codec layer beneath the container expanding by more
306 /// than this ratio (e.g. a `.tar.gz` bomb).
307 #[arg(long)]
308 max_ratio: Option<u64>,
309 /// Cap the memory stuffr will ask for, e.g. 512M or 2G.
310 ///
311 /// Bounds the codec layer beneath the container, which `--max-ratio`
312 /// cannot see: a pure xz/lzma/lzip decoder sizes its dictionary from
313 /// a value the file declares in its own header, before producing any
314 /// output. Defaults to 25% of available RAM, honouring cgroup limits.
315 #[arg(long, value_name = "SIZE")]
316 memory_limit: Option<String>,
317 /// Fail (exit 4) if the read approximated or lost anything.
318 #[arg(long)]
319 strict_fidelity: bool,
320 },
321 /// Recover what a damaged zip's scan can prove, even where its central
322 /// directory alone would show less — or would wrongly unify two records
323 /// that happen to share a name.
324 ///
325 /// The one recovery-biased verb in this tool. `list`, `cat`, `unpack` and
326 /// `test` all stay exactly as uncompromising as they already are, and it
327 /// is salvage's willingness to accept less-than-proven content that buys
328 /// them that freedom.
329 Salvage {
330 /// Input path.
331 ///
332 /// Salvage scans back and forth over the archive, which needs real
333 /// random access — unlike every other verb here, this one does not
334 /// accept `-` for stdin.
335 input: String,
336 /// Print every record the scan found, including ones this build
337 /// never writes (a shadow, a skipped partial, an unverified entry).
338 ///
339 /// A record that repeats an earlier record's name is marked one of
340 /// two ways, and they mean different things. `[shadowed: dup of #N]`
341 /// is a MEASUREMENT: name, declared length and checksum all agree
342 /// with record N, so nothing is lost by not writing this one.
343 /// `[name collision: #N uses this name too]` is the weaker fact:
344 /// the name repeats but the content was not proven identical, so
345 /// both records are worth having and `-C` writes the second one as
346 /// `name.salvaged-N`, never over the first. It is the marker
347 /// `stuffr list`'s own "repeat a name … and are shadowed" fidelity
348 /// warning corresponds to.
349 ///
350 /// `stuffr list` on the same archive can print FEWER rows than this:
351 /// a record a later one shadows is invisible to `list`, which only
352 /// ever sees what the central directory's own name index kept. Works
353 /// with no destination at all (neither `-C` nor `-o`) — listing is
354 /// not recovering, and this is the diagnostic half of the feature:
355 /// seeing what an archive holds without extracting anything.
356 #[arg(long)]
357 list: bool,
358 /// Recover entries into this directory.
359 ///
360 /// One of `-C`, `-o` or `--list` is required. Mutually exclusive
361 /// with `-o`: a directory tree and one named file are two different
362 /// destinations.
363 ///
364 /// A damaged archive can hold two records under one name. The
365 /// second one recovered lands as `name.salvaged-N`, where N is its
366 /// scan position, rather than over the first — both records exist,
367 /// both are worth having, and the run exits 4 so a script knows a
368 /// name had to be changed.
369 #[arg(short = 'C', long, value_name = "DIR")]
370 directory: Option<String>,
371 /// Recover exactly one entry's bytes to this file, naming it with
372 /// exactly one `--index N`.
373 ///
374 /// A `Partial` entry still lands under `FILE.partial`, never `FILE`
375 /// itself — the same rule `-C` follows, for the same reason: a
376 /// truncated file under the name you asked for would be
377 /// indistinguishable from a whole one. Refused (exit 2) with more
378 /// than one `--index`, or together with `-C`.
379 #[arg(short = 'o', long, value_name = "FILE")]
380 output: Option<String>,
381 /// Recover only the record at this SCAN position instead of every
382 /// one found. Repeatable with `-C`; `-o` takes exactly one.
383 ///
384 /// A scan position is 0-based and counts every record the scan
385 /// found, shadowed ones included — a DIFFERENT numbering from
386 /// `stuffr list`'s own index, which never counts a shadowed record at
387 /// all. On an archive where a name is duplicated, `list --index 6`
388 /// and `salvage --index 6` can name different entries, or `list`'s
389 /// may not exist at all. Always a scan position, never a list index.
390 #[arg(long, value_name = "N")]
391 index: Vec<usize>,
392 /// What to do with an entry whose payload could not be fully proven:
393 /// `keep` (default) writes it as `name.partial`, never under its
394 /// real name; `skip` recovers nothing for it; `ask` has no terminal
395 /// to ask from at this layer and is read conservatively as `skip`.
396 #[arg(long, value_name = "POLICY")]
397 partial: Option<String>,
398 /// Raise the structural ceiling on a single entry's declared length,
399 /// e.g. 8G. A declaration over the ceiling is refused before
400 /// anything is allocated on its say-so, either way.
401 #[arg(long, value_name = "SIZE")]
402 max_entry: Option<String>,
403 /// Demand proof: skip every partial entry regardless of `--partial`,
404 /// and every unattested one (a format with no checksum and no header
405 /// self-check, whose entries are otherwise written under their real
406 /// names). The strict path every other verb in this tool already
407 /// stands on, applied here on request instead of by default.
408 #[arg(long)]
409 strict: bool,
410 /// Use this format instead of detecting one.
411 ///
412 /// Selects among the formats this build's salvage can attempt to
413 /// scan — `zip`, `tar`, `ar`, `cpio`, `arc`, `zoo`, `lha`, `arj` — not
414 /// merely accepted or rejected as in an earlier stage. Naming one
415 /// this build has no salvage scanner for is refused at exit 3,
416 /// exactly as an undetected archive of that same format would be.
417 /// `ar`'s scanner walks members in order and cannot cross a damaged
418 /// header; it says on stderr where it stopped.
419 ///
420 /// Scanners exist for: zip, arc, zoo, lha, arj, tar, cpio, ar.
421 #[arg(long)]
422 format: Option<String>,
423 },
424}