CLIUtils / CLI11

CLI11 is a command line parser for C++11 and beyond that provides a rich feature set with a simple and intuitive interface.
https://cliutils.github.io/CLI11/book/
Other
3.26k stars 345 forks source link

Required options are not read from environment variables when added via group #1013

Open sjulti opened 6 months ago

sjulti commented 6 months ago

When adding an option to an option_group and marking it as required it is not read from environment (see code below).

Version: CLI 2.4.1

#include <CLI/CLI.hpp>

int main(int argc, char** argv)
{
    try
    {
        CLI::App app("test");
        std::string f;

#ifdef FAILS
        auto g = app.add_option_group("g");

        g->add_option("-f", f, "f")->envname("F")->required();
#else
        app.add_option("-f", f, "f")->envname("F")->required();
#endif
        app.parse(argc, argv);

        std::cout << "f=" << f << "\n";
    }
    catch (const std::exception& e)
    {
        std::cerr << "error: " << e.what() << "\n";
    }
}

Passing via args works fine though.

phlptp commented 5 months ago

I will look into it further at some point soon. Thanks

phlptp commented 1 month ago

I added the following test and it passes

TEST_CASE_METHOD(TApp,"groupEnvRequired", "[app]") {
    std::string str;
    auto group1 = app.add_option_group("group1");
    put_env("CLI11_TEST_GROUP_REQUIRED", "string_abc");
    group1->add_option("-f", str, "f")->envname("CLI11_TEST_GROUP_REQUIRED")->required();

    run();
    CHECK(str=="string_abc");
    unset_env("CLI11_TEST_GROUP_REQUIRED");
}

So I am unable to replicate the issue. it was possibly fixed in a previous update. Can anyone verify this is still an issue or identify something to change in the test case.