Crates.io | arg_dot_h |
lib.rs | arg_dot_h |
version | 0.1.2 |
source | src |
created_at | 2021-07-07 22:15:12.224825 |
updated_at | 2021-07-09 08:40:45.712831 |
description | Command line argument parsing crate reminiscent of how many C applications did it |
homepage | |
repository | https://codeberg.org/BubbyRoosh/arg_dot_h |
max_upload_size | |
id | 420082 |
size | 5,620 |
Argument parser somewhat following the style seen in many C applications:
Example:
use std::process;
use arg_dot_h::*;
fn help(argv0: &str) {
println!("{} [-b] [-s string]", argv0);
process::exit(1);
}
fn main() {
let mut argv0 = String::new();
let mut testbool = false;
let mut teststring = String::new();
// Parses std::env::args(). If an argument starts with '-', it will remove the '-' and iterate
// over the chars. After everything is parsed, anything that didn't start with '-' or wasn't taken with eargf is returned in a Vec<String>
let args = argbegin! {
// First argument is the variable the name of the program will be set to (since it can be renamed)
&mut argv0,
// All other arguments are like a match statement for each char in a valid argument.
// Run when the char is 'b'
'b' => testbool = true,
// eargf! returns the next argument. If a function (help(&argv0)) is passed, it will run
// that if there isn't a next argument. Otherwise it will return an empty string.
's' => teststring = eargf!(help(&argv0)),
// If the char doesn't match anything else, run the help function.
_ => help(&argv0)
};
/* If "-bs mystring more args" was passed, it would output:
true mystring
more
args
*/
println!("{} {}", testbool, teststring);
args.iter().for_each(|arg| println!("{}", arg));
}