add command line args support

This commit is contained in:
Denis-Cosmin Nutiu 2024-10-26 21:42:04 +03:00
parent e316ccb947
commit 61761d01e0

View file

@ -1,11 +1,42 @@
use crate::line::Line;
use clap::Parser;
use std::fs::read_to_string;
use std::process::exit;
mod line;
#[derive(Parser, Debug)]
#[command(
version = "0.1",
about = "A simple toy program to compare file differences.",
long_about = "A simple toy program to compare file differences. Written as an exercise to learn Rust and its ecosystem."
)]
struct CliArgs {
/// The path to the left file to be compared.
left_file_path: String,
/// The path to the right file to be compared.
right_file_path: String,
}
fn main() {
let left_file = read_to_string("a.txt").expect("Left file not found");
let right_file = read_to_string("b.txt").expect("Right file not found");
let args = CliArgs::parse();
let left_file = match read_to_string(&args.left_file_path) {
Ok(right_file) => right_file,
Err(_) => {
eprintln!("ERROR: File {} was not found.", &args.left_file_path);
exit(1)
}
};
let right_file = match read_to_string(&args.right_file_path) {
Ok(right_file) => right_file,
Err(_) => {
eprintln!("ERROR: File {} was not found.", &args.right_file_path);
exit(1)
}
};
let lines: Vec<Line> = line::compare_lines(&left_file, &right_file);