Retrieves and converts an option value to type `T`. Returns `Ok(None)` if the option was not set or its value is empty. Returns `Err` if the value cannot be converted to `T`. `T` can be any type that implements `FromStr` (e.g. `u32`, `String`), or one of this crate's types such as [`Toggle`], [`IntegerList`], [`Tuple`], or [`StringList`].
(&self, option: &str)
| 273 | /// or one of this crate's types such as [`Toggle`], [`IntegerList`], |
| 274 | /// [`Tuple`], or [`StringList`]. |
| 275 | pub fn convert<T: Parseable>(&self, option: &str) -> OptionParserResult<Option<T>> { |
| 276 | match self.options.get(option).and_then(|v| v.value.as_ref()) { |
| 277 | None => Ok(None), |
| 278 | Some(v) => { |
| 279 | Ok(if v.is_empty() { |
| 280 | None |
| 281 | } else { |
| 282 | Some(Parseable::from_str(v).map_err(|_| { |
| 283 | OptionParserError::Conversion(option.to_owned(), v.to_owned()) |
| 284 | })?) |
| 285 | }) |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | /// A boolean-like value that accepts `"on"`, `"true"`, `"off"`, `"false"`, or `""`. |