summaryrefslogtreecommitdiffstats
path: root/rust-cli/grrs/src/main.rs
blob: e5ed13b2fc5d8368747c85742bc142bca4dfdf3a (plain) (blame)
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
use std::fs::{File};
use::clap::Parser;

#[derive(Parser)]
struct Cli {
    pattern: String,
    path: std::path::PathBuf,
}

fn main() {
    let args = Cli::parse();
    if !args.path.exists() {
        eprintln!("The file at '{}' does not exist", &args.path.display());
        return;
    }
    if args.pattern.is_empty() {
        eprintln!("No pattern was supplied, see --help");
        return;
    }
    let file = File::open(args.path).expect("could not read file");
    let matches = grrs::find_matches(&file, &args.pattern).unwrap();
    if matches.len() < 1 {
        return;
    }
    for line in matches {
        println!("{}", line);
    }
}