Crates.io | getopt |
lib.rs | getopt |
version | 1.1.7 |
source | src |
created_at | 2018-11-06 03:32:29.668957 |
updated_at | 2024-06-23 16:38:39.426731 |
description | A minimal, (essentially) POSIX-compliant option parser |
homepage | |
repository | https://git.dragonma.us/rust/getopt |
max_upload_size | |
id | 94958 |
size | 29,077 |
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.
#![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(())
}