Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add failing test for repeating multi-argument options #140

Merged
merged 2 commits into from
Jun 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions include/CLI/Option.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -560,12 +560,18 @@ class Option : public OptionBase<Option> {
local_result = !callback_(partial_result);

} else {
// For now, vector of non size 1 types are not supported but possibility included here
if((get_items_expected() > 0 && results_.size() != static_cast<size_t>(get_items_expected())) ||
(get_items_expected() < 0 && results_.size() < static_cast<size_t>(-get_items_expected())))
throw ArgumentMismatch(get_name(), get_items_expected(), results_.size());
else
local_result = !callback_(results_);
// Exact number required
if(get_items_expected() > 0) {
if(results_.size() != static_cast<size_t>(get_items_expected()))
throw ArgumentMismatch(get_name(), get_items_expected(), results_.size());
// Variable length list
} else if(get_items_expected() < 0) {
// Require that this be a multiple of expected size and at least as many as expected
if(results_.size() < static_cast<size_t>(-get_items_expected()) ||
results_.size() % static_cast<size_t>(std::abs(get_type_size())) != 0)
throw ArgumentMismatch(get_name(), get_items_expected(), results_.size());
}
local_result = !callback_(results_);
}

if(local_result)
Expand Down
14 changes: 14 additions & 0 deletions tests/AppTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1581,3 +1581,17 @@ TEST_F(TApp, AddRemoveSetItemsNoCase) {
args = {"--type2", "TYpE2"};
EXPECT_THROW(run(), CLI::ConversionError);
}

// #128
TEST_F(TApp, RepeatingMultiArgumentOptions) {
std::vector<std::string> entries;
app.add_option("--entry", entries, "set a key and value")->type_name("KEY VALUE")->type_size(-2);

args = {"--entry", "key1", "value1", "--entry", "key2", "value2"};
EXPECT_NO_THROW(run());
EXPECT_EQ(entries, std::vector<std::string>({"key1", "value1", "key2", "value2"}));

app.reset();
args.pop_back();
ASSERT_THROW(run(), CLI::ArgumentMismatch);
}