From 61761d01e0fd1cbec366bb990f9f6bffcb9ea481 Mon Sep 17 00:00:00 2001 From: Denis Nutiu Date: Sat, 26 Oct 2024 21:42:04 +0300 Subject: [PATCH] add command line args support --- src/main.rs | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 799fe28..0bd4c93 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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::compare_lines(&left_file, &right_file);