27 releases (11 stable)

Uses old Rust 2015

1.1.6 Mar 30, 2024
1.1.4 Jan 19, 2024
1.1.3 Jan 6, 2022
1.1.1 Nov 11, 2021
0.3.1 Nov 30, 2018

#238 in Command line utilities

Download history 158/week @ 2023-12-18 61/week @ 2023-12-25 68/week @ 2024-01-01 92/week @ 2024-01-08 99/week @ 2024-01-15 68/week @ 2024-01-22 31/week @ 2024-01-29 59/week @ 2024-02-05 94/week @ 2024-02-12 104/week @ 2024-02-19 146/week @ 2024-02-26 99/week @ 2024-03-04 181/week @ 2024-03-11 177/week @ 2024-03-18 470/week @ 2024-03-25 753/week @ 2024-04-01

1,605 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