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
483 stars 58 forks source link

Unexpected behaviour when using choices() and invalid values #89

Open schenker opened 10 months ago

schenker commented 10 months ago

I'm encountering unexpected results with lyra 1.6.1 and the following program:

#include <iostream>

#include <lyra/lyra.hpp>

int main(int argc, char* argv[])
{
  std::string first_arg;
  std::string second_arg;
  auto cli = lyra::cli() | lyra::arg(first_arg, "first_arg").required().choices("A", "B") |
             lyra::arg(second_arg, "second_arg").required();

  const auto result = cli.parse({argc, argv});

  if (!result) {
    std::cerr << "Error: " << result.message() << std::endl;
    return EXIT_FAILURE;
  }

  std::cout << "first_arg: " << first_arg << ", second_arg: " << second_arg;

  return EXIT_SUCCESS;
}

Calling this program with

$ ./a.out arg1 B

gives me the this output:

first_arg: B, second_arg: arg1

The two arguments are mixed up. I would expect parse() to return false.