mirror of
https://github.com/CLIUtils/CLI11.git
synced 2025-05-01 13:13:53 +00:00
fix: trigger on parse positional (#713)
* 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>
This commit is contained in:
parent
10f3ab9025
commit
95e7f81d1d
@ -2559,8 +2559,22 @@ class App {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
opt->add_result(positional);
|
|
||||||
parse_order_.push_back(opt.get());
|
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();
|
args.pop_back();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -2579,7 +2593,18 @@ class App {
|
|||||||
continue;
|
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);
|
opt->add_result(positional);
|
||||||
|
if(opt->get_trigger_on_parse()) {
|
||||||
|
opt->run_callback();
|
||||||
|
}
|
||||||
parse_order_.push_back(opt.get());
|
parse_order_.push_back(opt.get());
|
||||||
args.pop_back();
|
args.pop_back();
|
||||||
return true;
|
return true;
|
||||||
|
@ -519,6 +519,26 @@ TEST_CASE_METHOD(TApp, "CustomDoubleOptionAlt", "[optiontype]") {
|
|||||||
CHECK(1.5 == Approx(custom_opt.second));
|
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
|
// now with independent type sizes and expected this is possible
|
||||||
TEST_CASE_METHOD(TApp, "vectorPair", "[optiontype]") {
|
TEST_CASE_METHOD(TApp, "vectorPair", "[optiontype]") {
|
||||||
|
|
||||||
@ -965,6 +985,23 @@ TEST_CASE_METHOD(TApp, "OnParseCall", "[optiontype]") {
|
|||||||
CHECK(3 == cnt);
|
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]") {
|
TEST_CASE_METHOD(TApp, "OnParseCallVector", "[optiontype]") {
|
||||||
|
|
||||||
std::vector<std::string> vec;
|
std::vector<std::string> vec;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user