Skip to content

Commit

Permalink
fix: trigger on parse positional (#713)
Browse files Browse the repository at this point in the history
* allow the trigger on parse modifier to work with positional arguments as well

* style: pre-commit.ci fixes

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
phlptp and pre-commit-ci[bot] authored Mar 21, 2022
1 parent 10f3ab9 commit 95e7f81
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
27 changes: 26 additions & 1 deletion include/CLI/App.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2559,8 +2559,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 @@ -2579,7 +2593,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

0 comments on commit 95e7f81

Please sign in to comment.