[Rust] A minimalistic, (essentially) POSIX-compliant option parser (https://crates.io/crates/getopt)

root

getopt

A minimalistic, (essentially) POSIX-compliant option parser.

getopt parses through options one at a time in the order they are given on the command line, stopping at the first non-option argument.

Example:

#![allow(unused_assignments, unused_variables)]

use getopt::prelude::*;
use std::{env, error::Error};

fn main() -> Result<(), Box<dyn Error>> {
    let mut args: Vec<String> = env::args().collect();
    let mut state = State::new();

    let mut a_flag = false;
    let mut b_flag = String::new();

    loop {
        match getopt(&args, "ab:", &mut state)? {
            Opt(None, _) => break,
            Opt(Some(&#39;a&#39;), None) => a_flag = true,
            Opt(Some(&#39;b&#39;), Some(string)) => b_flag = string,
            _ => unreachable!(),
        }
    }

    let args = args.split_off(state.index);

    // ...

    Ok(())
}