#option #parser #getopt

bin+lib getopt

A minimalistic, (essentially) POSIX-compliant option parser

11 releases

✓ Uses Rust 2018 edition

new 0.3.6 Jan 1, 2019
0.3.5 Dec 15, 2018
0.3.1 Nov 30, 2018
0.2.2 Nov 23, 2018
0.1.0 Nov 6, 2018

#60 in Command line utilities

Download history 13/week @ 2018-11-07 6/week @ 2018-11-14 25/week @ 2018-11-21 29/week @ 2018-11-28 84/week @ 2018-12-05 17/week @ 2018-12-12 35/week @ 2018-12-19 86/week @ 2018-12-26

106 downloads per month

BSD-3-Clause-Clear

23KB
359 lines

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

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

    // ...

    Ok(())
}

Links:

No runtime deps