From b48f8f74cf12c174a0d2e72307616a77e6635ae2 Mon Sep 17 00:00:00 2001 From: Rory Dudley Date: Mon, 8 Jul 2024 16:05:14 -0600 Subject: Allow passing a filename to dwvsh The dwvsh binary may optionally take a filename as the last argument. Instead of spawning an interactive shell, it will instead run a shell program at the path specified. --- src/main.rs | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index f87b69d..98b49ed 100644 --- a/src/main.rs +++ b/src/main.rs @@ -82,7 +82,7 @@ fn repl(away: &mut Arc>, env: &mut Environment) { } } -fn options() { +fn options(env: &mut Environment) { let args: Vec = env::args().collect(); for arg in args.iter() { if arg.eq("--version") { @@ -94,6 +94,33 @@ fn options() { std::process::exit(0); } } + + match args.last() { + Some(arg) => { + if args.len() > 1 && !arg.starts_with('-') { + let poetry = std::fs::read_to_string(arg) + .expect(format!("dwvsh: can't open input file: {}", arg).as_str()); + let poem = Poem::read(poetry, env); + let poem = match poem { + Ok(poem) => poem, + Err(e) => { + eprintln!("dwvsh: {}", e.to_string().to_lowercase()); + std::process::exit(1); + } + }; + + // Recite the poem + match poem.recite(env) { + Ok(_) => {} + Err(e) => eprintln!("dwvsh: {}", e.to_string().to_lowercase()), + } + + // Quit + std::process::exit(0); + } + } + None => {} + } } /// Shell entry @@ -123,7 +150,7 @@ fn main() { }; // Parse flags and other arguments - options(); + options(&mut env); // Begin evaluating commands repl(&mut away, &mut env); -- cgit v1.2.3