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

Unexpected behavior in lyra::group flags and and lyra::opt flags following the group. #78

Open bimalgaudel opened 1 year ago

bimalgaudel commented 1 year ago
#include <iostream>
#include <lyra/lyra.hpp>
#include <fmt/format.h>

int main(int argc, char *argv[]) {
    using std::cout;
    using std::cerr;
    using std::endl;

    bool show_help{};
    bool flag_this{};
    bool flag_that{};
    bool flag_extra{};

    lyra::cli cli;
    cli.add_argument(lyra::help(show_help)("Show help"));

    cli.add_argument(
            lyra::group()
            | lyra::opt(flag_this)["--flag-this"]
            | lyra::opt(flag_that)["--flag-that"]
    );

    cli.add_argument(lyra::opt(flag_extra)["--flag-extra"]);

    auto parse_result = cli.parse({argc, argv});
    if (!parse_result) {
        cerr << parse_result.message() << endl;
        return 1;
    } else if (show_help) {
        cout << cli << endl;
    } else {
        fmt::print("this |{:d}| that |{:d}| extra |{:d}|", flag_this, flag_that, flag_extra);
    }

    return 0;
}

Results: Run: <exe> --flag-this --flag-extra Output: this |1| that |0| extra |1| Run: <exe> --flag-extra --flag-this Output: Unrecognized token: --flag-this

Looks like a bug. The order should not matter.

retifrav commented 7 months ago

I got the same(?) issue. I have the following configuration:

| lyra::arg(configFile, "/path/to/config.json")
    ("Path to the config")
| lyra::opt(outputPath, "/path/to/folder/")
    ["--output"]
    ("Path to the output folder")

If I run my tool this way:

$ my-tool --output /tmp/some

then I get the problem decribed in the first post:

Unrecognized token: /tmp/some

And if I run it like this:

$ my-tool --output=/tmp/some

then it considers the entire --output=/tmp/some to be the value for configFile argument.

If I put the argument after the option:

| lyra::opt(outputPath, "/path/to/folder/")
    ["--output"]
    ("Path to the output folder")
| lyra::arg(configFile, "/path/to/config.json")
    ("Path to the config")

then everything works fine.

So yes, this is either a bug or a missing documentation saying that arguments should be added after options.