33 lines
712 B
Rust
33 lines
712 B
Rust
use clap::Parser;
|
|
|
|
// Search for a pattern in a file and display the lines that contain it.
|
|
#[derive(Parser)]
|
|
struct Cli {
|
|
pattern: String,
|
|
path: std::path::PathBuf,
|
|
}
|
|
|
|
fn main() -> Result<(), Box<dyn std::error::Error>>{
|
|
let args = Cli::parse();
|
|
|
|
// let content = std::fs::read_to_string(&args.path).expect("Could not read file.");
|
|
|
|
let result = std::fs::read_to_string(&args.path);
|
|
let content = match result {
|
|
Ok(content) => {
|
|
content
|
|
},
|
|
Err(error) => {
|
|
return Err(error.into());
|
|
}
|
|
};
|
|
|
|
for line in content.lines() {
|
|
if line.contains(&args.pattern) {
|
|
println!("{}", line)
|
|
}
|
|
}
|
|
|
|
Ok(())
|
|
}
|