Skip to content

Commit

Permalink
fix and test some reported issues
Browse files Browse the repository at this point in the history
  • Loading branch information
phlptp committed Oct 25, 2021
1 parent 70f8072 commit 1b2f584
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 4 deletions.
11 changes: 7 additions & 4 deletions include/CLI/App.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2824,10 +2824,13 @@ class App {
parse_order_.push_back(op.get());
}
}

// if we only partially completed a type then add an empty string for later processing
if(min_num > 0 && op->get_type_size_max() != min_num && (collected % op->get_type_size_max()) != 0) {
op->add_result(std::string{});
// if we only partially completed a type then add an empty string if allowed for later processing
if(min_num > 0 && (collected % op->get_type_size_max()) != 0) {
if(op->get_type_size_max() != op->get_type_size_min()) {
op->add_result(std::string{});
} else {
throw ArgumentMismatch::PartialType(op->get_name(), op->get_type_size_min(), op->get_type_name());
}
}
if(op->get_trigger_on_parse()) {
op->run_callback();
Expand Down
4 changes: 4 additions & 0 deletions include/CLI/Error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,10 @@ class ArgumentMismatch : public ParseError {
static ArgumentMismatch FlagOverride(std::string name) {
return ArgumentMismatch(name + " was given a disallowed flag override");
}
static ArgumentMismatch PartialType(std::string name, int num, std::string type) {
return ArgumentMismatch(name + ": " + type + " only partially specified: " + std::to_string(num) +
" required for each element");
}
};

/// Thrown when a requires option is missing
Expand Down
13 changes: 13 additions & 0 deletions tests/ConfigFileTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2103,6 +2103,19 @@ TEST_CASE_METHOD(TApp, "TomlOutputVector", "[config]") {
CHECK(str == "vector=[1, 2, 3]\n");
}

TEST_CASE_METHOD(TApp, "TomlOutputTuple", "[config]") {

std::tuple<double, double, double, double> t;
app.add_option("--tuple", t);
app.config_formatter(std::make_shared<CLI::ConfigTOML>());
args = {"--tuple", "1", "2", "3", "4"};

run();

std::string str = app.config_to_str();
CHECK(str == "tuple=[1, 2, 3, 4]\n");
}

TEST_CASE_METHOD(TApp, "ConfigOutputVectorCustom", "[config]") {

std::vector<int> v;
Expand Down
21 changes: 21 additions & 0 deletions tests/OptionTypeTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,27 @@ TEST_CASE_METHOD(TApp, "vectorPairFail", "[optiontype]") {
CHECK_THROWS_AS(run(), CLI::ConversionError);
}

TEST_CASE_METHOD(TApp, "vectorPairFail2", "[optiontype]") {

std::vector<std::pair<int, int>> custom_opt;

auto opt = app.add_option("--pairs", custom_opt);

args = {"--pairs", "1", "2", "3", "4"};

run();
CHECK(custom_opt.size() == 2U);

args = {"--pairs", "1", "2", "3"};

CHECK_THROWS_AS(run(), CLI::ArgumentMismatch);
// now change the type size to explicitly allow 1 or 2
opt->type_size(1, 2);

run();
CHECK(custom_opt.size() == 2U);
}

TEST_CASE_METHOD(TApp, "vectorPairTypeRange", "[optiontype]") {

std::vector<std::pair<int, std::string>> custom_opt;
Expand Down

0 comments on commit 1b2f584

Please sign in to comment.