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

immediate_mode execution order? #308

Closed
michaelquigley opened this issue Aug 13, 2019 · 3 comments
Closed

immediate_mode execution order? #308

michaelquigley opened this issue Aug 13, 2019 · 3 comments

Comments

@michaelquigley
Copy link

michaelquigley commented Aug 13, 2019

In reading the documention, it sounds like the following code example:

int main(int argc, char** argv) {
    try {
        CLI::App app("immediate_callback example");
        app.require_subcommand(1, 1);
        app.failure_message(CLI::FailureMessage::help);

        app.callback([&](){
            std::cout << "main callback" << std::endl;
        })->immediate_callback(true);

        app.add_subcommand("hello")->callback([&](){
            std::cout << "hello callback" << std::endl;
        })->immediate_callback(false);

        CLI11_PARSE(app, argc, argv);

        return 0;
    }
    catch(const std::runtime_error& re) {
        std::cerr << "exception [" << typeid(re).name() << "]: " << re.what() << std::endl;
    }
}

...should result in showing:

main callback
hello callback

...when I execute example hello, but instead, I'm getting:

hello callback
main callback

What am I missing? Or, is this a bug?

@phlptp
Copy link
Collaborator

phlptp commented Aug 13, 2019

There were some fixes related to immediate_callback and the main app in PR #292. So you need to make sure you are on the latest master vs the latest release.

On the latest master I ran a test

app.require_subcommand(1, 1);
    std::vector<int> v;
    app.callback([&v]() { v.push_back(1); })->immediate_callback(true);

    auto sub = app.add_subcommand("hello")->callback([&v]() { v.push_back(2); })->immediate_callback(false);
    args = {"hello"};
    run();
    // immediate_callback inherited
    ASSERT_EQ(v.size(), 2u);
    EXPECT_EQ(v[0], 1);
    EXPECT_EQ(v[1], 2);
    v.clear();
    sub->immediate_callback(true);
    run();
    // immediate_callback is now triggered for the main first
    ASSERT_EQ(v.size(), 2u);
    EXPECT_EQ(v[0], 2);
    EXPECT_EQ(v[1], 1);

And this has the expected behavior.
In the "hopefully not to distant future" we are reworking the callbacks a little to add a third one which will hopefully make it clearer which callbacks get executed when, at which point immediate_callback will be deprecated.

@michaelquigley
Copy link
Author

Makes perfect sense. How can I build a single CLI11.hpp from master?

@michaelquigley
Copy link
Author

Found the CLI11_SINGLE_FILE cmake option. Should be all set.

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