Function getopt_rs::getopt[][src]

pub fn getopt(
    args: &mut Vec<String>,
    optstring: &str,
    opts: &[Opt<'_>]
) -> Option<(char, Option<String>)>
Expand description

Get command line options.

args is the vector containing the command line arguments.
optstring is a string reference, like ab:c?, ab or hVv.

Each alphanumeric character is an option, and the following : and ? respectively mean that it takes a mandatory or optional value.

opts are the option rules.

If no matching option is found, None is returned. If a mandatory value is not given, an error message is displayed and Some(('?', None)) is returned.
If the option doesn’t take an argument or if an optional argument is not given, Some((opt, None)) is returned.

Example

#[macro_use]
extern crate getopt_rs;

use std::env;
use getopt_rs::{getopt, LongForm};

fn main() {
    let mut args = env::args().collect();
    while let Some(opt) = getopt(&mut args, 
                                 "ab?c:", 
                                 &[
                                    opt!('a'), 
                                    opt!('b', "bar"), 
                                    opt!('c', "ctrl", LongForm::SimpleDash)]) 
    {
        match opt {
            ('a', _) => println!("Found option 'a' that takes no argument."),
            ('b', val) => println!("Found option 'b' that takes an optional argument: {:?}.", val),
            ('c', val) => println!("Found option 'c' that takes a mandatory argument: {:?}", val.unwrap()),
            _ => return, /* An error occured, Some(('?', None)) is returned. */
        }
    }
}