add command line args support
This commit is contained in:
parent
e316ccb947
commit
61761d01e0
1 changed files with 33 additions and 2 deletions
35
src/main.rs
35
src/main.rs
|
@ -1,11 +1,42 @@
|
||||||
use crate::line::Line;
|
use crate::line::Line;
|
||||||
|
use clap::Parser;
|
||||||
use std::fs::read_to_string;
|
use std::fs::read_to_string;
|
||||||
|
use std::process::exit;
|
||||||
|
|
||||||
mod line;
|
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() {
|
fn main() {
|
||||||
let left_file = read_to_string("a.txt").expect("Left file not found");
|
let args = CliArgs::parse();
|
||||||
let right_file = read_to_string("b.txt").expect("Right file not found");
|
|
||||||
|
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);
|
let lines: Vec<Line> = line::compare_lines(&left_file, &right_file);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue