1
0
mirror of https://github.com/CLIUtils/CLI11.git synced 2025-04-30 04:33:53 +00:00

Fixing -N values min count throw

This commit is contained in:
Henry Fredrick Schreiner 2017-11-26 14:59:39 -05:00 committed by Henry Schreiner
parent 6638549adb
commit 958b0e511e
2 changed files with 28 additions and 3 deletions

View File

@ -1146,6 +1146,10 @@ class App {
for(const Option_p &opt : options_) { for(const Option_p &opt : options_) {
// Required or partially filled // Required or partially filled
if(opt->get_required() || opt->count() != 0) { if(opt->get_required() || opt->count() != 0) {
// Make sure enough -N arguments parsed (+N is already handled in parsing function)
if(opt->get_expected() < 0 && opt->count() < static_cast<size_t>(-opt->get_expected()))
throw ArgumentMismatch(opt->single_name() + ": At least " + std::to_string(-opt->get_expected()) +
" required");
// Required but empty // Required but empty
if(opt->get_required() && opt->count() == 0) if(opt->get_required() && opt->count() == 0)
@ -1408,8 +1412,6 @@ class App {
args.pop_back(); args.pop_back();
collected++; collected++;
} }
if(op->results_.size() < static_cast<size_t>(-num))
throw ArgumentMismatch(op->single_name() + ": At least " + std::to_string(-num) + " required");
} else { } else {
while(num > 0 && !args.empty()) { while(num > 0 && !args.empty()) {

View File

@ -324,10 +324,33 @@ TEST_F(TApp, RequiredOptsDoubleShort) {
EXPECT_THROW(run(), CLI::ArgumentMismatch); EXPECT_THROW(run(), CLI::ArgumentMismatch);
app.reset();
args = {"-s", "one", "-s", "one", "-s", "one"};
EXPECT_THROW(run(), CLI::ArgumentMismatch);
}
TEST_F(TApp, RequiredOptsDoubleNeg) {
std::vector<std::string> strs;
app.add_option("-s", strs)->required()->expected(-2);
args = {"-s", "one"};
EXPECT_THROW(run(), CLI::ArgumentMismatch);
app.reset();
args = {"-s", "one", "two", "-s", "three"};
EXPECT_NO_THROW(run());
EXPECT_EQ(strs, std::vector<std::string>({"one", "two", "three"}));
app.reset(); app.reset();
args = {"-s", "one", "two"}; args = {"-s", "one", "two"};
run(); EXPECT_NO_THROW(run());
EXPECT_EQ(strs, std::vector<std::string>({"one", "two"})); EXPECT_EQ(strs, std::vector<std::string>({"one", "two"}));
} }