Skip to content

Commit

Permalink
Add a ! operator to Validators for checking a false condition
Browse files Browse the repository at this point in the history
  • Loading branch information
phlptp committed Feb 20, 2019
1 parent 5e0bb1c commit a7d1aef
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 1 deletion.
2 changes: 1 addition & 1 deletion include/CLI/App.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ class App {
return this;
}

/// specify that the positional arguments are only at the end of the sequence
/// Specify that the positional arguments are only at the end of the sequence
App *positionals_at_end(bool value = true) {
positionals_at_end_ = value;
return this;
Expand Down
23 changes: 23 additions & 0 deletions include/CLI/Validators.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,29 @@ class Validator {
};
return newval;
}

/// Create a validator that fails when a given validator succeeds
Validator operator!() const {
Validator newval;
std::string typestring = tname;
if(tname.empty()) {
typestring = tname_function();
}
newval.tname = "NOT " + typestring;

std::string failString = "check " + typestring + " succeeded improperly";
// Give references (will make a copy in lambda function)
const std::function<std::string(std::string & res)> &f1 = func;

newval.func = [f1, failString](std::string &test) -> std::string {
std::string s1 = f1(test);
if(s1.empty())
return failString;
else
return std::string();
};
return newval;
}
};

// The implementation of the built in validators is using the Validator class;
Expand Down
18 changes: 18 additions & 0 deletions tests/AppTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1317,6 +1317,24 @@ TEST_F(TApp, FileExists) {
EXPECT_FALSE(CLI::ExistingFile(myfile).empty());
}

TEST_F(TApp, NotFileExists) {
std::string myfile{"TestNonFileNotUsed.txt"};
EXPECT_FALSE(CLI::ExistingFile(myfile).empty());

std::string filename = "Failed";
app.add_option("--file", filename)->check(!CLI::ExistingFile);
args = {"--file", myfile};

EXPECT_NO_THROW(run());

bool ok = static_cast<bool>(std::ofstream(myfile.c_str()).put('a')); // create file
EXPECT_TRUE(ok);
EXPECT_THROW(run(), CLI::ValidationError);

std::remove(myfile.c_str());
EXPECT_FALSE(CLI::ExistingFile(myfile).empty());
}

TEST_F(TApp, VectorFixedString) {
std::vector<std::string> strvec;
std::vector<std::string> answer{"mystring", "mystring2", "mystring3"};
Expand Down
14 changes: 14 additions & 0 deletions tests/SetTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,20 @@ TEST_F(TApp, InSetIgnoreCasePointer) {
EXPECT_THROW(run(), CLI::ArgumentMismatch);
}

TEST_F(TApp, NotInSetIgnoreCasePointer) {

std::set<std::string> *options = new std::set<std::string>{"one", "Two", "THREE"};
std::string choice;
app.add_option("-q,--quick", choice)->check(!CLI::IsMember(*options, CLI::ignore_case));

args = {"--quick", "One"};
EXPECT_THROW(run(), CLI::ValidationError);

args = {"--quick", "four"};
run();
EXPECT_EQ(choice, "four");
}

TEST_F(TApp, InSetIgnoreUnderscore) {

std::string choice;
Expand Down

0 comments on commit a7d1aef

Please sign in to comment.