Skip to content

Commit

Permalink
add const version of validator modifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
phlptp authored and henryiii committed Sep 3, 2019
1 parent 67c441b commit fc6e1c7
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
28 changes: 26 additions & 2 deletions include/CLI/Validators.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Validator {
std::function<std::string(std::string &)> func_{[](std::string &) { return std::string{}; }};
/// The name for search purposes of the Validator
std::string name_;
/// A validate will only apply to an indexed value (-1 is all elements)
/// A Validator will only apply to an indexed value (-1 is all elements)
int application_index_ = -1;
/// Enable for Validator to allow it to be disabled if need be
bool active_{true};
Expand All @@ -54,7 +54,7 @@ class Validator {
Validator() = default;
/// Construct a Validator with just the description string
explicit Validator(std::string validator_desc) : desc_function_([validator_desc]() { return validator_desc; }) {}
// Construct Validator from basic information
/// Construct Validator from basic information
Validator(std::function<std::string(std::string &)> op, std::string validator_desc, std::string validator_name = "")
: desc_function_([validator_desc]() { return validator_desc; }), func_(std::move(op)),
name_(std::move(validator_name)) {}
Expand Down Expand Up @@ -90,6 +90,12 @@ class Validator {
desc_function_ = [validator_desc]() { return validator_desc; };
return *this;
}
/// Specify the type string
Validator description(std::string validator_desc) const {
Validator newval(*this);
newval.desc_function_ = [validator_desc]() { return validator_desc; };
return newval;
}
/// Generate type description information for the Validator
std::string get_description() const {
if(active_) {
Expand All @@ -102,13 +108,25 @@ class Validator {
name_ = std::move(validator_name);
return *this;
}
/// Specify the type string
Validator name(std::string validator_name) const {
Validator newval(*this);
newval.name_ = std::move(validator_name);
return newval;
}
/// Get the name of the Validator
const std::string &get_name() const { return name_; }
/// Specify whether the Validator is active or not
Validator &active(bool active_val = true) {
active_ = active_val;
return *this;
}
/// Specify whether the Validator is active or not
Validator active(bool active_val = true) const {
Validator newval(*this);
newval.active_ = active_val;
return newval;
}

/// Specify whether the Validator can be modifying or not
Validator &non_modifying(bool no_modify = true) {
Expand All @@ -120,6 +138,12 @@ class Validator {
application_index_ = app_index;
return *this;
};
/// Specify the application index of a validator
Validator application_index(int app_index) const {
Validator newval(*this);
newval.application_index_ = app_index;
return newval;
};
/// Get the current value of the application index
int get_application_index() const { return application_index_; }
/// Get a boolean if the validator is active
Expand Down
13 changes: 8 additions & 5 deletions tests/AppTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1077,8 +1077,9 @@ TEST_F(TApp, PositionalValidation) {
std::string options;
std::string foo;

app.add_option("bar", options)->check(CLI::Number);
app.add_option("foo", foo);
app.add_option("bar", options)->check(CLI::Number.name("valbar"));
// disable the check on foo
app.add_option("foo", foo)->check(CLI::Number.active(false));
app.validate_positionals();
args = {"1", "param1"};
run();
Expand All @@ -1087,10 +1088,12 @@ TEST_F(TApp, PositionalValidation) {
EXPECT_EQ(foo, "param1");

args = {"param1", "1"};
run();
EXPECT_NO_THROW(run());

EXPECT_EQ(options, "1");
EXPECT_EQ(foo, "param1");

EXPECT_NE(app.get_option("bar")->get_validator("valbar"), nullptr);
}

TEST_F(TApp, PositionalNoSpaceLong) {
Expand Down Expand Up @@ -1696,12 +1699,12 @@ TEST_F(TApp, VectorIndexedValidator) {
run();
EXPECT_EQ(4u, app.count("-v"));
EXPECT_EQ(4u, vvec.size());
opt->check(CLI::Validator(CLI::PositiveNumber).application_index(0));
opt->check(CLI::PositiveNumber.application_index(0));
opt->check((!CLI::PositiveNumber).application_index(1));
EXPECT_NO_THROW(run());
EXPECT_EQ(4u, vvec.size());
// v[3] would be negative
opt->check(CLI::Validator(CLI::PositiveNumber).application_index(3));
opt->check(CLI::PositiveNumber.application_index(3));
EXPECT_THROW(run(), CLI::ValidationError);
}

Expand Down
10 changes: 10 additions & 0 deletions tests/HelpTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,16 @@ TEST(THelp, ValidatorsText) {
EXPECT_THAT(help, HasSubstr("UINT:INT in [0 - 12]")); // Loses UINT
}

TEST(THelp, ValidatorsTextCustom) {
CLI::App app;

std::string filename;
app.add_option("--f1", filename)->check(CLI::ExistingFile.description("Existing file"));

std::string help = app.help();
EXPECT_THAT(help, HasSubstr("Existing file"));
}

TEST(THelp, ValidatorsNonPathText) {
CLI::App app;

Expand Down

0 comments on commit fc6e1c7

Please sign in to comment.