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

set_config() on a subcommand CLI::App* #1056

Open
user-45-20 opened this issue Jul 10, 2024 · 2 comments
Open

set_config() on a subcommand CLI::App* #1056

user-45-20 opened this issue Jul 10, 2024 · 2 comments

Comments

@user-45-20
Copy link

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?

@phlptp
Copy link
Collaborator

phlptp commented Jul 10, 2024

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

phlptp commented Jul 10, 2024

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.

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