dragonmaus / getopt
Latest patch on October 12, 2019
srcUpdate permissionson October 12, 2019
.gitignoreAdd git repository mirrorson July 29, 2019
.ignoreAdd git repository mirrorson July 29, 2019
Cargo.tomlInline doc_comment and remove dependencyon April 17, 2019
LICENSEBump license and versionon January 1, 2019
README.mdUpdate READMEon July 29, 2019
_boringAdd git repository mirrorson July 29, 2019
README.md

getopt

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

getopt::Parser iterates over the provided arguments, producing options one at a time in the order in which they are given on the command line, and stopping at the first non-option argument.

Example:

#![allow(unused_assignments, unused_variables)]

use getopt::prelude::*;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut args: Vec<String> = std::env::args().collect();
    let mut opts = Parser::new(&args, "ab:");

    let mut a_flag = false;
    let mut b_flag = String::new();
    loop {
        match opts.next().transpose()? {
            None => break,
            Some(opt) => match opt {
                Opt('a', None) => a_flag = true,
                Opt('b', Some(string)) => b_flag = string.clone(),
                _ => unreachable!(),
            }
        }
    }

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

    // …

    Ok(())
}

Links: