#getopts #parser #opt

bin+lib getopt

A minimal, (essentially) POSIX-compliant option parser

30 releases (14 stable)

Uses old Rust 2015

new 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

#370 in Command line utilities

Download history 48/week @ 2025-01-29 104/week @ 2025-02-05 86/week @ 2025-02-12 121/week @ 2025-02-19 136/week @ 2025-02-26 65/week @ 2025-03-05 104/week @ 2025-03-12 62/week @ 2025-03-19 68/week @ 2025-03-26 23/week @ 2025-04-02 62/week @ 2025-04-09 44/week @ 2025-04-16 117/week @ 2025-04-23 62/week @ 2025-04-30 108/week @ 2025-05-07 312/week @ 2025-05-14

607 downloads per month
Used in 12 crates (11 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