1
0
mirror of https://github.com/CLIUtils/CLI11.git synced 2025-05-01 05:03:52 +00:00

fix and test some reported issues (#661)

This commit is contained in:
Philip Top 2021-11-01 20:12:55 -07:00 committed by GitHub
parent 728ac3a877
commit 815553211b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 45 additions and 4 deletions

View File

@ -2825,10 +2825,13 @@ class App {
parse_order_.push_back(op.get()); parse_order_.push_back(op.get());
} }
} }
// if we only partially completed a type then add an empty string if allowed for later processing
// if we only partially completed a type then add an empty string for later processing if(min_num > 0 && (collected % op->get_type_size_max()) != 0) {
if(min_num > 0 && op->get_type_size_max() != min_num && (collected % op->get_type_size_max()) != 0) { if(op->get_type_size_max() != op->get_type_size_min()) {
op->add_result(std::string{}); 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()) { if(op->get_trigger_on_parse()) {
op->run_callback(); op->run_callback();

View File

@ -277,6 +277,10 @@ class ArgumentMismatch : public ParseError {
static ArgumentMismatch FlagOverride(std::string name) { static ArgumentMismatch FlagOverride(std::string name) {
return ArgumentMismatch(name + " was given a disallowed flag override"); 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 /// Thrown when a requires option is missing

View File

@ -2103,6 +2103,19 @@ TEST_CASE_METHOD(TApp, "TomlOutputVector", "[config]") {
CHECK(str == "vector=[1, 2, 3]\n"); 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]") { TEST_CASE_METHOD(TApp, "ConfigOutputVectorCustom", "[config]") {
std::vector<int> v; std::vector<int> v;

View File

@ -554,6 +554,27 @@ TEST_CASE_METHOD(TApp, "vectorPairFail", "[optiontype]") {
CHECK_THROWS_AS(run(), CLI::ConversionError); 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]") { TEST_CASE_METHOD(TApp, "vectorPairTypeRange", "[optiontype]") {
std::vector<std::pair<int, std::string>> custom_opt; std::vector<std::pair<int, std::string>> custom_opt;