From bed282b801661c2d0f25b89da63216976cfd820d Mon Sep 17 00:00:00 2001 From: Rob Bradford Date: Thu, 2 Apr 2020 16:50:37 +0100 Subject: [PATCH] vmm: config: Add "valueless" options to OptionParser Valueless options are those like "off" or "tty" as used by the console options. Signed-off-by: Rob Bradford --- vmm/src/config.rs | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/vmm/src/config.rs b/vmm/src/config.rs index 5e36f8161..ea334c907 100644 --- a/vmm/src/config.rs +++ b/vmm/src/config.rs @@ -85,6 +85,7 @@ pub struct OptionParser { struct OptionParserValue { value: Option, + requires_value: bool, } #[derive(Debug)] @@ -113,14 +114,17 @@ impl OptionParser { for option in options_list.iter() { let parts: Vec<&str> = option.split('=').collect(); - if parts.len() != 2 { - return Err(OptionParserError::InvalidSyntax((*option).to_owned())); - } - match self.options.get_mut(parts[0]) { None => return Err(OptionParserError::UnknownOption(parts[0].to_owned())), Some(value) => { - value.value = Some(parts[1].trim().to_owned()); + if value.requires_value { + if parts.len() != 2 { + return Err(OptionParserError::InvalidSyntax((*option).to_owned())); + } + value.value = Some(parts[1].trim().to_owned()); + } else { + value.value = Some(String::new()); + } } } } @@ -129,8 +133,25 @@ impl OptionParser { } pub fn add(&mut self, option: &str) -> &mut Self { - self.options - .insert(option.to_owned(), OptionParserValue { value: None }); + self.options.insert( + option.to_owned(), + OptionParserValue { + value: None, + requires_value: true, + }, + ); + + self + } + + pub fn add_valueless(&mut self, option: &str) -> &mut Self { + self.options.insert( + option.to_owned(), + OptionParserValue { + value: None, + requires_value: false, + }, + ); self }