I want to only allow one of a number of options, i.e. --frequency=<x>
or --wavelength=<x>
or --energy=<x>
, but not more than one of them. Is there a way to do this with boost program options?
Handling mutually exclusive options is shown in one of the tutorials. The basic idea is to define a function to check if a pair of options are both present and handle that condition however you choose. It's a manual process, and there's nothing built into the library to perform the checks for you.
void conflicting_options(const bpo::variables_map& vm,
const char* opt1, const char* opt2)
{
if (vm.count(opt1) && !vm[opt1].defaulted()
&& vm.count(opt2) && !vm[opt2].defaulted())
throw std::logic_error(std::string("Conflicting options '")
+ opt1 + "' and '" + opt2 + "'.");
}
And then after you parse the command line
bpo::store(bpo::parse_command_line(argc, argv, desc), vm);
conflicting_options(vm, "frequency", "wavelength");
conflicting_options(vm, "frequency", "energy");
conflicting_options(vm, "wavelength", "energy");