I'm using the boost::program_options to specify the arguments to my C++ application. Is there a way to specify that one argument is required from a set of alternatives?
<application> [--one int-value1 | --two string-value2 | --three]
In the above, the user must pass exactly one of the alternatives: --one
, --two
, or --three
.
I could do this manually, but hope there is a built-in mechanism instead of this:
#include <boost/program_options.hpp>
namespace po = boost::program_options;
int main(int argc, char *argv[]) {
po::options_description options;
int band;
std::string titles_path;
options.add_options()
("one", po::value<int>(&band)->default_value(1))
("two", po::value<std::string>(&titles_path))
("three");
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, options), vm);
if (1 != (vm.count("one") + vm.count("two") + vm.count("three"))) {
std::cerr << options << std::endl;
return -11;
}
return 0;
}
Is there a better way to do this with boost options?
The program_options validator does not support parameter inter-dependencies (including negative ones).
Probably what you do right now is in fact the best option.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With