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

Infinite loop when non-existent argument is specified #73

Closed vadz closed 1 year ago

vadz commented 1 year ago

I've been testing the proposed snippet from #51 showing how to have options with multiple arguments and so compiled and ran this simple example:

#include <lyra/lyra.hpp>

int main(int argc, const char** argv)
{
  bool gotArgs = false;
  std::vector<std::string> args;

  auto cli = lyra::cli();

  cli.add_argument(lyra::group().sequential()
    | lyra::opt(gotArgs)["--args"]
    | lyra::arg(args, "args"));

  auto result = cli.parse({ argc, argv });
  if (!result) {
    std::cerr << "ERROR:" << cli << "\n";
    return EXIT_FAILURE;
  }

  std::cout << "got args = " << gotArgs << "\n";
  if(!args.empty()){
    for(auto const& a: args) {
      std::cout << "\targ: " << a << "\n";
    }
  }

  return EXIT_SUCCESS;
}

It works more or less as expected, but suffers from a fatal problem: running it with an unknown option or any arguments without using --args option simply hangs the program. I didn't have time to really debug this yet, but I see that it gets stuck in the do/while loop in lyra::arguments::parse_sequence and I don't really understand how is this supposed to work, i.e. why is p_result.value().have_tokens() always remains true.