views:

200

answers:

2

I have the following options_description:

po::options_description config("Configuration File or Command Line");
config.add_options()
  ("run-time,t", po::value<double>(&runTime_)->default_value(1440.0), "set max simulation duration")
  ("starting-iteration,i", po::value<long>(&startingIteration_)->default_value(1), "set starting simulation iteration")
  ("repetitions,r", po::value<long>(&repetitions_)->default_value(100), "set number of iterations")
  ...
;

As you can see the three shown have a long,short names employed. The long versions all work. However, none of the short ones do, and each time I try a -t 12345.0 or a -i 12345, etc., I get the following from Program_Options:

std::logic_error: in option 'starting-iteration': invalid option value

I'm using Boost 1.42 on Win32. Any thoughts on what might be going on here? Thanks!

+1  A: 

Your example looks good as far as it goes. The code below compiled with VS 2008 worked for me for an input of

boost_options.exe -i 321 -t 123.4 -r 20

and produced:

run-time=123.4
starting-iteration=321
repetitions=20

The code:

#include <iostream>
#include <exception>
#include <boost/program_options.hpp>
namespace po=boost::program_options;
int main(int argc, char* argv[])
{
    double runTime_;
    long startingIteration_;
    long repetitions_;
    try
    {
        po::options_description config("Configuration File or Command Line");
        config.add_options()
          ("run-time,t", po::value<double>(&runTime_)->default_value(1440.0), "set max simulation duration")
          ("starting-iteration,i", po::value<long>(&startingIteration_)->default_value(1), "set starting simulation iteration")
          ("repetitions,r", po::value<long>(&repetitions_)->default_value(100), "set number of iterations")
        ;
        po::variables_map vm;
        po::store(po::parse_command_line(argc, argv, config), vm);
        po::notify(vm);
        std::cout << "run-time=" << runTime_ << std::endl;
        std::cout << "starting-iteration=" << startingIteration_ << std::endl;
        std::cout << "repetitions=" << repetitions_ << std::endl;
    }
    catch(std::logic_error &err)
    {
        std::cerr << "std::logic_error:" << err.what() << std::endl;
    }
    catch(std::exception &err)
    {
        std::cerr << err.what() << std::endl;
    }
    return 0;
}
David Gladfelter
For what it's worth, this was compiled with boost 1.43. I don't expect that that would make a difference, though.
David Gladfelter
FWIW, works on 1.42 with VS 2005 as well.
tmitchell
Thanks for the working expansion - this gave me confidence in Boost.Program_Options and less confidence in what I was doing.As it turns out, my unit tests for confirming my short program options were passing, for example, `-r 20` as a single element in argv as opposed to two entries (`-r` and `20`).Sorry for the false alarm!
inajamaica
A: 

FYI you don't need to specify the template type to po::value, it will deduce it from the parameter.

Sam Miller