Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
bimalgaudel opened this issue Aug 27, 2023 · 1 comment

Comments

@bimalgaudel
Copy link

#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
Copy link

retifrav commented Feb 13, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants