diff options
author | Rory Dudley | 2024-07-08 16:05:14 -0600 |
---|---|---|
committer | Rory Dudley | 2024-07-08 16:05:14 -0600 |
commit | b48f8f74cf12c174a0d2e72307616a77e6635ae2 (patch) | |
tree | b48061705810604f658d91a3ca5cd55bcb8440a7 | |
parent | 13406827a6f13be62659cdb6dcaf3504b5e6210b (diff) | |
download | dwarvish-b48f8f74cf12c174a0d2e72307616a77e6635ae2.tar.gz |
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.
-rw-r--r-- | src/main.rs | 31 |
1 files 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<Mutex<bool>>, env: &mut Environment) { } } -fn options() { +fn options(env: &mut Environment) { let args: Vec<String> = 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); |