28 releases (12 stable)

Uses old Rust 2015

1.1.7 Jun 23, 2024
1.1.6 Mar 30, 2024
1.1.4 Jan 19, 2024
1.1.3 Jan 6, 2022
0.3.1 Nov 30, 2018

#278 in Command line utilities

Download history 288/week @ 2024-03-31 126/week @ 2024-04-07 147/week @ 2024-04-14 128/week @ 2024-04-21 103/week @ 2024-04-28 197/week @ 2024-05-05 166/week @ 2024-05-12 151/week @ 2024-05-19 168/week @ 2024-05-26 98/week @ 2024-06-02 66/week @ 2024-06-09 119/week @ 2024-06-16 250/week @ 2024-06-23 9/week @ 2024-06-30 44/week @ 2024-07-07 154/week @ 2024-07-14

474 downloads per month
Used in 11 crates (10 directly)

BSD-3-Clause-Clear

28KB
463 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::Opt;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut args: Vec<String> = std::env::args().collect();
    let mut opts = getopt::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(())
}

No runtime deps