getopt 0.3.4

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

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)]

extern crate getopt;

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('a'), None) => a_flag = true,
            Opt(Some('b'), Some(string)) => b_flag = string,
            _ => unreachable!(),
        }
    }

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

    // ...

    Ok(())
}

Links: