From 9e07df6c44b20bd6debaa234857879b0dc690364 Mon Sep 17 00:00:00 2001 From: Henry Fredrick Schreiner Date: Tue, 20 Mar 2018 09:32:21 +0100 Subject: [PATCH 1/4] Adding size instead of expected + unchangable --- include/CLI/App.hpp | 22 ++++++------- include/CLI/Error.hpp | 3 ++ include/CLI/Option.hpp | 71 ++++++++++++++++++++++++++++++------------ tests/CreationTest.cpp | 2 +- 4 files changed, 66 insertions(+), 32 deletions(-) diff --git a/include/CLI/App.hpp b/include/CLI/App.hpp index 237aeedcd..0c8907281 100644 --- a/include/CLI/App.hpp +++ b/include/CLI/App.hpp @@ -835,7 +835,7 @@ class App { std::string value; // Non-flags - if(opt->get_expected() != 0) { + if(opt->get_type_size() != 0) { // If the option was found on command line if(opt->count() > 0) @@ -1069,7 +1069,7 @@ class App { /// Currently checks to see if multiple positionals exist with -1 args void _validate() const { auto count = std::count_if(std::begin(options_), std::end(options_), [](const Option_p &opt) { - return opt->get_expected() == -1 && opt->get_positional(); + return opt->get_items_expected() < 0 && opt->get_positional(); }); if(count > 1) throw InvalidError(name_); @@ -1189,8 +1189,8 @@ class App { // Required or partially filled 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(-opt->get_expected())) - throw ArgumentMismatch::AtLeast(opt->single_name(), -opt->get_expected()); + if(opt->get_items_expected() < 0 && opt->count() < static_cast(-opt->get_items_expected())) + throw ArgumentMismatch::AtLeast(opt->single_name(), -opt->get_items_expected()); // Required but empty if(opt->get_required() && opt->count() == 0) @@ -1260,7 +1260,7 @@ class App { if(op->results_.empty()) { // Flag parsing - if(op->get_expected() == 0) { + if(op->get_type_size() == 0) { if(current.inputs.size() == 1) { std::string val = current.inputs.at(0); val = detail::to_lower(val); @@ -1320,9 +1320,9 @@ class App { size_t _count_remaining_positionals(bool required = false) const { size_t retval = 0; for(const Option_p &opt : options_) - if(opt->get_positional() && (!required || opt->get_required()) && opt->get_expected() > 0 && - static_cast(opt->count()) < opt->get_expected()) - retval = static_cast(opt->get_expected()) - opt->count(); + if(opt->get_positional() && (!required || opt->get_required()) && opt->get_items_expected() > 0 && + static_cast(opt->count()) < opt->get_items_expected()) + retval = static_cast(opt->get_items_expected()) - opt->count(); return retval; } @@ -1334,7 +1334,7 @@ class App { for(const Option_p &opt : options_) { // Eat options, one by one, until done if(opt->get_positional() && - (static_cast(opt->count()) < opt->get_expected() || opt->get_expected() < 0)) { + (static_cast(opt->count()) < opt->get_items_expected() || opt->get_items_expected() < 0)) { opt->add_result(positional); parse_order_.push_back(opt.get()); @@ -1421,7 +1421,7 @@ class App { // Get a reference to the pointer to make syntax bearable Option_p &op = *op_ptr; - int num = op->get_expected(); + int num = op->get_items_expected(); // Make sure we always eat the minimum for unlimited vectors int collected = 0; @@ -1459,7 +1459,7 @@ class App { // If there are any unlimited positionals, those also take priority if(std::any_of(std::begin(options_), std::end(options_), [](const Option_p &opt) { - return opt->get_positional() && opt->get_expected() < 0; + return opt->get_positional() && opt->get_items_expected() < 0; })) break; } diff --git a/include/CLI/Error.hpp b/include/CLI/Error.hpp index bd47806b0..0a6a73d59 100644 --- a/include/CLI/Error.hpp +++ b/include/CLI/Error.hpp @@ -91,6 +91,9 @@ class IncorrectConstruction : public ConstructionError { static IncorrectConstruction Set0Opt(std::string name) { return IncorrectConstruction(name + ": Cannot set 0 expected, use a flag instead"); } + static IncorrectConstruction SetFlag(std::string name) { + return IncorrectConstruction(name + ": Cannot set an expected number for flags"); + } static IncorrectConstruction ChangeNotVector(std::string name) { return IncorrectConstruction(name + ": You can only change the expected arguments for vectors"); } diff --git a/include/CLI/Option.hpp b/include/CLI/Option.hpp index 0cb1033d4..62d7e58a1 100644 --- a/include/CLI/Option.hpp +++ b/include/CLI/Option.hpp @@ -180,11 +180,13 @@ class Option : public OptionBase