Commit 3061782d3ffcd9b49fa3c7ac1d33f5c2340fbd91

Authored by Henry Fredrick Schreiner
1 parent fb16cb93

Adding check for Nonexistent

include/CLI.hpp
@@ -115,8 +115,11 @@ bool _ExistingDirectory(std::string filename) { @@ -115,8 +115,11 @@ bool _ExistingDirectory(std::string filename) {
115 } 115 }
116 116
117 bool _NonexistentPath(std::string filename) { 117 bool _NonexistentPath(std::string filename) {
  118 + std::cout << "Validating: " << filename << std::endl;
118 struct stat buffer; 119 struct stat buffer;
119 - return stat(filename.c_str(), &buffer) != 0; 120 + bool out = stat(filename.c_str(), &buffer) != 0;
  121 + std::cout << (out ? "Passed" : "Failed") << std::endl;
  122 + return out;
120 } 123 }
121 124
122 struct Error : public std::runtime_error { 125 struct Error : public std::runtime_error {
@@ -235,6 +238,12 @@ public: @@ -235,6 +238,12 @@ public:
235 238
236 /// Process the callback 239 /// Process the callback
237 bool run_callback() const { 240 bool run_callback() const {
  241 + if(opts.validators.size()>0) {
  242 + for(const std::string & result : flatten_results())
  243 + for(const std::function<bool(std::string)> &vali : opts.validators)
  244 + if(!vali(result))
  245 + return false;
  246 + }
238 return callback(results); 247 return callback(results);
239 } 248 }
240 249
@@ -315,6 +324,13 @@ public: @@ -315,6 +324,13 @@ public:
315 return out.str(); 324 return out.str();
316 } 325 }
317 326
  327 + std::vector<std::string> flatten_results() const {
  328 + std::vector<std::string> output;
  329 + for(const std::vector<std::string> result : results)
  330 + output.insert(std::end(output), std::begin(result), std::end(result));
  331 + return output;
  332 + }
  333 +
318 }; 334 };
319 335
320 336
tests/CLITest.cpp
1 1
2 #include "CLI.hpp" 2 #include "CLI.hpp"
3 #include "gtest/gtest.h" 3 #include "gtest/gtest.h"
4 - 4 +#include <fstream>
5 5
6 typedef std::vector<std::string> input_t; 6 typedef std::vector<std::string> input_t;
7 7
@@ -205,6 +205,28 @@ TEST_F(TApp, Reset) { @@ -205,6 +205,28 @@ TEST_F(TApp, Reset) {
205 } 205 }
206 206
207 207
  208 +TEST_F(TApp, FileNotExists) {
  209 + std::string myfile{"TestNonFileNotUsed.txt"};
  210 + EXPECT_TRUE(CLI::_NonexistentPath(myfile));
  211 +
  212 + std::string filename;
  213 + app.add_option("file", filename, "", CLI::NonexistentPath);
  214 + args = {"--file", myfile};
  215 +
  216 + run();
  217 + EXPECT_EQ(myfile, filename);
  218 +
  219 + app.reset();
  220 +
  221 +
  222 + bool ok = static_cast<bool>(std::ofstream(myfile.c_str()).put('a')); // create file
  223 + EXPECT_TRUE(ok);
  224 + EXPECT_THROW(run(), CLI::ParseError);
  225 +
  226 + std::remove(myfile.c_str());
  227 + EXPECT_FALSE(CLI::_ExistingFile(myfile));
  228 +}
  229 +
208 TEST_F(TApp, Basic) { 230 TEST_F(TApp, Basic) {
209 auto sub1 = app.add_subcommand("sub1"); 231 auto sub1 = app.add_subcommand("sub1");
210 auto sub2 = app.add_subcommand("sub2"); 232 auto sub2 = app.add_subcommand("sub2");