Boost logo

Boost Users :

From: Tarjei Knapstad (tarjei.knapstad_at_[hidden])
Date: 2007-04-26 09:47:06


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<std::string, std::string>
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<std::string, std::string>
custom_parser(std::string const & s)
{
    std::pair<std::string, std::string> option;
    if (s.find("=") != std::string::npos)
    {
        std::vector<std::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

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net