pub fn parse_key_value_options<'a>(
    flagname: &'a str,
    s: &'a str,
    delimiter: char
) -> impl Iterator<Item = KeyValuePair<'a>>
Expand description

Parse a string of delimiter-separated key-value options. This is intended to simplify parsing of command-line options that take a bunch of parameters encoded into the argument, e.g. for setting up an emulated hardware device. Returns an Iterator of KeyValuePair, which provides convenience functions to parse numeric values and performs appropriate error handling.

flagname - name of the command line parameter, used as context in error messages s - the string to parse delimiter - the character that separates individual pairs

Usage example:


fn parse_turbo_button_parameters(s: &str) -> Result<(String, u32, bool)> {
    let mut color = String::new();
    let mut speed = 0u32;
    let mut turbo = false;

    for opt in parse_key_value_options("turbo-button", s, ',') {
        match opt.key() {
            "color" => color = opt.value()?.to_string(),
            "speed" => speed = opt.parse_numeric::<u32>()?,
            "turbo" => turbo = opt.parse_or::<bool>(true)?,
            _ => return Err(opt.invalid_key_err()),
        }
    }

    Ok((color, speed, turbo))
}

assert_eq!(parse_turbo_button_parameters("color=red,speed=0xff,turbo").unwrap(),
           ("red".to_string(), 0xff, true))

TODO: upgrade delimiter to generic Pattern support once that has been stabilized at https://github.com/rust-lang/rust/issues/27721.