Boost logo

Boost Users :

Subject: [Boost-users] [boost program options] missing value validation
From: Andy F (andyf_at_[hidden])
Date: 2009-01-22 11:46:34


If I have a parameter which needs a value, but the value is not specified in the
command line, I get an unknown exception in VisualStudio 2005. Meaning, if I
declare a --compression=something, but then call the program with just
--compression, the exception is thrown at the po::store() method. How do I
intercept this and validate?

I am just using the code from the example program:

        po::options_description desc("Allowed options");
        desc.add_options()
            ("help", "produce help message")
                        ("compression", po::value<std::string>(), "set compression level")
        ;

        po::variables_map vm;
        po::store(po::parse_command_line(ac, av, desc), vm);
        po::notify(vm);

        if (vm.count("help")) {
                        std::cout << desc << "\n";
            return 1;
        }

        if (vm.count("compression")) {
                        std::cout << "Compression level was set to "
                                << vm["compression"].as<std::string>() << ".\n";
        } else {
                        std::cout << "Compression level was not set.\n";
        }


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