bfgroup / Lyra

A simple to use, composable, command line parser for C++ 11 and beyond
https://bfgroup.github.io/Lyra/
Boost Software License 1.0
471 stars 56 forks source link

Binding arg to a vector only allows for a single value #11

Closed mattgodbolt closed 4 years ago

mattgodbolt commented 4 years ago

The following code:

int Main(int argc, const char* argv[]) {
  bool help = false;
  std::string out_path{};
  std::vector<std::string> input_paths;
  auto cli = lyra::cli_parser() | lyra::help(help) | lyra::arg(input_paths, "input...").required()("Input files, in gzip compressed CSV format");

  auto result = cli.parse({argc, argv});
  if (!result) {
    std::cerr << "Error in command line: " << result.errorMessage() << "\n";
    std::cerr << cli;
    return 1;
  }
...
}

when passed ./test a b c gives the error:

Error in command line: Unrecognized token: b

How might I set it to take any number of arguments? I saw the cardinality() call, but there's essentially no upper bound on the number of arguments. I could specify some arbitrary high limit, but I suspect I'm doing it wrong :)

mattgodbolt commented 4 years ago

Update: removing the required and adding cardinality(1, std::numeric_limits<size_t>()) seems to do what I want. But it's not all that obvious :)

grafikrobot commented 4 years ago

Would adding something like a at_least(1) method that does the right thing be a good enough solution?