From e022a7eff4b9af4df3b3a852b911d89a04fdb98b Mon Sep 17 00:00:00 2001 From: Rory Dudley Date: Wed, 3 Jul 2024 15:42:32 -0600 Subject: Option parsing and version Add some basic logic for parsing commandline arguments. Also, use build.rs to embed the program version (and git commit) during the compile step. The program currently accepts the '--version' commandline argument to print the version, then quit. --- build.rs | 16 ++++++++++++++++ src/main.rs | 17 +++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 build.rs diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..fef4321 --- /dev/null +++ b/build.rs @@ -0,0 +1,16 @@ +use std::process::Command; + +fn main() { + let build = String::from_utf8_lossy( + &Command::new("git") + .args(&["rev-parse", "HEAD"]) + .output() + .unwrap() + .stdout, + ) + .to_string(); + println!( + "cargo::rustc-env=DWVSH_BUILD={}", + build.get(0..10).unwrap_or(&build) + ); +} diff --git a/src/main.rs b/src/main.rs index 821dde2..f87b69d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -82,6 +82,20 @@ fn repl(away: &mut Arc>, env: &mut Environment) { } } +fn options() { + let args: Vec = env::args().collect(); + for arg in args.iter() { + if arg.eq("--version") { + println!( + "dwvsh v{} ({})", + env!("CARGO_PKG_VERSION"), + env!("DWVSH_BUILD") + ); + std::process::exit(0); + } + } +} + /// Shell entry /// /// Shell setup and entry @@ -108,6 +122,9 @@ fn main() { .unwrap(); }; + // Parse flags and other arguments + options(); + // Begin evaluating commands repl(&mut away, &mut env); } -- cgit v1.2.3