Boost logo

Boost Users :

Subject: Re: [Boost-users] [boost program options] missing value validation
From: Vladimir Prus (vladimir_at_[hidden])
Date: 2009-01-22 13:55:16


Andy F wrote:

> 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";
> }

What is 'unknown exception'? You should have a try/catch block
around use of po::store, and the what() method of the caught
exception should contain something useful.

- Volodya


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