From 133c7d5075b6321915692c1de4b03c4254e73476 Mon Sep 17 00:00:00 2001 From: Henry Fredrick Schreiner Date: Sun, 26 Nov 2017 12:03:08 -0500 Subject: [PATCH] Allow suggestions for number of args --- include/CLI/App.hpp | 21 +++++++++++++-------- include/CLI/Error.hpp | 4 ++-- tests/AppTest.cpp | 19 +++++++++++++++++-- 3 files changed, 32 insertions(+), 12 deletions(-) diff --git a/include/CLI/App.hpp b/include/CLI/App.hpp index 5395228..efe9ef0 100644 --- a/include/CLI/App.hpp +++ b/include/CLI/App.hpp @@ -1371,10 +1371,10 @@ class App { } // Unlimited vector parser - if(num == -1) { - bool already_ate_one = false; // Make sure we always eat one + if(num < 0) { + int collected = 0; // Make sure we always eat the minimum while(!args.empty() && _recognize(args.back()) == detail::Classifer::NONE) { - if(already_ate_one) { + if(collected >= -num) { // We could break here for allow extras, but we don't // If any positionals remain, don't keep eating @@ -1390,8 +1390,11 @@ class App { op->add_result(args.back()); parse_order_.push_back(op.get()); args.pop_back(); - already_ate_one = true; + collected++; } + //if(collected < -num) + // throw ArgumentMismatch(op->single_name() + ": At least " + std::to_string(-num) + " required"); + } else { while(num > 0 && !args.empty()) { num--; @@ -1453,11 +1456,11 @@ class App { } else if(num == 0) { op->add_result(""); parse_order_.push_back(op.get()); - } else if(num == -1) { + } else if(num < 0) { // Unlimited vector parser - bool already_ate_one = false; // Make sure we always eat one + int collected = 0; // Make sure we always eat the minimum while(!args.empty() && _recognize(args.back()) == detail::Classifer::NONE) { - if(already_ate_one) { + if(collected >= -num) { // We could break here for allow extras, but we don't // If any positionals remain, don't keep eating @@ -1473,8 +1476,10 @@ class App { op->add_result(args.back()); parse_order_.push_back(op.get()); args.pop_back(); - already_ate_one = true; + collected++; } + //if(collected < -num) + // throw ArgumentMismatch(op->single_name() + ": At least " + std::to_string(-num) + " required"); } else { while(num > 0 && !args.empty()) { num--; diff --git a/include/CLI/Error.hpp b/include/CLI/Error.hpp index 99eb014..fc3624f 100644 --- a/include/CLI/Error.hpp +++ b/include/CLI/Error.hpp @@ -183,8 +183,8 @@ class ExcludesError : public ParseError { class ExtrasError : public ParseError { CLI11_ERROR_DEF(ParseError, ExtrasError) ExtrasError(std::vector args) - : ExtrasError((args.size() > 1 ? "The following argument was not expected: " - : "The following arguments were not expected: ") + + : ExtrasError((args.size() > 1 ? "The following arguments were not expected: " + : "The following argument was not expected: ") + detail::rjoin(args, " "), ExitCodes::ExtrasError) {} }; diff --git a/tests/AppTest.cpp b/tests/AppTest.cpp index 39cbaca..71e7e8d 100644 --- a/tests/AppTest.cpp +++ b/tests/AppTest.cpp @@ -235,12 +235,27 @@ TEST_F(TApp, MissingValueNonRequiredOpt) { app.add_option("-c,--count", count); args = {"-c"}; - EXPECT_ANY_THROW(run()); + EXPECT_THROW(run(), CLI::ArgumentMismatch); app.reset(); args = {"--count"}; - EXPECT_ANY_THROW(run()); + EXPECT_THROW(run(), CLI::ArgumentMismatch); +} + +TEST_F(TApp, MissingValueMoreThan) { + std::vector vals1; + std::vector vals2; + app.add_option("-v", vals1)->expected(-2); + app.add_option("--vals", vals2)->expected(-2); + + args = {"-v", "2"}; + EXPECT_THROW(run(), CLI::ArgumentMismatch); + + app.reset(); + + args = {"--vals","4"}; + EXPECT_THROW(run(), CLI::ArgumentMismatch); } TEST_F(TApp, NotRequiredOptsSingle) { -- libgit2 0.21.4