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

trigger on parse positional #713

Merged
merged 2 commits into from
Mar 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion include/CLI/App.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2558,8 +2558,22 @@ class App {
continue;
}
}
opt->add_result(positional);

parse_order_.push_back(opt.get());
/// if we require a separator add it here
if(opt->get_inject_separator()) {
if(!opt->results().empty() && !opt->results().back().empty()) {
opt->add_result(std::string{});
}
}
if(opt->get_trigger_on_parse() &&
opt->current_option_state_ == Option::option_state::callback_run) {
opt->clear();
}
opt->add_result(positional);
if(opt->get_trigger_on_parse()) {
opt->run_callback();
}
args.pop_back();
return true;
}
Expand All @@ -2578,7 +2592,18 @@ class App {
continue;
}
}
if(opt->get_inject_separator()) {
if(!opt->results().empty() && !opt->results().back().empty()) {
opt->add_result(std::string{});
}
}
if(opt->get_trigger_on_parse() && opt->current_option_state_ == Option::option_state::callback_run) {
opt->clear();
}
opt->add_result(positional);
if(opt->get_trigger_on_parse()) {
opt->run_callback();
}
parse_order_.push_back(opt.get());
args.pop_back();
return true;
Expand Down
37 changes: 37 additions & 0 deletions tests/OptionTypeTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,26 @@ TEST_CASE_METHOD(TApp, "CustomDoubleOptionAlt", "[optiontype]") {
CHECK(1.5 == Approx(custom_opt.second));
}

// now with tuple support this is possible
TEST_CASE_METHOD(TApp, "floatPair", "[optiontype]") {

std::pair<float, float> custom_opt;

auto *opt = app.add_option("--fp", custom_opt)->delimiter(',');
opt->default_str("3.4,2.7");

args = {"--fp", "12", "1.5"};

run();
CHECK(12.0f == Approx(custom_opt.first));
CHECK(1.5f == Approx(custom_opt.second));
args = {};
opt->force_callback();
run();
CHECK(3.4f == Approx(custom_opt.first));
CHECK(2.7f == Approx(custom_opt.second));
}

// now with independent type sizes and expected this is possible
TEST_CASE_METHOD(TApp, "vectorPair", "[optiontype]") {

Expand Down Expand Up @@ -965,6 +985,23 @@ TEST_CASE_METHOD(TApp, "OnParseCall", "[optiontype]") {
CHECK(3 == cnt);
}

TEST_CASE_METHOD(TApp, "OnParseCallPositional", "[optiontype]") {

int cnt{0};

auto *opt = app.add_option("pos",
[&cnt](const CLI::results_t &) {
++cnt;
return true;
})
->trigger_on_parse()
->allow_extra_args();
args = {"1", "2", "3"};
CHECK(opt->get_trigger_on_parse());
run();
CHECK(3 == cnt);
}

TEST_CASE_METHOD(TApp, "OnParseCallVector", "[optiontype]") {

std::vector<std::string> vec;
Expand Down