Boost logo

Boost Users :

Subject: [Boost-users] [Boost.Program_options] extra_parser for config files
From: Marti Cuquet (cuquet_at_[hidden])
Date: 2009-07-10 06:21:38


Hi,

I want to implement the --switch / --no-switch behavior.

I defined a bool option "switch" and then I parse the command line with
an extra parser that sets "switch" to "false" if it finds a --no-switch.

myopts.add_options()
  ("switch", po::value<bool>()->implicit_value(true), "Use switch.")
;
po::store (po::command_line_parser(argc,
argv).options(all).extra_parser(parse_no_options).run(), vm);

The extra parser is:

std::pair<std::string, std::string> parse_no_options
(const std::string& s, const std::string name) {
    if (s.find("--switch") != s.npos) {
        if (s.substr (2, 3) == "no-")
            return std::make_pair (s.substr (5), std::string("false"));
        else
            return std::make_pair (s.substr (2), std::string("true"));
    }

    return std::make_pair (std::string(), std::string());
}

This works fine for the command line.

I also need to implement this for the config file, but I do not know how
to pass the extra parser to the parse_config_file. Is there a way?

Thanks,
Marti


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