I'm trying to support non-conventional syntax on the form
"option=value", where boolean options are given as "option=on" or
"option=off".
For this I've implemented a custom parser that splits a string on the
'=' character and returns a std::pair
containing the option and it's value. This works fine for most things
except I can't figure out how to handle boolean options.
I've tried the following which just makes the option "disappear" if
it's set to "off" and returns a pair with the option and an empty
string as value if it's set to "on". It works for "off", but for "on"
I get an exception stating "extra parameter in 'option'". Is there any
way to get around this? Here's my custom parser:
//---------------- START: code
std::pair
custom_parser(std::string const & s)
{
std::pair option;
if (s.find("=") != std::string::npos)
{
std::vectorstd::string slist;
boost::split(slist, s, boost::is_any_of("="));
if (slist[1] == "on")
{
option.first = slist[0];
}
else if (slist[1] == "off")
{
// Return an empty option if the feature is turned off
}
else
{
option.first = slist[0];
option.second = slist[1];
}
}
return option;
}
//-------------- END: code
Any help appreciated!
Regards,
--
Tarjei