#parser #command-line #option #time

bin+lib getopt

A minimal, (essentially) POSIX-compliant option parser

24 releases (8 stable)

1.1.3 Jan 6, 2022
1.1.1 Nov 11, 2021
1.1.0 Dec 30, 2020
1.0.2 Nov 6, 2020
0.3.1 Nov 30, 2018

#349 in Command line utilities

Download history 61/week @ 2022-11-29 121/week @ 2022-12-06 90/week @ 2022-12-13 489/week @ 2022-12-20 308/week @ 2022-12-27 279/week @ 2023-01-03 133/week @ 2023-01-10 210/week @ 2023-01-17 330/week @ 2023-01-24 258/week @ 2023-01-31 167/week @ 2023-02-07 264/week @ 2023-02-14 395/week @ 2023-02-21 184/week @ 2023-02-28 249/week @ 2023-03-07 158/week @ 2023-03-14

1,021 downloads per month
Used in 11 crates (10 directly)

BSD-3-Clause-Clear

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