Skip to content

Commit d2b331f

Browse files
Fix Issue #1090, (#1108)
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>
1 parent ecdcf63 commit d2b331f

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

include/CLI/impl/App_inl.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1731,7 +1731,7 @@ CLI11_INLINE bool App::_parse_positional(std::vector<std::string> &args, bool ha
17311731
for(const Option_p &opt : options_) {
17321732
// Eat options, one by one, until done
17331733
if(opt->get_positional() &&
1734-
(static_cast<int>(opt->count()) < opt->get_items_expected_min() || opt->get_allow_extra_args())) {
1734+
(static_cast<int>(opt->count()) < opt->get_items_expected_max() || opt->get_allow_extra_args())) {
17351735
if(validate_positionals_) {
17361736
std::string pos = positional;
17371737
pos = opt->_validate(pos, 0);

tests/AppTest.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,6 +1236,56 @@ TEST_CASE_METHOD(TApp, "RequiredOptsDoubleNeg", "[app]") {
12361236
CHECK(std::vector<std::string>({"one", "two"}) == strs);
12371237
}
12381238

1239+
TEST_CASE_METHOD(TApp, "ExpectedRangeParam", "[app]") {
1240+
1241+
app.add_option("-s")->required()->expected(2, 4);
1242+
1243+
args = {"-s", "one"};
1244+
1245+
CHECK_THROWS_AS(run(), CLI::ArgumentMismatch);
1246+
1247+
args = {"-s", "one", "two"};
1248+
1249+
CHECK_NOTHROW(run());
1250+
1251+
args = {"-s", "one", "two", "three"};
1252+
1253+
CHECK_NOTHROW(run());
1254+
1255+
args = {"-s", "one", "two", "three", "four"};
1256+
1257+
CHECK_NOTHROW(run());
1258+
1259+
args = {"-s", "one", "two", "three", "four", "five"};
1260+
1261+
CHECK_THROWS_AS(run(), CLI::ExtrasError);
1262+
}
1263+
1264+
TEST_CASE_METHOD(TApp, "ExpectedRangePositional", "[app]") {
1265+
1266+
app.add_option("arg")->required()->expected(2, 4);
1267+
1268+
args = {"one"};
1269+
1270+
CHECK_THROWS_AS(run(), CLI::ArgumentMismatch);
1271+
1272+
args = {"one", "two"};
1273+
1274+
CHECK_NOTHROW(run());
1275+
1276+
args = {"one", "two", "three"};
1277+
1278+
CHECK_NOTHROW(run());
1279+
1280+
args = {"one", "two", "three", "four"};
1281+
1282+
CHECK_NOTHROW(run());
1283+
1284+
args = {"one", "two", "three", "four", "five"};
1285+
1286+
CHECK_THROWS_AS(run(), CLI::ExtrasError);
1287+
}
1288+
12391289
// This makes sure unlimited option priority is
12401290
// correct for space vs. no space #90
12411291
TEST_CASE_METHOD(TApp, "PositionalNoSpace", "[app]") {

0 commit comments

Comments
 (0)