Arg parsing in rust

This commit is contained in:
Greg Shuflin 2023-07-23 03:04:59 -07:00
parent ec74203e70
commit d7b402c764
4 changed files with 55 additions and 1 deletions

9
Cargo.lock generated
View File

@ -2,6 +2,15 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "lexopt"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baff4b617f7df3d896f97fe922b64817f6cd9a756bb81d40f8883f2f66dcb401"
[[package]]
name = "nmsrust"
version = "0.1.0"
dependencies = [
"lexopt",
]

View File

@ -10,3 +10,5 @@ crate-type = ["staticlib"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
lexopt = "0.3.0"

43
src/args.rs Normal file
View File

@ -0,0 +1,43 @@
use std::ffi::OsString;
use lexopt::prelude::*;
#[derive(Debug, Default)]
pub struct Args {
version: bool,
clear_screen: bool,
foreground: Option<OsString>,
autodecrypt: bool,
mask_blanks: bool,
}
pub fn parse_arguments() -> Result<Args, lexopt::Error> {
let mut parser = lexopt::Parser::from_env();
let mut args = Args::default();
while let Some(arg) = parser.next()? {
match arg {
Short('a') => {
args.autodecrypt = true;
}
Short('c') => {
args.clear_screen = true;
}
Short('f') => {
let foreground = parser.value()?;
args.foreground = Some(foreground);
}
Short('s') => {
args.mask_blanks = true;
}
Short('v') => {
args.version = true;
}
_ => return Err(arg.unexpected()),
}
}
Ok(args)
}

View File

@ -1,4 +1,4 @@
mod args;
#[no_mangle]
pub extern "C" fn rust_main() {