CLI11 icon indicating copy to clipboard operation
CLI11 copied to clipboard

set_config() on a subcommand CLI::App*

Open user-45-20 opened this issue 1 year ago • 2 comments

I'm trying to enable the use of configuration files at the subcommand level rather than using the global CLI::App instance. It doesn't seem to work as expected regardless of what I try:

#include <CLI/CLI.hpp>
#include <iostream>

int main(int argc, char **argv) {
    CLI::App app{"Main application"};

    auto sub = app.add_subcommand("sub");

    int sub_option;
    sub->add_option("--option", sub_option, "")->required();
    sub->set_config("--config");

    CLI11_PARSE(app, argc, argv);

    if(sub->parsed()) {
        std::cout << "Subcommand option: " << sub_option << std::endl;
    }

    return 0;
}
$ ./program sub --config config.ini
--option is required
Run with --help for more information.

I've tried two different config.ini files and I get the same behavior:

[sub]
option=42
option=42

If I instead do app.set_config() and then use config file (1) above, it works fine - but is there a way to define config files at the subcommand level instead?

user-45-20 avatar Jul 10 '24 16:07 user-45-20

CLI is not currently setup to process config files for individual subcommands. It would be possible using a callback and execute sub->_process_config_file(..) with the config file from the option. This is what has been done the few times I have seen a need for it. But usually we let the top level handle the config files.

phlptp avatar Jul 10 '24 17:07 phlptp

It would also be possible to create your subcommand as an option group, then add an alias so it will act as a subcommand but still read as part of the main from the perspective of a config file.

phlptp avatar Jul 10 '24 17:07 phlptp