#parser #option #getopt

bin+lib getopt

A minimal, (essentially) POSIX-compliant option parser

16 unstable releases (3 breaking)

✓ Uses Rust 2018 edition

0.4.2 Apr 17, 2019
0.4.1 Apr 8, 2019
0.3.9 Feb 21, 2019
0.3.6 Jan 1, 2019
0.1.0 Nov 6, 2018

#198 in Command line utilities

Download history 2/week @ 2018-12-25 24/week @ 2019-01-01 19/week @ 2019-01-08 24/week @ 2019-01-15 4/week @ 2019-01-22 4/week @ 2019-01-29 2/week @ 2019-02-05 5/week @ 2019-02-12 22/week @ 2019-02-19 38/week @ 2019-02-26 4/week @ 2019-03-05 4/week @ 2019-03-12 32/week @ 2019-03-19 187/week @ 2019-03-26 33/week @ 2019-04-02

124 downloads per month

BSD-3-Clause-Clear

27KB
451 lines

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:

No runtime deps