Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does boost::program_options support requiring one of a series of alternatives?

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?

like image 599
WilliamKF Avatar asked Mar 19 '13 14:03

WilliamKF


1 Answers

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.

like image 59
ypnos Avatar answered Nov 08 '22 05:11

ypnos