Skip to content

Commit

Permalink
add separate condition for index into vectors
Browse files Browse the repository at this point in the history
  • Loading branch information
phlptp committed Aug 18, 2019
1 parent 049036e commit f44a065
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
8 changes: 6 additions & 2 deletions include/CLI/Option.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -693,8 +693,12 @@ class Option : public OptionBase<Option> {
// Run the validators (can change the string)
if(!validators_.empty()) {
int index = 0;
if(type_size_ > 0 && multi_option_policy_ == CLI::MultiOptionPolicy::TakeLast) {
index = type_size_ - static_cast<int>(results_.size());
// this is not available until multi_option_policy with type_size_>0 is enabled and functional
// if(type_size_ > 0 && multi_option_policy_ == CLI::MultiOptionPolicy::TakeLast) {
// index = type_size_ - static_cast<int>(results_.size());
//}
if(type_size_ < 0 && multi_option_policy_ == CLI::MultiOptionPolicy::TakeLast) { // for vector operations
index = expected_ - static_cast<int>(results_.size());
}
for(std::string &result : results_) {
auto err_msg = _validate(result, index);
Expand Down
13 changes: 13 additions & 0 deletions tests/AppTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,19 @@ TEST_F(TApp, TakeLastOptMulti) {
EXPECT_EQ(vals, std::vector<int>({2, 3}));
}

TEST_F(TApp, TakeLastOptMultiCheck) {
std::vector<int> vals;
auto opt = app.add_option("--long", vals)->expected(2)->take_last();

opt->check(CLI::Validator(CLI::PositiveNumber).application_index(0));
opt->check((!CLI::PositiveNumber).application_index(1));
args = {"--long", "-1", "2", "-3"};

EXPECT_NO_THROW(run());

EXPECT_EQ(vals, std::vector<int>({2, -3}));
}

TEST_F(TApp, TakeFirstOptMulti) {
std::vector<int> vals;
app.add_option("--long", vals)->expected(2)->take_first();
Expand Down

0 comments on commit f44a065

Please sign in to comment.