30 releases (14 stable)

Uses old Rust 2015

1.1.9 May 18, 2025
1.1.7 Jun 23, 2024
1.1.6 Mar 30, 2024
1.1.3 Jan 6, 2022
0.3.1 Nov 30, 2018

#112 in Command-line interface

Download history 1359/week @ 2025-10-12 1207/week @ 2025-10-19 1672/week @ 2025-10-26 1987/week @ 2025-11-02 2133/week @ 2025-11-09 1327/week @ 2025-11-16 1371/week @ 2025-11-23 1531/week @ 2025-11-30 1248/week @ 2025-12-07 743/week @ 2025-12-14 296/week @ 2025-12-21 350/week @ 2025-12-28 615/week @ 2026-01-04 1219/week @ 2026-01-11 1360/week @ 2026-01-18 1098/week @ 2026-01-25

4,345 downloads per month
Used in 13 crates (12 directly)

BSD-3-Clause-Clear

27KB
437 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