#arguments-parser #arguments #parser #applications #arg #dot #did

arg_dot_h

Command line argument parsing crate reminiscent of how many C applications did it

3 releases

0.1.2 Jul 9, 2021
0.1.1 Jul 8, 2021
0.1.0 Jul 7, 2021

#34 in #did

ISC license

5KB
51 lines

arg_dot_h

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));
}

No runtime deps