diff --git a/CMakeLists.txt b/CMakeLists.txt index 8a97cf3..bc6e14b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,7 +15,7 @@ endif() # Be moderately paranoid with flags if(CMAKE_COMPILER_IS_GNUCC) - add_definitions("-Wall -Wextra") + add_definitions("-Wall -Wextra -pedantic") endif() if(MSVC) add_definitions("/W4") diff --git a/include/CLI/App.hpp b/include/CLI/App.hpp index 23bd35d..8a03274 100644 --- a/include/CLI/App.hpp +++ b/include/CLI/App.hpp @@ -112,9 +112,9 @@ public: } /// Add a subcommand. Like the constructor, you can override the help message addition by setting help=false - App* add_subcommand(std::string name, std::string description="", bool help=true) { + App* add_subcommand(std::string name_, std::string description="", bool help=true) { subcommands.emplace_back(new App(description, help)); - subcommands.back()->name = name; + subcommands.back()->name = name_; return subcommands.back().get(); } @@ -132,17 +132,17 @@ public: * program.add_option("filename", filename, "description of filename"); */ Option* add_option( - std::string name, + std::string name_, callback_t callback, std::string description="", bool defaulted=false ) { - Option myopt{name, description, callback, defaulted}; + Option myopt{name_, description, callback, defaulted}; if(std::find_if(std::begin(options), std::end(options), [&myopt](const Option_p &v){return *v == myopt;}) == std::end(options)) { options.emplace_back(); Option_p& option = options.back(); - option.reset(new Option(name, description, callback, defaulted)); + option.reset(new Option(name_, description, callback, defaulted)); return option.get(); } else throw OptionAlreadyAdded(myopt.get_name()); @@ -152,7 +152,7 @@ public: /// Add option for string template::value, detail::enabler> = detail::dummy> Option* add_option( - std::string name, + std::string name_, T &variable, ///< The variable to set std::string description="", bool defaulted=false @@ -169,7 +169,7 @@ public: return detail::lexical_cast(res[0][0], variable); }; - Option* retval = add_option(name, fun, description, defaulted); + Option* retval = add_option(name_, fun, description, defaulted); retval->typeval = detail::type_name(); if(defaulted) { std::stringstream out; @@ -182,7 +182,7 @@ public: /// Add option for vector of results template Option* add_option( - std::string name, + std::string name_, std::vector &variable, ///< The variable vector to set std::string description="", bool defaulted=false @@ -199,7 +199,7 @@ public: return variable.size() > 0 && retval; }; - Option* retval = add_option(name, fun, description, defaulted); + Option* retval = add_option(name_, fun, description, defaulted); retval->allow_vector = true; retval->_expected = -1; retval->typeval = detail::type_name(); @@ -211,14 +211,14 @@ public: /// Add option for flag Option* add_flag( - std::string name, + std::string name_, std::string description="" ) { CLI::callback_t fun = [](CLI::results_t){ return true; }; - Option* opt = add_option(name, fun, description, false); + Option* opt = add_option(name_, fun, description, false); if(opt->get_positional()) throw IncorrectConstruction("Flags cannot be positional"); opt->_expected = 0; @@ -229,7 +229,7 @@ public: template::value && !is_bool::value, detail::enabler> = detail::dummy> Option* add_flag( - std::string name, + std::string name_, T &count, ///< A varaible holding the count std::string description="" ) { @@ -240,7 +240,7 @@ public: return true; }; - Option* opt = add_option(name, fun, description, false); + Option* opt = add_option(name_, fun, description, false); if(opt->get_positional()) throw IncorrectConstruction("Flags cannot be positional"); opt->_expected = 0; @@ -251,7 +251,7 @@ public: template::value, detail::enabler> = detail::dummy> Option* add_flag( - std::string name, + std::string name_, T &count, ///< A varaible holding true if passed std::string description="" ) { @@ -262,7 +262,7 @@ public: return res.size() == 1; }; - Option* opt = add_option(name, fun, description, false); + Option* opt = add_option(name_, fun, description, false); if(opt->get_positional()) throw IncorrectConstruction("Flags cannot be positional"); opt->_expected = 0; @@ -273,7 +273,7 @@ public: /// Add set of options template Option* add_set( - std::string name, + std::string name_, T &member, ///< The selected member of the set std::set _options, ///< The set of posibilities std::string description="", @@ -293,7 +293,7 @@ public: return std::find(std::begin(_options), std::end(_options), member) != std::end(_options); }; - Option* retval = add_option(name, fun, description, defaulted); + Option* retval = add_option(name_, fun, description, defaulted); retval->typeval = detail::type_name(); retval->typeval += " in {" + detail::join(_options) + "}"; if(defaulted) { @@ -306,7 +306,7 @@ public: /// Add a configuration ini file option - Option* add_config(std::string name="--config", + Option* add_config(std::string name_="--config", std::string default_filename="", std::string help="Read an ini file", bool required=false) { @@ -316,7 +316,7 @@ public: remove_option(ini_setting); ini_file = default_filename; ini_required = required; - ini_setting = add_option(name, ini_file, help, default_filename!=""); + ini_setting = add_option(name_, ini_file, help, default_filename!=""); return ini_setting; } @@ -366,13 +366,13 @@ public: } /// Counts the number of times the given option was passed. - int count(std::string name) const { + int count(std::string name_) const { for(const Option_p &opt : options) { - if(opt->check_name(name)) { + if(opt->check_name(name_)) { return opt->count(); } } - throw OptionNotFound(name); + throw OptionNotFound(name_); } /// Makes a help message, with a column `wid` for column 1 @@ -547,7 +547,7 @@ protected: _parse_long(values, false); } - } catch (const FileError &e) { + } catch (const FileError &) { if(ini_required) throw; } @@ -615,16 +615,16 @@ protected: void _parse_short(std::vector &args) { std::string current = args.back(); - std::string name; + std::string name_; std::string rest; - if(!detail::split_short(current, name, rest)) + if(!detail::split_short(current, name_, rest)) throw HorribleError("Short"); args.pop_back(); - auto op_ptr = std::find_if(std::begin(options), std::end(options), [name](const Option_p &opt){return opt->check_sname(name);}); + auto op_ptr = std::find_if(std::begin(options), std::end(options), [name_](const Option_p &opt){return opt->check_sname(name_);}); if(op_ptr == std::end(options)) { - missing_options.push_back("-" + name); + missing_options.push_back("-" + name_); return; } @@ -651,9 +651,9 @@ protected: } } else while(num>0 && args.size() > 0) { num--; - std::string current = args.back(); + std::string current_ = args.back(); args.pop_back(); - op->add_result(vnum,current); + op->add_result(vnum, current_); } if(rest != "") { @@ -666,16 +666,16 @@ protected: void _parse_long(std::vector &args, bool overwrite=true) { std::string current = args.back(); - std::string name; + std::string name_; std::string value; - if(!detail::split_long(current, name, value)) + if(!detail::split_long(current, name_, value)) throw HorribleError("Long"); args.pop_back(); - auto op_ptr = std::find_if(std::begin(options), std::end(options), [name](const Option_p &v){return v->check_lname(name);}); + auto op_ptr = std::find_if(std::begin(options), std::end(options), [name_](const Option_p &v){return v->check_lname(name_);}); if(op_ptr == std::end(options)) { - missing_options.push_back("--" + name); + missing_options.push_back("--" + name_); return; } diff --git a/include/CLI/StringTools.hpp b/include/CLI/StringTools.hpp index d3fe4a2..67e2523 100644 --- a/include/CLI/StringTools.hpp +++ b/include/CLI/StringTools.hpp @@ -94,13 +94,13 @@ void format_help(std::stringstream &out, std::string name, std::string descripti /// Verify the first character of an option template bool valid_first_char(T c) { - return std::isalpha(c) || c=='_'; + return std::isalpha(c, std::locale::classic()) || c=='_'; } /// Verify following characters of an option template bool valid_later_char(T c) { - return std::isalnum(c) || c=='_' || c=='.' || c=='-'; + return std::isalnum(c, std::locale::classic()) || c=='_' || c=='.' || c=='-'; } /// Verify an option name diff --git a/include/CLI/Validators.hpp b/include/CLI/Validators.hpp index 796b521..44eaaed 100644 --- a/include/CLI/Validators.hpp +++ b/include/CLI/Validators.hpp @@ -27,7 +27,7 @@ namespace CLI { bool ExistingFile(std::string filename) { struct stat buffer; bool exist = stat(filename.c_str(), &buffer) == 0; - bool is_dir = buffer.st_mode & S_IFDIR; + bool is_dir = buffer.st_mode & S_IFDIR != 0; if(!exist) { std::cerr << "File does not exist: " << filename << std::endl; return false; @@ -43,7 +43,7 @@ bool ExistingFile(std::string filename) { bool ExistingDirectory(std::string filename) { struct stat buffer; bool exist = stat(filename.c_str(), &buffer) == 0; - bool is_dir = buffer.st_mode & S_IFDIR; + bool is_dir = buffer.st_mode & S_IFDIR != 0; if(!exist) { std::cerr << "Directory does not exist: " << filename << std::endl; return false; diff --git a/tests/AppTest.cpp b/tests/AppTest.cpp index 66f8511..ed48617 100644 --- a/tests/AppTest.cpp +++ b/tests/AppTest.cpp @@ -250,7 +250,7 @@ TEST_F(TApp, Reset) { EXPECT_EQ(1, app.count("--simple")); EXPECT_EQ(1, app.count("-d")); - EXPECT_FLOAT_EQ(1.2, doub); + EXPECT_DOUBLE_EQ(1.2, doub); app.reset(); @@ -261,7 +261,7 @@ TEST_F(TApp, Reset) { EXPECT_EQ(1, app.count("--simple")); EXPECT_EQ(1, app.count("-d")); - EXPECT_FLOAT_EQ(1.2, doub); + EXPECT_DOUBLE_EQ(1.2, doub); } @@ -489,7 +489,7 @@ TEST_F(TApp, RequiresChainedFlags) { TEST_F(TApp, Env) { - setenv("CLI11_TEST_ENV_TMP", "2", true); + put_env("CLI11_TEST_ENV_TMP", "2", true); int val=1; CLI::Option* vopt = app.add_option("--tmp", val)->envname("CLI11_TEST_ENV_TMP"); @@ -504,7 +504,7 @@ TEST_F(TApp, Env) { EXPECT_NO_THROW(run()); app.reset(); - unsetenv("CLI11_TEST_ENV_TMP"); + unset_env("CLI11_TEST_ENV_TMP"); EXPECT_THROW(run(), CLI::RequiredError); } diff --git a/tests/app_helper.hpp b/tests/app_helper.hpp index 363641b..c06739e 100644 --- a/tests/app_helper.hpp +++ b/tests/app_helper.hpp @@ -42,3 +42,20 @@ public: operator const std::string& () const {return _name;} const char* c_str() const {return _name.c_str();} }; + +inline void put_env(std::string name, std::string value) { +#ifdef _MSC_VER + _putenv_s(name.c_str(), value.c_str()); +#else + setenv(name.c_str(), value.c_str(), 1); +#endif +} + +inline void unset_env(std::string name) { +#ifdef _MSC_VER + _putenv_s(name.c_str(), ""); +#else + unsetenv(name.c_str()); +#endif +} +