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

lyra::group in "|" operator chain #65

Closed elegracer closed 1 year ago

elegracer commented 2 years ago

For lyra::group, I'd like to use the "|" operator chain way, but no example in the docs, and it seems that the callback passed into lyra::group isn't called.

#include "lyra/lyra.hpp"

#include "spdlog/fmt/fmt.h"
#include "spdlog/fmt/ostr.h"

int main(int argc, const char *argv[]) {

    bool show_help = false;
    auto name = std::string {};
    bool use_grade_id = false;
    int grade = 0;
    int id = 0;

    auto cli = lyra::help(show_help) | lyra::opt(name, "name")["-n"]["--name"].required()
               | (lyra::group([&](const lyra::group &) {
                      use_grade_id = true;
                      fmt::print("{}\n", use_grade_id);
                  })
                  | lyra::opt(id, "id")["--id"].required()
                  | lyra::opt(grade, "grade")["--grade"].required());

    auto cli_result = cli.parse({argc, argv});
    if (!cli_result) {
        fmt::print(stderr, "Error in command line: {}\n{}", cli_result.message(), cli);
        return EXIT_FAILURE;
    }

    if (show_help) {
        fmt::print("{}\n", cli);
        return EXIT_SUCCESS;
    }

    fmt::print("name: {}\n", name);
    fmt::print("use_grade_id: {}\n", use_grade_id);
    fmt::print("grade: {}\n", grade);
    fmt::print("id: {}\n", id);

    return EXIT_SUCCESS;
}

Am I using it wrong? or is it a bug?

Thanks!

vadz commented 2 years ago

This example seems to create an empty group, not containing anything, so it doesn't really work even if it compiles. But I don't see how can you use lyra::group() with the operator syntax neither, which is a pity because I prefer the more compact syntax, but it appears to be incompatible with something as simple as the standard --version option, as specifying it must make all the other options optional, which can only be achieved by using a group in Lyra, AFAICS.

I'd really like to know how to implement --version handling with Lyra in a simple way.

elegracer commented 2 years ago

I just want to use lyra::group with operator syntax, but can't find an example.

vadz commented 2 years ago

Yes, me too. I don't think it's possible right now because operator| only works with cli objects that are not convertible to arguments (it uses non-public inheritance) and so can't be consumed by a group. But I could be wrong...

grafikrobot commented 1 year ago

@elegracer like @vadz said you are creating an empty group. I think you meant to make --id and --grade part of the group. I created a test case for your example (and others) here https://github.com/bfgroup/Lyra/blob/develop/tests/issue_65_run_test.cpp#L128 that shows things working.

@vadz there are a couple of ways to implement a --version type argument. The most obvious one is to make it work like the help which uses a short circuit to ignore anything else https://github.com/bfgroup/Lyra/blob/develop/include/lyra/help.hpp#L32. Also I made a related fix on another issue which I think also resolved what you experienced with the op| and group. I added test cases to check for that to the test for this issue https://github.com/bfgroup/Lyra/blob/develop/tests/issue_65_run_test.cpp#L78.