1use std::ffi::OsString;
5use std::path::PathBuf;
6
7use crate::{print, search};
8
9pub const HELP: &str = "\
10trg — search inside compressed log archives
11(.tgz, .tar.zst, .tar.xz, .tar.bz2, .tar.lz4, .tar.Z, .zip)
12
13USAGE:
14 trg [OPTIONS] PATTERN PATH...
15 trg [OPTIONS] -e PATTERN... PATH...
16 trg [OPTIONS] -T PATH...
17
18At least one PATH is required. trg never searches the current directory by
19default: a silent walk of $PWD is not a safe guess for a tool whose exit 1
20is a claim that the logs are clean.
21
22MATCHING (same meaning as ripgrep):
23 -e, --regexp PAT add a pattern; repeatable
24 -i, --ignore-case case-insensitive
25 -S, --smart-case case-insensitive unless the pattern has uppercase
26 -w, --word-regexp match whole words only
27 -F, --fixed-strings treat patterns as literals
28 -v, --invert-match show non-matching lines
29 -a, --text search members that look binary
30 -m, --max-count NUM stop after NUM matches per member
31 --crlf treat CRLF as the line terminator
32
33OUTPUT (same meaning as ripgrep):
34 -n, --line-number show line numbers (default)
35 -N, --no-line-number hide line numbers
36 -A, --after NUM lines of trailing context
37 -B, --before NUM lines of leading context
38 -C, --context NUM lines of context on both sides
39 -o, --only-matching print only the matching part
40 -c, --count print a count per member
41 -l, --files-with-matches
42 print member paths only
43 -q, --quiet print nothing; exit code only
44 --json JSON Lines output
45 --color WHEN auto | always | never (auto: only on a tty)
46 --escape WHEN show control bytes from logs and member names as
47 text (\\x1b) instead of sending them to the
48 terminal: auto | always | never (auto: on a tty,
49 per stream; piped output stays byte-exact)
50 If several output modes are given, the most
51 suppressive wins: -q, then -l, then -c, then --json.
52
53ARCHIVES:
54 -g, --glob PAT filter MEMBERS inside archives; repeatable. Without
55 a `/` it also matches the member's file name. A
56 leading ! excludes; the last matching glob wins
57 --archive-glob PAT
58 when walking a directory, search only the archives
59 matching PAT; repeatable. Without a `/` it matches
60 the file name at any depth, with one the path below
61 the directory named. Files named explicitly are
62 always searched. A leading ! excludes, as for -g.
63 Matching nothing is exit 2
64 -T, --list-members list member paths without searching. Takes no
65 PATTERN at all: every positional is a PATH, so
66 `trg -T *.tgz` lists all of them
67 --archive-sep C separator in archive:member:line (default ':')
68 --no-sort allow output in completion order
69
70RESOURCES:
71 -j, --jobs NUM archives searched concurrently (default 1)
72 --turbo -j <ncpus> --nice 0
73 --nice NUM scheduling priority (default 10)
74 --no-nice do not lower priority
75 --no-drop-cache keep archive pages in the page cache
76 --load-limit N run one job at a time while the 1-min load average
77 from other processes exceeds N, and add jobs back
78 as it falls. Rechecked every 5 s
79
80 --examples worked examples of every option; try
81 `trg --examples | less`
82 -h, --help this message
83 -V, --version version
84
85EXIT CODES:
86 0 matches found, everything read
87 1 no matches, everything read
88 2 something went unread — never trust a 1 you did not get
89
90NOTES (documented, and surprising the first time):
91 -T lists every member, including ones a search skips as binary.
92 Two directories symlinking the same file search it once (identity dedup).
93 An intact archive behind a corrupt gzip trailer is a 0; truncation is
94 still caught and is a 2.
95 --turbo's ordered output is not bounded by the spill cap; peak RSS grows
96 with the match volume held in order.
97 On a broken pipe (`| head`) trg stops early and exits 0, leaving later
98 archives unread.
99 Compression is detected from content, never from the filename, so there is
100 no -z flag. gzip, zstd, xz, bzip2 and lz4 are recognised, each unwrapped
101 one level, so a logrotate'd access.log.1.gz, .zst, .xz, .bz2 or .lz4
102 inside the archive is searched as text rather than skipped as binary.
103 .Z (compress) is read too, but only as a whole file: a .Z member
104 inside an archive is reported unread, a 2.
105 A zip's members are searched like a tar's, compressed ones included. A
106 zip or tar inside another archive is searched as bytes, not opened.
107 .Z has no length and no checksum, so a truncated .Z reads as a shorter
108 file with no error. A .Z with no match is still a 1, with a note on
109 stderr that the file could not vouch for its own end.
110 --inflate and --inflate-budget are accepted and ignored since 0.7.0, with
111 a note on stderr: trg always streams.
112";
113
114#[derive(Debug, Clone, Copy, PartialEq, Eq)]
115pub enum ColorArg { Auto, Always, Never }
116
117pub struct Args {
118 pub patterns: Vec<String>,
119 pub paths: Vec<PathBuf>,
120 pub globs: Vec<String>,
121 pub archive_globs: Vec<String>,
122 pub jobs: usize,
123 pub nice: Option<i32>,
124 pub drop_cache: bool,
125 pub deprecated: Vec<&'static str>,
128 pub load_limit: Option<f64>,
129 pub archive_sep: char,
130 pub sort: bool,
131 pub list_members: bool,
132 pub count: bool,
133 pub files_with_matches: bool,
134 pub quiet: bool,
135 pub json: bool,
136 pub color: ColorArg,
137 pub escape: ColorArg,
138 pub search: search::SearchOpts,
139 pub print: print::PrintOpts,
140}
141
142impl Default for Args {
143 fn default() -> Self {
144 Self {
145 patterns: Vec::new(),
146 paths: Vec::new(),
147 globs: Vec::new(),
148 archive_globs: Vec::new(),
149 jobs: 1,
150 nice: Some(10),
151 drop_cache: true,
152 deprecated: Vec::new(),
153 load_limit: None,
154 archive_sep: ':',
155 sort: true,
156 list_members: false,
157 count: false,
158 files_with_matches: false,
159 quiet: false,
160 json: false,
161 color: ColorArg::Auto,
162 escape: ColorArg::Auto,
163 search: search::SearchOpts::default(),
164 print: print::PrintOpts::default(),
165 }
166 }
167}
168
169fn parse_size(s: &str) -> anyhow::Result<u64> {
171 let s = s.trim();
172 let (num, mult) = match s.chars().last() {
173 Some('K') | Some('k') => (&s[..s.len() - 1], 1024),
174 Some('M') | Some('m') => (&s[..s.len() - 1], 1024 * 1024),
175 Some('G') | Some('g') => (&s[..s.len() - 1], 1024 * 1024 * 1024),
176 _ => (s, 1),
177 };
178 let n: u64 = num.parse()?;
179 n.checked_mul(mult)
180 .ok_or_else(|| anyhow::anyhow!("--inflate-budget value too large: {s}"))
181}
182
183fn deprecate(a: &mut Args, flag: &'static str) {
184 if !a.deprecated.contains(&flag) {
185 a.deprecated.push(flag);
186 }
187}
188
189pub fn parse() -> anyhow::Result<Args> {
190 parse_from(std::env::args_os())
191}
192
193pub fn parse_from<I>(argv: I) -> anyhow::Result<Args>
194where
195 I: IntoIterator<Item = OsString>,
196{
197 use lexopt::prelude::*;
198
199 let mut a = Args::default();
200 let mut explicit_jobs = false;
201 let mut explicit_nice = false;
202 let mut ctx_both: Option<usize> = None;
203 let mut positionals: Vec<OsString> = Vec::new();
207 let mut explicit_patterns = false;
208
209 let mut p = lexopt::Parser::from_iter(argv);
210 while let Some(arg) = p.next()? {
211 match arg {
212 Short('e') | Long("regexp") => {
213 a.patterns.push(p.value()?.string()?);
214 explicit_patterns = true;
215 }
216 Short('i') | Long("ignore-case") => a.search.case_insensitive = true,
217 Short('S') | Long("smart-case") => a.search.smart_case = true,
218 Short('w') | Long("word-regexp") => a.search.word = true,
219 Short('F') | Long("fixed-strings") => a.search.fixed = true,
220 Short('v') | Long("invert-match") => a.search.invert = true,
221 Short('a') | Long("text") => a.search.text = true,
222 Short('m') | Long("max-count") => {
223 a.search.max_count = Some(p.value()?.parse()?)
224 }
225 Long("crlf") => a.search.crlf = true,
226
227 Short('n') | Long("line-number") => a.search.line_numbers = true,
228 Short('N') | Long("no-line-number") => a.search.line_numbers = false,
229 Short('A') | Long("after") | Long("after-context") => {
230 a.search.after = p.value()?.parse()?
231 }
232 Short('B') | Long("before") | Long("before-context") => {
233 a.search.before = p.value()?.parse()?
234 }
235 Short('C') | Long("context") => ctx_both = Some(p.value()?.parse()?),
236 Short('o') | Long("only-matching") => a.print.only_matching = true,
237 Short('c') | Long("count") => a.count = true,
238 Short('l') | Long("files-with-matches") => a.files_with_matches = true,
239 Short('q') | Long("quiet") => a.quiet = true,
240 Long("json") => a.json = true,
241 Long("color") => {
242 a.color = match p.value()?.string()?.as_str() {
243 "auto" => ColorArg::Auto,
244 "always" => ColorArg::Always,
245 "never" => ColorArg::Never,
246 other => anyhow::bail!("--color expects auto|always|never, got '{other}'"),
247 }
248 }
249 Long("escape") => {
250 a.escape = match p.value()?.string()?.as_str() {
251 "auto" => ColorArg::Auto,
252 "always" => ColorArg::Always,
253 "never" => ColorArg::Never,
254 other => anyhow::bail!("--escape expects auto|always|never, got '{other}'"),
255 }
256 }
257
258 Short('g') | Long("glob") => a.globs.push(p.value()?.string()?),
259 Long("archive-glob") => a.archive_globs.push(p.value()?.string()?),
260 Short('T') | Long("list-members") => a.list_members = true,
261 Long("archive-sep") => {
262 let v = p.value()?.string()?;
263 let mut it = v.chars();
264 match (it.next(), it.next()) {
265 (Some(c), None) => a.archive_sep = c,
266 _ => anyhow::bail!("--archive-sep expects exactly one character"),
267 }
268 }
269 Long("no-sort") => a.sort = false,
270
271 Short('j') | Long("jobs") => {
272 a.jobs = p.value()?.parse()?;
273 explicit_jobs = true;
274 }
275 Long("turbo") => {
276 if !explicit_jobs { a.jobs = num_cpus::get(); }
277 if !explicit_nice { a.nice = Some(0); }
278 }
279 Long("nice") => {
280 a.nice = Some(p.value()?.parse()?);
281 explicit_nice = true;
282 }
283 Long("no-nice") => {
284 a.nice = None;
285 explicit_nice = true;
286 }
287 Long("no-drop-cache") => a.drop_cache = false,
288 Long("inflate") => {
293 match p.value()?.string()?.as_str() {
294 "auto" | "stream" | "buffer" => {}
295 other => anyhow::bail!("--inflate expects auto|stream|buffer, got '{other}'"),
296 }
297 deprecate(&mut a, "--inflate");
298 }
299 Long("inflate-budget") => {
300 parse_size(&p.value()?.string()?)?;
301 deprecate(&mut a, "--inflate-budget");
302 }
303 Long("load-limit") => a.load_limit = Some(p.value()?.parse()?),
304
305 Short('h') | Long("help") => {
306 print!("{HELP}");
307 std::process::exit(0);
308 }
309 Long("examples") => {
310 print!("{}", crate::examples::render());
311 std::process::exit(0);
312 }
313 Short('V') | Long("version") => {
314 println!("trg {}", env!("CARGO_PKG_VERSION"));
315 std::process::exit(0);
316 }
317
318 Value(v) => positionals.push(v),
319 other => return Err(other.unexpected().into()),
320 }
321 }
322
323 if let Some(n) = ctx_both {
326 a.search.before = n;
327 a.search.after = n;
328 }
329 a.print.mode = if a.quiet {
334 print::Mode::Quiet
335 } else if a.files_with_matches {
336 print::Mode::FilesWithMatches
337 } else if a.count {
338 print::Mode::Count
339 } else if a.json {
340 print::Mode::Json
341 } else {
342 print::Mode::Standard
343 };
344
345 let mut rest = positionals.into_iter();
359 if !a.list_members
360 && !explicit_patterns
361 && let Some(first) = rest.next()
362 {
363 a.patterns.push(first.string()?);
364 }
365 a.paths.extend(rest.map(PathBuf::from));
366
367 if a.patterns.is_empty() && !a.list_members {
369 anyhow::bail!("no pattern given\n\n{HELP}");
370 }
371 if a.paths.is_empty() {
379 anyhow::bail!("no path given: name at least one archive or directory\n\n{HELP}");
380 }
381 Ok(a)
382}
383
384#[cfg(test)]
385mod tests {
386 use super::*;
387
388 fn p(args: &[&str]) -> Args {
389 let argv: Vec<std::ffi::OsString> =
390 std::iter::once("trg".into()).chain(args.iter().map(|s| s.into())).collect();
391 parse_from(argv).unwrap()
392 }
393
394 #[test]
395 fn defaults_match_the_spec() {
396 let a = p(&["pat", "f.tgz"]);
397 assert_eq!(a.jobs, 1);
398 assert_eq!(a.nice, Some(10));
399 assert!(a.drop_cache);
400 assert!(a.deprecated.is_empty());
401 assert_eq!(a.archive_sep, ':');
402 assert!(a.sort);
403 assert_eq!(a.search.heap_limit, 16 * 1024 * 1024);
404 assert!(a.load_limit.is_none());
405 }
406
407 #[test]
408 fn first_positional_is_the_pattern_rest_are_paths() {
409 let a = p(&["NEEDLE", "a.tgz", "b.tgz"]);
410 assert_eq!(a.patterns, vec!["NEEDLE"]);
411 assert_eq!(a.paths.len(), 2);
412 }
413
414 #[test]
415 fn dash_e_patterns_do_not_consume_the_first_positional() {
416 let a = p(&["-e", "one", "-e", "two", "a.tgz"]);
417 assert_eq!(a.patterns, vec!["one", "two"]);
418 assert_eq!(a.paths.len(), 1);
419 }
420
421 #[test]
422 fn turbo_raises_jobs_and_clears_nice() {
423 let a = p(&["--turbo", "pat", "f.tgz"]);
424 assert_eq!(a.jobs, num_cpus::get());
425 assert_eq!(a.nice, Some(0));
426 assert!(a.drop_cache, "cache dropping stays on under --turbo");
427 }
428
429 #[test]
430 fn explicit_j_after_turbo_wins() {
431 let a = p(&["--turbo", "-j", "2", "pat", "f.tgz"]);
432 assert_eq!(a.jobs, 2);
433 }
434
435 #[test]
436 fn no_nice_disables_priority_lowering() {
437 let a = p(&["--no-nice", "pat", "f.tgz"]);
438 assert_eq!(a.nice, None);
439 }
440
441 #[test]
442 fn context_flags_match_rg_semantics() {
443 let a = p(&["-C", "3", "pat", "f.tgz"]);
444 assert_eq!((a.search.before, a.search.after), (3, 3));
445 let a = p(&["-A", "2", "-B", "1", "pat", "f.tgz"]);
446 assert_eq!((a.search.before, a.search.after), (1, 2));
447 }
448
449 #[test]
450 fn n_capital_disables_line_numbers() {
451 assert!(p(&["pat", "f.tgz"]).search.line_numbers);
452 assert!(!p(&["-N", "pat", "f.tgz"]).search.line_numbers);
453 }
454
455 #[test]
456 fn size_suffixes_parse() {
457 assert_eq!(parse_size("512K").unwrap(), 512 * 1024);
458 assert_eq!(parse_size("2M").unwrap(), 2 * 1024 * 1024);
459 assert_eq!(parse_size("1G").unwrap(), 1024 * 1024 * 1024);
460 assert_eq!(parse_size("1024").unwrap(), 1024);
461 assert_eq!(p(&["--inflate-budget", "1G", "p", "f"]).deprecated, ["--inflate-budget"]);
462 }
463
464 #[test]
465 fn globs_accumulate() {
466 let a = p(&["-g", "*.access.log", "-g", "*.error.log", "pat", "f.tgz"]);
467 assert_eq!(a.globs.len(), 2);
468 }
469
470 #[test]
471 fn archive_sep_is_configurable() {
472 assert_eq!(p(&["--archive-sep", "!", "pat", "f.tgz"]).archive_sep, '!');
473 }
474
475 #[test]
476 fn unknown_flag_is_an_error_not_a_pattern() {
477 let argv: Vec<std::ffi::OsString> =
478 ["trg", "--nonsense", "pat", "f.tgz"].iter().map(|s| s.into()).collect();
479 assert!(parse_from(argv).is_err());
480 }
481
482 #[test]
483 fn inflate_mode_rejects_garbage() {
484 let argv: Vec<std::ffi::OsString> =
485 ["trg", "--inflate", "sideways", "p", "f"].iter().map(|s| s.into()).collect();
486 assert!(parse_from(argv).is_err());
487 }
488
489 #[test]
490 fn crlf_flag_sets_the_search_opt_and_defaults_to_false() {
491 assert!(!p(&["pat", "f.tgz"]).search.crlf);
492 assert!(p(&["--crlf", "pat", "f.tgz"]).search.crlf);
493 }
494
495 #[test]
496 fn inflate_budget_overflow_is_an_error_not_a_wrapped_value() {
497 let argv: Vec<std::ffi::OsString> =
498 ["trg", "--inflate-budget", "18446744073709551615G", "p", "f"]
499 .iter().map(|s| s.into()).collect();
500 assert!(parse_from(argv).is_err());
501 }
502
503 #[test]
504 fn output_mode_precedence_is_most_suppressive_wins() {
505 assert_eq!(p(&["pat", "f"]).print.mode, print::Mode::Standard);
506 assert_eq!(p(&["-c", "pat", "f"]).print.mode, print::Mode::Count);
507 assert_eq!(p(&["-l", "pat", "f"]).print.mode, print::Mode::FilesWithMatches);
508 assert_eq!(p(&["-q", "pat", "f"]).print.mode, print::Mode::Quiet);
509 assert_eq!(p(&["--json", "pat", "f"]).print.mode, print::Mode::Json);
510 assert_eq!(p(&["-c", "-l", "pat", "f"]).print.mode, print::Mode::FilesWithMatches);
511 assert_eq!(p(&["-c", "-l", "-q", "pat", "f"]).print.mode, print::Mode::Quiet);
512 assert_eq!(p(&["--json", "-c", "pat", "f"]).print.mode, print::Mode::Count);
513 }
514
515 #[test]
516 fn color_parses_all_three_and_rejects_the_rest() {
517 assert_eq!(p(&["pat", "f"]).color, ColorArg::Auto);
518 assert_eq!(p(&["--color", "always", "pat", "f"]).color, ColorArg::Always);
519 assert_eq!(p(&["--color", "never", "pat", "f"]).color, ColorArg::Never);
520 let argv: Vec<std::ffi::OsString> =
521 ["trg", "--color", "nonsense", "pat", "f"].iter().map(|s| s.into()).collect();
522 assert!(parse_from(argv).is_err());
523 }
524
525 #[test]
530 fn dash_t_with_one_positional_takes_it_as_a_path_not_a_pattern() {
531 let a = p(&["-T", "archive.tgz"]);
532 assert!(a.patterns.is_empty(), "-T needs no pattern, got {:?}", a.patterns);
533 assert_eq!(a.paths, vec![PathBuf::from("archive.tgz")]);
534 }
535
536 #[test]
540 fn dash_t_takes_every_positional_as_a_path() {
541 let a = p(&["-T", "a.tgz", "b.tgz", "c.tgz"]);
542 assert!(a.patterns.is_empty(), "-T takes no pattern, got {:?}", a.patterns);
543 assert_eq!(
544 a.paths,
545 vec![PathBuf::from("a.tgz"), PathBuf::from("b.tgz"), PathBuf::from("c.tgz")]
546 );
547 }
548
549 #[test]
552 fn dash_t_after_the_positionals_parses_the_same() {
553 let a = p(&["a.tgz", "b.tgz", "-T"]);
554 assert!(a.patterns.is_empty(), "got {:?}", a.patterns);
555 assert_eq!(a.paths, vec![PathBuf::from("a.tgz"), PathBuf::from("b.tgz")]);
556 }
557
558 #[test]
562 fn an_explicit_e_makes_every_positional_a_path_whatever_the_order() {
563 let a = p(&["a.tgz", "-e", "pat", "b.tgz"]);
564 assert_eq!(a.patterns, vec!["pat"]);
565 assert_eq!(a.paths, vec![PathBuf::from("a.tgz"), PathBuf::from("b.tgz")]);
566 }
567
568 #[test]
571 fn dash_t_with_an_explicit_e_pattern_keeps_both() {
572 let a = p(&["-T", "-e", "pat", "archive.tgz"]);
573 assert_eq!(a.patterns, vec!["pat"]);
574 assert_eq!(a.paths, vec![PathBuf::from("archive.tgz")]);
575 }
576
577 fn err(args: &[&str]) -> String {
578 let argv: Vec<std::ffi::OsString> =
579 std::iter::once("trg".into()).chain(args.iter().map(|s| s.into())).collect();
580 match parse_from(argv) {
581 Ok(_) => panic!("expected a usage error, got a parse"),
582 Err(e) => format!("{e:#}"),
583 }
584 }
585
586 #[test]
589 fn a_missing_pattern_is_still_an_error_without_dash_t() {
590 assert!(err(&[]).contains("no pattern given"));
591 assert!(err(&["-e", "pat"]).contains("no path given"), "pattern present, path missing");
592 }
593
594 #[test]
598 fn a_missing_path_is_a_usage_error_not_an_empty_clean_run() {
599 assert!(err(&["needle"]).contains("no path given"), "pattern with no path");
600 assert!(err(&["-T"]).contains("no path given"), "-T with no path");
601 assert!(err(&["-q", "needle"]).contains("no path given"), "-q must not hide it");
602 }
603
604 #[test]
608 fn one_positional_is_the_pattern_and_leaves_no_path() {
609 assert!(err(&["archive.tgz"]).contains("no path given"));
610 }
611
612 #[test]
613 fn inflate_budget_accepts_u64_max_with_no_suffix() {
614 assert_eq!(parse_size(&u64::MAX.to_string()).unwrap(), u64::MAX);
615 }
616
617 #[test]
618 fn inflate_budget_accepts_a_large_value_with_a_suffix_just_under_the_boundary() {
619 let n = u64::MAX / (1024 * 1024 * 1024);
621 assert_eq!(parse_size(&format!("{n}G")).unwrap(), n * 1024 * 1024 * 1024);
622 }
623}