Commit 958b0e511e31353adb484d19f35cd1a689399e8b
Committed by
Henry Schreiner
1 parent
6638549a
Fixing -N values min count throw
Showing
2 changed files
with
28 additions
and
3 deletions
include/CLI/App.hpp
| ... | ... | @@ -1146,6 +1146,10 @@ class App { |
| 1146 | 1146 | for(const Option_p &opt : options_) { |
| 1147 | 1147 | // Required or partially filled |
| 1148 | 1148 | if(opt->get_required() || opt->count() != 0) { |
| 1149 | + // Make sure enough -N arguments parsed (+N is already handled in parsing function) | |
| 1150 | + if(opt->get_expected() < 0 && opt->count() < static_cast<size_t>(-opt->get_expected())) | |
| 1151 | + throw ArgumentMismatch(opt->single_name() + ": At least " + std::to_string(-opt->get_expected()) + | |
| 1152 | + " required"); | |
| 1149 | 1153 | |
| 1150 | 1154 | // Required but empty |
| 1151 | 1155 | if(opt->get_required() && opt->count() == 0) |
| ... | ... | @@ -1408,8 +1412,6 @@ class App { |
| 1408 | 1412 | args.pop_back(); |
| 1409 | 1413 | collected++; |
| 1410 | 1414 | } |
| 1411 | - if(op->results_.size() < static_cast<size_t>(-num)) | |
| 1412 | - throw ArgumentMismatch(op->single_name() + ": At least " + std::to_string(-num) + " required"); | |
| 1413 | 1415 | |
| 1414 | 1416 | } else { |
| 1415 | 1417 | while(num > 0 && !args.empty()) { | ... | ... |
tests/AppTest.cpp
| ... | ... | @@ -325,9 +325,32 @@ TEST_F(TApp, RequiredOptsDoubleShort) { |
| 325 | 325 | EXPECT_THROW(run(), CLI::ArgumentMismatch); |
| 326 | 326 | |
| 327 | 327 | app.reset(); |
| 328 | + | |
| 329 | + args = {"-s", "one", "-s", "one", "-s", "one"}; | |
| 330 | + | |
| 331 | + EXPECT_THROW(run(), CLI::ArgumentMismatch); | |
| 332 | +} | |
| 333 | + | |
| 334 | +TEST_F(TApp, RequiredOptsDoubleNeg) { | |
| 335 | + std::vector<std::string> strs; | |
| 336 | + app.add_option("-s", strs)->required()->expected(-2); | |
| 337 | + | |
| 338 | + args = {"-s", "one"}; | |
| 339 | + | |
| 340 | + EXPECT_THROW(run(), CLI::ArgumentMismatch); | |
| 341 | + | |
| 342 | + app.reset(); | |
| 343 | + | |
| 344 | + args = {"-s", "one", "two", "-s", "three"}; | |
| 345 | + | |
| 346 | + EXPECT_NO_THROW(run()); | |
| 347 | + | |
| 348 | + EXPECT_EQ(strs, std::vector<std::string>({"one", "two", "three"})); | |
| 349 | + | |
| 350 | + app.reset(); | |
| 328 | 351 | args = {"-s", "one", "two"}; |
| 329 | 352 | |
| 330 | - run(); | |
| 353 | + EXPECT_NO_THROW(run()); | |
| 331 | 354 | |
| 332 | 355 | EXPECT_EQ(strs, std::vector<std::string>({"one", "two"})); |
| 333 | 356 | } | ... | ... |