tags:

views:

631

answers:

1

For instance, using the following syntax:

 -I [file] -A 1 2 3

Question:

How to check if a file was specified and additionally if three (integer) values were specified.

I understand the following:

po::options_descriptions desc("Allowed options");
desc.add_options()

How to then use the specified arguments, for instance:

    if (argv[3] == 1) {
        ...
    }

Regards

+3  A: 

You use the variables_map to check whether options were specified. If you added an option called "file" and your variables_map was called vm:

if(vm.count("myoption")) { ... } // Returns 0 if myoption not specified. 1 or more if it was.

Once you've used add_options to add some options, you can access them like so, assuming that you've setup a variables_map named vm:

vm["myoption"].as<int>() // Will return an int, assuming your option is an int
vm["myoption"].as<std::string>() // Will return an std::string, assuming your option is an int

In your case, you want to convert one of the specified options to a sequence of integers. You can do that like so:

vm["myoption"].as< std::vector<int> >()

Which will return a vector containing the 3 integers, which you can index and use just like any normal vector. To see if there are specifically 3, just use the size() vector member function.

The boost tutorial on this is located here.

Joseph Garvin