Skip to content

Commit

Permalink
Fix Issue #1090, (#1108)
Browse files Browse the repository at this point in the history
max, min on positional was not respected, specifically max positionals
only filled up to min, and skipped over optional options.

Fixes #1090

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
phlptp and pre-commit-ci[bot] authored Dec 30, 2024
1 parent ecdcf63 commit d2b331f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
2 changes: 1 addition & 1 deletion include/CLI/impl/App_inl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1731,7 +1731,7 @@ CLI11_INLINE bool App::_parse_positional(std::vector<std::string> &args, bool ha
for(const Option_p &opt : options_) {
// Eat options, one by one, until done
if(opt->get_positional() &&
(static_cast<int>(opt->count()) < opt->get_items_expected_min() || opt->get_allow_extra_args())) {
(static_cast<int>(opt->count()) < opt->get_items_expected_max() || opt->get_allow_extra_args())) {
if(validate_positionals_) {
std::string pos = positional;
pos = opt->_validate(pos, 0);
Expand Down
50 changes: 50 additions & 0 deletions tests/AppTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1236,6 +1236,56 @@ TEST_CASE_METHOD(TApp, "RequiredOptsDoubleNeg", "[app]") {
CHECK(std::vector<std::string>({"one", "two"}) == strs);
}

TEST_CASE_METHOD(TApp, "ExpectedRangeParam", "[app]") {

app.add_option("-s")->required()->expected(2, 4);

args = {"-s", "one"};

CHECK_THROWS_AS(run(), CLI::ArgumentMismatch);

args = {"-s", "one", "two"};

CHECK_NOTHROW(run());

args = {"-s", "one", "two", "three"};

CHECK_NOTHROW(run());

args = {"-s", "one", "two", "three", "four"};

CHECK_NOTHROW(run());

args = {"-s", "one", "two", "three", "four", "five"};

CHECK_THROWS_AS(run(), CLI::ExtrasError);
}

TEST_CASE_METHOD(TApp, "ExpectedRangePositional", "[app]") {

app.add_option("arg")->required()->expected(2, 4);

args = {"one"};

CHECK_THROWS_AS(run(), CLI::ArgumentMismatch);

args = {"one", "two"};

CHECK_NOTHROW(run());

args = {"one", "two", "three"};

CHECK_NOTHROW(run());

args = {"one", "two", "three", "four"};

CHECK_NOTHROW(run());

args = {"one", "two", "three", "four", "five"};

CHECK_THROWS_AS(run(), CLI::ExtrasError);
}

// This makes sure unlimited option priority is
// correct for space vs. no space #90
TEST_CASE_METHOD(TApp, "PositionalNoSpace", "[app]") {
Expand Down

0 comments on commit d2b331f

Please sign in to comment.