7

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?

WilliamKF
  • 41,123
  • 68
  • 193
  • 295

1 Answers1

5

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.

ypnos
  • 50,202
  • 14
  • 95
  • 141
  • Is there a way to get a nicer help/usage message? Like in a usual unix app and as WilliamKF wanted it: [--one int-value1 | --two string-value2 | --three]. So it's at least visible in the help what should be passed. – Ela782 Jul 24 '14 at 23:01